]>
Commit | Line | Data |
---|---|---|
b0bbbd85 | 1 | /* C Compatible Compiler Preprocessor (CCCP) |
f803dcf8 | 2 | Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc. |
2af5e9e2 RK |
3 | Written by Paul Rubin, June 1986 |
4 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
b0bbbd85 RS |
5 | |
6 | This program is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | ||
20 | In other words, you are welcome to use, share and improve this program. | |
21 | You are forbidden to forbid anyone else to use, share and improve | |
22 | what you give them. Help stamp out software-hoarding! */ | |
23 | \f | |
24 | typedef unsigned char U_CHAR; | |
25 | ||
26 | #ifdef EMACS | |
27 | #define NO_SHORTNAMES | |
28 | #include "../src/config.h" | |
29 | #ifdef open | |
30 | #undef open | |
31 | #undef read | |
32 | #undef write | |
33 | #endif /* open */ | |
34 | #endif /* EMACS */ | |
35 | ||
36 | /* The macro EMACS is defined when cpp is distributed as part of Emacs, | |
37 | for the sake of machines with limited C compilers. */ | |
38 | #ifndef EMACS | |
39 | #include "config.h" | |
40 | #endif /* not EMACS */ | |
41 | ||
42 | #ifndef STANDARD_INCLUDE_DIR | |
43 | #define STANDARD_INCLUDE_DIR "/usr/include" | |
44 | #endif | |
45 | ||
46 | #ifndef LOCAL_INCLUDE_DIR | |
47 | #define LOCAL_INCLUDE_DIR "/usr/local/include" | |
48 | #endif | |
49 | ||
68a8ca25 | 50 | #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */ |
6842690e RK |
51 | #ifdef __STDC__ |
52 | #define PTR_INT_TYPE ptrdiff_t | |
53 | #else | |
54 | #define PTR_INT_TYPE long | |
55 | #endif | |
68a8ca25 | 56 | #endif /* 0 */ |
6842690e | 57 | |
b0bbbd85 RS |
58 | #include "pcp.h" |
59 | ||
5c18abb4 RS |
60 | /* By default, colon separates directories in a path. */ |
61 | #ifndef PATH_SEPARATOR | |
62 | #define PATH_SEPARATOR ':' | |
63 | #endif | |
64 | ||
b0bbbd85 RS |
65 | #include <sys/types.h> |
66 | #include <sys/stat.h> | |
67 | #include <ctype.h> | |
68 | #include <stdio.h> | |
9e263fc4 | 69 | #include <signal.h> |
b0bbbd85 | 70 | |
25cbb59e RK |
71 | /* The following symbols should be autoconfigured: |
72 | HAVE_FCNTL_H | |
73 | HAVE_STDLIB_H | |
74 | HAVE_SYS_TIME_H | |
75 | HAVE_UNISTD_H | |
76 | STDC_HEADERS | |
77 | TIME_WITH_SYS_TIME | |
78 | In the mean time, we'll get by with approximations based | |
79 | on existing GCC configuration symbols. */ | |
80 | ||
81 | #ifdef POSIX | |
82 | # ifndef HAVE_STDLIB_H | |
83 | # define HAVE_STDLIB_H 1 | |
84 | # endif | |
85 | # ifndef HAVE_UNISTD_H | |
86 | # define HAVE_UNISTD_H 1 | |
87 | # endif | |
88 | # ifndef STDC_HEADERS | |
89 | # define STDC_HEADERS 1 | |
90 | # endif | |
91 | #endif /* defined (POSIX) */ | |
92 | ||
93 | #if defined (POSIX) || (defined (USG) && !defined (VMS)) | |
94 | # ifndef HAVE_FCNTL_H | |
95 | # define HAVE_FCNTL_H 1 | |
96 | # endif | |
97 | #endif | |
98 | ||
99 | #ifndef RLIMIT_STACK | |
100 | # include <time.h> | |
b0bbbd85 | 101 | #else |
25cbb59e RK |
102 | # if TIME_WITH_SYS_TIME |
103 | # include <sys/time.h> | |
104 | # include <time.h> | |
105 | # else | |
106 | # if HAVE_SYS_TIME_H | |
107 | # include <sys/time.h> | |
108 | # else | |
109 | # include <time.h> | |
110 | # endif | |
111 | # endif | |
112 | # include <sys/resource.h> | |
113 | #endif | |
114 | ||
115 | #if HAVE_FCNTL_H | |
116 | # include <fcntl.h> | |
117 | #endif | |
b0bbbd85 | 118 | |
6d34466a JW |
119 | /* This defines "errno" properly for VMS, and gives us EACCES. */ |
120 | #include <errno.h> | |
121 | ||
25cbb59e RK |
122 | #if HAVE_STDLIB_H |
123 | # include <stdlib.h> | |
124 | #else | |
125 | char *getenv (); | |
126 | #endif | |
127 | ||
128 | #if STDC_HEADERS | |
129 | # include <string.h> | |
130 | # ifndef bcmp | |
131 | # define bcmp(a, b, n) memcmp (a, b, n) | |
132 | # endif | |
133 | # ifndef bcopy | |
134 | # define bcopy(s, d, n) memcpy (d, s, n) | |
135 | # endif | |
136 | # ifndef bzero | |
137 | # define bzero(d, n) memset (d, 0, n) | |
138 | # endif | |
139 | #else /* !STDC_HEADERS */ | |
140 | char *index (); | |
141 | char *rindex (); | |
142 | ||
143 | # if !defined (BSTRING) && (defined (USG) || defined (VMS)) | |
144 | ||
145 | # ifndef bcmp | |
146 | # define bcmp my_bcmp | |
147 | static int | |
148 | my_bcmp (a, b, n) | |
149 | register char *a; | |
150 | register char *b; | |
151 | register unsigned n; | |
152 | { | |
153 | while (n-- > 0) | |
154 | if (*a++ != *b++) | |
155 | return 1; | |
156 | ||
157 | return 0; | |
158 | } | |
159 | # endif /* !defined (bcmp) */ | |
160 | ||
161 | # ifndef bcopy | |
162 | # define bcopy my_bcopy | |
163 | static void | |
164 | my_bcopy (s, d, n) | |
165 | register char *s; | |
166 | register char *d; | |
167 | register unsigned n; | |
168 | { | |
169 | while (n-- > 0) | |
170 | *d++ = *s++; | |
171 | } | |
172 | # endif /* !defined (bcopy) */ | |
173 | ||
174 | # ifndef bzero | |
175 | # define bzero my_bzero | |
176 | static void | |
177 | my_bzero (b, length) | |
178 | register char *b; | |
179 | register unsigned length; | |
180 | { | |
181 | while (length-- > 0) | |
182 | *b++ = 0; | |
183 | } | |
184 | # endif /* !defined (bzero) */ | |
185 | ||
186 | # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */ | |
187 | #endif /* ! STDC_HEADERS */ | |
188 | ||
189 | #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) | |
190 | # define __attribute__(x) | |
191 | #endif | |
192 | ||
193 | #ifndef PROTO | |
194 | # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__) | |
195 | # define PROTO(ARGS) ARGS | |
196 | # else | |
197 | # define PROTO(ARGS) () | |
198 | # endif | |
199 | #endif | |
200 | ||
201 | #if defined (__STDC__) && defined (HAVE_VPRINTF) | |
202 | # include <stdarg.h> | |
203 | # define VA_START(va_list, var) va_start (va_list, var) | |
204 | # define PRINTF_ALIST(msg) char *msg, ... | |
205 | # define PRINTF_DCL(msg) | |
206 | # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n))) | |
207 | #else | |
208 | # include <varargs.h> | |
209 | # define VA_START(va_list, var) va_start (va_list) | |
210 | # define PRINTF_ALIST(msg) msg, va_alist | |
211 | # define PRINTF_DCL(msg) char *msg; va_dcl | |
212 | # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n))) | |
213 | # define vfprintf(file, msg, args) \ | |
214 | { \ | |
215 | char *a0 = va_arg(args, char *); \ | |
216 | char *a1 = va_arg(args, char *); \ | |
217 | char *a2 = va_arg(args, char *); \ | |
218 | char *a3 = va_arg(args, char *); \ | |
219 | fprintf (file, msg, a0, a1, a2, a3); \ | |
220 | } | |
221 | #endif | |
222 | ||
223 | #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2) | |
224 | #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3) | |
225 | #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4) | |
226 | ||
227 | #if HAVE_UNISTD_H | |
228 | # include <unistd.h> | |
229 | #endif | |
230 | ||
b0bbbd85 RS |
231 | /* VMS-specific definitions */ |
232 | #ifdef VMS | |
b0bbbd85 RS |
233 | #include <descrip.h> |
234 | #define O_RDONLY 0 /* Open arg for Read/Only */ | |
235 | #define O_WRONLY 1 /* Open arg for Write/Only */ | |
ad0c9fa1 RS |
236 | #define read(fd,buf,size) VMS_read (fd,buf,size) |
237 | #define write(fd,buf,size) VMS_write (fd,buf,size) | |
238 | #define open(fname,mode,prot) VMS_open (fname,mode,prot) | |
239 | #define fopen(fname,mode) VMS_fopen (fname,mode) | |
240 | #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile) | |
241 | #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt) | |
bd8cb5e2 RK |
242 | #define fstat(fd,stbuf) VMS_fstat (fd,stbuf) |
243 | #define stat(name,stbuf) VMS_stat (name,stbuf) | |
244 | static int VMS_fstat (), VMS_stat (); | |
106d7d7d | 245 | static char * VMS_strncat (); |
b0bbbd85 RS |
246 | static int VMS_read (); |
247 | static int VMS_write (); | |
248 | static int VMS_open (); | |
249 | static FILE * VMS_fopen (); | |
250 | static FILE * VMS_freopen (); | |
251 | static void hack_vms_include_specification (); | |
252 | typedef struct { unsigned :16, :16, :16; } vms_ino_t; | |
253 | #define ino_t vms_ino_t | |
6489924b | 254 | #define INCLUDE_LEN_FUDGE 10 /* leave room for VMS syntax conversion */ |
b0bbbd85 RS |
255 | #ifdef __GNUC__ |
256 | #define BSTRING /* VMS/GCC supplies the bstring routines */ | |
257 | #endif /* __GNUC__ */ | |
258 | #endif /* VMS */ | |
259 | ||
260 | #ifndef O_RDONLY | |
261 | #define O_RDONLY 0 | |
262 | #endif | |
263 | ||
264 | #undef MIN | |
265 | #undef MAX | |
266 | #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) | |
267 | #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) | |
268 | ||
f5f1d163 TW |
269 | /* Find the largest host integer type and set its size and type. */ |
270 | ||
271 | #ifndef HOST_BITS_PER_WIDE_INT | |
272 | ||
273 | #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT | |
274 | #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG | |
275 | #define HOST_WIDE_INT long | |
276 | #else | |
277 | #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT | |
278 | #define HOST_WIDE_INT int | |
279 | #endif | |
280 | ||
281 | #endif | |
282 | ||
b0bbbd85 RS |
283 | #ifndef S_ISREG |
284 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | |
285 | #endif | |
286 | ||
2e4f4529 JW |
287 | #ifndef S_ISDIR |
288 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | |
289 | #endif | |
290 | ||
390e01bf TW |
291 | /* Define a generic NULL if one hasn't already been defined. */ |
292 | ||
8d9bfdc5 RK |
293 | #ifndef NULL |
294 | #define NULL 0 | |
295 | #endif | |
296 | ||
390e01bf TW |
297 | #ifndef GENERIC_PTR |
298 | #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__) | |
299 | #define GENERIC_PTR void * | |
3e06027d | 300 | #else |
390e01bf | 301 | #define GENERIC_PTR char * |
8d9bfdc5 | 302 | #endif |
3e06027d | 303 | #endif |
8d9bfdc5 | 304 | |
390e01bf TW |
305 | #ifndef NULL_PTR |
306 | #define NULL_PTR ((GENERIC_PTR)0) | |
307 | #endif | |
308 | ||
6489924b RS |
309 | #ifndef INCLUDE_LEN_FUDGE |
310 | #define INCLUDE_LEN_FUDGE 0 | |
311 | #endif | |
312 | ||
b0bbbd85 RS |
313 | /* External declarations. */ |
314 | ||
b0bbbd85 | 315 | extern char *version_string; |
02cc38b5 RK |
316 | #ifndef VMS |
317 | #ifndef HAVE_STRERROR | |
b0bbbd85 | 318 | extern int sys_nerr; |
2d2daf45 | 319 | #if defined(bsd4_4) |
5d672cd7 JW |
320 | extern const char *const sys_errlist[]; |
321 | #else | |
b0bbbd85 | 322 | extern char *sys_errlist[]; |
5d672cd7 | 323 | #endif |
02cc38b5 RK |
324 | #else /* HAVE_STERRROR */ |
325 | char *strerror (); | |
326 | #endif | |
327 | #else /* VMS */ | |
328 | char *strerror (int,...); | |
329 | #endif | |
25cbb59e RK |
330 | int parse_escape PROTO((char **)); |
331 | HOST_WIDE_INT parse_c_expression PROTO((char *)); | |
b0bbbd85 RS |
332 | |
333 | #ifndef errno | |
334 | extern int errno; | |
335 | #endif | |
b0bbbd85 RS |
336 | \f |
337 | #ifndef FAILURE_EXIT_CODE | |
338 | #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */ | |
339 | #endif | |
340 | ||
341 | #ifndef SUCCESS_EXIT_CODE | |
342 | #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */ | |
343 | #endif | |
344 | ||
345 | /* Name under which this program was invoked. */ | |
346 | ||
347 | static char *progname; | |
348 | ||
eda5fa7b | 349 | /* Nonzero means use extra default include directories for C++. */ |
b0bbbd85 RS |
350 | |
351 | static int cplusplus; | |
352 | ||
eda5fa7b RS |
353 | /* Nonzero means handle cplusplus style comments */ |
354 | ||
355 | static int cplusplus_comments; | |
356 | ||
b0bbbd85 RS |
357 | /* Nonzero means handle #import, for objective C. */ |
358 | ||
359 | static int objc; | |
360 | ||
361 | /* Nonzero means this is an assembly file, and allow | |
362 | unknown directives, which could be comments. */ | |
363 | ||
364 | static int lang_asm; | |
365 | ||
366 | /* Current maximum length of directory names in the search path | |
367 | for include files. (Altered as we get more of them.) */ | |
368 | ||
369 | static int max_include_len; | |
370 | ||
371 | /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */ | |
372 | ||
253245df | 373 | static int for_lint = 0; |
b0bbbd85 RS |
374 | |
375 | /* Nonzero means copy comments into the output file. */ | |
376 | ||
377 | static int put_out_comments = 0; | |
378 | ||
379 | /* Nonzero means don't process the ANSI trigraph sequences. */ | |
380 | ||
381 | static int no_trigraphs = 0; | |
382 | ||
383 | /* Nonzero means print the names of included files rather than | |
384 | the preprocessed output. 1 means just the #include "...", | |
385 | 2 means #include <...> as well. */ | |
386 | ||
387 | static int print_deps = 0; | |
388 | ||
86739c7b DE |
389 | /* Nonzero if missing .h files in -M output are assumed to be generated |
390 | files and not errors. */ | |
391 | ||
392 | static int print_deps_missing_files = 0; | |
393 | ||
b0bbbd85 RS |
394 | /* Nonzero means print names of header files (-H). */ |
395 | ||
396 | static int print_include_names = 0; | |
397 | ||
398 | /* Nonzero means don't output line number information. */ | |
399 | ||
adcfa681 | 400 | static int no_line_directives; |
b0bbbd85 | 401 | |
bbd4b75b RS |
402 | /* Nonzero means output the text in failing conditionals, |
403 | inside #failed ... #endfailed. */ | |
404 | ||
405 | static int output_conditionals; | |
406 | ||
b0bbbd85 RS |
407 | /* dump_only means inhibit output of the preprocessed text |
408 | and instead output the definitions of all user-defined | |
409 | macros in a form suitable for use as input to cccp. | |
410 | dump_names means pass #define and the macro name through to output. | |
411 | dump_definitions means pass the whole definition (plus #define) through | |
412 | */ | |
413 | ||
414 | static enum {dump_none, dump_only, dump_names, dump_definitions} | |
415 | dump_macros = dump_none; | |
416 | ||
417 | /* Nonzero means pass all #define and #undef directives which we actually | |
418 | process through to the output stream. This feature is used primarily | |
419 | to allow cc1 to record the #defines and #undefs for the sake of | |
420 | debuggers which understand about preprocessor macros, but it may | |
421 | also be useful with -E to figure out how symbols are defined, and | |
422 | where they are defined. */ | |
423 | static int debug_output = 0; | |
424 | ||
b0bbbd85 RS |
425 | /* Nonzero indicates special processing used by the pcp program. The |
426 | special effects of this mode are: | |
427 | ||
428 | Inhibit all macro expansion, except those inside #if directives. | |
429 | ||
430 | Process #define directives normally, and output their contents | |
431 | to the output file. | |
432 | ||
433 | Output preconditions to pcp_outfile indicating all the relevant | |
434 | preconditions for use of this file in a later cpp run. | |
435 | */ | |
436 | static FILE *pcp_outfile; | |
437 | ||
438 | /* Nonzero means we are inside an IF during a -pcp run. In this mode | |
439 | macro expansion is done, and preconditions are output for all macro | |
440 | uses requiring them. */ | |
441 | static int pcp_inside_if; | |
442 | ||
5f12e361 RS |
443 | /* Nonzero means never to include precompiled files. |
444 | This is 1 since there's no way now to make precompiled files, | |
445 | so it's not worth testing for them. */ | |
446 | static int no_precomp = 1; | |
b0bbbd85 RS |
447 | |
448 | /* Nonzero means give all the error messages the ANSI standard requires. */ | |
449 | ||
450 | int pedantic; | |
451 | ||
452 | /* Nonzero means try to make failure to fit ANSI C an error. */ | |
453 | ||
454 | static int pedantic_errors; | |
455 | ||
456 | /* Nonzero means don't print warning messages. -w. */ | |
457 | ||
458 | static int inhibit_warnings = 0; | |
459 | ||
460 | /* Nonzero means warn if slash-star appears in a comment. */ | |
461 | ||
462 | static int warn_comments; | |
463 | ||
464 | /* Nonzero means warn if a macro argument is (or would be) | |
465 | stringified with -traditional. */ | |
466 | ||
467 | static int warn_stringify; | |
468 | ||
469 | /* Nonzero means warn if there are any trigraphs. */ | |
470 | ||
471 | static int warn_trigraphs; | |
472 | ||
2aa7ec37 RS |
473 | /* Nonzero means warn if #import is used. */ |
474 | ||
475 | static int warn_import = 1; | |
476 | ||
b0bbbd85 RS |
477 | /* Nonzero means turn warnings into errors. */ |
478 | ||
479 | static int warnings_are_errors; | |
480 | ||
481 | /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */ | |
482 | ||
483 | int traditional; | |
484 | ||
485 | /* Nonzero causes output not to be done, | |
486 | but directives such as #define that have side effects | |
487 | are still obeyed. */ | |
488 | ||
489 | static int no_output; | |
490 | ||
42e2194b RK |
491 | /* Nonzero means this file was included with a -imacros or -include |
492 | command line and should not be recorded as an include file. */ | |
493 | ||
494 | static int no_record_file; | |
495 | ||
b0bbbd85 RS |
496 | /* Nonzero means that we have finished processing the command line options. |
497 | This flag is used to decide whether or not to issue certain errors | |
498 | and/or warnings. */ | |
499 | ||
500 | static int done_initializing = 0; | |
5b01bc66 JW |
501 | |
502 | /* Line where a newline was first seen in a string constant. */ | |
503 | ||
504 | static int multiline_string_line = 0; | |
b0bbbd85 RS |
505 | \f |
506 | /* I/O buffer structure. | |
507 | The `fname' field is nonzero for source files and #include files | |
508 | and for the dummy text used for -D and -U. | |
509 | It is zero for rescanning results of macro expansion | |
510 | and for expanding macro arguments. */ | |
9b1674a8 | 511 | #define INPUT_STACK_MAX 400 |
b0bbbd85 RS |
512 | static struct file_buf { |
513 | char *fname; | |
adcfa681 | 514 | /* Filename specified with #line directive. */ |
b0bbbd85 RS |
515 | char *nominal_fname; |
516 | /* Record where in the search path this file was found. | |
517 | For #include_next. */ | |
518 | struct file_name_list *dir; | |
519 | int lineno; | |
520 | int length; | |
521 | U_CHAR *buf; | |
522 | U_CHAR *bufp; | |
523 | /* Macro that this level is the expansion of. | |
524 | Included so that we can reenable the macro | |
525 | at the end of this level. */ | |
526 | struct hashnode *macro; | |
527 | /* Value of if_stack at start of this file. | |
528 | Used to prohibit unmatched #endif (etc) in an include file. */ | |
529 | struct if_stack *if_stack; | |
530 | /* Object to be freed at end of input at this level. */ | |
531 | U_CHAR *free_ptr; | |
532 | /* True if this is a header file included using <FILENAME>. */ | |
533 | char system_header_p; | |
534 | } instack[INPUT_STACK_MAX]; | |
535 | ||
536 | static int last_error_tick; /* Incremented each time we print it. */ | |
537 | static int input_file_stack_tick; /* Incremented when the status changes. */ | |
538 | ||
539 | /* Current nesting level of input sources. | |
540 | `instack[indepth]' is the level currently being read. */ | |
541 | static int indepth = -1; | |
542 | #define CHECK_DEPTH(code) \ | |
543 | if (indepth >= (INPUT_STACK_MAX - 1)) \ | |
544 | { \ | |
545 | error_with_line (line_for_error (instack[indepth].lineno), \ | |
546 | "macro or `#include' recursion too deep"); \ | |
547 | code; \ | |
548 | } | |
549 | ||
550 | /* Current depth in #include directives that use <...>. */ | |
551 | static int system_include_depth = 0; | |
552 | ||
553 | typedef struct file_buf FILE_BUF; | |
554 | ||
555 | /* The output buffer. Its LENGTH field is the amount of room allocated | |
556 | for the buffer, not the number of chars actually present. To get | |
557 | that, subtract outbuf.buf from outbuf.bufp. */ | |
558 | ||
559 | #define OUTBUF_SIZE 10 /* initial size of output buffer */ | |
560 | static FILE_BUF outbuf; | |
561 | ||
562 | /* Grow output buffer OBUF points at | |
563 | so it can hold at least NEEDED more chars. */ | |
564 | ||
565 | #define check_expand(OBUF, NEEDED) \ | |
566 | (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \ | |
567 | ? grow_outbuf ((OBUF), (NEEDED)) : 0) | |
568 | ||
569 | struct file_name_list | |
570 | { | |
571 | struct file_name_list *next; | |
572 | char *fname; | |
573 | /* If the following is nonzero, it is a macro name. | |
574 | Don't include the file again if that macro is defined. */ | |
575 | U_CHAR *control_macro; | |
d2a22862 RS |
576 | /* If the following is nonzero, it is a C-language system include |
577 | directory. */ | |
578 | int c_system_include_path; | |
6e7f952e JW |
579 | /* Mapping of file names for this directory. */ |
580 | struct file_name_map *name_map; | |
581 | /* Non-zero if name_map is valid. */ | |
582 | int got_name_map; | |
b0bbbd85 RS |
583 | }; |
584 | ||
585 | /* #include "file" looks in source file dir, then stack. */ | |
586 | /* #include <file> just looks in the stack. */ | |
587 | /* -I directories are added to the end, then the defaults are added. */ | |
acf7262c JM |
588 | /* The */ |
589 | static struct default_include { | |
590 | char *fname; /* The name of the directory. */ | |
591 | int cplusplus; /* Only look here if we're compiling C++. */ | |
592 | int cxx_aware; /* Includes in this directory don't need to | |
593 | be wrapped in extern "C" when compiling | |
594 | C++. */ | |
595 | } include_defaults_array[] | |
b0bbbd85 RS |
596 | #ifdef INCLUDE_DEFAULTS |
597 | = INCLUDE_DEFAULTS; | |
598 | #else | |
599 | = { | |
600 | /* Pick up GNU C++ specific include files. */ | |
acf7262c | 601 | { GPLUSPLUS_INCLUDE_DIR, 1, 1 }, |
b0bbbd85 | 602 | #ifdef CROSS_COMPILE |
ee40befe RS |
603 | /* This is the dir for fixincludes. Put it just before |
604 | the files that we fix. */ | |
acf7262c | 605 | { GCC_INCLUDE_DIR, 0, 0 }, |
b0bbbd85 RS |
606 | /* For cross-compilation, this dir name is generated |
607 | automatically in Makefile.in. */ | |
acf7262c | 608 | { CROSS_INCLUDE_DIR, 0, 0 }, |
ee40befe | 609 | /* This is another place that the target system's headers might be. */ |
858072f0 | 610 | { TOOL_INCLUDE_DIR, 0, 0 }, |
b0bbbd85 | 611 | #else /* not CROSS_COMPILE */ |
acf7262c | 612 | /* This should be /usr/local/include and should come before |
ee40befe | 613 | the fixincludes-fixed header files. */ |
acf7262c | 614 | { LOCAL_INCLUDE_DIR, 0, 1 }, |
ceaff0a8 RS |
615 | /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here. |
616 | Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */ | |
858072f0 | 617 | { TOOL_INCLUDE_DIR, 0, 0 }, |
9d9dadd2 RS |
618 | /* This is the dir for fixincludes. Put it just before |
619 | the files that we fix. */ | |
acf7262c | 620 | { GCC_INCLUDE_DIR, 0, 0 }, |
b0bbbd85 RS |
621 | /* Some systems have an extra dir of include files. */ |
622 | #ifdef SYSTEM_INCLUDE_DIR | |
acf7262c | 623 | { SYSTEM_INCLUDE_DIR, 0, 0 }, |
b0bbbd85 | 624 | #endif |
acf7262c | 625 | { STANDARD_INCLUDE_DIR, 0, 0 }, |
b0bbbd85 | 626 | #endif /* not CROSS_COMPILE */ |
acf7262c | 627 | { 0, 0, 0 } |
b0bbbd85 RS |
628 | }; |
629 | #endif /* no INCLUDE_DEFAULTS */ | |
630 | ||
631 | /* The code looks at the defaults through this pointer, rather than through | |
632 | the constant structure above. This pointer gets changed if an environment | |
633 | variable specifies other defaults. */ | |
634 | static struct default_include *include_defaults = include_defaults_array; | |
635 | ||
636 | static struct file_name_list *include = 0; /* First dir to search */ | |
637 | /* First dir to search for <file> */ | |
6489924b RS |
638 | /* This is the first element to use for #include <...>. |
639 | If it is 0, use the entire chain for such includes. */ | |
b0bbbd85 | 640 | static struct file_name_list *first_bracket_include = 0; |
6489924b RS |
641 | /* This is the first element in the chain that corresponds to |
642 | a directory of system header files. */ | |
643 | static struct file_name_list *first_system_include = 0; | |
b0bbbd85 RS |
644 | static struct file_name_list *last_include = 0; /* Last in chain */ |
645 | ||
646 | /* Chain of include directories to put at the end of the other chain. */ | |
647 | static struct file_name_list *after_include = 0; | |
648 | static struct file_name_list *last_after_include = 0; /* Last in chain */ | |
649 | ||
b0866c74 JW |
650 | /* Chain to put at the start of the system include files. */ |
651 | static struct file_name_list *before_system = 0; | |
652 | static struct file_name_list *last_before_system = 0; /* Last in chain */ | |
653 | ||
b0bbbd85 RS |
654 | /* List of included files that contained #pragma once. */ |
655 | static struct file_name_list *dont_repeat_files = 0; | |
656 | ||
657 | /* List of other included files. | |
658 | If ->control_macro if nonzero, the file had a #ifndef | |
659 | around the entire contents, and ->control_macro gives the macro name. */ | |
660 | static struct file_name_list *all_include_files = 0; | |
661 | ||
bec42276 RS |
662 | /* Directory prefix that should replace `/usr' in the standard |
663 | include file directories. */ | |
664 | static char *include_prefix; | |
665 | ||
b0bbbd85 RS |
666 | /* Global list of strings read in from precompiled files. This list |
667 | is kept in the order the strings are read in, with new strings being | |
668 | added at the end through stringlist_tailp. We use this list to output | |
669 | the strings at the end of the run. | |
670 | */ | |
671 | static STRINGDEF *stringlist; | |
672 | static STRINGDEF **stringlist_tailp = &stringlist; | |
673 | ||
674 | ||
675 | /* Structure returned by create_definition */ | |
676 | typedef struct macrodef MACRODEF; | |
677 | struct macrodef | |
678 | { | |
679 | struct definition *defn; | |
680 | U_CHAR *symnam; | |
681 | int symlen; | |
682 | }; | |
b0bbbd85 | 683 | \f |
5bdc1512 RK |
684 | enum sharp_token_type { |
685 | NO_SHARP_TOKEN, /* token not present */ | |
686 | ||
687 | SHARP_TOKEN = '#', /* token spelled with # only */ | |
688 | WHITE_SHARP_TOKEN, /* token spelled with # and white space */ | |
689 | ||
690 | PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */ | |
691 | WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */ | |
692 | }; | |
693 | ||
b0bbbd85 RS |
694 | /* Structure allocated for every #define. For a simple replacement |
695 | such as | |
696 | #define foo bar , | |
697 | nargs = -1, the `pattern' list is null, and the expansion is just | |
698 | the replacement text. Nargs = 0 means a functionlike macro with no args, | |
699 | e.g., | |
700 | #define getchar() getc (stdin) . | |
701 | When there are args, the expansion is the replacement text with the | |
702 | args squashed out, and the reflist is a list describing how to | |
703 | build the output from the input: e.g., "3 chars, then the 1st arg, | |
704 | then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg". | |
705 | The chars here come from the expansion. Whatever is left of the | |
706 | expansion after the last arg-occurrence is copied after that arg. | |
707 | Note that the reflist can be arbitrarily long--- | |
708 | its length depends on the number of times the arguments appear in | |
709 | the replacement text, not how many args there are. Example: | |
710 | #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and | |
711 | pattern list | |
712 | { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL } | |
713 | where (x, y) means (nchars, argno). */ | |
714 | ||
715 | typedef struct definition DEFINITION; | |
716 | struct definition { | |
717 | int nargs; | |
718 | int length; /* length of expansion string */ | |
719 | int predefined; /* True if the macro was builtin or */ | |
720 | /* came from the command line */ | |
721 | U_CHAR *expansion; | |
722 | int line; /* Line number of definition */ | |
723 | char *file; /* File of definition */ | |
5ff1a832 | 724 | char rest_args; /* Nonzero if last arg. absorbs the rest */ |
b0bbbd85 RS |
725 | struct reflist { |
726 | struct reflist *next; | |
91dbf5e7 | 727 | |
5bdc1512 RK |
728 | enum sharp_token_type stringify; /* set if a # operator before arg */ |
729 | enum sharp_token_type raw_before; /* set if a ## operator before arg */ | |
730 | enum sharp_token_type raw_after; /* set if a ## operator after arg */ | |
91dbf5e7 | 731 | |
5ff1a832 | 732 | char rest_args; /* Nonzero if this arg. absorbs the rest */ |
b0bbbd85 RS |
733 | int nchars; /* Number of literal chars to copy before |
734 | this arg occurrence. */ | |
735 | int argno; /* Number of arg to substitute (origin-0) */ | |
736 | } *pattern; | |
737 | union { | |
738 | /* Names of macro args, concatenated in reverse order | |
739 | with comma-space between them. | |
740 | The only use of this is that we warn on redefinition | |
741 | if this differs between the old and new definitions. */ | |
742 | U_CHAR *argnames; | |
743 | } args; | |
744 | }; | |
745 | ||
746 | /* different kinds of things that can appear in the value field | |
747 | of a hash node. Actually, this may be useless now. */ | |
748 | union hashval { | |
b0bbbd85 RS |
749 | char *cpval; |
750 | DEFINITION *defn; | |
751 | KEYDEF *keydef; | |
752 | }; | |
753 | ||
5ff1a832 RS |
754 | /* |
755 | * special extension string that can be added to the last macro argument to | |
756 | * allow it to absorb the "rest" of the arguments when expanded. Ex: | |
ad0c9fa1 RS |
757 | * #define wow(a, b...) process (b, a, b) |
758 | * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); } | |
759 | * { wow (one, two); } -> { process (two, one, two); } | |
5ff1a832 | 760 | * if this "rest_arg" is used with the concat token '##' and if it is not |
f72aed24 | 761 | * supplied then the token attached to with ## will not be outputted. Ex: |
ad0c9fa1 RS |
762 | * #define wow (a, b...) process (b ## , a, ## b) |
763 | * { wow (1, 2); } -> { process (2, 1, 2); } | |
764 | * { wow (one); } -> { process (one); { | |
5ff1a832 RS |
765 | */ |
766 | static char rest_extension[] = "..."; | |
767 | #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1) | |
b0bbbd85 RS |
768 | |
769 | /* The structure of a node in the hash table. The hash table | |
adcfa681 | 770 | has entries for all tokens defined by #define directives (type T_MACRO), |
b0bbbd85 RS |
771 | plus some special tokens like __LINE__ (these each have their own |
772 | type, and the appropriate code is run when that type of node is seen. | |
773 | It does not contain control words like "#define", which are recognized | |
774 | by a separate piece of code. */ | |
775 | ||
776 | /* different flavors of hash nodes --- also used in keyword table */ | |
777 | enum node_type { | |
778 | T_DEFINE = 1, /* the `#define' keyword */ | |
779 | T_INCLUDE, /* the `#include' keyword */ | |
780 | T_INCLUDE_NEXT, /* the `#include_next' keyword */ | |
781 | T_IMPORT, /* the `#import' keyword */ | |
782 | T_IFDEF, /* the `#ifdef' keyword */ | |
783 | T_IFNDEF, /* the `#ifndef' keyword */ | |
784 | T_IF, /* the `#if' keyword */ | |
785 | T_ELSE, /* `#else' */ | |
786 | T_PRAGMA, /* `#pragma' */ | |
787 | T_ELIF, /* `#elif' */ | |
788 | T_UNDEF, /* `#undef' */ | |
789 | T_LINE, /* `#line' */ | |
790 | T_ERROR, /* `#error' */ | |
791 | T_WARNING, /* `#warning' */ | |
792 | T_ENDIF, /* `#endif' */ | |
793 | T_SCCS, /* `#sccs', used on system V. */ | |
794 | T_IDENT, /* `#ident', used on system V. */ | |
795 | T_ASSERT, /* `#assert', taken from system V. */ | |
796 | T_UNASSERT, /* `#unassert', taken from system V. */ | |
797 | T_SPECLINE, /* special symbol `__LINE__' */ | |
798 | T_DATE, /* `__DATE__' */ | |
799 | T_FILE, /* `__FILE__' */ | |
800 | T_BASE_FILE, /* `__BASE_FILE__' */ | |
801 | T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */ | |
802 | T_VERSION, /* `__VERSION__' */ | |
803 | T_SIZE_TYPE, /* `__SIZE_TYPE__' */ | |
804 | T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */ | |
805 | T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */ | |
0df69870 ILT |
806 | T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */ |
807 | T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */ | |
b0bbbd85 RS |
808 | T_TIME, /* `__TIME__' */ |
809 | T_CONST, /* Constant value, used by `__STDC__' */ | |
810 | T_MACRO, /* macro defined by `#define' */ | |
811 | T_DISABLED, /* macro temporarily turned off for rescan */ | |
812 | T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */ | |
813 | T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */ | |
814 | T_UNUSED /* Used for something not defined. */ | |
815 | }; | |
816 | ||
817 | struct hashnode { | |
818 | struct hashnode *next; /* double links for easy deletion */ | |
819 | struct hashnode *prev; | |
820 | struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash | |
821 | chain is kept, in case the node is the head | |
822 | of the chain and gets deleted. */ | |
823 | enum node_type type; /* type of special token */ | |
824 | int length; /* length of token, for quick comparison */ | |
825 | U_CHAR *name; /* the actual name */ | |
826 | union hashval value; /* pointer to expansion, or whatever */ | |
827 | }; | |
828 | ||
829 | typedef struct hashnode HASHNODE; | |
830 | ||
831 | /* Some definitions for the hash table. The hash function MUST be | |
832 | computed as shown in hashf () below. That is because the rescan | |
833 | loop computes the hash value `on the fly' for most tokens, | |
834 | in order to avoid the overhead of a lot of procedure calls to | |
835 | the hashf () function. Hashf () only exists for the sake of | |
836 | politeness, for use when speed isn't so important. */ | |
837 | ||
838 | #define HASHSIZE 1403 | |
839 | static HASHNODE *hashtab[HASHSIZE]; | |
840 | #define HASHSTEP(old, c) ((old << 2) + c) | |
841 | #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */ | |
842 | ||
843 | /* Symbols to predefine. */ | |
844 | ||
845 | #ifdef CPP_PREDEFINES | |
846 | static char *predefs = CPP_PREDEFINES; | |
847 | #else | |
848 | static char *predefs = ""; | |
849 | #endif | |
850 | \f | |
851 | /* We let tm.h override the types used here, to handle trivial differences | |
852 | such as the choice of unsigned int or long unsigned int for size_t. | |
853 | When machines start needing nontrivial differences in the size type, | |
854 | it would be best to do something here to figure out automatically | |
855 | from other information what type to use. */ | |
856 | ||
767d412c | 857 | /* The string value for __SIZE_TYPE__. */ |
b0bbbd85 RS |
858 | |
859 | #ifndef SIZE_TYPE | |
860 | #define SIZE_TYPE "long unsigned int" | |
861 | #endif | |
862 | ||
767d412c | 863 | /* The string value for __PTRDIFF_TYPE__. */ |
b0bbbd85 RS |
864 | |
865 | #ifndef PTRDIFF_TYPE | |
866 | #define PTRDIFF_TYPE "long int" | |
867 | #endif | |
868 | ||
767d412c | 869 | /* The string value for __WCHAR_TYPE__. */ |
b0bbbd85 RS |
870 | |
871 | #ifndef WCHAR_TYPE | |
872 | #define WCHAR_TYPE "int" | |
873 | #endif | |
767d412c JM |
874 | char * wchar_type = WCHAR_TYPE; |
875 | #undef WCHAR_TYPE | |
0df69870 ILT |
876 | |
877 | /* The string value for __USER_LABEL_PREFIX__ */ | |
878 | ||
879 | #ifndef USER_LABEL_PREFIX | |
880 | #define USER_LABEL_PREFIX "" | |
881 | #endif | |
882 | ||
883 | /* The string value for __REGISTER_PREFIX__ */ | |
884 | ||
885 | #ifndef REGISTER_PREFIX | |
886 | #define REGISTER_PREFIX "" | |
887 | #endif | |
b0bbbd85 RS |
888 | \f |
889 | /* In the definition of a #assert name, this structure forms | |
890 | a list of the individual values asserted. | |
891 | Each value is itself a list of "tokens". | |
892 | These are strings that are compared by name. */ | |
893 | ||
894 | struct tokenlist_list { | |
895 | struct tokenlist_list *next; | |
896 | struct arglist *tokens; | |
897 | }; | |
898 | ||
899 | struct assertion_hashnode { | |
900 | struct assertion_hashnode *next; /* double links for easy deletion */ | |
901 | struct assertion_hashnode *prev; | |
902 | /* also, a back pointer to this node's hash | |
903 | chain is kept, in case the node is the head | |
904 | of the chain and gets deleted. */ | |
905 | struct assertion_hashnode **bucket_hdr; | |
906 | int length; /* length of token, for quick comparison */ | |
907 | U_CHAR *name; /* the actual name */ | |
908 | /* List of token-sequences. */ | |
909 | struct tokenlist_list *value; | |
910 | }; | |
911 | ||
912 | typedef struct assertion_hashnode ASSERTION_HASHNODE; | |
913 | ||
914 | /* Some definitions for the hash table. The hash function MUST be | |
34a2d6f3 | 915 | computed as shown in hashf below. That is because the rescan |
b0bbbd85 RS |
916 | loop computes the hash value `on the fly' for most tokens, |
917 | in order to avoid the overhead of a lot of procedure calls to | |
34a2d6f3 | 918 | the hashf function. hashf only exists for the sake of |
b0bbbd85 RS |
919 | politeness, for use when speed isn't so important. */ |
920 | ||
921 | #define ASSERTION_HASHSIZE 37 | |
922 | static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE]; | |
923 | ||
924 | /* Nonzero means inhibit macroexpansion of what seem to be | |
925 | assertion tests, in rescan. For #if. */ | |
926 | static int assertions_flag; | |
927 | \f | |
928 | /* `struct directive' defines one #-directive, including how to handle it. */ | |
929 | ||
25cbb59e RK |
930 | #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *)) |
931 | ||
b0bbbd85 RS |
932 | struct directive { |
933 | int length; /* Length of name */ | |
25cbb59e | 934 | int (*func) DO_PROTO; /* Function to handle directive */ |
b0bbbd85 RS |
935 | char *name; /* Name of directive */ |
936 | enum node_type type; /* Code which describes which directive. */ | |
937 | char angle_brackets; /* Nonzero => <...> is special. */ | |
938 | char traditional_comments; /* Nonzero: keep comments if -traditional. */ | |
939 | char pass_thru; /* Copy preprocessed directive to output file. */ | |
940 | }; | |
941 | ||
25cbb59e RK |
942 | /* These functions are declared to return int instead of void since they |
943 | are going to be placed in the table and some old compilers have trouble with | |
944 | pointers to functions returning void. */ | |
945 | ||
946 | static int do_assert DO_PROTO; | |
947 | static int do_define DO_PROTO; | |
948 | static int do_elif DO_PROTO; | |
949 | static int do_else DO_PROTO; | |
950 | static int do_endif DO_PROTO; | |
951 | static int do_error DO_PROTO; | |
952 | static int do_ident DO_PROTO; | |
953 | static int do_if DO_PROTO; | |
954 | static int do_include DO_PROTO; | |
955 | static int do_line DO_PROTO; | |
956 | static int do_pragma DO_PROTO; | |
957 | #ifdef SCCS_DIRECTIVE | |
958 | static int do_sccs DO_PROTO; | |
959 | #endif | |
960 | static int do_unassert DO_PROTO; | |
961 | static int do_undef DO_PROTO; | |
962 | static int do_warning DO_PROTO; | |
963 | static int do_xifdef DO_PROTO; | |
964 | ||
b0bbbd85 RS |
965 | /* Here is the actual list of #-directives, most-often-used first. */ |
966 | ||
967 | static struct directive directive_table[] = { | |
968 | { 6, do_define, "define", T_DEFINE, 0, 1}, | |
969 | { 2, do_if, "if", T_IF}, | |
970 | { 5, do_xifdef, "ifdef", T_IFDEF}, | |
971 | { 6, do_xifdef, "ifndef", T_IFNDEF}, | |
972 | { 5, do_endif, "endif", T_ENDIF}, | |
973 | { 4, do_else, "else", T_ELSE}, | |
974 | { 4, do_elif, "elif", T_ELIF}, | |
975 | { 4, do_line, "line", T_LINE}, | |
976 | { 7, do_include, "include", T_INCLUDE, 1}, | |
977 | { 12, do_include, "include_next", T_INCLUDE_NEXT, 1}, | |
978 | { 6, do_include, "import", T_IMPORT, 1}, | |
979 | { 5, do_undef, "undef", T_UNDEF}, | |
980 | { 5, do_error, "error", T_ERROR}, | |
981 | { 7, do_warning, "warning", T_WARNING}, | |
982 | #ifdef SCCS_DIRECTIVE | |
983 | { 4, do_sccs, "sccs", T_SCCS}, | |
984 | #endif | |
985 | { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1}, | |
a3fb124a | 986 | { 5, do_ident, "ident", T_IDENT}, |
b0bbbd85 RS |
987 | { 6, do_assert, "assert", T_ASSERT}, |
988 | { 8, do_unassert, "unassert", T_UNASSERT}, | |
989 | { -1, 0, "", T_UNUSED}, | |
990 | }; | |
991 | ||
992 | /* When a directive handler is called, | |
91dbf5e7 | 993 | this points to the # (or the : of the %:) that started the directive. */ |
b0bbbd85 RS |
994 | U_CHAR *directive_start; |
995 | ||
996 | /* table to tell if char can be part of a C identifier. */ | |
997 | U_CHAR is_idchar[256]; | |
998 | /* table to tell if char can be first char of a c identifier. */ | |
999 | U_CHAR is_idstart[256]; | |
1000 | /* table to tell if c is horizontal space. */ | |
1001 | U_CHAR is_hor_space[256]; | |
1002 | /* table to tell if c is horizontal or vertical space. */ | |
1003 | static U_CHAR is_space[256]; | |
c03413c7 RK |
1004 | /* names of some characters */ |
1005 | static char *char_name[256]; | |
b0bbbd85 RS |
1006 | |
1007 | #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0) | |
1008 | #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0) | |
1009 | ||
1010 | static int errors = 0; /* Error counter for exit code */ | |
1011 | ||
d50c1d49 RS |
1012 | /* Name of output file, for error messages. */ |
1013 | static char *out_fname; | |
1014 | ||
b0bbbd85 RS |
1015 | /* Zero means dollar signs are punctuation. |
1016 | -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise. | |
1017 | This must be 0 for correct processing of this ANSI C program: | |
1018 | #define foo(a) #a | |
ad0c9fa1 | 1019 | #define lose(b) foo (b) |
b0bbbd85 | 1020 | #define test$ |
ad0c9fa1 | 1021 | lose (test) */ |
b0bbbd85 RS |
1022 | static int dollars_in_ident; |
1023 | #ifndef DOLLARS_IN_IDENTIFIERS | |
1024 | #define DOLLARS_IN_IDENTIFIERS 1 | |
1025 | #endif | |
1026 | ||
b0bbbd85 RS |
1027 | |
1028 | /* Stack of conditionals currently in progress | |
1029 | (including both successful and failing conditionals). */ | |
1030 | ||
1031 | struct if_stack { | |
1032 | struct if_stack *next; /* for chaining to the next stack frame */ | |
1033 | char *fname; /* copied from input when frame is made */ | |
1034 | int lineno; /* similarly */ | |
1035 | int if_succeeded; /* true if a leg of this if-group | |
1036 | has been passed through rescan */ | |
1037 | U_CHAR *control_macro; /* For #ifndef at start of file, | |
1038 | this is the macro name tested. */ | |
1039 | enum node_type type; /* type of last directive seen in this group */ | |
1040 | }; | |
1041 | typedef struct if_stack IF_STACK_FRAME; | |
1042 | static IF_STACK_FRAME *if_stack = NULL; | |
1043 | ||
1044 | /* Buffer of -M output. */ | |
1045 | static char *deps_buffer; | |
1046 | ||
1047 | /* Number of bytes allocated in above. */ | |
1048 | static int deps_allocated_size; | |
1049 | ||
1050 | /* Number of bytes used. */ | |
1051 | static int deps_size; | |
1052 | ||
1053 | /* Number of bytes since the last newline. */ | |
1054 | static int deps_column; | |
1055 | ||
b0bbbd85 RS |
1056 | /* Nonzero means -I- has been seen, |
1057 | so don't look for #include "foo" the source-file directory. */ | |
1058 | static int ignore_srcdir; | |
1059 | \f | |
25cbb59e RK |
1060 | static int safe_read PROTO((int, char *, int)); |
1061 | static void safe_write PROTO((int, char *, int)); | |
1062 | ||
1063 | int main PROTO((int, char **)); | |
1064 | ||
1065 | static void path_include PROTO((char *)); | |
1066 | ||
1067 | static U_CHAR *index0 PROTO((U_CHAR *, int, size_t)); | |
1068 | ||
1069 | static void trigraph_pcp PROTO((FILE_BUF *)); | |
1070 | ||
1071 | static void newline_fix PROTO((U_CHAR *)); | |
1072 | static void name_newline_fix PROTO((U_CHAR *)); | |
1073 | ||
1074 | static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *)); | |
1075 | ||
1076 | static void rescan PROTO((FILE_BUF *, int)); | |
1077 | ||
1078 | static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int)); | |
1079 | ||
1080 | static int handle_directive PROTO((FILE_BUF *, FILE_BUF *)); | |
1081 | ||
1082 | static struct tm *timestamp PROTO((void)); | |
1083 | static void special_symbol PROTO((HASHNODE *, FILE_BUF *)); | |
1084 | ||
1085 | static int redundant_include_p PROTO((char *)); | |
1086 | static is_system_include PROTO((char *)); | |
1087 | ||
1088 | static char *read_filename_string PROTO((int, FILE *)); | |
1089 | static struct file_name_map *read_name_map PROTO((char *)); | |
1090 | static int open_include_file PROTO((char *, struct file_name_list *)); | |
1091 | ||
1092 | static void finclude PROTO((int, char *, FILE_BUF *, int, struct file_name_list *)); | |
1093 | static void record_control_macro PROTO((char *, U_CHAR *)); | |
1094 | ||
1095 | static int import_hash PROTO((char *)); | |
1096 | static int lookup_import PROTO((char *, struct file_name_list *)); | |
1097 | static void add_import PROTO((int, char *)); | |
1098 | ||
1099 | static char *check_precompiled PROTO((int, char *, char **)); | |
1100 | static int check_preconditions PROTO((char *)); | |
1101 | static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *)); | |
1102 | static void pcstring_used PROTO((HASHNODE *)); | |
1103 | static void write_output PROTO((void)); | |
1104 | static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *)); | |
1105 | ||
1106 | static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
1107 | ||
1108 | static int check_macro_name PROTO((U_CHAR *, char *)); | |
1109 | static int compare_defs PROTO((DEFINITION *, DEFINITION *)); | |
1110 | static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int)); | |
1111 | ||
1112 | static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *)); | |
1113 | ||
1114 | int check_assertion PROTO((U_CHAR *, int, int, struct arglist *)); | |
1115 | static int compare_token_lists PROTO((struct arglist *, struct arglist *)); | |
1116 | ||
1117 | static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *)); | |
1118 | static void free_token_list PROTO((struct arglist *)); | |
1119 | ||
1120 | static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int)); | |
1121 | static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int)); | |
1122 | static void delete_assertion PROTO((ASSERTION_HASHNODE *)); | |
1123 | ||
1124 | static void do_once PROTO((void)); | |
1125 | ||
1126 | static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int)); | |
1127 | static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *)); | |
1128 | static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *)); | |
1129 | static void validate_else PROTO((U_CHAR *)); | |
1130 | ||
1131 | static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int)); | |
1132 | static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *)); | |
1133 | static char *quote_string PROTO((char *, char *)); | |
1134 | static U_CHAR *skip_paren_group PROTO((FILE_BUF *)); | |
1135 | ||
1136 | /* Last arg to output_line_directive. */ | |
1137 | enum file_change_code {same_file, enter_file, leave_file}; | |
1138 | static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code)); | |
1139 | ||
1140 | static void macroexpand PROTO((HASHNODE *, FILE_BUF *)); | |
1141 | ||
1142 | struct argdata; | |
1143 | static char *macarg PROTO((struct argdata *, int)); | |
1144 | ||
1145 | static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int)); | |
1146 | ||
1147 | static int discard_comments PROTO((U_CHAR *, int, int)); | |
1148 | ||
1149 | static int change_newlines PROTO((U_CHAR *, int)); | |
1150 | ||
1151 | char *my_strerror PROTO((int)); | |
1152 | void error PRINTF_PROTO_1((char *, ...)); | |
1153 | static void verror PROTO((char *, va_list)); | |
1154 | static void error_from_errno PROTO((char *)); | |
1155 | void warning PRINTF_PROTO_1((char *, ...)); | |
1156 | static void vwarning PROTO((char *, va_list)); | |
1157 | static void error_with_line PRINTF_PROTO_2((int, char *, ...)); | |
1158 | static void verror_with_line PROTO((int, char *, va_list)); | |
1159 | static void vwarning_with_line PROTO((int, char *, va_list)); | |
1160 | void pedwarn PRINTF_PROTO_1((char *, ...)); | |
1161 | void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...)); | |
1162 | static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...)); | |
1163 | ||
1164 | static void print_containing_files PROTO((void)); | |
1165 | ||
1166 | static int line_for_error PROTO((int)); | |
1167 | static int grow_outbuf PROTO((FILE_BUF *, int)); | |
1168 | ||
1169 | static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int)); | |
1170 | HASHNODE *lookup PROTO((U_CHAR *, int, int)); | |
1171 | static void delete_macro PROTO((HASHNODE *)); | |
1172 | static int hashf PROTO((U_CHAR *, int, int)); | |
1173 | ||
1174 | static void dump_single_macro PROTO((HASHNODE *, FILE *)); | |
1175 | static void dump_all_macros PROTO((void)); | |
1176 | static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *)); | |
1177 | static void dump_arg_n PROTO((DEFINITION *, int, FILE *)); | |
1178 | ||
1179 | static void initialize_char_syntax PROTO((void)); | |
1180 | static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *)); | |
1181 | ||
1182 | static void make_definition PROTO((char *, FILE_BUF *)); | |
1183 | static void make_undef PROTO((char *, FILE_BUF *)); | |
1184 | ||
1185 | static void make_assertion PROTO((char *, char *)); | |
1186 | ||
1187 | static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *)); | |
1188 | ||
1189 | static void deps_output PROTO((char *, int)); | |
1190 | ||
1191 | static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn)); | |
1192 | void fancy_abort PROTO((void)) __attribute__ ((noreturn)); | |
1193 | static void perror_with_name PROTO((char *)); | |
1194 | static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn)); | |
1195 | static void pipe_closed PROTO((int)) __attribute__ ((noreturn)); | |
1196 | ||
1197 | static void memory_full PROTO((void)) __attribute__ ((noreturn)); | |
1198 | GENERIC_PTR xmalloc PROTO((size_t)); | |
1199 | static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t)); | |
1200 | static GENERIC_PTR xcalloc PROTO((size_t, size_t)); | |
1201 | static char *savestring PROTO((char *)); | |
1202 | ||
1203 | static int file_size_and_mode PROTO((int, int *, long int *)); | |
1204 | static void output_dots PROTO((FILE *, int)); | |
1205 | \f | |
bb7de822 | 1206 | /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME, |
53e52f00 RK |
1207 | retrying if necessary. Return a negative value if an error occurs, |
1208 | otherwise return the actual number of bytes read, | |
1209 | which must be LEN unless end-of-file was reached. */ | |
bb7de822 RS |
1210 | |
1211 | static int | |
1212 | safe_read (desc, ptr, len) | |
1213 | int desc; | |
1214 | char *ptr; | |
1215 | int len; | |
1216 | { | |
1217 | int left = len; | |
1218 | while (left > 0) { | |
1219 | int nchars = read (desc, ptr, left); | |
1220 | if (nchars < 0) | |
c9a8a295 RS |
1221 | { |
1222 | #ifdef EINTR | |
1223 | if (errno == EINTR) | |
1224 | continue; | |
1225 | #endif | |
1226 | return nchars; | |
1227 | } | |
bb7de822 RS |
1228 | if (nchars == 0) |
1229 | break; | |
1230 | ptr += nchars; | |
1231 | left -= nchars; | |
1232 | } | |
1233 | return len - left; | |
1234 | } | |
1235 | ||
1236 | /* Write LEN bytes at PTR to descriptor DESC, | |
1237 | retrying if necessary, and treating any real error as fatal. */ | |
1238 | ||
1239 | static void | |
1240 | safe_write (desc, ptr, len) | |
1241 | int desc; | |
1242 | char *ptr; | |
1243 | int len; | |
1244 | { | |
1245 | while (len > 0) { | |
1246 | int written = write (desc, ptr, len); | |
1247 | if (written < 0) | |
c9a8a295 RS |
1248 | { |
1249 | #ifdef EINTR | |
1250 | if (errno == EINTR) | |
1251 | continue; | |
1252 | #endif | |
1253 | pfatal_with_name (out_fname); | |
1254 | } | |
bb7de822 RS |
1255 | ptr += written; |
1256 | len -= written; | |
1257 | } | |
1258 | } | |
1259 | \f | |
b0bbbd85 RS |
1260 | int |
1261 | main (argc, argv) | |
1262 | int argc; | |
1263 | char **argv; | |
1264 | { | |
1265 | int st_mode; | |
1266 | long st_size; | |
d50c1d49 | 1267 | char *in_fname; |
25cbb59e | 1268 | char *cp; |
b0bbbd85 RS |
1269 | int f, i; |
1270 | FILE_BUF *fp; | |
1271 | char **pend_files = (char **) xmalloc (argc * sizeof (char *)); | |
1272 | char **pend_defs = (char **) xmalloc (argc * sizeof (char *)); | |
1273 | char **pend_undefs = (char **) xmalloc (argc * sizeof (char *)); | |
1274 | char **pend_assertions = (char **) xmalloc (argc * sizeof (char *)); | |
1275 | char **pend_includes = (char **) xmalloc (argc * sizeof (char *)); | |
1276 | ||
1277 | /* Record the option used with each element of pend_assertions. | |
1278 | This is preparation for supporting more than one option for making | |
1279 | an assertion. */ | |
1280 | char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *)); | |
1281 | int inhibit_predefs = 0; | |
1282 | int no_standard_includes = 0; | |
2378088a | 1283 | int no_standard_cplusplus_includes = 0; |
b0bbbd85 RS |
1284 | int missing_newline = 0; |
1285 | ||
1286 | /* Non-0 means don't output the preprocessed program. */ | |
1287 | int inhibit_output = 0; | |
e6157ab4 RS |
1288 | /* Non-0 means -v, so print the full set of include dirs. */ |
1289 | int verbose = 0; | |
b0bbbd85 | 1290 | |
566609f8 RS |
1291 | /* File name which deps are being written to. |
1292 | This is 0 if deps are being written to stdout. */ | |
1293 | char *deps_file = 0; | |
d0691cfb RS |
1294 | /* Fopen file mode to open deps_file with. */ |
1295 | char *deps_mode = "a"; | |
b0bbbd85 RS |
1296 | /* Stream on which to print the dependency information. */ |
1297 | FILE *deps_stream = 0; | |
1298 | /* Target-name to write with the dependency information. */ | |
1299 | char *deps_target = 0; | |
1300 | ||
1301 | #ifdef RLIMIT_STACK | |
1302 | /* Get rid of any avoidable limit on stack size. */ | |
1303 | { | |
1304 | struct rlimit rlim; | |
1305 | ||
1306 | /* Set the stack limit huge so that alloca (particularly stringtab | |
1307 | * in dbxread.c) does not fail. */ | |
1308 | getrlimit (RLIMIT_STACK, &rlim); | |
1309 | rlim.rlim_cur = rlim.rlim_max; | |
1310 | setrlimit (RLIMIT_STACK, &rlim); | |
1311 | } | |
1312 | #endif /* RLIMIT_STACK defined */ | |
1313 | ||
0e576005 | 1314 | #ifdef SIGPIPE |
9e263fc4 | 1315 | signal (SIGPIPE, pipe_closed); |
0e576005 | 1316 | #endif |
9e263fc4 | 1317 | |
25cbb59e RK |
1318 | cp = argv[0] + strlen (argv[0]); |
1319 | while (cp != argv[0] && cp[-1] != '/' | |
7cc322e3 | 1320 | #ifdef DIR_SEPARATOR |
25cbb59e | 1321 | && cp[-1] != DIR_SEPARATOR |
7cc322e3 RK |
1322 | #endif |
1323 | ) | |
25cbb59e RK |
1324 | --cp; |
1325 | progname = cp; | |
3dac0de5 | 1326 | |
b0bbbd85 RS |
1327 | #ifdef VMS |
1328 | { | |
1329 | /* Remove directories from PROGNAME. */ | |
25cbb59e | 1330 | char *p; |
71efde97 RK |
1331 | char *s = progname; |
1332 | ||
1333 | if ((p = rindex (s, ':')) != 0) s = p + 1; /* skip device */ | |
1334 | if ((p = rindex (s, ']')) != 0) s = p + 1; /* skip directory */ | |
1335 | if ((p = rindex (s, '>')) != 0) s = p + 1; /* alternate (int'n'l) dir */ | |
1336 | s = progname = savestring (s); | |
1337 | if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */ | |
1338 | if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */ | |
1339 | && (p[1] == 'e' || p[1] == 'E') | |
1340 | && (p[2] == 'x' || p[2] == 'X') | |
1341 | && (p[3] == 'e' || p[3] == 'E') | |
1342 | && !p[4]) | |
1343 | *p = '\0'; | |
b0bbbd85 RS |
1344 | } |
1345 | #endif | |
1346 | ||
1347 | in_fname = NULL; | |
1348 | out_fname = NULL; | |
1349 | ||
1350 | /* Initialize is_idchar to allow $. */ | |
1351 | dollars_in_ident = 1; | |
1352 | initialize_char_syntax (); | |
1353 | dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0; | |
1354 | ||
adcfa681 | 1355 | no_line_directives = 0; |
b0bbbd85 RS |
1356 | no_trigraphs = 1; |
1357 | dump_macros = dump_none; | |
1358 | no_output = 0; | |
1359 | cplusplus = 0; | |
eda5fa7b | 1360 | cplusplus_comments = 0; |
b0bbbd85 | 1361 | |
4c9a05bc RK |
1362 | bzero ((char *) pend_files, argc * sizeof (char *)); |
1363 | bzero ((char *) pend_defs, argc * sizeof (char *)); | |
1364 | bzero ((char *) pend_undefs, argc * sizeof (char *)); | |
1365 | bzero ((char *) pend_assertions, argc * sizeof (char *)); | |
1366 | bzero ((char *) pend_includes, argc * sizeof (char *)); | |
b0bbbd85 RS |
1367 | |
1368 | /* Process switches and find input file name. */ | |
1369 | ||
1370 | for (i = 1; i < argc; i++) { | |
1371 | if (argv[i][0] != '-') { | |
1372 | if (out_fname != NULL) | |
1373 | fatal ("Usage: %s [switches] input output", argv[0]); | |
1374 | else if (in_fname != NULL) | |
1375 | out_fname = argv[i]; | |
1376 | else | |
1377 | in_fname = argv[i]; | |
1378 | } else { | |
1379 | switch (argv[i][1]) { | |
1380 | ||
1381 | case 'i': | |
1382 | if (!strcmp (argv[i], "-include")) { | |
1383 | if (i + 1 == argc) | |
d0691cfb | 1384 | fatal ("Filename missing after `-include' option"); |
b0bbbd85 RS |
1385 | else |
1386 | pend_includes[i] = argv[i+1], i++; | |
1387 | } | |
1388 | if (!strcmp (argv[i], "-imacros")) { | |
1389 | if (i + 1 == argc) | |
d0691cfb | 1390 | fatal ("Filename missing after `-imacros' option"); |
b0bbbd85 RS |
1391 | else |
1392 | pend_files[i] = argv[i+1], i++; | |
1393 | } | |
bec42276 RS |
1394 | if (!strcmp (argv[i], "-iprefix")) { |
1395 | if (i + 1 == argc) | |
d0691cfb | 1396 | fatal ("Filename missing after `-iprefix' option"); |
bec42276 RS |
1397 | else |
1398 | include_prefix = argv[++i]; | |
1399 | } | |
bbd4b75b RS |
1400 | if (!strcmp (argv[i], "-ifoutput")) { |
1401 | output_conditionals = 1; | |
1402 | } | |
b0866c74 JW |
1403 | if (!strcmp (argv[i], "-isystem")) { |
1404 | struct file_name_list *dirtmp; | |
1405 | ||
1406 | if (i + 1 == argc) | |
1407 | fatal ("Filename missing after `-isystem' option"); | |
1408 | ||
1409 | dirtmp = (struct file_name_list *) | |
1410 | xmalloc (sizeof (struct file_name_list)); | |
1411 | dirtmp->next = 0; | |
1412 | dirtmp->control_macro = 0; | |
1413 | dirtmp->c_system_include_path = 1; | |
25cbb59e | 1414 | dirtmp->fname = xmalloc (strlen (argv[i+1]) + 1); |
b0866c74 JW |
1415 | strcpy (dirtmp->fname, argv[++i]); |
1416 | dirtmp->got_name_map = 0; | |
1417 | ||
1418 | if (before_system == 0) | |
1419 | before_system = dirtmp; | |
1420 | else | |
1421 | last_before_system->next = dirtmp; | |
1422 | last_before_system = dirtmp; /* Tail follows the last one */ | |
1423 | } | |
d0691cfb RS |
1424 | /* Add directory to end of path for includes, |
1425 | with the default prefix at the front of its name. */ | |
1426 | if (!strcmp (argv[i], "-iwithprefix")) { | |
1427 | struct file_name_list *dirtmp; | |
d19184d6 RS |
1428 | char *prefix; |
1429 | ||
1430 | if (include_prefix != 0) | |
1431 | prefix = include_prefix; | |
1432 | else { | |
1433 | prefix = savestring (GCC_INCLUDE_DIR); | |
1434 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
1435 | if (!strcmp (prefix + strlen (prefix) - 8, "/include")) | |
1436 | prefix[strlen (prefix) - 7] = 0; | |
1437 | } | |
d0691cfb RS |
1438 | |
1439 | dirtmp = (struct file_name_list *) | |
1440 | xmalloc (sizeof (struct file_name_list)); | |
1441 | dirtmp->next = 0; /* New one goes on the end */ | |
1442 | dirtmp->control_macro = 0; | |
d2a22862 | 1443 | dirtmp->c_system_include_path = 0; |
d0691cfb RS |
1444 | if (i + 1 == argc) |
1445 | fatal ("Directory name missing after `-iwithprefix' option"); | |
1446 | ||
25cbb59e | 1447 | dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1); |
d19184d6 | 1448 | strcpy (dirtmp->fname, prefix); |
d0691cfb | 1449 | strcat (dirtmp->fname, argv[++i]); |
6e7f952e | 1450 | dirtmp->got_name_map = 0; |
d0691cfb RS |
1451 | |
1452 | if (after_include == 0) | |
1453 | after_include = dirtmp; | |
1454 | else | |
1455 | last_after_include->next = dirtmp; | |
1456 | last_after_include = dirtmp; /* Tail follows the last one */ | |
1457 | } | |
d19184d6 RS |
1458 | /* Add directory to main path for includes, |
1459 | with the default prefix at the front of its name. */ | |
1460 | if (!strcmp (argv[i], "-iwithprefixbefore")) { | |
1461 | struct file_name_list *dirtmp; | |
1462 | char *prefix; | |
1463 | ||
1464 | if (include_prefix != 0) | |
1465 | prefix = include_prefix; | |
1466 | else { | |
1467 | prefix = savestring (GCC_INCLUDE_DIR); | |
1468 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
1469 | if (!strcmp (prefix + strlen (prefix) - 8, "/include")) | |
1470 | prefix[strlen (prefix) - 7] = 0; | |
1471 | } | |
1472 | ||
1473 | dirtmp = (struct file_name_list *) | |
1474 | xmalloc (sizeof (struct file_name_list)); | |
1475 | dirtmp->next = 0; /* New one goes on the end */ | |
1476 | dirtmp->control_macro = 0; | |
d2a22862 | 1477 | dirtmp->c_system_include_path = 0; |
d19184d6 RS |
1478 | if (i + 1 == argc) |
1479 | fatal ("Directory name missing after `-iwithprefixbefore' option"); | |
1480 | ||
25cbb59e | 1481 | dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1); |
d19184d6 RS |
1482 | strcpy (dirtmp->fname, prefix); |
1483 | strcat (dirtmp->fname, argv[++i]); | |
6e7f952e | 1484 | dirtmp->got_name_map = 0; |
d19184d6 RS |
1485 | |
1486 | append_include_chain (dirtmp, dirtmp); | |
1487 | } | |
b0bbbd85 RS |
1488 | /* Add directory to end of path for includes. */ |
1489 | if (!strcmp (argv[i], "-idirafter")) { | |
1490 | struct file_name_list *dirtmp; | |
1491 | ||
1492 | dirtmp = (struct file_name_list *) | |
1493 | xmalloc (sizeof (struct file_name_list)); | |
1494 | dirtmp->next = 0; /* New one goes on the end */ | |
1495 | dirtmp->control_macro = 0; | |
d2a22862 | 1496 | dirtmp->c_system_include_path = 0; |
b0bbbd85 | 1497 | if (i + 1 == argc) |
d0691cfb | 1498 | fatal ("Directory name missing after `-idirafter' option"); |
b0bbbd85 RS |
1499 | else |
1500 | dirtmp->fname = argv[++i]; | |
6e7f952e | 1501 | dirtmp->got_name_map = 0; |
b0bbbd85 | 1502 | |
6489924b RS |
1503 | if (after_include == 0) |
1504 | after_include = dirtmp; | |
1505 | else | |
1506 | last_after_include->next = dirtmp; | |
1507 | last_after_include = dirtmp; /* Tail follows the last one */ | |
b0bbbd85 RS |
1508 | } |
1509 | break; | |
1510 | ||
1511 | case 'o': | |
1512 | if (out_fname != NULL) | |
1513 | fatal ("Output filename specified twice"); | |
1514 | if (i + 1 == argc) | |
1515 | fatal ("Filename missing after -o option"); | |
1516 | out_fname = argv[++i]; | |
1517 | if (!strcmp (out_fname, "-")) | |
1518 | out_fname = ""; | |
1519 | break; | |
1520 | ||
1521 | case 'p': | |
1522 | if (!strcmp (argv[i], "-pedantic")) | |
1523 | pedantic = 1; | |
1524 | else if (!strcmp (argv[i], "-pedantic-errors")) { | |
1525 | pedantic = 1; | |
1526 | pedantic_errors = 1; | |
1527 | } else if (!strcmp (argv[i], "-pcp")) { | |
115ee359 RK |
1528 | char *pcp_fname; |
1529 | if (i + 1 == argc) | |
1530 | fatal ("Filename missing after -pcp option"); | |
1531 | pcp_fname = argv[++i]; | |
b0bbbd85 RS |
1532 | pcp_outfile = |
1533 | ((pcp_fname[0] != '-' || pcp_fname[1] != '\0') | |
1534 | ? fopen (pcp_fname, "w") | |
25cbb59e | 1535 | : stdout); |
b0bbbd85 RS |
1536 | if (pcp_outfile == 0) |
1537 | pfatal_with_name (pcp_fname); | |
1538 | no_precomp = 1; | |
1539 | } | |
1540 | break; | |
1541 | ||
1542 | case 't': | |
1543 | if (!strcmp (argv[i], "-traditional")) { | |
1544 | traditional = 1; | |
1545 | if (dollars_in_ident > 0) | |
1546 | dollars_in_ident = 1; | |
1547 | } else if (!strcmp (argv[i], "-trigraphs")) { | |
1548 | no_trigraphs = 0; | |
1549 | } | |
1550 | break; | |
1551 | ||
1552 | case 'l': | |
1553 | if (! strcmp (argv[i], "-lang-c")) | |
eda5fa7b | 1554 | cplusplus = 0, cplusplus_comments = 0, objc = 0; |
b0bbbd85 | 1555 | if (! strcmp (argv[i], "-lang-c++")) |
eda5fa7b | 1556 | cplusplus = 1, cplusplus_comments = 1, objc = 0; |
45149828 RS |
1557 | if (! strcmp (argv[i], "-lang-c-c++-comments")) |
1558 | cplusplus = 0, cplusplus_comments = 1, objc = 0; | |
b0bbbd85 | 1559 | if (! strcmp (argv[i], "-lang-objc")) |
eda5fa7b | 1560 | objc = 1, cplusplus = 0, cplusplus_comments = 1; |
b0bbbd85 | 1561 | if (! strcmp (argv[i], "-lang-objc++")) |
eda5fa7b | 1562 | objc = 1, cplusplus = 1, cplusplus_comments = 1; |
b0bbbd85 RS |
1563 | if (! strcmp (argv[i], "-lang-asm")) |
1564 | lang_asm = 1; | |
1565 | if (! strcmp (argv[i], "-lint")) | |
253245df | 1566 | for_lint = 1; |
b0bbbd85 RS |
1567 | break; |
1568 | ||
1569 | case '+': | |
eda5fa7b | 1570 | cplusplus = 1, cplusplus_comments = 1; |
b0bbbd85 RS |
1571 | break; |
1572 | ||
1573 | case 'w': | |
1574 | inhibit_warnings = 1; | |
1575 | break; | |
1576 | ||
1577 | case 'W': | |
1578 | if (!strcmp (argv[i], "-Wtrigraphs")) | |
1579 | warn_trigraphs = 1; | |
1580 | else if (!strcmp (argv[i], "-Wno-trigraphs")) | |
1581 | warn_trigraphs = 0; | |
1582 | else if (!strcmp (argv[i], "-Wcomment")) | |
1583 | warn_comments = 1; | |
1584 | else if (!strcmp (argv[i], "-Wno-comment")) | |
1585 | warn_comments = 0; | |
1586 | else if (!strcmp (argv[i], "-Wcomments")) | |
1587 | warn_comments = 1; | |
1588 | else if (!strcmp (argv[i], "-Wno-comments")) | |
1589 | warn_comments = 0; | |
1590 | else if (!strcmp (argv[i], "-Wtraditional")) | |
1591 | warn_stringify = 1; | |
1592 | else if (!strcmp (argv[i], "-Wno-traditional")) | |
1593 | warn_stringify = 0; | |
2aa7ec37 RS |
1594 | else if (!strcmp (argv[i], "-Wimport")) |
1595 | warn_import = 1; | |
1596 | else if (!strcmp (argv[i], "-Wno-import")) | |
1597 | warn_import = 0; | |
b0bbbd85 RS |
1598 | else if (!strcmp (argv[i], "-Werror")) |
1599 | warnings_are_errors = 1; | |
1600 | else if (!strcmp (argv[i], "-Wno-error")) | |
1601 | warnings_are_errors = 0; | |
1602 | else if (!strcmp (argv[i], "-Wall")) | |
1603 | { | |
1604 | warn_trigraphs = 1; | |
1605 | warn_comments = 1; | |
1606 | } | |
1607 | break; | |
1608 | ||
1609 | case 'M': | |
e648e0e3 | 1610 | /* The style of the choices here is a bit mixed. |
86739c7b DE |
1611 | The chosen scheme is a hybrid of keeping all options in one string |
1612 | and specifying each option in a separate argument: | |
1613 | -M|-MM|-MD file|-MMD file [-MG]. An alternative is: | |
e648e0e3 DE |
1614 | -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely: |
1615 | -M[M][G][D file]. This is awkward to handle in specs, and is not | |
1616 | as extensible. */ | |
1617 | /* ??? -MG must be specified in addition to one of -M or -MM. | |
86739c7b DE |
1618 | This can be relaxed in the future without breaking anything. |
1619 | The converse isn't true. */ | |
1620 | ||
e648e0e3 | 1621 | /* -MG isn't valid with -MD or -MMD. This is checked for later. */ |
86739c7b DE |
1622 | if (!strcmp (argv[i], "-MG")) |
1623 | { | |
1624 | print_deps_missing_files = 1; | |
1625 | break; | |
1626 | } | |
b0bbbd85 RS |
1627 | if (!strcmp (argv[i], "-M")) |
1628 | print_deps = 2; | |
1629 | else if (!strcmp (argv[i], "-MM")) | |
1630 | print_deps = 1; | |
1631 | else if (!strcmp (argv[i], "-MD")) | |
1632 | print_deps = 2; | |
1633 | else if (!strcmp (argv[i], "-MMD")) | |
1634 | print_deps = 1; | |
1635 | /* For -MD and -MMD options, write deps on file named by next arg. */ | |
1636 | if (!strcmp (argv[i], "-MD") | |
1637 | || !strcmp (argv[i], "-MMD")) { | |
115ee359 RK |
1638 | if (i + 1 == argc) |
1639 | fatal ("Filename missing after %s option", argv[i]); | |
b0bbbd85 RS |
1640 | i++; |
1641 | deps_file = argv[i]; | |
d0691cfb | 1642 | deps_mode = "w"; |
b0bbbd85 RS |
1643 | } else { |
1644 | /* For -M and -MM, write deps on standard output | |
1645 | and suppress the usual output. */ | |
1646 | deps_stream = stdout; | |
1647 | inhibit_output = 1; | |
1648 | } | |
1649 | break; | |
1650 | ||
1651 | case 'd': | |
1652 | { | |
1653 | char *p = argv[i] + 2; | |
1654 | char c; | |
25cbb59e | 1655 | while ((c = *p++)) { |
b0bbbd85 RS |
1656 | /* Arg to -d specifies what parts of macros to dump */ |
1657 | switch (c) { | |
1658 | case 'M': | |
1659 | dump_macros = dump_only; | |
1660 | no_output = 1; | |
1661 | break; | |
1662 | case 'N': | |
1663 | dump_macros = dump_names; | |
1664 | break; | |
1665 | case 'D': | |
1666 | dump_macros = dump_definitions; | |
1667 | break; | |
1668 | } | |
1669 | } | |
1670 | } | |
1671 | break; | |
1672 | ||
1673 | case 'g': | |
1674 | if (argv[i][2] == '3') | |
1675 | debug_output = 1; | |
1676 | break; | |
1677 | ||
1678 | case 'v': | |
1679 | fprintf (stderr, "GNU CPP version %s", version_string); | |
1680 | #ifdef TARGET_VERSION | |
1681 | TARGET_VERSION; | |
1682 | #endif | |
1683 | fprintf (stderr, "\n"); | |
e6157ab4 | 1684 | verbose = 1; |
b0bbbd85 RS |
1685 | break; |
1686 | ||
1687 | case 'H': | |
1688 | print_include_names = 1; | |
1689 | break; | |
1690 | ||
1691 | case 'D': | |
2af5e9e2 RK |
1692 | if (argv[i][2] != 0) |
1693 | pend_defs[i] = argv[i] + 2; | |
1694 | else if (i + 1 == argc) | |
1695 | fatal ("Macro name missing after -D option"); | |
1696 | else | |
1697 | i++, pend_defs[i] = argv[i]; | |
b0bbbd85 RS |
1698 | break; |
1699 | ||
1700 | case 'A': | |
1701 | { | |
2af5e9e2 | 1702 | char *p; |
b0bbbd85 RS |
1703 | |
1704 | if (argv[i][2] != 0) | |
1705 | p = argv[i] + 2; | |
1706 | else if (i + 1 == argc) | |
1707 | fatal ("Assertion missing after -A option"); | |
1708 | else | |
1709 | p = argv[++i]; | |
1710 | ||
1711 | if (!strcmp (p, "-")) { | |
1712 | /* -A- eliminates all predefined macros and assertions. | |
1713 | Let's include also any that were specified earlier | |
1714 | on the command line. That way we can get rid of any | |
1715 | that were passed automatically in from GCC. */ | |
1716 | int j; | |
1717 | inhibit_predefs = 1; | |
1718 | for (j = 0; j < i; j++) | |
1719 | pend_defs[j] = pend_assertions[j] = 0; | |
1720 | } else { | |
1721 | pend_assertions[i] = p; | |
1722 | pend_assertion_options[i] = "-A"; | |
1723 | } | |
1724 | } | |
1725 | break; | |
1726 | ||
1727 | case 'U': /* JF #undef something */ | |
1728 | if (argv[i][2] != 0) | |
1729 | pend_undefs[i] = argv[i] + 2; | |
1730 | else if (i + 1 == argc) | |
1731 | fatal ("Macro name missing after -U option"); | |
1732 | else | |
1733 | pend_undefs[i] = argv[i+1], i++; | |
1734 | break; | |
1735 | ||
1736 | case 'C': | |
1737 | put_out_comments = 1; | |
1738 | break; | |
1739 | ||
1740 | case 'E': /* -E comes from cc -E; ignore it. */ | |
1741 | break; | |
1742 | ||
1743 | case 'P': | |
adcfa681 | 1744 | no_line_directives = 1; |
b0bbbd85 RS |
1745 | break; |
1746 | ||
1747 | case '$': /* Don't include $ in identifiers. */ | |
1748 | dollars_in_ident = 0; | |
1749 | break; | |
1750 | ||
1751 | case 'I': /* Add directory to path for includes. */ | |
1752 | { | |
1753 | struct file_name_list *dirtmp; | |
1754 | ||
6489924b | 1755 | if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) { |
b0bbbd85 | 1756 | ignore_srcdir = 1; |
6489924b RS |
1757 | /* Don't use any preceding -I directories for #include <...>. */ |
1758 | first_bracket_include = 0; | |
1759 | } | |
b0bbbd85 RS |
1760 | else { |
1761 | dirtmp = (struct file_name_list *) | |
1762 | xmalloc (sizeof (struct file_name_list)); | |
1763 | dirtmp->next = 0; /* New one goes on the end */ | |
1764 | dirtmp->control_macro = 0; | |
d2a22862 | 1765 | dirtmp->c_system_include_path = 0; |
b0bbbd85 RS |
1766 | if (argv[i][2] != 0) |
1767 | dirtmp->fname = argv[i] + 2; | |
1768 | else if (i + 1 == argc) | |
1769 | fatal ("Directory name missing after -I option"); | |
1770 | else | |
1771 | dirtmp->fname = argv[++i]; | |
6e7f952e | 1772 | dirtmp->got_name_map = 0; |
6489924b RS |
1773 | append_include_chain (dirtmp, dirtmp); |
1774 | } | |
b0bbbd85 RS |
1775 | } |
1776 | break; | |
1777 | ||
1778 | case 'n': | |
1779 | if (!strcmp (argv[i], "-nostdinc")) | |
1780 | /* -nostdinc causes no default include directories. | |
1781 | You must specify all include-file directories with -I. */ | |
1782 | no_standard_includes = 1; | |
2378088a PB |
1783 | else if (!strcmp (argv[i], "-nostdinc++")) |
1784 | /* -nostdinc++ causes no default C++-specific include directories. */ | |
1785 | no_standard_cplusplus_includes = 1; | |
b0bbbd85 RS |
1786 | else if (!strcmp (argv[i], "-noprecomp")) |
1787 | no_precomp = 1; | |
1788 | break; | |
1789 | ||
1790 | case 'u': | |
1791 | /* Sun compiler passes undocumented switch "-undef". | |
1792 | Let's assume it means to inhibit the predefined symbols. */ | |
1793 | inhibit_predefs = 1; | |
1794 | break; | |
1795 | ||
1796 | case '\0': /* JF handle '-' as file name meaning stdin or stdout */ | |
1797 | if (in_fname == NULL) { | |
1798 | in_fname = ""; | |
1799 | break; | |
1800 | } else if (out_fname == NULL) { | |
1801 | out_fname = ""; | |
1802 | break; | |
1803 | } /* else fall through into error */ | |
1804 | ||
1805 | default: | |
1806 | fatal ("Invalid option `%s'", argv[i]); | |
1807 | } | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | /* Add dirs from CPATH after dirs from -I. */ | |
1812 | /* There seems to be confusion about what CPATH should do, | |
1813 | so for the moment it is not documented. */ | |
1814 | /* Some people say that CPATH should replace the standard include dirs, | |
1815 | but that seems pointless: it comes before them, so it overrides them | |
1816 | anyway. */ | |
25cbb59e RK |
1817 | cp = getenv ("CPATH"); |
1818 | if (cp && ! no_standard_includes) | |
1819 | path_include (cp); | |
b0bbbd85 RS |
1820 | |
1821 | /* Now that dollars_in_ident is known, initialize is_idchar. */ | |
1822 | initialize_char_syntax (); | |
1823 | ||
1824 | /* Initialize output buffer */ | |
1825 | ||
1826 | outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE); | |
1827 | outbuf.bufp = outbuf.buf; | |
1828 | outbuf.length = OUTBUF_SIZE; | |
1829 | ||
1830 | /* Do partial setup of input buffer for the sake of generating | |
1831 | early #line directives (when -g is in effect). */ | |
1832 | ||
1833 | fp = &instack[++indepth]; | |
1834 | if (in_fname == NULL) | |
1835 | in_fname = ""; | |
1836 | fp->nominal_fname = fp->fname = in_fname; | |
1837 | fp->lineno = 0; | |
1838 | ||
767d412c JM |
1839 | /* In C++, wchar_t is a distinct basic type, and we can expect |
1840 | __wchar_t to be defined by cc1plus. */ | |
1841 | if (cplusplus) | |
1842 | wchar_type = "__wchar_t"; | |
1843 | ||
b0bbbd85 RS |
1844 | /* Install __LINE__, etc. Must follow initialize_char_syntax |
1845 | and option processing. */ | |
1846 | initialize_builtins (fp, &outbuf); | |
1847 | ||
1848 | /* Do standard #defines and assertions | |
1849 | that identify system and machine type. */ | |
1850 | ||
1851 | if (!inhibit_predefs) { | |
1852 | char *p = (char *) alloca (strlen (predefs) + 1); | |
1853 | strcpy (p, predefs); | |
1854 | while (*p) { | |
1855 | char *q; | |
1856 | while (*p == ' ' || *p == '\t') | |
1857 | p++; | |
1858 | /* Handle -D options. */ | |
1859 | if (p[0] == '-' && p[1] == 'D') { | |
1860 | q = &p[2]; | |
1861 | while (*p && *p != ' ' && *p != '\t') | |
1862 | p++; | |
1863 | if (*p != 0) | |
1864 | *p++= 0; | |
1865 | if (debug_output) | |
adcfa681 | 1866 | output_line_directive (fp, &outbuf, 0, same_file); |
b0bbbd85 RS |
1867 | make_definition (q, &outbuf); |
1868 | while (*p == ' ' || *p == '\t') | |
1869 | p++; | |
1870 | } else if (p[0] == '-' && p[1] == 'A') { | |
1871 | /* Handle -A options (assertions). */ | |
1872 | char *assertion; | |
1873 | char *past_name; | |
1874 | char *value; | |
1875 | char *past_value; | |
1876 | char *termination; | |
1877 | int save_char; | |
1878 | ||
1879 | assertion = &p[2]; | |
1880 | past_name = assertion; | |
1881 | /* Locate end of name. */ | |
1882 | while (*past_name && *past_name != ' ' | |
1883 | && *past_name != '\t' && *past_name != '(') | |
1884 | past_name++; | |
1885 | /* Locate `(' at start of value. */ | |
1886 | value = past_name; | |
1887 | while (*value && (*value == ' ' || *value == '\t')) | |
1888 | value++; | |
1889 | if (*value++ != '(') | |
1890 | abort (); | |
1891 | while (*value && (*value == ' ' || *value == '\t')) | |
1892 | value++; | |
1893 | past_value = value; | |
1894 | /* Locate end of value. */ | |
1895 | while (*past_value && *past_value != ' ' | |
1896 | && *past_value != '\t' && *past_value != ')') | |
1897 | past_value++; | |
1898 | termination = past_value; | |
1899 | while (*termination && (*termination == ' ' || *termination == '\t')) | |
1900 | termination++; | |
1901 | if (*termination++ != ')') | |
1902 | abort (); | |
1903 | if (*termination && *termination != ' ' && *termination != '\t') | |
1904 | abort (); | |
1905 | /* Temporarily null-terminate the value. */ | |
1906 | save_char = *termination; | |
1907 | *termination = '\0'; | |
1908 | /* Install the assertion. */ | |
1909 | make_assertion ("-A", assertion); | |
1910 | *termination = (char) save_char; | |
1911 | p = termination; | |
1912 | while (*p == ' ' || *p == '\t') | |
1913 | p++; | |
1914 | } else { | |
1915 | abort (); | |
1916 | } | |
1917 | } | |
1918 | } | |
1919 | ||
1920 | /* Now handle the command line options. */ | |
1921 | ||
2bbd52a8 RS |
1922 | /* Do -U's, -D's and -A's in the order they were seen. */ |
1923 | for (i = 1; i < argc; i++) { | |
49df5f37 RS |
1924 | if (pend_undefs[i]) { |
1925 | if (debug_output) | |
adcfa681 | 1926 | output_line_directive (fp, &outbuf, 0, same_file); |
49df5f37 RS |
1927 | make_undef (pend_undefs[i], &outbuf); |
1928 | } | |
b0bbbd85 RS |
1929 | if (pend_defs[i]) { |
1930 | if (debug_output) | |
adcfa681 | 1931 | output_line_directive (fp, &outbuf, 0, same_file); |
b0bbbd85 RS |
1932 | make_definition (pend_defs[i], &outbuf); |
1933 | } | |
2bbd52a8 RS |
1934 | if (pend_assertions[i]) |
1935 | make_assertion (pend_assertion_options[i], pend_assertions[i]); | |
1936 | } | |
b0bbbd85 | 1937 | |
b0bbbd85 RS |
1938 | done_initializing = 1; |
1939 | ||
1940 | { /* read the appropriate environment variable and if it exists | |
1941 | replace include_defaults with the listed path. */ | |
1942 | char *epath = 0; | |
1943 | switch ((objc << 1) + cplusplus) | |
1944 | { | |
1945 | case 0: | |
1946 | epath = getenv ("C_INCLUDE_PATH"); | |
1947 | break; | |
1948 | case 1: | |
1949 | epath = getenv ("CPLUS_INCLUDE_PATH"); | |
1950 | break; | |
1951 | case 2: | |
1952 | epath = getenv ("OBJC_INCLUDE_PATH"); | |
1953 | break; | |
1954 | case 3: | |
1955 | epath = getenv ("OBJCPLUS_INCLUDE_PATH"); | |
1956 | break; | |
1957 | } | |
1958 | /* If the environment var for this language is set, | |
1959 | add to the default list of include directories. */ | |
1960 | if (epath) { | |
1961 | char *nstore = (char *) alloca (strlen (epath) + 2); | |
1962 | int num_dirs; | |
1963 | char *startp, *endp; | |
1964 | ||
1965 | for (num_dirs = 1, startp = epath; *startp; startp++) | |
4c64aaf6 | 1966 | if (*startp == PATH_SEPARATOR) |
b0bbbd85 RS |
1967 | num_dirs++; |
1968 | include_defaults | |
1969 | = (struct default_include *) xmalloc ((num_dirs | |
1970 | * sizeof (struct default_include)) | |
1971 | + sizeof (include_defaults_array)); | |
1972 | startp = endp = epath; | |
1973 | num_dirs = 0; | |
1974 | while (1) { | |
2aa7ec37 | 1975 | /* Handle cases like c:/usr/lib:d:/gcc/lib */ |
5c18abb4 RS |
1976 | if ((*endp == PATH_SEPARATOR |
1977 | #if 0 /* Obsolete, now that we use semicolons as the path separator. */ | |
2aa7ec37 | 1978 | #ifdef __MSDOS__ |
6489924b | 1979 | && (endp-startp != 1 || !isalpha (*startp)) |
5c18abb4 | 1980 | #endif |
2aa7ec37 RS |
1981 | #endif |
1982 | ) | |
5c18abb4 | 1983 | || *endp == 0) { |
b0bbbd85 RS |
1984 | strncpy (nstore, startp, endp-startp); |
1985 | if (endp == startp) | |
1986 | strcpy (nstore, "."); | |
1987 | else | |
1988 | nstore[endp-startp] = '\0'; | |
1989 | ||
b0bbbd85 RS |
1990 | include_defaults[num_dirs].fname = savestring (nstore); |
1991 | include_defaults[num_dirs].cplusplus = cplusplus; | |
acf7262c | 1992 | include_defaults[num_dirs].cxx_aware = 1; |
b0bbbd85 RS |
1993 | num_dirs++; |
1994 | if (*endp == '\0') | |
1995 | break; | |
1996 | endp = startp = endp + 1; | |
1997 | } else | |
1998 | endp++; | |
1999 | } | |
2000 | /* Put the usual defaults back in at the end. */ | |
4c9a05bc RK |
2001 | bcopy ((char *) include_defaults_array, |
2002 | (char *) &include_defaults[num_dirs], | |
b0bbbd85 RS |
2003 | sizeof (include_defaults_array)); |
2004 | } | |
2005 | } | |
2006 | ||
b0866c74 JW |
2007 | append_include_chain (before_system, last_before_system); |
2008 | first_system_include = before_system; | |
2009 | ||
b0bbbd85 RS |
2010 | /* Unless -fnostdinc, |
2011 | tack on the standard include file dirs to the specified list */ | |
2012 | if (!no_standard_includes) { | |
2013 | struct default_include *p = include_defaults; | |
bec42276 | 2014 | char *specd_prefix = include_prefix; |
b0bbbd85 RS |
2015 | char *default_prefix = savestring (GCC_INCLUDE_DIR); |
2016 | int default_len = 0; | |
2017 | /* Remove the `include' from /usr/local/lib/gcc.../include. */ | |
2018 | if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) { | |
2019 | default_len = strlen (default_prefix) - 7; | |
2020 | default_prefix[default_len] = 0; | |
2021 | } | |
2022 | /* Search "translated" versions of GNU directories. | |
2023 | These have /usr/local/lib/gcc... replaced by specd_prefix. */ | |
2024 | if (specd_prefix != 0 && default_len != 0) | |
2025 | for (p = include_defaults; p->fname; p++) { | |
2026 | /* Some standard dirs are only for C++. */ | |
2378088a | 2027 | if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) { |
b0bbbd85 RS |
2028 | /* Does this dir start with the prefix? */ |
2029 | if (!strncmp (p->fname, default_prefix, default_len)) { | |
2030 | /* Yes; change prefix and add to search list. */ | |
2031 | struct file_name_list *new | |
2032 | = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
2033 | int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len; | |
25cbb59e | 2034 | char *str = xmalloc (this_len + 1); |
b0bbbd85 RS |
2035 | strcpy (str, specd_prefix); |
2036 | strcat (str, p->fname + default_len); | |
2037 | new->fname = str; | |
2038 | new->control_macro = 0; | |
acf7262c | 2039 | new->c_system_include_path = !p->cxx_aware; |
6e7f952e | 2040 | new->got_name_map = 0; |
6489924b RS |
2041 | append_include_chain (new, new); |
2042 | if (first_system_include == 0) | |
2043 | first_system_include = new; | |
b0bbbd85 RS |
2044 | } |
2045 | } | |
2046 | } | |
2047 | /* Search ordinary names for GNU include directories. */ | |
2048 | for (p = include_defaults; p->fname; p++) { | |
2049 | /* Some standard dirs are only for C++. */ | |
2378088a | 2050 | if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) { |
b0bbbd85 RS |
2051 | struct file_name_list *new |
2052 | = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
2053 | new->control_macro = 0; | |
acf7262c | 2054 | new->c_system_include_path = !p->cxx_aware; |
b0bbbd85 | 2055 | new->fname = p->fname; |
6e7f952e | 2056 | new->got_name_map = 0; |
6489924b RS |
2057 | append_include_chain (new, new); |
2058 | if (first_system_include == 0) | |
2059 | first_system_include = new; | |
b0bbbd85 RS |
2060 | } |
2061 | } | |
2062 | } | |
2063 | ||
2064 | /* Tack the after_include chain at the end of the include chain. */ | |
6489924b RS |
2065 | append_include_chain (after_include, last_after_include); |
2066 | if (first_system_include == 0) | |
2067 | first_system_include = after_include; | |
b0bbbd85 | 2068 | |
e6157ab4 RS |
2069 | /* With -v, print the list of dirs to search. */ |
2070 | if (verbose) { | |
2071 | struct file_name_list *p; | |
2072 | fprintf (stderr, "#include \"...\" search starts here:\n"); | |
2073 | for (p = include; p; p = p->next) { | |
2074 | if (p == first_bracket_include) | |
2075 | fprintf (stderr, "#include <...> search starts here:\n"); | |
2076 | fprintf (stderr, " %s\n", p->fname); | |
2077 | } | |
2078 | fprintf (stderr, "End of search list.\n"); | |
2079 | } | |
2080 | ||
b0bbbd85 RS |
2081 | /* Scan the -imacros files before the main input. |
2082 | Much like #including them, but with no_output set | |
2083 | so that only their macro definitions matter. */ | |
2084 | ||
42e2194b | 2085 | no_output++; no_record_file++; |
b0bbbd85 RS |
2086 | for (i = 1; i < argc; i++) |
2087 | if (pend_files[i]) { | |
2088 | int fd = open (pend_files[i], O_RDONLY, 0666); | |
2089 | if (fd < 0) { | |
2090 | perror_with_name (pend_files[i]); | |
2091 | return FAILURE_EXIT_CODE; | |
2092 | } | |
8d9bfdc5 | 2093 | finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR); |
b0bbbd85 | 2094 | } |
42e2194b | 2095 | no_output--; no_record_file--; |
b0bbbd85 RS |
2096 | |
2097 | /* Copy the entire contents of the main input file into | |
2098 | the stacked input buffer previously allocated for it. */ | |
2099 | ||
2100 | /* JF check for stdin */ | |
2101 | if (in_fname == NULL || *in_fname == 0) { | |
2102 | in_fname = ""; | |
2103 | f = 0; | |
2104 | } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0) | |
2105 | goto perror; | |
2106 | ||
e648e0e3 DE |
2107 | /* -MG doesn't select the form of output and must be specified with one of |
2108 | -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't | |
2109 | inhibit compilation. */ | |
2110 | if (print_deps_missing_files && (print_deps == 0 || !inhibit_output)) | |
2111 | fatal ("-MG must be specified with one of -M or -MM"); | |
86739c7b | 2112 | |
b0bbbd85 RS |
2113 | /* Either of two environment variables can specify output of deps. |
2114 | Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET", | |
2115 | where OUTPUT_FILE is the file to write deps info to | |
2116 | and DEPS_TARGET is the target to mention in the deps. */ | |
2117 | ||
2118 | if (print_deps == 0 | |
2119 | && (getenv ("SUNPRO_DEPENDENCIES") != 0 | |
2120 | || getenv ("DEPENDENCIES_OUTPUT") != 0)) { | |
2121 | char *spec = getenv ("DEPENDENCIES_OUTPUT"); | |
2122 | char *s; | |
2123 | char *output_file; | |
2124 | ||
2125 | if (spec == 0) { | |
2126 | spec = getenv ("SUNPRO_DEPENDENCIES"); | |
2127 | print_deps = 2; | |
2128 | } | |
2129 | else | |
2130 | print_deps = 1; | |
2131 | ||
2132 | s = spec; | |
2133 | /* Find the space before the DEPS_TARGET, if there is one. */ | |
43ae693e | 2134 | /* This should use index. (mrs) */ |
b0bbbd85 RS |
2135 | while (*s != 0 && *s != ' ') s++; |
2136 | if (*s != 0) { | |
2137 | deps_target = s + 1; | |
25cbb59e | 2138 | output_file = xmalloc (s - spec + 1); |
b0bbbd85 RS |
2139 | bcopy (spec, output_file, s - spec); |
2140 | output_file[s - spec] = 0; | |
2141 | } | |
2142 | else { | |
2143 | deps_target = 0; | |
2144 | output_file = spec; | |
2145 | } | |
2146 | ||
2147 | deps_file = output_file; | |
d0691cfb | 2148 | deps_mode = "a"; |
b0bbbd85 RS |
2149 | } |
2150 | ||
2151 | /* For -M, print the expected object file name | |
2152 | as the target of this Make-rule. */ | |
2153 | if (print_deps) { | |
2154 | deps_allocated_size = 200; | |
25cbb59e | 2155 | deps_buffer = xmalloc (deps_allocated_size); |
b0bbbd85 RS |
2156 | deps_buffer[0] = 0; |
2157 | deps_size = 0; | |
2158 | deps_column = 0; | |
2159 | ||
2160 | if (deps_target) { | |
389bb508 RK |
2161 | deps_output (deps_target, ':'); |
2162 | } else if (*in_fname == 0) { | |
2163 | deps_output ("-", ':'); | |
2164 | } else { | |
2165 | char *p, *q; | |
b0bbbd85 | 2166 | int len; |
389bb508 RK |
2167 | |
2168 | /* Discard all directory prefixes from filename. */ | |
7cc322e3 RK |
2169 | if ((q = rindex (in_fname, '/')) != NULL |
2170 | #ifdef DIR_SEPARATOR | |
2171 | && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL | |
2172 | #endif | |
2173 | ) | |
389bb508 RK |
2174 | ++q; |
2175 | else | |
2176 | q = in_fname; | |
2177 | ||
2178 | /* Copy remainder to mungable area. */ | |
3c0e5268 | 2179 | p = (char *) alloca (strlen(q) + 8); |
389bb508 RK |
2180 | strcpy (p, q); |
2181 | ||
b0bbbd85 RS |
2182 | /* Output P, but remove known suffixes. */ |
2183 | len = strlen (p); | |
389bb508 RK |
2184 | q = p + len; |
2185 | if (len >= 2 | |
2186 | && p[len - 2] == '.' | |
2187 | && index("cCsSm", p[len - 1])) | |
2188 | q = p + (len - 2); | |
2189 | else if (len >= 3 | |
2190 | && p[len - 3] == '.' | |
b0bbbd85 RS |
2191 | && p[len - 2] == 'c' |
2192 | && p[len - 1] == 'c') | |
389bb508 RK |
2193 | q = p + (len - 3); |
2194 | else if (len >= 4 | |
2195 | && p[len - 4] == '.' | |
c9bf18a5 RS |
2196 | && p[len - 3] == 'c' |
2197 | && p[len - 2] == 'x' | |
2198 | && p[len - 1] == 'x') | |
389bb508 | 2199 | q = p + (len - 4); |
a0c2b8e9 RK |
2200 | else if (len >= 4 |
2201 | && p[len - 4] == '.' | |
2202 | && p[len - 3] == 'c' | |
2203 | && p[len - 2] == 'p' | |
2204 | && p[len - 1] == 'p') | |
2205 | q = p + (len - 4); | |
389bb508 | 2206 | |
b0bbbd85 | 2207 | /* Supply our own suffix. */ |
49df5f37 | 2208 | #ifndef VMS |
389bb508 | 2209 | strcpy (q, ".o"); |
49df5f37 | 2210 | #else |
389bb508 | 2211 | strcpy (q, ".obj"); |
49df5f37 | 2212 | #endif |
389bb508 RK |
2213 | |
2214 | deps_output (p, ':'); | |
0016a02d | 2215 | deps_output (in_fname, ' '); |
b0bbbd85 RS |
2216 | } |
2217 | } | |
2218 | ||
2219 | file_size_and_mode (f, &st_mode, &st_size); | |
2220 | fp->nominal_fname = fp->fname = in_fname; | |
2221 | fp->lineno = 1; | |
2222 | fp->system_header_p = 0; | |
2223 | /* JF all this is mine about reading pipes and ttys */ | |
2224 | if (! S_ISREG (st_mode)) { | |
2225 | /* Read input from a file that is not a normal disk file. | |
2226 | We cannot preallocate a buffer with the correct size, | |
2227 | so we must read in the file a piece at the time and make it bigger. */ | |
2228 | int size; | |
2229 | int bsize; | |
2230 | int cnt; | |
b0bbbd85 RS |
2231 | |
2232 | bsize = 2000; | |
2233 | size = 0; | |
2234 | fp->buf = (U_CHAR *) xmalloc (bsize + 2); | |
b0bbbd85 | 2235 | for (;;) { |
25cbb59e | 2236 | cnt = safe_read (f, (char *) fp->buf + size, bsize - size); |
b0bbbd85 | 2237 | if (cnt < 0) goto perror; /* error! */ |
b0bbbd85 | 2238 | size += cnt; |
53e52f00 RK |
2239 | if (size != bsize) break; /* End of file */ |
2240 | bsize *= 2; | |
2241 | fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2); | |
b0bbbd85 RS |
2242 | } |
2243 | fp->length = size; | |
2244 | } else { | |
2245 | /* Read a file whose size we can determine in advance. | |
2246 | For the sake of VMS, st_size is just an upper bound. */ | |
b0bbbd85 | 2247 | fp->buf = (U_CHAR *) xmalloc (st_size + 2); |
25cbb59e | 2248 | fp->length = safe_read (f, (char *) fp->buf, st_size); |
53e52f00 | 2249 | if (fp->length < 0) goto perror; |
b0bbbd85 RS |
2250 | } |
2251 | fp->bufp = fp->buf; | |
2252 | fp->if_stack = if_stack; | |
2253 | ||
2254 | /* Make sure data ends with a newline. And put a null after it. */ | |
2255 | ||
2256 | if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n') | |
2257 | /* Backslash-newline at end is not good enough. */ | |
2258 | || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) { | |
2259 | fp->buf[fp->length++] = '\n'; | |
2260 | missing_newline = 1; | |
2261 | } | |
2262 | fp->buf[fp->length] = '\0'; | |
2263 | ||
2264 | /* Unless inhibited, convert trigraphs in the input. */ | |
2265 | ||
2266 | if (!no_trigraphs) | |
2267 | trigraph_pcp (fp); | |
2268 | ||
2269 | /* Now that we know the input file is valid, open the output. */ | |
2270 | ||
2271 | if (!out_fname || !strcmp (out_fname, "")) | |
2272 | out_fname = "stdout"; | |
2273 | else if (! freopen (out_fname, "w", stdout)) | |
2274 | pfatal_with_name (out_fname); | |
2275 | ||
adcfa681 | 2276 | output_line_directive (fp, &outbuf, 0, same_file); |
b0bbbd85 RS |
2277 | |
2278 | /* Scan the -include files before the main input. */ | |
2279 | ||
42e2194b | 2280 | no_record_file++; |
b0bbbd85 RS |
2281 | for (i = 1; i < argc; i++) |
2282 | if (pend_includes[i]) { | |
2283 | int fd = open (pend_includes[i], O_RDONLY, 0666); | |
2284 | if (fd < 0) { | |
2285 | perror_with_name (pend_includes[i]); | |
2286 | return FAILURE_EXIT_CODE; | |
2287 | } | |
8d9bfdc5 | 2288 | finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR); |
b0bbbd85 | 2289 | } |
42e2194b | 2290 | no_record_file--; |
b0bbbd85 RS |
2291 | |
2292 | /* Scan the input, processing macros and directives. */ | |
2293 | ||
2294 | rescan (&outbuf, 0); | |
2295 | ||
5105ecec RS |
2296 | if (missing_newline) |
2297 | fp->lineno--; | |
2298 | ||
b0bbbd85 RS |
2299 | if (pedantic && missing_newline) |
2300 | pedwarn ("file does not end in newline"); | |
2301 | ||
2302 | /* Now we have processed the entire input | |
2303 | Write whichever kind of output has been requested. */ | |
2304 | ||
2305 | if (dump_macros == dump_only) | |
2306 | dump_all_macros (); | |
2307 | else if (! inhibit_output) { | |
2308 | write_output (); | |
2309 | } | |
2310 | ||
2311 | if (print_deps) { | |
566609f8 RS |
2312 | /* Don't actually write the deps file if compilation has failed. */ |
2313 | if (errors == 0) { | |
d0691cfb | 2314 | if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode))) |
566609f8 | 2315 | pfatal_with_name (deps_file); |
b0bbbd85 RS |
2316 | fputs (deps_buffer, deps_stream); |
2317 | putc ('\n', deps_stream); | |
566609f8 RS |
2318 | if (deps_file) { |
2319 | if (ferror (deps_stream) || fclose (deps_stream) != 0) | |
b0bbbd85 RS |
2320 | fatal ("I/O error on output"); |
2321 | } | |
2322 | } | |
2323 | } | |
2324 | ||
c6469b46 RS |
2325 | if (pcp_outfile && pcp_outfile != stdout |
2326 | && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0)) | |
2327 | fatal ("I/O error on `-pcp' output"); | |
2328 | ||
566609f8 | 2329 | if (ferror (stdout) || fclose (stdout) != 0) |
b0bbbd85 RS |
2330 | fatal ("I/O error on output"); |
2331 | ||
2332 | if (errors) | |
2333 | exit (FAILURE_EXIT_CODE); | |
2334 | exit (SUCCESS_EXIT_CODE); | |
2335 | ||
2336 | perror: | |
2337 | pfatal_with_name (in_fname); | |
2338 | return 0; | |
2339 | } | |
2340 | \f | |
2341 | /* Given a colon-separated list of file names PATH, | |
2342 | add all the names to the search path for include files. */ | |
2343 | ||
2344 | static void | |
2345 | path_include (path) | |
2346 | char *path; | |
2347 | { | |
2348 | char *p; | |
2349 | ||
2350 | p = path; | |
2351 | ||
2352 | if (*p) | |
2353 | while (1) { | |
2354 | char *q = p; | |
2355 | char *name; | |
2356 | struct file_name_list *dirtmp; | |
2357 | ||
2358 | /* Find the end of this name. */ | |
4c64aaf6 | 2359 | while (*q != 0 && *q != PATH_SEPARATOR) q++; |
b0bbbd85 RS |
2360 | if (p == q) { |
2361 | /* An empty name in the path stands for the current directory. */ | |
25cbb59e | 2362 | name = xmalloc (2); |
b0bbbd85 RS |
2363 | name[0] = '.'; |
2364 | name[1] = 0; | |
2365 | } else { | |
2366 | /* Otherwise use the directory that is named. */ | |
25cbb59e | 2367 | name = xmalloc (q - p + 1); |
b0bbbd85 RS |
2368 | bcopy (p, name, q - p); |
2369 | name[q - p] = 0; | |
2370 | } | |
2371 | ||
2372 | dirtmp = (struct file_name_list *) | |
2373 | xmalloc (sizeof (struct file_name_list)); | |
2374 | dirtmp->next = 0; /* New one goes on the end */ | |
2375 | dirtmp->control_macro = 0; | |
d2a22862 | 2376 | dirtmp->c_system_include_path = 0; |
b0bbbd85 | 2377 | dirtmp->fname = name; |
6e7f952e | 2378 | dirtmp->got_name_map = 0; |
6489924b | 2379 | append_include_chain (dirtmp, dirtmp); |
b0bbbd85 RS |
2380 | |
2381 | /* Advance past this name. */ | |
2382 | p = q; | |
2383 | if (*p == 0) | |
2384 | break; | |
2385 | /* Skip the colon. */ | |
2386 | p++; | |
2387 | } | |
2388 | } | |
2389 | \f | |
0e7e3fc1 RK |
2390 | /* Return the address of the first character in S that equals C. |
2391 | S is an array of length N, possibly containing '\0's, and followed by '\0'. | |
2392 | Return 0 if there is no such character. Assume that C itself is not '\0'. | |
2393 | If we knew we could use memchr, we could just invoke memchr (S, C, N), | |
2394 | but unfortunately memchr isn't autoconfigured yet. */ | |
2395 | ||
2396 | static U_CHAR * | |
2397 | index0 (s, c, n) | |
2398 | U_CHAR *s; | |
2399 | int c; | |
25cbb59e | 2400 | size_t n; |
0e7e3fc1 | 2401 | { |
25cbb59e | 2402 | char *p = (char *) s; |
0e7e3fc1 | 2403 | for (;;) { |
25cbb59e | 2404 | char *q = index (p, c); |
0e7e3fc1 RK |
2405 | if (q) |
2406 | return (U_CHAR *) q; | |
2407 | else { | |
25cbb59e | 2408 | size_t l = strlen (p); |
0e7e3fc1 RK |
2409 | if (l == n) |
2410 | return 0; | |
2411 | l++; | |
25cbb59e | 2412 | p += l; |
0e7e3fc1 RK |
2413 | n -= l; |
2414 | } | |
2415 | } | |
2416 | } | |
2417 | \f | |
b0bbbd85 RS |
2418 | /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF |
2419 | before main CCCP processing. Name `pcp' is also in honor of the | |
2420 | drugs the trigraph designers must have been on. | |
2421 | ||
2422 | Using an extra pass through the buffer takes a little extra time, | |
2423 | but is infinitely less hairy than trying to handle trigraphs inside | |
2424 | strings, etc. everywhere, and also makes sure that trigraphs are | |
2425 | only translated in the top level of processing. */ | |
2426 | ||
2427 | static void | |
2428 | trigraph_pcp (buf) | |
2429 | FILE_BUF *buf; | |
2430 | { | |
0e7e3fc1 | 2431 | register U_CHAR c, *fptr, *bptr, *sptr, *lptr; |
b0bbbd85 RS |
2432 | int len; |
2433 | ||
2434 | fptr = bptr = sptr = buf->buf; | |
0e7e3fc1 | 2435 | lptr = fptr + buf->length; |
25cbb59e | 2436 | while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) { |
b0bbbd85 RS |
2437 | if (*++sptr != '?') |
2438 | continue; | |
2439 | switch (*++sptr) { | |
2440 | case '=': | |
2441 | c = '#'; | |
2442 | break; | |
2443 | case '(': | |
2444 | c = '['; | |
2445 | break; | |
2446 | case '/': | |
2447 | c = '\\'; | |
2448 | break; | |
2449 | case ')': | |
2450 | c = ']'; | |
2451 | break; | |
2452 | case '\'': | |
2453 | c = '^'; | |
2454 | break; | |
2455 | case '<': | |
2456 | c = '{'; | |
2457 | break; | |
2458 | case '!': | |
2459 | c = '|'; | |
2460 | break; | |
2461 | case '>': | |
2462 | c = '}'; | |
2463 | break; | |
2464 | case '-': | |
2465 | c = '~'; | |
2466 | break; | |
2467 | case '?': | |
2468 | sptr--; | |
2469 | continue; | |
2470 | default: | |
2471 | continue; | |
2472 | } | |
2473 | len = sptr - fptr - 2; | |
4c9a05bc RK |
2474 | |
2475 | /* BSD doc says bcopy () works right for overlapping strings. In ANSI | |
2476 | C, this will be memmove (). */ | |
b0bbbd85 | 2477 | if (bptr != fptr && len > 0) |
4c9a05bc RK |
2478 | bcopy ((char *) fptr, (char *) bptr, len); |
2479 | ||
b0bbbd85 RS |
2480 | bptr += len; |
2481 | *bptr++ = c; | |
2482 | fptr = ++sptr; | |
2483 | } | |
2484 | len = buf->length - (fptr - buf->buf); | |
2485 | if (bptr != fptr && len > 0) | |
4c9a05bc | 2486 | bcopy ((char *) fptr, (char *) bptr, len); |
b0bbbd85 RS |
2487 | buf->length -= fptr - bptr; |
2488 | buf->buf[buf->length] = '\0'; | |
2489 | if (warn_trigraphs && fptr != bptr) | |
2490 | warning ("%d trigraph(s) encountered", (fptr - bptr) / 2); | |
2491 | } | |
2492 | \f | |
2493 | /* Move all backslash-newline pairs out of embarrassing places. | |
2494 | Exchange all such pairs following BP | |
2aa7ec37 | 2495 | with any potentially-embarrassing characters that follow them. |
b0bbbd85 RS |
2496 | Potentially-embarrassing characters are / and * |
2497 | (because a backslash-newline inside a comment delimiter | |
2498 | would cause it not to be recognized). */ | |
2499 | ||
2500 | static void | |
2501 | newline_fix (bp) | |
2502 | U_CHAR *bp; | |
2503 | { | |
2504 | register U_CHAR *p = bp; | |
b0bbbd85 RS |
2505 | |
2506 | /* First count the backslash-newline pairs here. */ | |
2507 | ||
f9cf182e RK |
2508 | while (p[0] == '\\' && p[1] == '\n') |
2509 | p += 2; | |
b0bbbd85 RS |
2510 | |
2511 | /* What follows the backslash-newlines is not embarrassing. */ | |
2512 | ||
f9cf182e | 2513 | if (*p != '/' && *p != '*') |
b0bbbd85 RS |
2514 | return; |
2515 | ||
2516 | /* Copy all potentially embarrassing characters | |
2517 | that follow the backslash-newline pairs | |
2518 | down to where the pairs originally started. */ | |
2519 | ||
2520 | while (*p == '*' || *p == '/') | |
2521 | *bp++ = *p++; | |
2522 | ||
2523 | /* Now write the same number of pairs after the embarrassing chars. */ | |
f9cf182e | 2524 | while (bp < p) { |
b0bbbd85 RS |
2525 | *bp++ = '\\'; |
2526 | *bp++ = '\n'; | |
2527 | } | |
2528 | } | |
2529 | ||
2530 | /* Like newline_fix but for use within a directive-name. | |
2531 | Move any backslash-newlines up past any following symbol constituents. */ | |
2532 | ||
2533 | static void | |
2534 | name_newline_fix (bp) | |
2535 | U_CHAR *bp; | |
2536 | { | |
2537 | register U_CHAR *p = bp; | |
b0bbbd85 RS |
2538 | |
2539 | /* First count the backslash-newline pairs here. */ | |
f9cf182e RK |
2540 | while (p[0] == '\\' && p[1] == '\n') |
2541 | p += 2; | |
b0bbbd85 RS |
2542 | |
2543 | /* What follows the backslash-newlines is not embarrassing. */ | |
2544 | ||
f9cf182e | 2545 | if (!is_idchar[*p]) |
b0bbbd85 RS |
2546 | return; |
2547 | ||
2548 | /* Copy all potentially embarrassing characters | |
2549 | that follow the backslash-newline pairs | |
2550 | down to where the pairs originally started. */ | |
2551 | ||
2552 | while (is_idchar[*p]) | |
2553 | *bp++ = *p++; | |
2554 | ||
2555 | /* Now write the same number of pairs after the embarrassing chars. */ | |
f9cf182e | 2556 | while (bp < p) { |
b0bbbd85 RS |
2557 | *bp++ = '\\'; |
2558 | *bp++ = '\n'; | |
2559 | } | |
2560 | } | |
2561 | \f | |
2562 | /* Look for lint commands in comments. | |
2563 | ||
2564 | When we come in here, ibp points into a comment. Limit is as one expects. | |
2565 | scan within the comment -- it should start, after lwsp, with a lint command. | |
2566 | If so that command is returned as a (constant) string. | |
2567 | ||
2568 | Upon return, any arg will be pointed to with argstart and will be | |
2569 | arglen long. Note that we don't parse that arg since it will just | |
2570 | be printed out again. | |
2571 | */ | |
2572 | ||
2573 | static char * | |
2574 | get_lintcmd (ibp, limit, argstart, arglen, cmdlen) | |
2575 | register U_CHAR *ibp; | |
2576 | register U_CHAR *limit; | |
2577 | U_CHAR **argstart; /* point to command arg */ | |
2578 | int *arglen, *cmdlen; /* how long they are */ | |
2579 | { | |
2580 | long linsize; | |
2581 | register U_CHAR *numptr; /* temp for arg parsing */ | |
2582 | ||
2583 | *arglen = 0; | |
2584 | ||
2585 | SKIP_WHITE_SPACE (ibp); | |
2586 | ||
2587 | if (ibp >= limit) return NULL; | |
2588 | ||
2589 | linsize = limit - ibp; | |
2590 | ||
2591 | /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */ | |
25cbb59e | 2592 | if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) { |
b0bbbd85 RS |
2593 | *cmdlen = 10; |
2594 | return "NOTREACHED"; | |
2595 | } | |
25cbb59e | 2596 | if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) { |
b0bbbd85 RS |
2597 | *cmdlen = 8; |
2598 | return "ARGSUSED"; | |
2599 | } | |
25cbb59e | 2600 | if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) { |
a8b9d9a4 | 2601 | *cmdlen = 11; |
b0bbbd85 RS |
2602 | return "LINTLIBRARY"; |
2603 | } | |
25cbb59e | 2604 | if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) { |
b0bbbd85 RS |
2605 | *cmdlen = 7; |
2606 | ibp += 7; linsize -= 7; | |
2607 | if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS"; | |
2608 | ||
2609 | /* OK, read a number */ | |
2610 | for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr); | |
2611 | numptr++); | |
2612 | *arglen = numptr - *argstart; | |
2613 | return "VARARGS"; | |
2614 | } | |
2615 | return NULL; | |
2616 | } | |
2617 | \f | |
2618 | /* | |
2619 | * The main loop of the program. | |
2620 | * | |
2621 | * Read characters from the input stack, transferring them to the | |
2622 | * output buffer OP. | |
2623 | * | |
2624 | * Macros are expanded and push levels on the input stack. | |
2625 | * At the end of such a level it is popped off and we keep reading. | |
2626 | * At the end of any other kind of level, we return. | |
2627 | * #-directives are handled, except within macros. | |
2628 | * | |
2629 | * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input | |
2630 | * and insert them when appropriate. This is set while scanning macro | |
2631 | * arguments before substitution. It is zero when scanning for final output. | |
2632 | * There are three types of Newline markers: | |
2633 | * * Newline - follows a macro name that was not expanded | |
2634 | * because it appeared inside an expansion of the same macro. | |
2635 | * This marker prevents future expansion of that identifier. | |
2636 | * When the input is rescanned into the final output, these are deleted. | |
2637 | * These are also deleted by ## concatenation. | |
2638 | * * Newline Space (or Newline and any other whitespace character) | |
2639 | * stands for a place that tokens must be separated or whitespace | |
2640 | * is otherwise desirable, but where the ANSI standard specifies there | |
2641 | * is no whitespace. This marker turns into a Space (or whichever other | |
2642 | * whitespace char appears in the marker) in the final output, | |
2643 | * but it turns into nothing in an argument that is stringified with #. | |
2644 | * Such stringified arguments are the only place where the ANSI standard | |
2645 | * specifies with precision that whitespace may not appear. | |
2646 | * | |
2647 | * During this function, IP->bufp is kept cached in IBP for speed of access. | |
2648 | * Likewise, OP->bufp is kept in OBP. Before calling a subroutine | |
2649 | * IBP, IP and OBP must be copied back to memory. IP and IBP are | |
2650 | * copied back with the RECACHE macro. OBP must be copied back from OP->bufp | |
2651 | * explicitly, and before RECACHE, since RECACHE uses OBP. | |
2652 | */ | |
2653 | ||
2654 | static void | |
2655 | rescan (op, output_marks) | |
2656 | FILE_BUF *op; | |
2657 | int output_marks; | |
2658 | { | |
2659 | /* Character being scanned in main loop. */ | |
2660 | register U_CHAR c; | |
2661 | ||
2662 | /* Length of pending accumulated identifier. */ | |
2663 | register int ident_length = 0; | |
2664 | ||
2665 | /* Hash code of pending accumulated identifier. */ | |
2666 | register int hash = 0; | |
2667 | ||
2668 | /* Current input level (&instack[indepth]). */ | |
2669 | FILE_BUF *ip; | |
2670 | ||
2671 | /* Pointer for scanning input. */ | |
2672 | register U_CHAR *ibp; | |
2673 | ||
2674 | /* Pointer to end of input. End of scan is controlled by LIMIT. */ | |
2675 | register U_CHAR *limit; | |
2676 | ||
2677 | /* Pointer for storing output. */ | |
2678 | register U_CHAR *obp; | |
2679 | ||
2680 | /* REDO_CHAR is nonzero if we are processing an identifier | |
2681 | after backing up over the terminating character. | |
2682 | Sometimes we process an identifier without backing up over | |
2683 | the terminating character, if the terminating character | |
2684 | is not special. Backing up is done so that the terminating character | |
2685 | will be dispatched on again once the identifier is dealt with. */ | |
2686 | int redo_char = 0; | |
2687 | ||
2688 | /* 1 if within an identifier inside of which a concatenation | |
2689 | marker (Newline -) has been seen. */ | |
2690 | int concatenated = 0; | |
2691 | ||
2692 | /* While scanning a comment or a string constant, | |
2693 | this records the line it started on, for error messages. */ | |
2694 | int start_line; | |
2695 | ||
b0bbbd85 RS |
2696 | /* Record position of last `real' newline. */ |
2697 | U_CHAR *beg_of_line; | |
2698 | ||
2699 | /* Pop the innermost input stack level, assuming it is a macro expansion. */ | |
2700 | ||
2701 | #define POPMACRO \ | |
2702 | do { ip->macro->type = T_MACRO; \ | |
2703 | if (ip->free_ptr) free (ip->free_ptr); \ | |
2704 | --indepth; } while (0) | |
2705 | ||
2706 | /* Reload `rescan's local variables that describe the current | |
2707 | level of the input stack. */ | |
2708 | ||
2709 | #define RECACHE \ | |
2710 | do { ip = &instack[indepth]; \ | |
2711 | ibp = ip->bufp; \ | |
2712 | limit = ip->buf + ip->length; \ | |
2713 | op->bufp = obp; \ | |
2714 | check_expand (op, limit - ibp); \ | |
2715 | beg_of_line = 0; \ | |
2716 | obp = op->bufp; } while (0) | |
2717 | ||
2718 | if (no_output && instack[indepth].fname != 0) | |
bbd4b75b | 2719 | skip_if_group (&instack[indepth], 1, NULL); |
b0bbbd85 RS |
2720 | |
2721 | obp = op->bufp; | |
2722 | RECACHE; | |
34a2d6f3 | 2723 | |
b0bbbd85 RS |
2724 | beg_of_line = ibp; |
2725 | ||
2726 | /* Our caller must always put a null after the end of | |
2727 | the input at each input stack level. */ | |
2728 | if (*limit != 0) | |
2729 | abort (); | |
2730 | ||
2731 | while (1) { | |
2732 | c = *ibp++; | |
2733 | *obp++ = c; | |
2734 | ||
2735 | switch (c) { | |
2736 | case '\\': | |
01bffe73 RK |
2737 | if (*ibp == '\n' && !ip->macro) { |
2738 | /* At the top level, always merge lines ending with backslash-newline, | |
2739 | even in middle of identifier. But do not merge lines in a macro, | |
2740 | since backslash might be followed by a newline-space marker. */ | |
b0bbbd85 RS |
2741 | ++ibp; |
2742 | ++ip->lineno; | |
2743 | --obp; /* remove backslash from obuf */ | |
2744 | break; | |
2745 | } | |
01bffe73 RK |
2746 | /* If ANSI, backslash is just another character outside a string. */ |
2747 | if (!traditional) | |
2748 | goto randomchar; | |
b0bbbd85 RS |
2749 | /* Otherwise, backslash suppresses specialness of following char, |
2750 | so copy it here to prevent the switch from seeing it. | |
2751 | But first get any pending identifier processed. */ | |
2752 | if (ident_length > 0) | |
2753 | goto specialchar; | |
01bffe73 RK |
2754 | if (ibp < limit) |
2755 | *obp++ = *ibp++; | |
b0bbbd85 RS |
2756 | break; |
2757 | ||
91dbf5e7 RK |
2758 | case '%': |
2759 | if (ident_length || ip->macro || traditional) | |
2760 | goto randomchar; | |
91dbf5e7 RK |
2761 | while (*ibp == '\\' && ibp[1] == '\n') { |
2762 | ibp += 2; | |
2763 | ++ip->lineno; | |
2764 | } | |
2765 | if (*ibp != ':') | |
2766 | break; | |
2767 | /* Treat this %: digraph as if it were #. */ | |
2768 | /* Fall through. */ | |
2769 | ||
b0bbbd85 RS |
2770 | case '#': |
2771 | if (assertions_flag) { | |
2772 | /* Copy #foo (bar lose) without macro expansion. */ | |
91dbf5e7 | 2773 | obp[-1] = '#'; /* In case it was '%'. */ |
b0bbbd85 RS |
2774 | SKIP_WHITE_SPACE (ibp); |
2775 | while (is_idchar[*ibp]) | |
2776 | *obp++ = *ibp++; | |
2777 | SKIP_WHITE_SPACE (ibp); | |
2778 | if (*ibp == '(') { | |
2779 | ip->bufp = ibp; | |
2780 | skip_paren_group (ip); | |
4c9a05bc | 2781 | bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp); |
b0bbbd85 RS |
2782 | obp += ip->bufp - ibp; |
2783 | ibp = ip->bufp; | |
2784 | } | |
2785 | } | |
2786 | ||
46181992 | 2787 | /* If this is expanding a macro definition, don't recognize |
adcfa681 | 2788 | preprocessing directives. */ |
46181992 RS |
2789 | if (ip->macro != 0) |
2790 | goto randomchar; | |
3be5fb23 RK |
2791 | /* If this is expand_into_temp_buffer, |
2792 | don't recognize them either. Warn about them | |
46181992 RS |
2793 | only after an actual newline at this level, |
2794 | not at the beginning of the input level. */ | |
3be5fb23 RK |
2795 | if (! ip->fname) { |
2796 | if (ip->buf != beg_of_line) | |
2797 | warning ("preprocessing directive not recognized within macro arg"); | |
b0bbbd85 | 2798 | goto randomchar; |
3be5fb23 | 2799 | } |
b0bbbd85 RS |
2800 | if (ident_length) |
2801 | goto specialchar; | |
2802 | ||
34a2d6f3 | 2803 | |
b0bbbd85 RS |
2804 | /* # keyword: a # must be first nonblank char on the line */ |
2805 | if (beg_of_line == 0) | |
2806 | goto randomchar; | |
2807 | { | |
2808 | U_CHAR *bp; | |
2809 | ||
2810 | /* Scan from start of line, skipping whitespace, comments | |
2811 | and backslash-newlines, and see if we reach this #. | |
2812 | If not, this # is not special. */ | |
2813 | bp = beg_of_line; | |
49df5f37 | 2814 | /* If -traditional, require # to be at beginning of line. */ |
91dbf5e7 | 2815 | if (!traditional) { |
49df5f37 RS |
2816 | while (1) { |
2817 | if (is_hor_space[*bp]) | |
b0bbbd85 | 2818 | bp++; |
49df5f37 RS |
2819 | else if (*bp == '\\' && bp[1] == '\n') |
2820 | bp += 2; | |
2821 | else if (*bp == '/' && bp[1] == '*') { | |
2822 | bp += 2; | |
2823 | while (!(*bp == '*' && bp[1] == '/')) | |
2824 | bp++; | |
2825 | bp += 2; | |
2826 | } | |
80512db7 JW |
2827 | /* There is no point in trying to deal with C++ // comments here, |
2828 | because if there is one, then this # must be part of the | |
2829 | comment and we would never reach here. */ | |
49df5f37 | 2830 | else break; |
b0bbbd85 | 2831 | } |
91dbf5e7 RK |
2832 | if (c == '%') { |
2833 | if (bp[0] != '%') | |
2834 | break; | |
2835 | while (bp[1] == '\\' && bp[2] == '\n') | |
2836 | bp += 2; | |
2837 | if (bp + 1 != ibp) | |
2838 | break; | |
2839 | /* %: appears at start of line; skip past the ':' too. */ | |
2840 | bp++; | |
2841 | ibp++; | |
2842 | } | |
2843 | } | |
b0bbbd85 RS |
2844 | if (bp + 1 != ibp) |
2845 | goto randomchar; | |
2846 | } | |
2847 | ||
2848 | /* This # can start a directive. */ | |
2849 | ||
2850 | --obp; /* Don't copy the '#' */ | |
2851 | ||
2852 | ip->bufp = ibp; | |
2853 | op->bufp = obp; | |
2854 | if (! handle_directive (ip, op)) { | |
2855 | #ifdef USE_C_ALLOCA | |
2856 | alloca (0); | |
2857 | #endif | |
2858 | /* Not a known directive: treat it as ordinary text. | |
2859 | IP, OP, IBP, etc. have not been changed. */ | |
2860 | if (no_output && instack[indepth].fname) { | |
2861 | /* If not generating expanded output, | |
2862 | what we do with ordinary text is skip it. | |
2863 | Discard everything until next # directive. */ | |
bbd4b75b | 2864 | skip_if_group (&instack[indepth], 1, 0); |
b0bbbd85 RS |
2865 | RECACHE; |
2866 | beg_of_line = ibp; | |
2867 | break; | |
2868 | } | |
91dbf5e7 | 2869 | *obp++ = '#'; /* Copy # (even if it was originally %:). */ |
9e92edb4 RK |
2870 | /* Don't expand an identifier that could be a macro directive. |
2871 | (Section 3.8.3 of the ANSI C standard) */ | |
2872 | SKIP_WHITE_SPACE (ibp); | |
2873 | if (is_idstart[*ibp]) | |
2874 | { | |
2875 | *obp++ = *ibp++; | |
2876 | while (is_idchar[*ibp]) | |
2877 | *obp++ = *ibp++; | |
2878 | } | |
b0bbbd85 RS |
2879 | goto randomchar; |
2880 | } | |
2881 | #ifdef USE_C_ALLOCA | |
2882 | alloca (0); | |
2883 | #endif | |
2884 | /* A # directive has been successfully processed. */ | |
2885 | /* If not generating expanded output, ignore everything until | |
2886 | next # directive. */ | |
2887 | if (no_output && instack[indepth].fname) | |
bbd4b75b | 2888 | skip_if_group (&instack[indepth], 1, 0); |
b0bbbd85 RS |
2889 | obp = op->bufp; |
2890 | RECACHE; | |
2891 | beg_of_line = ibp; | |
2892 | break; | |
2893 | ||
2894 | case '\"': /* skip quoted string */ | |
2895 | case '\'': | |
2896 | /* A single quoted string is treated like a double -- some | |
2897 | programs (e.g., troff) are perverse this way */ | |
2898 | ||
2899 | if (ident_length) | |
2900 | goto specialchar; | |
2901 | ||
2902 | start_line = ip->lineno; | |
2903 | ||
2904 | /* Skip ahead to a matching quote. */ | |
2905 | ||
2906 | while (1) { | |
2907 | if (ibp >= limit) { | |
e8db4813 RS |
2908 | if (ip->macro != 0) { |
2909 | /* try harder: this string crosses a macro expansion boundary. | |
2910 | This can happen naturally if -traditional. | |
2911 | Otherwise, only -D can make a macro with an unmatched quote. */ | |
2912 | POPMACRO; | |
2913 | RECACHE; | |
2914 | continue; | |
2915 | } | |
2916 | if (!traditional) { | |
b0bbbd85 RS |
2917 | error_with_line (line_for_error (start_line), |
2918 | "unterminated string or character constant"); | |
2919 | error_with_line (multiline_string_line, | |
2920 | "possible real start of unterminated constant"); | |
2921 | multiline_string_line = 0; | |
2922 | } | |
2923 | break; | |
2924 | } | |
2925 | *obp++ = *ibp; | |
2926 | switch (*ibp++) { | |
2927 | case '\n': | |
2928 | ++ip->lineno; | |
2929 | ++op->lineno; | |
2930 | /* Traditionally, end of line ends a string constant with no error. | |
2931 | So exit the loop and record the new line. */ | |
2932 | if (traditional) { | |
2933 | beg_of_line = ibp; | |
2934 | goto while2end; | |
2935 | } | |
27a5574b | 2936 | if (c == '\'') { |
b0bbbd85 | 2937 | error_with_line (line_for_error (start_line), |
27a5574b | 2938 | "unterminated character constant"); |
b0bbbd85 RS |
2939 | goto while2end; |
2940 | } | |
27a5574b RS |
2941 | if (pedantic && multiline_string_line == 0) { |
2942 | pedwarn_with_line (line_for_error (start_line), | |
2943 | "string constant runs past end of line"); | |
2944 | } | |
b0bbbd85 RS |
2945 | if (multiline_string_line == 0) |
2946 | multiline_string_line = ip->lineno - 1; | |
2947 | break; | |
2948 | ||
2949 | case '\\': | |
2950 | if (ibp >= limit) | |
2951 | break; | |
2952 | if (*ibp == '\n') { | |
2953 | /* Backslash newline is replaced by nothing at all, | |
2954 | but keep the line counts correct. */ | |
2955 | --obp; | |
2956 | ++ibp; | |
2957 | ++ip->lineno; | |
2958 | } else { | |
2959 | /* ANSI stupidly requires that in \\ the second \ | |
2960 | is *not* prevented from combining with a newline. */ | |
2961 | while (*ibp == '\\' && ibp[1] == '\n') { | |
2962 | ibp += 2; | |
2963 | ++ip->lineno; | |
2964 | } | |
2965 | *obp++ = *ibp++; | |
2966 | } | |
2967 | break; | |
2968 | ||
2969 | case '\"': | |
2970 | case '\'': | |
2971 | if (ibp[-1] == c) | |
2972 | goto while2end; | |
2973 | break; | |
2974 | } | |
2975 | } | |
2976 | while2end: | |
2977 | break; | |
2978 | ||
2979 | case '/': | |
2980 | if (*ibp == '\\' && ibp[1] == '\n') | |
2981 | newline_fix (ibp); | |
2982 | ||
2983 | if (*ibp != '*' | |
eda5fa7b | 2984 | && !(cplusplus_comments && *ibp == '/')) |
b0bbbd85 RS |
2985 | goto randomchar; |
2986 | if (ip->macro != 0) | |
2987 | goto randomchar; | |
2988 | if (ident_length) | |
2989 | goto specialchar; | |
2990 | ||
2991 | if (*ibp == '/') { | |
2992 | /* C++ style comment... */ | |
2993 | start_line = ip->lineno; | |
2994 | ||
2995 | --ibp; /* Back over the slash */ | |
2996 | --obp; | |
2997 | ||
2998 | /* Comments are equivalent to spaces. */ | |
2999 | if (! put_out_comments) | |
3000 | *obp++ = ' '; | |
3001 | else { | |
3002 | /* must fake up a comment here */ | |
3003 | *obp++ = '/'; | |
3004 | *obp++ = '/'; | |
3005 | } | |
3006 | { | |
3007 | U_CHAR *before_bp = ibp+2; | |
3008 | ||
3009 | while (ibp < limit) { | |
80512db7 | 3010 | if (ibp[-1] != '\\' && *ibp == '\n') { |
b0bbbd85 | 3011 | if (put_out_comments) { |
4c9a05bc | 3012 | bcopy ((char *) before_bp, (char *) obp, ibp - before_bp); |
b0bbbd85 RS |
3013 | obp += ibp - before_bp; |
3014 | } | |
3015 | break; | |
80512db7 JW |
3016 | } else { |
3017 | if (*ibp == '\n') { | |
3018 | ++ip->lineno; | |
3019 | /* Copy the newline into the output buffer, in order to | |
3020 | avoid the pain of a #line every time a multiline comment | |
3021 | is seen. */ | |
3022 | if (!put_out_comments) | |
3023 | *obp++ = '\n'; | |
3024 | ++op->lineno; | |
3025 | } | |
3026 | ibp++; | |
b0bbbd85 RS |
3027 | } |
3028 | } | |
3029 | break; | |
3030 | } | |
3031 | } | |
3032 | ||
3033 | /* Ordinary C comment. Skip it, optionally copying it to output. */ | |
3034 | ||
3035 | start_line = ip->lineno; | |
3036 | ||
3037 | ++ibp; /* Skip the star. */ | |
3038 | ||
3039 | /* If this cpp is for lint, we peek inside the comments: */ | |
253245df | 3040 | if (for_lint) { |
b0bbbd85 RS |
3041 | U_CHAR *argbp; |
3042 | int cmdlen, arglen; | |
3043 | char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen); | |
3044 | ||
3045 | if (lintcmd != NULL) { | |
b7150e45 | 3046 | op->bufp = obp; |
b415f25e | 3047 | check_expand (op, cmdlen + arglen + 14); |
b7150e45 | 3048 | obp = op->bufp; |
b0bbbd85 RS |
3049 | /* I believe it is always safe to emit this newline: */ |
3050 | obp[-1] = '\n'; | |
4c9a05bc | 3051 | bcopy ("#pragma lint ", (char *) obp, 13); |
b0bbbd85 | 3052 | obp += 13; |
4c9a05bc | 3053 | bcopy (lintcmd, (char *) obp, cmdlen); |
b0bbbd85 RS |
3054 | obp += cmdlen; |
3055 | ||
3056 | if (arglen != 0) { | |
3057 | *(obp++) = ' '; | |
4c9a05bc | 3058 | bcopy (argbp, (char *) obp, arglen); |
b0bbbd85 RS |
3059 | obp += arglen; |
3060 | } | |
3061 | ||
3062 | /* OK, now bring us back to the state we were in before we entered | |
b7150e45 RK |
3063 | this branch. We need #line because the #pragma's newline always |
3064 | messes up the line count. */ | |
3065 | op->bufp = obp; | |
adcfa681 | 3066 | output_line_directive (ip, op, 0, same_file); |
b415f25e | 3067 | check_expand (op, limit - ibp + 2); |
b7150e45 | 3068 | obp = op->bufp; |
b0bbbd85 RS |
3069 | *(obp++) = '/'; |
3070 | } | |
3071 | } | |
3072 | ||
3073 | /* Comments are equivalent to spaces. | |
3074 | Note that we already output the slash; we might not want it. | |
3075 | For -traditional, a comment is equivalent to nothing. */ | |
3076 | if (! put_out_comments) { | |
3077 | if (traditional) | |
3078 | obp--; | |
3079 | else | |
3080 | obp[-1] = ' '; | |
3081 | } | |
3082 | else | |
3083 | *obp++ = '*'; | |
3084 | ||
3085 | { | |
3086 | U_CHAR *before_bp = ibp; | |
3087 | ||
3088 | while (ibp < limit) { | |
3089 | switch (*ibp++) { | |
3090 | case '/': | |
1942e820 | 3091 | if (warn_comments && *ibp == '*') |
ad0c9fa1 | 3092 | warning ("`/*' within comment"); |
b0bbbd85 RS |
3093 | break; |
3094 | case '*': | |
3095 | if (*ibp == '\\' && ibp[1] == '\n') | |
3096 | newline_fix (ibp); | |
3097 | if (ibp >= limit || *ibp == '/') | |
3098 | goto comment_end; | |
3099 | break; | |
3100 | case '\n': | |
3101 | ++ip->lineno; | |
3102 | /* Copy the newline into the output buffer, in order to | |
3103 | avoid the pain of a #line every time a multiline comment | |
3104 | is seen. */ | |
3105 | if (!put_out_comments) | |
3106 | *obp++ = '\n'; | |
3107 | ++op->lineno; | |
3108 | } | |
3109 | } | |
3110 | comment_end: | |
3111 | ||
3112 | if (ibp >= limit) | |
3113 | error_with_line (line_for_error (start_line), | |
3114 | "unterminated comment"); | |
3115 | else { | |
3116 | ibp++; | |
3117 | if (put_out_comments) { | |
4c9a05bc | 3118 | bcopy ((char *) before_bp, (char *) obp, ibp - before_bp); |
b0bbbd85 RS |
3119 | obp += ibp - before_bp; |
3120 | } | |
3121 | } | |
3122 | } | |
3123 | break; | |
3124 | ||
3125 | case '$': | |
3126 | if (!dollars_in_ident) | |
3127 | goto randomchar; | |
3128 | goto letter; | |
3129 | ||
3130 | case '0': case '1': case '2': case '3': case '4': | |
3131 | case '5': case '6': case '7': case '8': case '9': | |
3132 | /* If digit is not part of identifier, it starts a number, | |
3133 | which means that following letters are not an identifier. | |
3134 | "0x5" does not refer to an identifier "x5". | |
3135 | So copy all alphanumerics that follow without accumulating | |
3136 | as an identifier. Periods also, for sake of "3.e7". */ | |
3137 | ||
3138 | if (ident_length == 0) { | |
1942e820 RK |
3139 | for (;;) { |
3140 | while (ibp[0] == '\\' && ibp[1] == '\n') { | |
b0bbbd85 RS |
3141 | ++ip->lineno; |
3142 | ibp += 2; | |
3143 | } | |
3144 | c = *ibp++; | |
fd12bc82 | 3145 | if (!is_idchar[c] && c != '.') { |
b0bbbd85 RS |
3146 | --ibp; |
3147 | break; | |
3148 | } | |
3149 | *obp++ = c; | |
3150 | /* A sign can be part of a preprocessing number | |
3151 | if it follows an e. */ | |
3152 | if (c == 'e' || c == 'E') { | |
1942e820 | 3153 | while (ibp[0] == '\\' && ibp[1] == '\n') { |
b0bbbd85 RS |
3154 | ++ip->lineno; |
3155 | ibp += 2; | |
3156 | } | |
1942e820 | 3157 | if (*ibp == '+' || *ibp == '-') { |
b0bbbd85 RS |
3158 | *obp++ = *ibp++; |
3159 | /* But traditional C does not let the token go past the sign. */ | |
3160 | if (traditional) | |
3161 | break; | |
3162 | } | |
3163 | } | |
3164 | } | |
3165 | break; | |
3166 | } | |
3167 | /* fall through */ | |
3168 | ||
3169 | case '_': | |
3170 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
3171 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
3172 | case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': | |
3173 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
3174 | case 'y': case 'z': | |
3175 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
3176 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
3177 | case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': | |
3178 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
3179 | case 'Y': case 'Z': | |
3180 | letter: | |
3181 | ident_length++; | |
3182 | /* Compute step of hash function, to avoid a proc call on every token */ | |
3183 | hash = HASHSTEP (hash, c); | |
3184 | break; | |
3185 | ||
3186 | case '\n': | |
d0691cfb RS |
3187 | if (ip->fname == 0 && *ibp == '-') { |
3188 | /* Newline - inhibits expansion of preceding token. | |
3189 | If expanding a macro arg, we keep the newline -. | |
3190 | In final output, it is deleted. | |
3191 | We recognize Newline - in macro bodies and macro args. */ | |
3192 | if (! concatenated) { | |
3193 | ident_length = 0; | |
3194 | hash = 0; | |
3195 | } | |
3196 | ibp++; | |
3197 | if (!output_marks) { | |
3198 | obp--; | |
3199 | } else { | |
3200 | /* If expanding a macro arg, keep the newline -. */ | |
3201 | *obp++ = '-'; | |
3202 | } | |
3203 | break; | |
3204 | } | |
3205 | ||
b0bbbd85 | 3206 | /* If reprocessing a macro expansion, newline is a special marker. */ |
d0691cfb | 3207 | else if (ip->macro != 0) { |
b0bbbd85 RS |
3208 | /* Newline White is a "funny space" to separate tokens that are |
3209 | supposed to be separate but without space between. | |
3210 | Here White means any whitespace character. | |
3211 | Newline - marks a recursive macro use that is not | |
3212 | supposed to be expandable. */ | |
3213 | ||
d0691cfb | 3214 | if (is_space[*ibp]) { |
b0bbbd85 RS |
3215 | /* Newline Space does not prevent expansion of preceding token |
3216 | so expand the preceding token and then come back. */ | |
3217 | if (ident_length > 0) | |
3218 | goto specialchar; | |
3219 | ||
3220 | /* If generating final output, newline space makes a space. */ | |
3221 | if (!output_marks) { | |
3222 | obp[-1] = *ibp++; | |
3223 | /* And Newline Newline makes a newline, so count it. */ | |
3224 | if (obp[-1] == '\n') | |
3225 | op->lineno++; | |
3226 | } else { | |
3227 | /* If expanding a macro arg, keep the newline space. | |
3228 | If the arg gets stringified, newline space makes nothing. */ | |
3229 | *obp++ = *ibp++; | |
3230 | } | |
3231 | } else abort (); /* Newline followed by something random? */ | |
3232 | break; | |
3233 | } | |
3234 | ||
3235 | /* If there is a pending identifier, handle it and come back here. */ | |
3236 | if (ident_length > 0) | |
3237 | goto specialchar; | |
3238 | ||
3239 | beg_of_line = ibp; | |
3240 | ||
3241 | /* Update the line counts and output a #line if necessary. */ | |
3242 | ++ip->lineno; | |
3243 | ++op->lineno; | |
3244 | if (ip->lineno != op->lineno) { | |
3245 | op->bufp = obp; | |
adcfa681 | 3246 | output_line_directive (ip, op, 1, same_file); |
b415f25e | 3247 | check_expand (op, limit - ibp); |
b0bbbd85 RS |
3248 | obp = op->bufp; |
3249 | } | |
3250 | break; | |
3251 | ||
3252 | /* Come here either after (1) a null character that is part of the input | |
3253 | or (2) at the end of the input, because there is a null there. */ | |
3254 | case 0: | |
3255 | if (ibp <= limit) | |
3256 | /* Our input really contains a null character. */ | |
3257 | goto randomchar; | |
3258 | ||
3259 | /* At end of a macro-expansion level, pop it and read next level. */ | |
3260 | if (ip->macro != 0) { | |
3261 | obp--; | |
3262 | ibp--; | |
3263 | /* If traditional, and we have an identifier that ends here, | |
3264 | process it now, so we get the right error for recursion. */ | |
3265 | if (traditional && ident_length | |
3266 | && ! is_idchar[*instack[indepth - 1].bufp]) { | |
3267 | redo_char = 1; | |
3268 | goto randomchar; | |
3269 | } | |
3270 | POPMACRO; | |
3271 | RECACHE; | |
3272 | break; | |
3273 | } | |
3274 | ||
3275 | /* If we don't have a pending identifier, | |
3276 | return at end of input. */ | |
3277 | if (ident_length == 0) { | |
3278 | obp--; | |
3279 | ibp--; | |
3280 | op->bufp = obp; | |
3281 | ip->bufp = ibp; | |
3282 | goto ending; | |
3283 | } | |
3284 | ||
3285 | /* If we do have a pending identifier, just consider this null | |
3286 | a special character and arrange to dispatch on it again. | |
3287 | The second time, IDENT_LENGTH will be zero so we will return. */ | |
3288 | ||
3289 | /* Fall through */ | |
3290 | ||
3291 | specialchar: | |
3292 | ||
3293 | /* Handle the case of a character such as /, ', " or null | |
3294 | seen following an identifier. Back over it so that | |
3295 | after the identifier is processed the special char | |
3296 | will be dispatched on again. */ | |
3297 | ||
3298 | ibp--; | |
3299 | obp--; | |
3300 | redo_char = 1; | |
3301 | ||
3302 | default: | |
3303 | ||
3304 | randomchar: | |
3305 | ||
3306 | if (ident_length > 0) { | |
3307 | register HASHNODE *hp; | |
3308 | ||
3309 | /* We have just seen an identifier end. If it's a macro, expand it. | |
3310 | ||
3311 | IDENT_LENGTH is the length of the identifier | |
3312 | and HASH is its hash code. | |
3313 | ||
3314 | The identifier has already been copied to the output, | |
3315 | so if it is a macro we must remove it. | |
3316 | ||
3317 | If REDO_CHAR is 0, the char that terminated the identifier | |
3318 | has been skipped in the output and the input. | |
3319 | OBP-IDENT_LENGTH-1 points to the identifier. | |
3320 | If the identifier is a macro, we must back over the terminator. | |
3321 | ||
3322 | If REDO_CHAR is 1, the terminating char has already been | |
3323 | backed over. OBP-IDENT_LENGTH points to the identifier. */ | |
3324 | ||
3325 | if (!pcp_outfile || pcp_inside_if) { | |
b0bbbd85 RS |
3326 | for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL; |
3327 | hp = hp->next) { | |
3328 | ||
3329 | if (hp->length == ident_length) { | |
3330 | int obufp_before_macroname; | |
3331 | int op_lineno_before_macroname; | |
3332 | register int i = ident_length; | |
3333 | register U_CHAR *p = hp->name; | |
3334 | register U_CHAR *q = obp - i; | |
3335 | int disabled; | |
3336 | ||
3337 | if (! redo_char) | |
3338 | q--; | |
3339 | ||
3340 | do { /* All this to avoid a strncmp () */ | |
3341 | if (*p++ != *q++) | |
3342 | goto hashcollision; | |
3343 | } while (--i); | |
3344 | ||
3345 | /* We found a use of a macro name. | |
3346 | see if the context shows it is a macro call. */ | |
3347 | ||
3348 | /* Back up over terminating character if not already done. */ | |
3349 | if (! redo_char) { | |
3350 | ibp--; | |
3351 | obp--; | |
3352 | } | |
3353 | ||
3354 | /* Save this as a displacement from the beginning of the output | |
3355 | buffer. We can not save this as a position in the output | |
3356 | buffer, because it may get realloc'ed by RECACHE. */ | |
3357 | obufp_before_macroname = (obp - op->buf) - ident_length; | |
3358 | op_lineno_before_macroname = op->lineno; | |
3359 | ||
3360 | if (hp->type == T_PCSTRING) { | |
3361 | pcstring_used (hp); /* Mark the definition of this key | |
3362 | as needed, ensuring that it | |
3363 | will be output. */ | |
3364 | break; /* Exit loop, since the key cannot have a | |
3365 | definition any longer. */ | |
3366 | } | |
3367 | ||
3368 | /* Record whether the macro is disabled. */ | |
3369 | disabled = hp->type == T_DISABLED; | |
3370 | ||
3371 | /* This looks like a macro ref, but if the macro was disabled, | |
3372 | just copy its name and put in a marker if requested. */ | |
3373 | ||
3374 | if (disabled) { | |
3375 | #if 0 | |
3376 | /* This error check caught useful cases such as | |
ad0c9fa1 RS |
3377 | #define foo(x,y) bar (x (y,0), y) |
3378 | foo (foo, baz) */ | |
b0bbbd85 RS |
3379 | if (traditional) |
3380 | error ("recursive use of macro `%s'", hp->name); | |
3381 | #endif | |
3382 | ||
3383 | if (output_marks) { | |
3384 | check_expand (op, limit - ibp + 2); | |
3385 | *obp++ = '\n'; | |
3386 | *obp++ = '-'; | |
3387 | } | |
3388 | break; | |
3389 | } | |
3390 | ||
3391 | /* If macro wants an arglist, verify that a '(' follows. | |
3392 | first skip all whitespace, copying it to the output | |
3393 | after the macro name. Then, if there is no '(', | |
3394 | decide this is not a macro call and leave things that way. */ | |
3395 | if ((hp->type == T_MACRO || hp->type == T_DISABLED) | |
3396 | && hp->value.defn->nargs >= 0) | |
3397 | { | |
3398 | U_CHAR *old_ibp = ibp; | |
3399 | U_CHAR *old_obp = obp; | |
3400 | int old_iln = ip->lineno; | |
3401 | int old_oln = op->lineno; | |
3402 | ||
3403 | while (1) { | |
3404 | /* Scan forward over whitespace, copying it to the output. */ | |
3405 | if (ibp == limit && ip->macro != 0) { | |
3406 | POPMACRO; | |
3407 | RECACHE; | |
3408 | old_ibp = ibp; | |
3409 | old_obp = obp; | |
3410 | old_iln = ip->lineno; | |
3411 | old_oln = op->lineno; | |
3412 | } | |
3413 | /* A comment: copy it unchanged or discard it. */ | |
1942e820 | 3414 | else if (*ibp == '/' && ibp[1] == '*') { |
b0bbbd85 RS |
3415 | if (put_out_comments) { |
3416 | *obp++ = '/'; | |
3417 | *obp++ = '*'; | |
3418 | } else if (! traditional) { | |
3419 | *obp++ = ' '; | |
3420 | } | |
3421 | ibp += 2; | |
3422 | while (ibp + 1 != limit | |
3423 | && !(ibp[0] == '*' && ibp[1] == '/')) { | |
3424 | /* We need not worry about newline-marks, | |
3425 | since they are never found in comments. */ | |
3426 | if (*ibp == '\n') { | |
3427 | /* Newline in a file. Count it. */ | |
3428 | ++ip->lineno; | |
3429 | ++op->lineno; | |
3430 | } | |
3431 | if (put_out_comments) | |
3432 | *obp++ = *ibp++; | |
3433 | else | |
3434 | ibp++; | |
3435 | } | |
3436 | ibp += 2; | |
3437 | if (put_out_comments) { | |
3438 | *obp++ = '*'; | |
3439 | *obp++ = '/'; | |
3440 | } | |
3441 | } | |
3442 | else if (is_space[*ibp]) { | |
3443 | *obp++ = *ibp++; | |
3444 | if (ibp[-1] == '\n') { | |
3445 | if (ip->macro == 0) { | |
3446 | /* Newline in a file. Count it. */ | |
3447 | ++ip->lineno; | |
3448 | ++op->lineno; | |
3449 | } else if (!output_marks) { | |
3450 | /* A newline mark, and we don't want marks | |
3451 | in the output. If it is newline-hyphen, | |
3452 | discard it entirely. Otherwise, it is | |
3453 | newline-whitechar, so keep the whitechar. */ | |
3454 | obp--; | |
3455 | if (*ibp == '-') | |
3456 | ibp++; | |
3457 | else { | |
3458 | if (*ibp == '\n') | |
3459 | ++op->lineno; | |
3460 | *obp++ = *ibp++; | |
3461 | } | |
3462 | } else { | |
3463 | /* A newline mark; copy both chars to the output. */ | |
3464 | *obp++ = *ibp++; | |
3465 | } | |
3466 | } | |
3467 | } | |
3468 | else break; | |
3469 | } | |
3470 | if (*ibp != '(') { | |
3471 | /* It isn't a macro call. | |
3472 | Put back the space that we just skipped. */ | |
3473 | ibp = old_ibp; | |
3474 | obp = old_obp; | |
3475 | ip->lineno = old_iln; | |
3476 | op->lineno = old_oln; | |
3477 | /* Exit the for loop. */ | |
3478 | break; | |
3479 | } | |
3480 | } | |
3481 | ||
3482 | /* This is now known to be a macro call. | |
3483 | Discard the macro name from the output, | |
02c1d408 | 3484 | along with any following whitespace just copied, |
e1fe312f | 3485 | but preserve newlines if not outputting marks since this |
02c1d408 | 3486 | is more likely to do the right thing with line numbers. */ |
b0bbbd85 | 3487 | obp = op->buf + obufp_before_macroname; |
e1fe312f | 3488 | if (output_marks) |
02c1d408 RK |
3489 | op->lineno = op_lineno_before_macroname; |
3490 | else { | |
3491 | int newlines = op->lineno - op_lineno_before_macroname; | |
3492 | while (0 < newlines--) | |
3493 | *obp++ = '\n'; | |
3494 | } | |
d0691cfb RS |
3495 | |
3496 | /* Prevent accidental token-pasting with a character | |
3497 | before the macro call. */ | |
f803dcf8 RK |
3498 | if (!traditional && obp != op->buf) { |
3499 | switch (obp[-1]) { | |
15e779e8 RK |
3500 | case '!': case '%': case '&': case '*': |
3501 | case '+': case '-': case '/': case ':': | |
3502 | case '<': case '=': case '>': case '^': | |
3503 | case '|': | |
f803dcf8 RK |
3504 | /* If we are expanding a macro arg, make a newline marker |
3505 | to separate the tokens. If we are making real output, | |
3506 | a plain space will do. */ | |
3507 | if (output_marks) | |
3508 | *obp++ = '\n'; | |
3509 | *obp++ = ' '; | |
3510 | } | |
d0691cfb RS |
3511 | } |
3512 | ||
b0bbbd85 RS |
3513 | /* Expand the macro, reading arguments as needed, |
3514 | and push the expansion on the input stack. */ | |
3515 | ip->bufp = ibp; | |
3516 | op->bufp = obp; | |
3517 | macroexpand (hp, op); | |
3518 | ||
3519 | /* Reexamine input stack, since macroexpand has pushed | |
3520 | a new level on it. */ | |
3521 | obp = op->bufp; | |
3522 | RECACHE; | |
3523 | break; | |
3524 | } | |
3525 | hashcollision: | |
3526 | ; | |
3527 | } /* End hash-table-search loop */ | |
3528 | } | |
3529 | ident_length = hash = 0; /* Stop collecting identifier */ | |
3530 | redo_char = 0; | |
3531 | concatenated = 0; | |
3532 | } /* End if (ident_length > 0) */ | |
3533 | } /* End switch */ | |
3534 | } /* End per-char loop */ | |
3535 | ||
3536 | /* Come here to return -- but first give an error message | |
3537 | if there was an unterminated successful conditional. */ | |
3538 | ending: | |
2af5e9e2 RK |
3539 | if (if_stack != ip->if_stack) |
3540 | { | |
25cbb59e | 3541 | char *str; |
2af5e9e2 RK |
3542 | |
3543 | switch (if_stack->type) | |
3544 | { | |
3545 | case T_IF: | |
3546 | str = "if"; | |
3547 | break; | |
3548 | case T_IFDEF: | |
3549 | str = "ifdef"; | |
3550 | break; | |
3551 | case T_IFNDEF: | |
3552 | str = "ifndef"; | |
3553 | break; | |
3554 | case T_ELSE: | |
3555 | str = "else"; | |
3556 | break; | |
3557 | case T_ELIF: | |
3558 | str = "elif"; | |
3559 | break; | |
25cbb59e RK |
3560 | default: |
3561 | abort (); | |
2af5e9e2 RK |
3562 | } |
3563 | ||
3564 | error_with_line (line_for_error (if_stack->lineno), | |
3565 | "unterminated `#%s' conditional", str); | |
b0bbbd85 RS |
3566 | } |
3567 | if_stack = ip->if_stack; | |
3568 | } | |
3569 | \f | |
3570 | /* | |
3571 | * Rescan a string into a temporary buffer and return the result | |
3572 | * as a FILE_BUF. Note this function returns a struct, not a pointer. | |
3573 | * | |
3574 | * OUTPUT_MARKS nonzero means keep Newline markers found in the input | |
3575 | * and insert such markers when appropriate. See `rescan' for details. | |
3576 | * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately | |
3577 | * before substitution; it is 0 for other uses. | |
3578 | */ | |
3579 | static FILE_BUF | |
3580 | expand_to_temp_buffer (buf, limit, output_marks, assertions) | |
3581 | U_CHAR *buf, *limit; | |
3582 | int output_marks, assertions; | |
3583 | { | |
3584 | register FILE_BUF *ip; | |
3585 | FILE_BUF obuf; | |
3586 | int length = limit - buf; | |
3587 | U_CHAR *buf1; | |
3588 | int odepth = indepth; | |
3589 | int save_assertions_flag = assertions_flag; | |
3590 | ||
3591 | assertions_flag = assertions; | |
3592 | ||
3593 | if (length < 0) | |
3594 | abort (); | |
3595 | ||
3596 | /* Set up the input on the input stack. */ | |
3597 | ||
3598 | buf1 = (U_CHAR *) alloca (length + 1); | |
3599 | { | |
3600 | register U_CHAR *p1 = buf; | |
3601 | register U_CHAR *p2 = buf1; | |
3602 | ||
3603 | while (p1 != limit) | |
3604 | *p2++ = *p1++; | |
3605 | } | |
3606 | buf1[length] = 0; | |
3607 | ||
3608 | /* Set up to receive the output. */ | |
3609 | ||
3610 | obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */ | |
3611 | obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length); | |
3612 | obuf.fname = 0; | |
3613 | obuf.macro = 0; | |
3614 | obuf.free_ptr = 0; | |
3615 | ||
3616 | CHECK_DEPTH ({return obuf;}); | |
3617 | ||
3618 | ++indepth; | |
3619 | ||
3620 | ip = &instack[indepth]; | |
3621 | ip->fname = 0; | |
3622 | ip->nominal_fname = 0; | |
3623 | ip->system_header_p = 0; | |
3624 | ip->macro = 0; | |
3625 | ip->free_ptr = 0; | |
3626 | ip->length = length; | |
3627 | ip->buf = ip->bufp = buf1; | |
3628 | ip->if_stack = if_stack; | |
3629 | ||
3630 | ip->lineno = obuf.lineno = 1; | |
3631 | ||
3632 | /* Scan the input, create the output. */ | |
3633 | rescan (&obuf, output_marks); | |
3634 | ||
3635 | /* Pop input stack to original state. */ | |
3636 | --indepth; | |
3637 | ||
3638 | if (indepth != odepth) | |
3639 | abort (); | |
3640 | ||
3641 | /* Record the output. */ | |
3642 | obuf.length = obuf.bufp - obuf.buf; | |
3643 | ||
3644 | assertions_flag = save_assertions_flag; | |
3645 | return obuf; | |
3646 | } | |
3647 | \f | |
3648 | /* | |
3649 | * Process a # directive. Expects IP->bufp to point after the '#', as in | |
adcfa681 | 3650 | * `#define foo bar'. Passes to the directive handler |
b0bbbd85 | 3651 | * (do_define, do_include, etc.): the addresses of the 1st and |
adcfa681 RK |
3652 | * last chars of the directive (starting immediately after the # |
3653 | * keyword), plus op and the keyword table pointer. If the directive | |
b0bbbd85 | 3654 | * contains comments it is copied into a temporary buffer sans comments |
adcfa681 | 3655 | * and the temporary buffer is passed to the directive handler instead. |
b0bbbd85 RS |
3656 | * Likewise for backslash-newlines. |
3657 | * | |
3658 | * Returns nonzero if this was a known # directive. | |
3659 | * Otherwise, returns zero, without advancing the input pointer. | |
3660 | */ | |
3661 | ||
3662 | static int | |
3663 | handle_directive (ip, op) | |
3664 | FILE_BUF *ip, *op; | |
3665 | { | |
3666 | register U_CHAR *bp, *cp; | |
3667 | register struct directive *kt; | |
3668 | register int ident_length; | |
3669 | U_CHAR *resume_p; | |
3670 | ||
adcfa681 | 3671 | /* Nonzero means we must copy the entire directive |
b0bbbd85 | 3672 | to get rid of comments or backslash-newlines. */ |
adcfa681 | 3673 | int copy_directive = 0; |
b0bbbd85 RS |
3674 | |
3675 | U_CHAR *ident, *after_ident; | |
3676 | ||
3677 | bp = ip->bufp; | |
3678 | ||
3679 | /* Record where the directive started. do_xifdef needs this. */ | |
3680 | directive_start = bp - 1; | |
3681 | ||
3682 | /* Skip whitespace and \-newline. */ | |
3683 | while (1) { | |
3684 | if (is_hor_space[*bp]) { | |
c03413c7 RK |
3685 | if (*bp != ' ' && *bp != '\t' && pedantic) |
3686 | pedwarn ("%s in preprocessing directive", char_name[*bp]); | |
b0bbbd85 | 3687 | bp++; |
80512db7 JW |
3688 | } else if (*bp == '/' && (bp[1] == '*' |
3689 | || (cplusplus_comments && bp[1] == '/'))) { | |
3690 | ip->bufp = bp + 2; | |
2aa7ec37 | 3691 | skip_to_end_of_comment (ip, &ip->lineno, 0); |
b0bbbd85 RS |
3692 | bp = ip->bufp; |
3693 | } else if (*bp == '\\' && bp[1] == '\n') { | |
3694 | bp += 2; ip->lineno++; | |
3695 | } else break; | |
3696 | } | |
3697 | ||
3698 | /* Now find end of directive name. | |
3699 | If we encounter a backslash-newline, exchange it with any following | |
3700 | symbol-constituents so that we end up with a contiguous name. */ | |
3701 | ||
3702 | cp = bp; | |
3703 | while (1) { | |
3704 | if (is_idchar[*cp]) | |
3705 | cp++; | |
3706 | else { | |
3707 | if (*cp == '\\' && cp[1] == '\n') | |
3708 | name_newline_fix (cp); | |
3709 | if (is_idchar[*cp]) | |
3710 | cp++; | |
3711 | else break; | |
3712 | } | |
3713 | } | |
3714 | ident_length = cp - bp; | |
3715 | ident = bp; | |
3716 | after_ident = cp; | |
3717 | ||
3718 | /* A line of just `#' becomes blank. */ | |
3719 | ||
3720 | if (ident_length == 0 && *after_ident == '\n') { | |
3721 | ip->bufp = after_ident; | |
3722 | return 1; | |
3723 | } | |
3724 | ||
3725 | if (ident_length == 0 || !is_idstart[*ident]) { | |
3726 | U_CHAR *p = ident; | |
3727 | while (is_idchar[*p]) { | |
3728 | if (*p < '0' || *p > '9') | |
3729 | break; | |
3730 | p++; | |
3731 | } | |
3732 | /* Handle # followed by a line number. */ | |
3733 | if (p != ident && !is_idchar[*p]) { | |
3734 | static struct directive line_directive_table[] = { | |
3735 | { 4, do_line, "line", T_LINE}, | |
3736 | }; | |
3737 | if (pedantic) | |
3738 | pedwarn ("`#' followed by integer"); | |
3739 | after_ident = ident; | |
3740 | kt = line_directive_table; | |
3741 | goto old_linenum; | |
3742 | } | |
3743 | ||
3744 | /* Avoid error for `###' and similar cases unless -pedantic. */ | |
3745 | if (p == ident) { | |
3746 | while (*p == '#' || is_hor_space[*p]) p++; | |
3747 | if (*p == '\n') { | |
3748 | if (pedantic && !lang_asm) | |
adcfa681 | 3749 | warning ("invalid preprocessing directive"); |
b0bbbd85 RS |
3750 | return 0; |
3751 | } | |
3752 | } | |
3753 | ||
3754 | if (!lang_asm) | |
adcfa681 | 3755 | error ("invalid preprocessing directive name"); |
b0bbbd85 RS |
3756 | |
3757 | return 0; | |
3758 | } | |
3759 | ||
3760 | /* | |
3761 | * Decode the keyword and call the appropriate expansion | |
3762 | * routine, after moving the input pointer up to the next line. | |
3763 | */ | |
3764 | for (kt = directive_table; kt->length > 0; kt++) { | |
25cbb59e | 3765 | if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) { |
b0bbbd85 RS |
3766 | register U_CHAR *buf; |
3767 | register U_CHAR *limit; | |
3768 | int unterminated; | |
3769 | int junk; | |
625bbc60 | 3770 | int *already_output; |
b0bbbd85 RS |
3771 | |
3772 | /* Nonzero means do not delete comments within the directive. | |
3773 | #define needs this when -traditional. */ | |
3774 | int keep_comments; | |
3775 | ||
3776 | old_linenum: | |
3777 | ||
3778 | limit = ip->buf + ip->length; | |
3779 | unterminated = 0; | |
625bbc60 | 3780 | already_output = 0; |
b0bbbd85 RS |
3781 | keep_comments = traditional && kt->traditional_comments; |
3782 | /* #import is defined only in Objective C, or when on the NeXT. */ | |
25cbb59e RK |
3783 | if (kt->type == T_IMPORT |
3784 | && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1))) | |
b0bbbd85 RS |
3785 | break; |
3786 | ||
adcfa681 | 3787 | /* Find the end of this directive (first newline not backslashed |
b0bbbd85 | 3788 | and not in a string or comment). |
adcfa681 | 3789 | Set COPY_DIRECTIVE if the directive must be copied |
b0bbbd85 RS |
3790 | (it contains a backslash-newline or a comment). */ |
3791 | ||
3792 | buf = bp = after_ident; | |
3793 | while (bp < limit) { | |
3794 | register U_CHAR c = *bp++; | |
3795 | switch (c) { | |
3796 | case '\\': | |
3797 | if (bp < limit) { | |
3798 | if (*bp == '\n') { | |
3799 | ip->lineno++; | |
adcfa681 | 3800 | copy_directive = 1; |
01bffe73 RK |
3801 | bp++; |
3802 | } else if (traditional) | |
3803 | bp++; | |
b0bbbd85 RS |
3804 | } |
3805 | break; | |
3806 | ||
3807 | case '\'': | |
3808 | case '\"': | |
adcfa681 | 3809 | bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated); |
b0bbbd85 RS |
3810 | /* Don't bother calling the directive if we already got an error |
3811 | message due to unterminated string. Skip everything and pretend | |
3812 | we called the directive. */ | |
3813 | if (unterminated) { | |
3814 | if (traditional) { | |
3815 | /* Traditional preprocessing permits unterminated strings. */ | |
3816 | ip->bufp = bp; | |
3817 | goto endloop1; | |
3818 | } | |
3819 | ip->bufp = bp; | |
3820 | return 1; | |
3821 | } | |
3822 | break; | |
3823 | ||
3824 | /* <...> is special for #include. */ | |
3825 | case '<': | |
3826 | if (!kt->angle_brackets) | |
3827 | break; | |
a957c794 RK |
3828 | while (bp < limit && *bp != '>' && *bp != '\n') { |
3829 | if (*bp == '\\' && bp[1] == '\n') { | |
3830 | ip->lineno++; | |
adcfa681 | 3831 | copy_directive = 1; |
a957c794 RK |
3832 | bp++; |
3833 | } | |
3834 | bp++; | |
3835 | } | |
b0bbbd85 RS |
3836 | break; |
3837 | ||
3838 | case '/': | |
3839 | if (*bp == '\\' && bp[1] == '\n') | |
3840 | newline_fix (bp); | |
3841 | if (*bp == '*' | |
eda5fa7b | 3842 | || (cplusplus_comments && *bp == '/')) { |
b0bbbd85 RS |
3843 | U_CHAR *obp = bp - 1; |
3844 | ip->bufp = bp + 1; | |
2aa7ec37 | 3845 | skip_to_end_of_comment (ip, &ip->lineno, 0); |
b0bbbd85 | 3846 | bp = ip->bufp; |
adcfa681 | 3847 | /* No need to copy the directive because of a comment at the end; |
b0bbbd85 RS |
3848 | just don't include the comment in the directive. */ |
3849 | if (bp == limit || *bp == '\n') { | |
3850 | bp = obp; | |
3851 | goto endloop1; | |
3852 | } | |
3853 | /* Don't remove the comments if -traditional. */ | |
3854 | if (! keep_comments) | |
adcfa681 | 3855 | copy_directive++; |
b0bbbd85 RS |
3856 | } |
3857 | break; | |
3858 | ||
3859 | case '\f': | |
c03413c7 | 3860 | case '\r': |
b0bbbd85 RS |
3861 | case '\v': |
3862 | if (pedantic) | |
c03413c7 | 3863 | pedwarn ("%s in preprocessing directive", char_name[c]); |
b0bbbd85 RS |
3864 | break; |
3865 | ||
3866 | case '\n': | |
3867 | --bp; /* Point to the newline */ | |
3868 | ip->bufp = bp; | |
3869 | goto endloop1; | |
3870 | } | |
3871 | } | |
3872 | ip->bufp = bp; | |
3873 | ||
3874 | endloop1: | |
3875 | resume_p = ip->bufp; | |
3876 | /* BP is the end of the directive. | |
3877 | RESUME_P is the next interesting data after the directive. | |
3878 | A comment may come between. */ | |
3879 | ||
3880 | /* If a directive should be copied through, and -E was given, | |
3881 | pass it through before removing comments. */ | |
3882 | if (!no_output && kt->pass_thru && put_out_comments) { | |
3883 | int len; | |
3884 | ||
3885 | /* Output directive name. */ | |
3886 | check_expand (op, kt->length + 2); | |
3887 | /* Make sure # is at the start of a line */ | |
3888 | if (op->bufp > op->buf && op->bufp[-1] != '\n') { | |
3889 | op->lineno++; | |
3890 | *op->bufp++ = '\n'; | |
3891 | } | |
3892 | *op->bufp++ = '#'; | |
3893 | bcopy (kt->name, op->bufp, kt->length); | |
3894 | op->bufp += kt->length; | |
3895 | ||
3896 | /* Output arguments. */ | |
3897 | len = (bp - buf); | |
3898 | check_expand (op, len); | |
4c9a05bc | 3899 | bcopy (buf, (char *) op->bufp, len); |
b0bbbd85 RS |
3900 | op->bufp += len; |
3901 | /* Take account of any (escaped) newlines just output. */ | |
3902 | while (--len >= 0) | |
3903 | if (buf[len] == '\n') | |
3904 | op->lineno++; | |
3905 | ||
3906 | already_output = &junk; | |
3907 | } /* Don't we need a newline or #line? */ | |
3908 | ||
adcfa681 | 3909 | if (copy_directive) { |
b0bbbd85 | 3910 | register U_CHAR *xp = buf; |
adcfa681 | 3911 | /* Need to copy entire directive into temp buffer before dispatching */ |
b0bbbd85 | 3912 | |
adcfa681 | 3913 | cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus |
b0bbbd85 RS |
3914 | some slop */ |
3915 | buf = cp; | |
3916 | ||
3917 | /* Copy to the new buffer, deleting comments | |
3918 | and backslash-newlines (and whitespace surrounding the latter). */ | |
3919 | ||
3920 | while (xp < bp) { | |
3921 | register U_CHAR c = *xp++; | |
3922 | *cp++ = c; | |
3923 | ||
3924 | switch (c) { | |
3925 | case '\n': | |
3926 | abort (); /* A bare newline should never part of the line. */ | |
3927 | break; | |
3928 | ||
3929 | /* <...> is special for #include. */ | |
3930 | case '<': | |
3931 | if (!kt->angle_brackets) | |
3932 | break; | |
3933 | while (xp < bp && c != '>') { | |
3934 | c = *xp++; | |
3935 | if (c == '\\' && xp < bp && *xp == '\n') | |
3936 | xp++; | |
3937 | else | |
3938 | *cp++ = c; | |
3939 | } | |
3940 | break; | |
3941 | ||
3942 | case '\\': | |
3943 | if (*xp == '\n') { | |
3944 | xp++; | |
3945 | cp--; | |
3946 | if (cp != buf && is_space[cp[-1]]) { | |
3947 | while (cp != buf && is_space[cp[-1]]) cp--; | |
3948 | cp++; | |
3949 | SKIP_WHITE_SPACE (xp); | |
3950 | } else if (is_space[*xp]) { | |
3951 | *cp++ = *xp++; | |
3952 | SKIP_WHITE_SPACE (xp); | |
3953 | } | |
2e420a17 | 3954 | } else if (traditional && xp < bp) { |
1a5b457d | 3955 | *cp++ = *xp++; |
b0bbbd85 RS |
3956 | } |
3957 | break; | |
3958 | ||
3959 | case '\'': | |
3960 | case '\"': | |
3961 | { | |
3962 | register U_CHAR *bp1 | |
8d9bfdc5 RK |
3963 | = skip_quoted_string (xp - 1, bp, ip->lineno, |
3964 | NULL_PTR, NULL_PTR, NULL_PTR); | |
b0bbbd85 RS |
3965 | while (xp != bp1) |
3966 | if (*xp == '\\') { | |
3967 | if (*++xp != '\n') | |
3968 | *cp++ = '\\'; | |
3969 | else | |
3970 | xp++; | |
3971 | } else | |
3972 | *cp++ = *xp++; | |
3973 | } | |
3974 | break; | |
3975 | ||
3976 | case '/': | |
3977 | if (*xp == '*' | |
eda5fa7b | 3978 | || (cplusplus_comments && *xp == '/')) { |
b0bbbd85 | 3979 | ip->bufp = xp + 1; |
adcfa681 | 3980 | /* If we already copied the directive through, |
b0bbbd85 | 3981 | already_output != 0 prevents outputting comment now. */ |
2aa7ec37 | 3982 | skip_to_end_of_comment (ip, already_output, 0); |
b0bbbd85 RS |
3983 | if (keep_comments) |
3984 | while (xp != ip->bufp) | |
3985 | *cp++ = *xp++; | |
3986 | /* Delete or replace the slash. */ | |
3987 | else if (traditional) | |
3988 | cp--; | |
3989 | else | |
3990 | cp[-1] = ' '; | |
3991 | xp = ip->bufp; | |
3992 | } | |
3993 | } | |
3994 | } | |
3995 | ||
3996 | /* Null-terminate the copy. */ | |
3997 | ||
3998 | *cp = 0; | |
3999 | } else | |
4000 | cp = bp; | |
4001 | ||
4002 | ip->bufp = resume_p; | |
4003 | ||
4004 | /* Some directives should be written out for cc1 to process, | |
4005 | just as if they were not defined. And sometimes we're copying | |
4006 | definitions through. */ | |
4007 | ||
4008 | if (!no_output && already_output == 0 | |
4009 | && (kt->pass_thru | |
4010 | || (kt->type == T_DEFINE | |
4011 | && (dump_macros == dump_names | |
4012 | || dump_macros == dump_definitions)))) { | |
4013 | int len; | |
4014 | ||
4015 | /* Output directive name. */ | |
4016 | check_expand (op, kt->length + 1); | |
4017 | *op->bufp++ = '#'; | |
4c9a05bc | 4018 | bcopy (kt->name, (char *) op->bufp, kt->length); |
b0bbbd85 RS |
4019 | op->bufp += kt->length; |
4020 | ||
4021 | if (kt->pass_thru || dump_macros == dump_definitions) { | |
4022 | /* Output arguments. */ | |
4023 | len = (cp - buf); | |
4024 | check_expand (op, len); | |
4c9a05bc | 4025 | bcopy (buf, (char *) op->bufp, len); |
b0bbbd85 | 4026 | op->bufp += len; |
448251cf RS |
4027 | } else if (kt->type == T_DEFINE && dump_macros == dump_names) { |
4028 | U_CHAR *xp = buf; | |
4029 | U_CHAR *yp; | |
4030 | SKIP_WHITE_SPACE (xp); | |
4031 | yp = xp; | |
4032 | while (is_idchar[*xp]) xp++; | |
4033 | len = (xp - yp); | |
4034 | check_expand (op, len + 1); | |
4035 | *op->bufp++ = ' '; | |
4036 | bcopy (yp, op->bufp, len); | |
4037 | op->bufp += len; | |
b0bbbd85 RS |
4038 | } |
4039 | } /* Don't we need a newline or #line? */ | |
4040 | ||
adcfa681 | 4041 | /* Call the appropriate directive handler. buf now points to |
b0bbbd85 RS |
4042 | either the appropriate place in the input buffer, or to |
4043 | the temp buffer if it was necessary to make one. cp | |
4044 | points to the first char after the contents of the (possibly | |
adcfa681 | 4045 | copied) directive, in either case. */ |
b0bbbd85 RS |
4046 | (*kt->func) (buf, cp, op, kt); |
4047 | check_expand (op, ip->length - (ip->bufp - ip->buf)); | |
4048 | ||
4049 | return 1; | |
4050 | } | |
4051 | } | |
4052 | ||
4053 | /* It is deliberate that we don't warn about undefined directives. | |
4054 | That is the responsibility of cc1. */ | |
4055 | return 0; | |
4056 | } | |
4057 | \f | |
bfa30b22 RK |
4058 | static struct tm * |
4059 | timestamp () | |
4060 | { | |
4061 | static struct tm *timebuf; | |
4062 | if (!timebuf) { | |
0852bc0c | 4063 | time_t t = time ((time_t *)0); |
bfa30b22 RK |
4064 | timebuf = localtime (&t); |
4065 | } | |
4066 | return timebuf; | |
4067 | } | |
4068 | ||
b0bbbd85 RS |
4069 | static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
4070 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | |
4071 | }; | |
4072 | ||
4073 | /* | |
4074 | * expand things like __FILE__. Place the expansion into the output | |
4075 | * buffer *without* rescanning. | |
4076 | */ | |
4077 | ||
4078 | static void | |
4079 | special_symbol (hp, op) | |
4080 | HASHNODE *hp; | |
4081 | FILE_BUF *op; | |
4082 | { | |
4083 | char *buf; | |
4084 | int i, len; | |
4085 | int true_indepth; | |
4086 | FILE_BUF *ip = NULL; | |
bfa30b22 | 4087 | struct tm *timebuf; |
b0bbbd85 RS |
4088 | |
4089 | int paren = 0; /* For special `defined' keyword */ | |
4090 | ||
4091 | if (pcp_outfile && pcp_inside_if | |
4092 | && hp->type != T_SPEC_DEFINED && hp->type != T_CONST) | |
4093 | error ("Predefined macro `%s' used inside `#if' during precompilation", | |
4094 | hp->name); | |
4095 | ||
4096 | for (i = indepth; i >= 0; i--) | |
4097 | if (instack[i].fname != NULL) { | |
4098 | ip = &instack[i]; | |
4099 | break; | |
4100 | } | |
4101 | if (ip == NULL) { | |
4102 | error ("cccp error: not in any file?!"); | |
4103 | return; /* the show must go on */ | |
4104 | } | |
4105 | ||
4106 | switch (hp->type) { | |
4107 | case T_FILE: | |
4108 | case T_BASE_FILE: | |
4109 | { | |
4110 | char *string; | |
4111 | if (hp->type == T_FILE) | |
4112 | string = ip->nominal_fname; | |
4113 | else | |
4114 | string = instack[0].nominal_fname; | |
4115 | ||
4116 | if (string) | |
4117 | { | |
f7531123 | 4118 | buf = (char *) alloca (3 + 4 * strlen (string)); |
bb2f42b1 | 4119 | quote_string (buf, string); |
b0bbbd85 RS |
4120 | } |
4121 | else | |
4122 | buf = "\"\""; | |
4123 | ||
4124 | break; | |
4125 | } | |
4126 | ||
4127 | case T_INCLUDE_LEVEL: | |
4128 | true_indepth = 0; | |
4129 | for (i = indepth; i >= 0; i--) | |
4130 | if (instack[i].fname != NULL) | |
4131 | true_indepth++; | |
4132 | ||
4133 | buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */ | |
4134 | sprintf (buf, "%d", true_indepth - 1); | |
4135 | break; | |
4136 | ||
4137 | case T_VERSION: | |
4138 | buf = (char *) alloca (3 + strlen (version_string)); | |
4139 | sprintf (buf, "\"%s\"", version_string); | |
4140 | break; | |
4141 | ||
9e7270cd | 4142 | #ifndef NO_BUILTIN_SIZE_TYPE |
b0bbbd85 | 4143 | case T_SIZE_TYPE: |
767d412c | 4144 | buf = SIZE_TYPE; |
b0bbbd85 | 4145 | break; |
9e7270cd | 4146 | #endif |
b0bbbd85 | 4147 | |
9e7270cd | 4148 | #ifndef NO_BUILTIN_PTRDIFF_TYPE |
b0bbbd85 | 4149 | case T_PTRDIFF_TYPE: |
767d412c | 4150 | buf = PTRDIFF_TYPE; |
b0bbbd85 | 4151 | break; |
9e7270cd | 4152 | #endif |
b0bbbd85 RS |
4153 | |
4154 | case T_WCHAR_TYPE: | |
767d412c | 4155 | buf = wchar_type; |
b0bbbd85 RS |
4156 | break; |
4157 | ||
0df69870 | 4158 | case T_USER_LABEL_PREFIX_TYPE: |
767d412c | 4159 | buf = USER_LABEL_PREFIX; |
0df69870 ILT |
4160 | break; |
4161 | ||
4162 | case T_REGISTER_PREFIX_TYPE: | |
767d412c | 4163 | buf = REGISTER_PREFIX; |
0df69870 ILT |
4164 | break; |
4165 | ||
b0bbbd85 | 4166 | case T_CONST: |
91dbf5e7 | 4167 | buf = hp->value.cpval; |
b0bbbd85 RS |
4168 | if (pcp_inside_if && pcp_outfile) |
4169 | /* Output a precondition for this macro use */ | |
91dbf5e7 | 4170 | fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf); |
b0bbbd85 RS |
4171 | break; |
4172 | ||
4173 | case T_SPECLINE: | |
4174 | buf = (char *) alloca (10); | |
4175 | sprintf (buf, "%d", ip->lineno); | |
4176 | break; | |
4177 | ||
4178 | case T_DATE: | |
4179 | case T_TIME: | |
4180 | buf = (char *) alloca (20); | |
bfa30b22 | 4181 | timebuf = timestamp (); |
b0bbbd85 RS |
4182 | if (hp->type == T_DATE) |
4183 | sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon], | |
4184 | timebuf->tm_mday, timebuf->tm_year + 1900); | |
4185 | else | |
4186 | sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min, | |
4187 | timebuf->tm_sec); | |
4188 | break; | |
4189 | ||
4190 | case T_SPEC_DEFINED: | |
4191 | buf = " 0 "; /* Assume symbol is not defined */ | |
4192 | ip = &instack[indepth]; | |
4193 | SKIP_WHITE_SPACE (ip->bufp); | |
4194 | if (*ip->bufp == '(') { | |
4195 | paren++; | |
4196 | ip->bufp++; /* Skip over the paren */ | |
4197 | SKIP_WHITE_SPACE (ip->bufp); | |
4198 | } | |
4199 | ||
4200 | if (!is_idstart[*ip->bufp]) | |
4201 | goto oops; | |
25cbb59e | 4202 | if ((hp = lookup (ip->bufp, -1, -1))) { |
b0bbbd85 | 4203 | if (pcp_outfile && pcp_inside_if |
4f845465 RK |
4204 | && (hp->type == T_CONST |
4205 | || (hp->type == T_MACRO && hp->value.defn->predefined))) | |
b0bbbd85 RS |
4206 | /* Output a precondition for this macro use. */ |
4207 | fprintf (pcp_outfile, "#define %s\n", hp->name); | |
4208 | buf = " 1 "; | |
4209 | } | |
4210 | else | |
4211 | if (pcp_outfile && pcp_inside_if) { | |
4212 | /* Output a precondition for this macro use */ | |
4213 | U_CHAR *cp = ip->bufp; | |
4214 | fprintf (pcp_outfile, "#undef "); | |
4215 | while (is_idchar[*cp]) /* Ick! */ | |
4216 | fputc (*cp++, pcp_outfile); | |
4217 | putc ('\n', pcp_outfile); | |
4218 | } | |
4219 | while (is_idchar[*ip->bufp]) | |
4220 | ++ip->bufp; | |
4221 | SKIP_WHITE_SPACE (ip->bufp); | |
4222 | if (paren) { | |
4223 | if (*ip->bufp != ')') | |
4224 | goto oops; | |
4225 | ++ip->bufp; | |
4226 | } | |
4227 | break; | |
4228 | ||
4229 | oops: | |
4230 | ||
4231 | error ("`defined' without an identifier"); | |
4232 | break; | |
4233 | ||
4234 | default: | |
4235 | error ("cccp error: invalid special hash type"); /* time for gdb */ | |
4236 | abort (); | |
4237 | } | |
4238 | len = strlen (buf); | |
4239 | check_expand (op, len); | |
4c9a05bc | 4240 | bcopy (buf, (char *) op->bufp, len); |
b0bbbd85 RS |
4241 | op->bufp += len; |
4242 | ||
4243 | return; | |
4244 | } | |
4245 | ||
4246 | \f | |
4247 | /* Routines to handle #directives */ | |
4248 | ||
4249 | /* Handle #include and #import. | |
4250 | This function expects to see "fname" or <fname> on the input. */ | |
4251 | ||
4252 | static int | |
4253 | do_include (buf, limit, op, keyword) | |
4254 | U_CHAR *buf, *limit; | |
4255 | FILE_BUF *op; | |
4256 | struct directive *keyword; | |
4257 | { | |
4258 | int importing = (keyword->type == T_IMPORT); | |
4259 | int skip_dirs = (keyword->type == T_INCLUDE_NEXT); | |
4260 | static int import_warning = 0; | |
4261 | char *fname; /* Dynamically allocated fname buffer */ | |
4262 | char *pcftry; | |
4263 | char *pcfname; | |
4264 | U_CHAR *fbeg, *fend; /* Beginning and end of fname */ | |
4265 | ||
4266 | struct file_name_list *search_start = include; /* Chain of dirs to search */ | |
4267 | struct file_name_list dsp[1]; /* First in chain, if #include "..." */ | |
00ae06b5 | 4268 | struct file_name_list *searchptr = 0; |
b0bbbd85 RS |
4269 | int flen; |
4270 | ||
4271 | int f; /* file number */ | |
4272 | ||
4273 | int retried = 0; /* Have already tried macro | |
4274 | expanding the include line*/ | |
1faf9603 | 4275 | int angle_brackets = 0; /* 0 for "...", 1 for <...> */ |
b0bbbd85 RS |
4276 | int pcf = -1; |
4277 | char *pcfbuf; | |
720e51b7 | 4278 | char *pcfbuflimit; |
b0bbbd85 RS |
4279 | int pcfnum; |
4280 | f= -1; /* JF we iz paranoid! */ | |
4281 | ||
1faf9603 | 4282 | if (importing && warn_import && !inhibit_warnings |
2aa7ec37 | 4283 | && !instack[indepth].system_header_p && !import_warning) { |
b0bbbd85 RS |
4284 | import_warning = 1; |
4285 | warning ("using `#import' is not recommended"); | |
4286 | fprintf (stderr, "The fact that a certain header file need not be processed more than once\n"); | |
4287 | fprintf (stderr, "should be indicated in the header file, not where it is used.\n"); | |
4288 | fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n"); | |
4289 | fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n"); | |
4290 | fprintf (stderr, " #define _FOO_H_INCLUDED\n"); | |
4291 | fprintf (stderr, " ... <real contents of file> ...\n"); | |
4292 | fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n"); | |
4293 | fprintf (stderr, "Then users can use `#include' any number of times.\n"); | |
4294 | fprintf (stderr, "GNU C automatically avoids processing the file more than once\n"); | |
4295 | fprintf (stderr, "when it is equipped with such a conditional.\n"); | |
4296 | } | |
4297 | ||
4298 | get_filename: | |
4299 | ||
4300 | fbeg = buf; | |
4301 | SKIP_WHITE_SPACE (fbeg); | |
4302 | /* Discard trailing whitespace so we can easily see | |
4303 | if we have parsed all the significant chars we were given. */ | |
4304 | while (limit != fbeg && is_hor_space[limit[-1]]) limit--; | |
4305 | ||
4306 | switch (*fbeg++) { | |
4307 | case '\"': | |
b1af4777 | 4308 | { |
b0bbbd85 | 4309 | FILE_BUF *fp; |
b1af4777 RS |
4310 | /* Copy the operand text, concatenating the strings. */ |
4311 | { | |
4312 | U_CHAR *fin = fbeg; | |
4313 | fbeg = (U_CHAR *) alloca (limit - fbeg + 1); | |
4314 | fend = fbeg; | |
4315 | while (fin != limit) { | |
4316 | while (fin != limit && *fin != '\"') | |
4317 | *fend++ = *fin++; | |
4318 | fin++; | |
4319 | if (fin == limit) | |
4320 | break; | |
4321 | /* If not at the end, there had better be another string. */ | |
4322 | /* Skip just horiz space, and don't go past limit. */ | |
4323 | while (fin != limit && is_hor_space[*fin]) fin++; | |
4324 | if (fin != limit && *fin == '\"') | |
4325 | fin++; | |
4326 | else | |
4327 | goto fail; | |
4328 | } | |
4329 | } | |
50f15104 | 4330 | *fend = 0; |
b0bbbd85 RS |
4331 | |
4332 | /* We have "filename". Figure out directory this source | |
4333 | file is coming from and put it on the front of the list. */ | |
4334 | ||
4335 | /* If -I- was specified, don't search current dir, only spec'd ones. */ | |
4336 | if (ignore_srcdir) break; | |
4337 | ||
4338 | for (fp = &instack[indepth]; fp >= instack; fp--) | |
4339 | { | |
4340 | int n; | |
4341 | char *ep,*nam; | |
4342 | ||
4343 | if ((nam = fp->nominal_fname) != NULL) { | |
4344 | /* Found a named file. Figure out dir of the file, | |
4345 | and put it in front of the search list. */ | |
4346 | dsp[0].next = search_start; | |
4347 | search_start = dsp; | |
4348 | #ifndef VMS | |
4349 | ep = rindex (nam, '/'); | |
40ddf499 RK |
4350 | #ifdef DIR_SEPARATOR |
4351 | if (ep == NULL) ep = rindex (nam, DIR_SEPARATOR); | |
4352 | else { | |
4353 | char *tmp = rindex (nam, DIR_SEPARATOR); | |
4354 | if (tmp != NULL && tmp > ep) ep = tmp; | |
4355 | } | |
4356 | #endif | |
b0bbbd85 RS |
4357 | #else /* VMS */ |
4358 | ep = rindex (nam, ']'); | |
4359 | if (ep == NULL) ep = rindex (nam, '>'); | |
4360 | if (ep == NULL) ep = rindex (nam, ':'); | |
4361 | if (ep != NULL) ep++; | |
4362 | #endif /* VMS */ | |
4363 | if (ep != NULL) { | |
4364 | n = ep - nam; | |
4365 | dsp[0].fname = (char *) alloca (n + 1); | |
4366 | strncpy (dsp[0].fname, nam, n); | |
4367 | dsp[0].fname[n] = '\0'; | |
6489924b RS |
4368 | if (n + INCLUDE_LEN_FUDGE > max_include_len) |
4369 | max_include_len = n + INCLUDE_LEN_FUDGE; | |
b0bbbd85 RS |
4370 | } else { |
4371 | dsp[0].fname = 0; /* Current directory */ | |
4372 | } | |
6e7f952e | 4373 | dsp[0].got_name_map = 0; |
b0bbbd85 RS |
4374 | break; |
4375 | } | |
4376 | } | |
4377 | break; | |
4378 | } | |
b0bbbd85 RS |
4379 | |
4380 | case '<': | |
4381 | fend = fbeg; | |
4382 | while (fend != limit && *fend != '>') fend++; | |
4383 | if (*fend == '>' && fend + 1 == limit) { | |
1faf9603 | 4384 | angle_brackets = 1; |
b0bbbd85 RS |
4385 | /* If -I-, start with the first -I dir after the -I-. */ |
4386 | if (first_bracket_include) | |
4387 | search_start = first_bracket_include; | |
4388 | break; | |
4389 | } | |
4390 | goto fail; | |
4391 | ||
4392 | default: | |
fe701b40 RK |
4393 | #ifdef VMS |
4394 | /* | |
4395 | * Support '#include xyz' like VAX-C to allow for easy use of all the | |
4396 | * decwindow include files. It defaults to '#include <xyz.h>' (so the | |
4397 | * code from case '<' is repeated here) and generates a warning. | |
71efde97 | 4398 | * (Note: macro expansion of `xyz' takes precedence.) |
fe701b40 | 4399 | */ |
71efde97 | 4400 | if (retried && isalpha(*(--fbeg))) { |
fe701b40 RK |
4401 | fend = fbeg; |
4402 | while (fend != limit && (!isspace(*fend))) fend++; | |
4403 | warning ("VAX-C-style include specification found, use '#include <filename.h>' !"); | |
4404 | if (fend == limit) { | |
4405 | angle_brackets = 1; | |
4406 | /* If -I-, start with the first -I dir after the -I-. */ | |
4407 | if (first_bracket_include) | |
4408 | search_start = first_bracket_include; | |
4409 | break; | |
4410 | } | |
4411 | } | |
4412 | #endif | |
4413 | ||
b0bbbd85 RS |
4414 | fail: |
4415 | if (retried) { | |
50f15104 RS |
4416 | error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name); |
4417 | return 0; | |
b0bbbd85 | 4418 | } else { |
53afc256 RK |
4419 | /* Expand buffer and then remove any newline markers. |
4420 | We can't just tell expand_to_temp_buffer to omit the markers, | |
4421 | since it would put extra spaces in include file names. */ | |
9dff2028 RK |
4422 | FILE_BUF trybuf; |
4423 | U_CHAR *src; | |
4424 | trybuf = expand_to_temp_buffer (buf, limit, 1, 0); | |
4425 | src = trybuf.buf; | |
b0bbbd85 | 4426 | buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1); |
53afc256 RK |
4427 | limit = buf; |
4428 | while (src != trybuf.bufp) { | |
4429 | switch ((*limit++ = *src++)) { | |
4430 | case '\n': | |
4431 | limit--; | |
4432 | src++; | |
4433 | break; | |
4434 | ||
4435 | case '\'': | |
4436 | case '\"': | |
4437 | { | |
4438 | U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0, | |
4439 | NULL_PTR, NULL_PTR, NULL_PTR); | |
4440 | while (src != src1) | |
4441 | *limit++ = *src++; | |
4442 | } | |
4443 | break; | |
4444 | } | |
4445 | } | |
4446 | *limit = 0; | |
b0bbbd85 RS |
4447 | free (trybuf.buf); |
4448 | retried++; | |
4449 | goto get_filename; | |
4450 | } | |
4451 | } | |
4452 | ||
4453 | /* For #include_next, skip in the search path | |
4454 | past the dir in which the containing file was found. */ | |
4455 | if (skip_dirs) { | |
4456 | FILE_BUF *fp; | |
4457 | for (fp = &instack[indepth]; fp >= instack; fp--) | |
4458 | if (fp->fname != NULL) { | |
4459 | /* fp->dir is null if the containing file was specified | |
4460 | with an absolute file name. In that case, don't skip anything. */ | |
4461 | if (fp->dir) | |
4462 | search_start = fp->dir->next; | |
4463 | break; | |
4464 | } | |
4465 | } | |
4466 | ||
4467 | flen = fend - fbeg; | |
33b039cc RS |
4468 | |
4469 | if (flen == 0) | |
4470 | { | |
50f15104 | 4471 | error ("empty file name in `#%s'", keyword->name); |
33b039cc RS |
4472 | return 0; |
4473 | } | |
4474 | ||
b0bbbd85 RS |
4475 | /* Allocate this permanently, because it gets stored in the definitions |
4476 | of macros. */ | |
25cbb59e | 4477 | fname = xmalloc (max_include_len + flen + 4); |
b0bbbd85 | 4478 | /* + 2 above for slash and terminating null. */ |
fe701b40 | 4479 | /* + 2 added for '.h' on VMS (to support '#include filename') */ |
b0bbbd85 | 4480 | |
b0bbbd85 RS |
4481 | /* If specified file name is absolute, just open it. */ |
4482 | ||
40ddf499 RK |
4483 | if (*fbeg == '/' |
4484 | #ifdef DIR_SEPARATOR | |
4485 | || *fbeg == DIR_SEPARATOR | |
4486 | #endif | |
4487 | ) { | |
25cbb59e | 4488 | strncpy (fname, (char *) fbeg, flen); |
b0bbbd85 | 4489 | fname[flen] = 0; |
1faf9603 | 4490 | if (redundant_include_p (fname)) |
3ed8294e | 4491 | return 0; |
b0bbbd85 | 4492 | if (importing) |
6e7f952e | 4493 | f = lookup_import (fname, NULL_PTR); |
b0bbbd85 | 4494 | else |
6e7f952e | 4495 | f = open_include_file (fname, NULL_PTR); |
b0bbbd85 RS |
4496 | if (f == -2) |
4497 | return 0; /* Already included this file */ | |
4498 | } else { | |
4499 | /* Search directory path, trying to open the file. | |
4500 | Copy each filename tried into FNAME. */ | |
4501 | ||
4502 | for (searchptr = search_start; searchptr; searchptr = searchptr->next) { | |
4503 | if (searchptr->fname) { | |
4504 | /* The empty string in a search path is ignored. | |
4505 | This makes it possible to turn off entirely | |
4506 | a standard piece of the list. */ | |
4507 | if (searchptr->fname[0] == 0) | |
4508 | continue; | |
4509 | strcpy (fname, searchptr->fname); | |
4510 | strcat (fname, "/"); | |
4511 | fname[strlen (fname) + flen] = 0; | |
4512 | } else { | |
4513 | fname[0] = 0; | |
4514 | } | |
25cbb59e | 4515 | strncat (fname, (char *) fbeg, flen); |
b0bbbd85 RS |
4516 | #ifdef VMS |
4517 | /* Change this 1/2 Unix 1/2 VMS file specification into a | |
4518 | full VMS file specification */ | |
4519 | if (searchptr->fname && (searchptr->fname[0] != 0)) { | |
4520 | /* Fix up the filename */ | |
4521 | hack_vms_include_specification (fname); | |
4522 | } else { | |
4523 | /* This is a normal VMS filespec, so use it unchanged. */ | |
4524 | strncpy (fname, fbeg, flen); | |
4525 | fname[flen] = 0; | |
fe701b40 RK |
4526 | /* if it's '#include filename', add the missing .h */ |
4527 | if (index(fname,'.')==NULL) { | |
4528 | strcat (fname, ".h"); | |
4529 | } | |
b0bbbd85 RS |
4530 | } |
4531 | #endif /* VMS */ | |
52cf95a7 DE |
4532 | /* ??? There are currently 3 separate mechanisms for avoiding processing |
4533 | of redundant include files: #import, #pragma once, and | |
4534 | redundant_include_p. It would be nice if they were unified. */ | |
4535 | if (redundant_include_p (fname)) | |
4536 | return 0; | |
b0bbbd85 | 4537 | if (importing) |
6e7f952e | 4538 | f = lookup_import (fname, searchptr); |
b0bbbd85 | 4539 | else |
6e7f952e | 4540 | f = open_include_file (fname, searchptr); |
b0bbbd85 RS |
4541 | if (f == -2) |
4542 | return 0; /* Already included this file */ | |
6d34466a JW |
4543 | #ifdef EACCES |
4544 | else if (f == -1 && errno == EACCES) | |
4545 | warning ("Header file %s exists, but is not readable", fname); | |
4546 | #endif | |
b0bbbd85 RS |
4547 | if (f >= 0) |
4548 | break; | |
4549 | } | |
4550 | } | |
4551 | ||
4552 | if (f < 0) { | |
4553 | /* A file that was not found. */ | |
4554 | ||
25cbb59e | 4555 | strncpy (fname, (char *) fbeg, flen); |
b0bbbd85 | 4556 | fname[flen] = 0; |
86739c7b DE |
4557 | /* If generating dependencies and -MG was specified, we assume missing |
4558 | files are leaf files, living in the same directory as the source file | |
4559 | or other similar place; these missing files may be generated from | |
4560 | other files and may not exist yet (eg: y.tab.h). */ | |
4561 | if (print_deps_missing_files | |
4562 | && print_deps > (angle_brackets || (system_include_depth > 0))) | |
4563 | { | |
86739c7b DE |
4564 | /* If it was requested as a system header file, |
4565 | then assume it belongs in the first place to look for such. */ | |
4566 | if (angle_brackets) | |
4567 | { | |
4568 | for (searchptr = search_start; searchptr; searchptr = searchptr->next) | |
4569 | { | |
4570 | if (searchptr->fname) | |
4571 | { | |
4572 | char *p; | |
4573 | ||
4574 | if (searchptr->fname[0] == 0) | |
4575 | continue; | |
4576 | p = xmalloc (strlen (searchptr->fname) | |
4577 | + strlen (fname) + 2); | |
4578 | strcpy (p, searchptr->fname); | |
4579 | strcat (p, "/"); | |
4580 | strcat (p, fname); | |
4581 | deps_output (p, ' '); | |
4582 | break; | |
4583 | } | |
4584 | } | |
4585 | } | |
4586 | else | |
4587 | { | |
4588 | /* Otherwise, omit the directory, as if the file existed | |
4589 | in the directory with the source. */ | |
4590 | deps_output (fname, ' '); | |
4591 | } | |
4592 | } | |
a94d16c6 JW |
4593 | /* If -M was specified, and this header file won't be added to the |
4594 | dependency list, then don't count this as an error, because we can | |
4595 | still produce correct output. Otherwise, we can't produce correct | |
4596 | output, because there may be dependencies we need inside the missing | |
4597 | file, and we don't know what directory this missing file exists in. */ | |
86739c7b | 4598 | else if (print_deps |
a94d16c6 JW |
4599 | && (print_deps <= (angle_brackets || (system_include_depth > 0)))) |
4600 | warning ("No include path in which to find %s", fname); | |
4601 | else if (search_start) | |
68a8ca25 RS |
4602 | error_from_errno (fname); |
4603 | else | |
4604 | error ("No include path in which to find %s", fname); | |
b0bbbd85 | 4605 | } else { |
b0bbbd85 RS |
4606 | /* Check to see if this include file is a once-only include file. |
4607 | If so, give up. */ | |
4608 | ||
4609 | struct file_name_list* ptr; | |
4610 | ||
4611 | for (ptr = dont_repeat_files; ptr; ptr = ptr->next) { | |
4612 | if (!strcmp (ptr->fname, fname)) { | |
4613 | close (f); | |
4614 | return 0; /* This file was once'd. */ | |
4615 | } | |
4616 | } | |
4617 | ||
4618 | for (ptr = all_include_files; ptr; ptr = ptr->next) { | |
4619 | if (!strcmp (ptr->fname, fname)) | |
4620 | break; /* This file was included before. */ | |
4621 | } | |
4622 | ||
4623 | if (ptr == 0) { | |
4624 | /* This is the first time for this file. */ | |
4625 | /* Add it to list of files included. */ | |
4626 | ||
4627 | ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
4628 | ptr->control_macro = 0; | |
d2a22862 | 4629 | ptr->c_system_include_path = 0; |
b0bbbd85 RS |
4630 | ptr->next = all_include_files; |
4631 | all_include_files = ptr; | |
4632 | ptr->fname = savestring (fname); | |
6e7f952e | 4633 | ptr->got_name_map = 0; |
b0bbbd85 RS |
4634 | |
4635 | /* For -M, add this file to the dependencies. */ | |
389bb508 RK |
4636 | if (print_deps > (angle_brackets || (system_include_depth > 0))) |
4637 | deps_output (fname, ' '); | |
b0bbbd85 RS |
4638 | } |
4639 | ||
4640 | /* Handle -H option. */ | |
334eb142 RS |
4641 | if (print_include_names) { |
4642 | output_dots (stderr, indepth); | |
b0bbbd85 | 4643 | fprintf (stderr, "%s\n", fname); |
334eb142 | 4644 | } |
b0bbbd85 | 4645 | |
1faf9603 | 4646 | if (angle_brackets) |
b0bbbd85 RS |
4647 | system_include_depth++; |
4648 | ||
4649 | /* Actually process the file. */ | |
4650 | add_import (f, fname); /* Record file on "seen" list for #import. */ | |
4651 | ||
4652 | pcftry = (char *) alloca (strlen (fname) + 30); | |
4653 | pcfbuf = 0; | |
4654 | pcfnum = 0; | |
4655 | ||
b0bbbd85 | 4656 | if (!no_precomp) |
52cf95a7 DE |
4657 | { |
4658 | struct stat stat_f; | |
b0bbbd85 | 4659 | |
52cf95a7 DE |
4660 | fstat (f, &stat_f); |
4661 | ||
4662 | do { | |
4663 | sprintf (pcftry, "%s%d", fname, pcfnum++); | |
4664 | ||
4665 | pcf = open (pcftry, O_RDONLY, 0666); | |
4666 | if (pcf != -1) | |
4667 | { | |
4668 | struct stat s; | |
4669 | ||
4670 | fstat (pcf, &s); | |
4671 | if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino, | |
4672 | sizeof (s.st_ino)) | |
4673 | || stat_f.st_dev != s.st_dev) | |
4674 | { | |
4675 | pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit); | |
4676 | /* Don't need it any more. */ | |
4677 | close (pcf); | |
4678 | } | |
4679 | else | |
4680 | { | |
4681 | /* Don't need it at all. */ | |
4682 | close (pcf); | |
4683 | break; | |
4684 | } | |
4685 | } | |
4686 | } while (pcf != -1 && !pcfbuf); | |
4687 | } | |
b0bbbd85 RS |
4688 | |
4689 | /* Actually process the file */ | |
4690 | if (pcfbuf) { | |
4691 | pcfname = xmalloc (strlen (pcftry) + 1); | |
4692 | strcpy (pcfname, pcftry); | |
25cbb59e RK |
4693 | pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit, |
4694 | (U_CHAR *) fname, op); | |
b0bbbd85 RS |
4695 | } |
4696 | else | |
1faf9603 | 4697 | finclude (f, fname, op, is_system_include (fname), searchptr); |
b0bbbd85 | 4698 | |
1faf9603 | 4699 | if (angle_brackets) |
b0bbbd85 RS |
4700 | system_include_depth--; |
4701 | } | |
4702 | return 0; | |
4703 | } | |
4704 | ||
3ed8294e RS |
4705 | /* Return nonzero if there is no need to include file NAME |
4706 | because it has already been included and it contains a conditional | |
4707 | to make a repeated include do nothing. */ | |
4708 | ||
4709 | static int | |
1faf9603 | 4710 | redundant_include_p (name) |
3ed8294e RS |
4711 | char *name; |
4712 | { | |
4713 | struct file_name_list *l = all_include_files; | |
4714 | for (; l; l = l->next) | |
4715 | if (! strcmp (name, l->fname) | |
4716 | && l->control_macro | |
4717 | && lookup (l->control_macro, -1, -1)) | |
4718 | return 1; | |
4719 | return 0; | |
4720 | } | |
4721 | ||
1faf9603 RS |
4722 | /* Return nonzero if the given FILENAME is an absolute pathname which |
4723 | designates a file within one of the known "system" include file | |
4724 | directories. We assume here that if the given FILENAME looks like | |
4725 | it is the name of a file which resides either directly in a "system" | |
4726 | include file directory, or within any subdirectory thereof, then the | |
4727 | given file must be a "system" include file. This function tells us | |
0031ac57 RS |
4728 | if we should suppress pedantic errors/warnings for the given FILENAME. |
4729 | ||
4730 | The value is 2 if the file is a C-language system header file | |
4731 | for which C++ should (on most systems) assume `extern "C"'. */ | |
1faf9603 RS |
4732 | |
4733 | static int | |
4734 | is_system_include (filename) | |
4735 | register char *filename; | |
4736 | { | |
4737 | struct file_name_list *searchptr; | |
4738 | ||
6489924b | 4739 | for (searchptr = first_system_include; searchptr; |
1faf9603 RS |
4740 | searchptr = searchptr->next) |
4741 | if (searchptr->fname) { | |
4742 | register char *sys_dir = searchptr->fname; | |
4743 | register unsigned length = strlen (sys_dir); | |
4744 | ||
40ddf499 RK |
4745 | if (! strncmp (sys_dir, filename, length) |
4746 | && (filename[length] == '/' | |
4747 | #ifdef DIR_SEPARATOR | |
4748 | || filename[length] == DIR_SEPARATOR | |
4749 | #endif | |
4750 | )) { | |
4751 | if (searchptr->c_system_include_path) | |
4752 | return 2; | |
4753 | else | |
4754 | return 1; | |
4755 | } | |
1faf9603 RS |
4756 | } |
4757 | return 0; | |
4758 | } | |
4759 | \f | |
6e7f952e JW |
4760 | /* The file_name_map structure holds a mapping of file names for a |
4761 | particular directory. This mapping is read from the file named | |
4762 | FILE_NAME_MAP_FILE in that directory. Such a file can be used to | |
4763 | map filenames on a file system with severe filename restrictions, | |
4764 | such as DOS. The format of the file name map file is just a series | |
4765 | of lines with two tokens on each line. The first token is the name | |
4766 | to map, and the second token is the actual name to use. */ | |
4767 | ||
4768 | struct file_name_map | |
4769 | { | |
4770 | struct file_name_map *map_next; | |
4771 | char *map_from; | |
4772 | char *map_to; | |
4773 | }; | |
4774 | ||
4775 | #define FILE_NAME_MAP_FILE "header.gcc" | |
4776 | ||
4777 | /* Read a space delimited string of unlimited length from a stdio | |
4778 | file. */ | |
4779 | ||
4780 | static char * | |
4781 | read_filename_string (ch, f) | |
4782 | int ch; | |
4783 | FILE *f; | |
4784 | { | |
4785 | char *alloc, *set; | |
4786 | int len; | |
4787 | ||
4788 | len = 20; | |
4789 | set = alloc = xmalloc (len + 1); | |
4790 | if (! is_space[ch]) | |
4791 | { | |
4792 | *set++ = ch; | |
4793 | while ((ch = getc (f)) != EOF && ! is_space[ch]) | |
4794 | { | |
4795 | if (set - alloc == len) | |
4796 | { | |
4797 | len *= 2; | |
4798 | alloc = xrealloc (alloc, len + 1); | |
4799 | set = alloc + len / 2; | |
4800 | } | |
4801 | *set++ = ch; | |
4802 | } | |
4803 | } | |
4804 | *set = '\0'; | |
4805 | ungetc (ch, f); | |
4806 | return alloc; | |
4807 | } | |
4808 | ||
4809 | /* Read the file name map file for DIRNAME. */ | |
4810 | ||
4811 | static struct file_name_map * | |
4812 | read_name_map (dirname) | |
4813 | char *dirname; | |
4814 | { | |
4815 | /* This structure holds a linked list of file name maps, one per | |
4816 | directory. */ | |
4817 | struct file_name_map_list | |
4818 | { | |
4819 | struct file_name_map_list *map_list_next; | |
4820 | char *map_list_name; | |
4821 | struct file_name_map *map_list_map; | |
4822 | }; | |
4823 | static struct file_name_map_list *map_list; | |
4824 | register struct file_name_map_list *map_list_ptr; | |
4825 | char *name; | |
4826 | FILE *f; | |
4827 | ||
4828 | for (map_list_ptr = map_list; map_list_ptr; | |
4829 | map_list_ptr = map_list_ptr->map_list_next) | |
4830 | if (! strcmp (map_list_ptr->map_list_name, dirname)) | |
4831 | return map_list_ptr->map_list_map; | |
4832 | ||
4833 | map_list_ptr = ((struct file_name_map_list *) | |
4834 | xmalloc (sizeof (struct file_name_map_list))); | |
4835 | map_list_ptr->map_list_name = savestring (dirname); | |
4836 | map_list_ptr->map_list_map = NULL; | |
4837 | ||
4838 | name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2); | |
4839 | strcpy (name, dirname); | |
4840 | if (*dirname) | |
4841 | strcat (name, "/"); | |
4842 | strcat (name, FILE_NAME_MAP_FILE); | |
4843 | f = fopen (name, "r"); | |
4844 | if (!f) | |
4845 | map_list_ptr->map_list_map = NULL; | |
4846 | else | |
4847 | { | |
4848 | int ch; | |
4849 | int dirlen = strlen (dirname); | |
4850 | ||
4851 | while ((ch = getc (f)) != EOF) | |
4852 | { | |
4853 | char *from, *to; | |
4854 | struct file_name_map *ptr; | |
4855 | ||
4856 | if (is_space[ch]) | |
4857 | continue; | |
4858 | from = read_filename_string (ch, f); | |
4859 | while ((ch = getc (f)) != EOF && is_hor_space[ch]) | |
4860 | ; | |
4861 | to = read_filename_string (ch, f); | |
4862 | ||
4863 | ptr = ((struct file_name_map *) | |
4864 | xmalloc (sizeof (struct file_name_map))); | |
4865 | ptr->map_from = from; | |
4866 | ||
4867 | /* Make the real filename absolute. */ | |
4868 | if (*to == '/') | |
4869 | ptr->map_to = to; | |
4870 | else | |
4871 | { | |
4872 | ptr->map_to = xmalloc (dirlen + strlen (to) + 2); | |
4873 | strcpy (ptr->map_to, dirname); | |
4874 | ptr->map_to[dirlen] = '/'; | |
4875 | strcpy (ptr->map_to + dirlen + 1, to); | |
4876 | free (to); | |
4877 | } | |
4878 | ||
4879 | ptr->map_next = map_list_ptr->map_list_map; | |
4880 | map_list_ptr->map_list_map = ptr; | |
4881 | ||
4882 | while ((ch = getc (f)) != '\n') | |
4883 | if (ch == EOF) | |
4884 | break; | |
4885 | } | |
4886 | fclose (f); | |
4887 | } | |
4888 | ||
4889 | map_list_ptr->map_list_next = map_list; | |
4890 | map_list = map_list_ptr; | |
4891 | ||
4892 | return map_list_ptr->map_list_map; | |
4893 | } | |
4894 | ||
4895 | /* Try to open include file FILENAME. SEARCHPTR is the directory | |
4896 | being tried from the include file search path. This function maps | |
4897 | filenames on file systems based on information read by | |
4898 | read_name_map. */ | |
4899 | ||
4900 | static int | |
4901 | open_include_file (filename, searchptr) | |
4902 | char *filename; | |
4903 | struct file_name_list *searchptr; | |
4904 | { | |
4905 | register struct file_name_map *map; | |
4906 | register char *from; | |
4907 | char *p, *dir; | |
4908 | ||
4909 | if (searchptr && ! searchptr->got_name_map) | |
4910 | { | |
4911 | searchptr->name_map = read_name_map (searchptr->fname | |
4912 | ? searchptr->fname : "."); | |
4913 | searchptr->got_name_map = 1; | |
4914 | } | |
4915 | ||
4916 | /* First check the mapping for the directory we are using. */ | |
4917 | if (searchptr && searchptr->name_map) | |
4918 | { | |
4919 | from = filename; | |
4920 | if (searchptr->fname) | |
4921 | from += strlen (searchptr->fname) + 1; | |
4922 | for (map = searchptr->name_map; map; map = map->map_next) | |
4923 | { | |
4924 | if (! strcmp (map->map_from, from)) | |
4925 | { | |
4926 | /* Found a match. */ | |
4927 | return open (map->map_to, O_RDONLY, 0666); | |
4928 | } | |
4929 | } | |
4930 | } | |
4931 | ||
4932 | /* Try to find a mapping file for the particular directory we are | |
4933 | looking in. Thus #include <sys/types.h> will look up sys/types.h | |
4934 | in /usr/include/header.gcc and look up types.h in | |
4935 | /usr/include/sys/header.gcc. */ | |
4936 | p = rindex (filename, '/'); | |
40ddf499 RK |
4937 | #ifdef DIR_SEPARATOR |
4938 | if (! p) p = rindex (filename, DIR_SEPARATOR); | |
4939 | else { | |
4940 | char *tmp = rindex (filename, DIR_SEPARATOR); | |
4941 | if (tmp != NULL && tmp > p) p = tmp; | |
4942 | } | |
4943 | #endif | |
6e7f952e JW |
4944 | if (! p) |
4945 | p = filename; | |
4946 | if (searchptr | |
4947 | && searchptr->fname | |
4948 | && strlen (searchptr->fname) == p - filename | |
4949 | && ! strncmp (searchptr->fname, filename, p - filename)) | |
4950 | { | |
4951 | /* FILENAME is in SEARCHPTR, which we've already checked. */ | |
4952 | return open (filename, O_RDONLY, 0666); | |
4953 | } | |
4954 | ||
4955 | if (p == filename) | |
4956 | { | |
4957 | dir = "."; | |
4958 | from = filename; | |
4959 | } | |
4960 | else | |
4961 | { | |
4962 | dir = (char *) alloca (p - filename + 1); | |
4963 | bcopy (filename, dir, p - filename); | |
4964 | dir[p - filename] = '\0'; | |
4965 | from = p + 1; | |
4966 | } | |
4967 | for (map = read_name_map (dir); map; map = map->map_next) | |
4968 | if (! strcmp (map->map_from, from)) | |
4969 | return open (map->map_to, O_RDONLY, 0666); | |
4970 | ||
4971 | return open (filename, O_RDONLY, 0666); | |
4972 | } | |
4973 | \f | |
b0bbbd85 RS |
4974 | /* Process the contents of include file FNAME, already open on descriptor F, |
4975 | with output to OP. | |
b126e7ce RS |
4976 | SYSTEM_HEADER_P is 1 if this file resides in any one of the known |
4977 | "system" include directories (as decided by the `is_system_include' | |
4978 | function above). | |
b0bbbd85 RS |
4979 | DIRPTR is the link in the dir path through which this file was found, |
4980 | or 0 if the file name was absolute. */ | |
4981 | ||
4982 | static void | |
4983 | finclude (f, fname, op, system_header_p, dirptr) | |
4984 | int f; | |
4985 | char *fname; | |
4986 | FILE_BUF *op; | |
4987 | int system_header_p; | |
4988 | struct file_name_list *dirptr; | |
4989 | { | |
4990 | int st_mode; | |
4991 | long st_size; | |
4992 | long i; | |
4993 | FILE_BUF *fp; /* For input stack frame */ | |
4994 | int missing_newline = 0; | |
4995 | ||
4996 | CHECK_DEPTH (return;); | |
4997 | ||
4998 | if (file_size_and_mode (f, &st_mode, &st_size) < 0) | |
d75e62a9 RS |
4999 | { |
5000 | perror_with_name (fname); | |
5001 | close (f); | |
5002 | return; | |
5003 | } | |
b0bbbd85 RS |
5004 | |
5005 | fp = &instack[indepth + 1]; | |
4c9a05bc | 5006 | bzero ((char *) fp, sizeof (FILE_BUF)); |
b0bbbd85 RS |
5007 | fp->nominal_fname = fp->fname = fname; |
5008 | fp->length = 0; | |
5009 | fp->lineno = 1; | |
5010 | fp->if_stack = if_stack; | |
5011 | fp->system_header_p = system_header_p; | |
5012 | fp->dir = dirptr; | |
5013 | ||
5014 | if (S_ISREG (st_mode)) { | |
b576c6b6 | 5015 | fp->buf = (U_CHAR *) xmalloc (st_size + 2); |
b0bbbd85 RS |
5016 | fp->bufp = fp->buf; |
5017 | ||
5018 | /* Read the file contents, knowing that st_size is an upper bound | |
5019 | on the number of bytes we can read. */ | |
25cbb59e | 5020 | fp->length = safe_read (f, (char *) fp->buf, st_size); |
53e52f00 | 5021 | if (fp->length < 0) goto nope; |
b0bbbd85 | 5022 | } |
2e4f4529 JW |
5023 | else if (S_ISDIR (st_mode)) { |
5024 | error ("directory `%s' specified in #include", fname); | |
5025 | close (f); | |
5026 | return; | |
5027 | } else { | |
b0bbbd85 RS |
5028 | /* Cannot count its file size before reading. |
5029 | First read the entire file into heap and | |
5030 | copy them into buffer on stack. */ | |
5031 | ||
b0bbbd85 RS |
5032 | int bsize = 2000; |
5033 | ||
5034 | st_size = 0; | |
53e52f00 | 5035 | fp->buf = (U_CHAR *) xmalloc (bsize + 2); |
b0bbbd85 RS |
5036 | |
5037 | for (;;) { | |
25cbb59e | 5038 | i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size); |
b0bbbd85 RS |
5039 | if (i < 0) |
5040 | goto nope; /* error! */ | |
b0bbbd85 | 5041 | st_size += i; |
53e52f00 RK |
5042 | if (st_size != bsize) |
5043 | break; /* End of file */ | |
5044 | bsize *= 2; | |
5045 | fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2); | |
b0bbbd85 | 5046 | } |
b0bbbd85 | 5047 | fp->bufp = fp->buf; |
b0bbbd85 | 5048 | fp->length = st_size; |
b0bbbd85 RS |
5049 | } |
5050 | ||
625bbc60 RS |
5051 | if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n') |
5052 | /* Backslash-newline at end is not good enough. */ | |
5053 | || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) { | |
5054 | fp->buf[fp->length++] = '\n'; | |
5055 | missing_newline = 1; | |
5056 | } | |
5057 | fp->buf[fp->length] = '\0'; | |
5058 | ||
b0bbbd85 RS |
5059 | /* Close descriptor now, so nesting does not use lots of descriptors. */ |
5060 | close (f); | |
5061 | ||
cd1ceb3c JW |
5062 | /* Must do this before calling trigraph_pcp, so that the correct file name |
5063 | will be printed in warning messages. */ | |
5064 | ||
5065 | indepth++; | |
5066 | input_file_stack_tick++; | |
5067 | ||
b0bbbd85 RS |
5068 | if (!no_trigraphs) |
5069 | trigraph_pcp (fp); | |
5070 | ||
adcfa681 | 5071 | output_line_directive (fp, op, 0, enter_file); |
b0bbbd85 RS |
5072 | rescan (op, 0); |
5073 | ||
5105ecec RS |
5074 | if (missing_newline) |
5075 | fp->lineno--; | |
5076 | ||
b0bbbd85 RS |
5077 | if (pedantic && missing_newline) |
5078 | pedwarn ("file does not end in newline"); | |
5079 | ||
5080 | indepth--; | |
5081 | input_file_stack_tick++; | |
adcfa681 | 5082 | output_line_directive (&instack[indepth], op, 0, leave_file); |
b576c6b6 | 5083 | free (fp->buf); |
b0bbbd85 RS |
5084 | return; |
5085 | ||
5086 | nope: | |
5087 | ||
5088 | perror_with_name (fname); | |
5089 | close (f); | |
b576c6b6 | 5090 | free (fp->buf); |
b0bbbd85 RS |
5091 | } |
5092 | ||
5093 | /* Record that inclusion of the file named FILE | |
5094 | should be controlled by the macro named MACRO_NAME. | |
5095 | This means that trying to include the file again | |
5096 | will do something if that macro is defined. */ | |
5097 | ||
5098 | static void | |
5099 | record_control_macro (file, macro_name) | |
5100 | char *file; | |
5101 | U_CHAR *macro_name; | |
5102 | { | |
5103 | struct file_name_list *new; | |
5104 | ||
5105 | for (new = all_include_files; new; new = new->next) { | |
5106 | if (!strcmp (new->fname, file)) { | |
5107 | new->control_macro = macro_name; | |
5108 | return; | |
5109 | } | |
5110 | } | |
5111 | ||
5112 | /* If the file is not in all_include_files, something's wrong. */ | |
5113 | abort (); | |
5114 | } | |
5115 | \f | |
5116 | /* Maintain and search list of included files, for #import. */ | |
5117 | ||
5118 | #define IMPORT_HASH_SIZE 31 | |
5119 | ||
5120 | struct import_file { | |
5121 | char *name; | |
5122 | ino_t inode; | |
5123 | dev_t dev; | |
5124 | struct import_file *next; | |
5125 | }; | |
5126 | ||
5127 | /* Hash table of files already included with #include or #import. */ | |
5128 | ||
5129 | static struct import_file *import_hash_table[IMPORT_HASH_SIZE]; | |
5130 | ||
5131 | /* Hash a file name for import_hash_table. */ | |
5132 | ||
5133 | static int | |
5134 | import_hash (f) | |
5135 | char *f; | |
5136 | { | |
5137 | int val = 0; | |
5138 | ||
5139 | while (*f) val += *f++; | |
5140 | return (val%IMPORT_HASH_SIZE); | |
5141 | } | |
5142 | ||
5143 | /* Search for file FILENAME in import_hash_table. | |
5144 | Return -2 if found, either a matching name or a matching inode. | |
5145 | Otherwise, open the file and return a file descriptor if successful | |
5146 | or -1 if unsuccessful. */ | |
5147 | ||
5148 | static int | |
6e7f952e | 5149 | lookup_import (filename, searchptr) |
b0bbbd85 | 5150 | char *filename; |
6e7f952e | 5151 | struct file_name_list *searchptr; |
b0bbbd85 RS |
5152 | { |
5153 | struct import_file *i; | |
5154 | int h; | |
5155 | int hashval; | |
5156 | struct stat sb; | |
5157 | int fd; | |
5158 | ||
5159 | hashval = import_hash (filename); | |
5160 | ||
5161 | /* Attempt to find file in list of already included files */ | |
5162 | i = import_hash_table[hashval]; | |
5163 | ||
5164 | while (i) { | |
5165 | if (!strcmp (filename, i->name)) | |
5166 | return -2; /* return found */ | |
5167 | i = i->next; | |
5168 | } | |
5169 | /* Open it and try a match on inode/dev */ | |
6e7f952e | 5170 | fd = open_include_file (filename, searchptr); |
b0bbbd85 RS |
5171 | if (fd < 0) |
5172 | return fd; | |
5173 | fstat (fd, &sb); | |
5174 | for (h = 0; h < IMPORT_HASH_SIZE; h++) { | |
5175 | i = import_hash_table[h]; | |
5176 | while (i) { | |
5177 | /* Compare the inode and the device. | |
5178 | Supposedly on some systems the inode is not a scalar. */ | |
4c9a05bc | 5179 | if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino)) |
b0bbbd85 RS |
5180 | && i->dev == sb.st_dev) { |
5181 | close (fd); | |
5182 | return -2; /* return found */ | |
5183 | } | |
5184 | i = i->next; | |
5185 | } | |
5186 | } | |
5187 | return fd; /* Not found, return open file */ | |
5188 | } | |
5189 | ||
5190 | /* Add the file FNAME, open on descriptor FD, to import_hash_table. */ | |
5191 | ||
5192 | static void | |
5193 | add_import (fd, fname) | |
5194 | int fd; | |
5195 | char *fname; | |
5196 | { | |
5197 | struct import_file *i; | |
5198 | int hashval; | |
5199 | struct stat sb; | |
5200 | ||
5201 | hashval = import_hash (fname); | |
5202 | fstat (fd, &sb); | |
5203 | i = (struct import_file *)xmalloc (sizeof (struct import_file)); | |
25cbb59e | 5204 | i->name = xmalloc (strlen (fname)+1); |
b0bbbd85 | 5205 | strcpy (i->name, fname); |
4c9a05bc | 5206 | bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino)); |
b0bbbd85 RS |
5207 | i->dev = sb.st_dev; |
5208 | i->next = import_hash_table[hashval]; | |
5209 | import_hash_table[hashval] = i; | |
5210 | } | |
5211 | \f | |
5212 | /* Load the specified precompiled header into core, and verify its | |
5213 | preconditions. PCF indicates the file descriptor to read, which must | |
5214 | be a regular file. FNAME indicates the file name of the original | |
5215 | header. *LIMIT will be set to an address one past the end of the file. | |
5216 | If the preconditions of the file are not satisfied, the buffer is | |
5217 | freed and we return 0. If the preconditions are satisfied, return | |
5218 | the address of the buffer following the preconditions. The buffer, in | |
5219 | this case, should never be freed because various pieces of it will | |
5220 | be referred to until all precompiled strings are output at the end of | |
5221 | the run. | |
5222 | */ | |
5223 | static char * | |
5224 | check_precompiled (pcf, fname, limit) | |
5225 | int pcf; | |
5226 | char *fname; | |
5227 | char **limit; | |
5228 | { | |
5229 | int st_mode; | |
5230 | long st_size; | |
5231 | int length = 0; | |
5232 | char *buf; | |
b0bbbd85 RS |
5233 | char *cp; |
5234 | ||
5235 | if (pcp_outfile) | |
5236 | return 0; | |
5237 | ||
5238 | if (file_size_and_mode (pcf, &st_mode, &st_size) < 0) | |
5239 | return 0; | |
5240 | ||
5241 | if (S_ISREG (st_mode)) | |
5242 | { | |
5243 | buf = xmalloc (st_size + 2); | |
53e52f00 RK |
5244 | length = safe_read (pcf, buf, st_size); |
5245 | if (length < 0) | |
5246 | goto nope; | |
b0bbbd85 RS |
5247 | } |
5248 | else | |
5249 | abort (); | |
5250 | ||
5251 | if (length > 0 && buf[length-1] != '\n') | |
5252 | buf[length++] = '\n'; | |
5253 | buf[length] = '\0'; | |
5254 | ||
5255 | *limit = buf + length; | |
5256 | ||
5257 | /* File is in core. Check the preconditions. */ | |
5258 | if (!check_preconditions (buf)) | |
5259 | goto nope; | |
5260 | for (cp = buf; *cp; cp++) | |
5261 | ; | |
5262 | #ifdef DEBUG_PCP | |
5263 | fprintf (stderr, "Using preinclude %s\n", fname); | |
5264 | #endif | |
5265 | return cp + 1; | |
5266 | ||
5267 | nope: | |
5268 | #ifdef DEBUG_PCP | |
5269 | fprintf (stderr, "Cannot use preinclude %s\n", fname); | |
5270 | #endif | |
5271 | free (buf); | |
5272 | return 0; | |
5273 | } | |
5274 | ||
5275 | /* PREC (null terminated) points to the preconditions of a | |
5276 | precompiled header. These are a series of #define and #undef | |
5277 | lines which must match the current contents of the hash | |
5278 | table. */ | |
5279 | static int | |
5280 | check_preconditions (prec) | |
5281 | char *prec; | |
5282 | { | |
5283 | MACRODEF mdef; | |
5284 | char *lineend; | |
5285 | ||
5286 | while (*prec) { | |
25cbb59e | 5287 | lineend = index (prec, '\n'); |
b0bbbd85 RS |
5288 | |
5289 | if (*prec++ != '#') { | |
5290 | error ("Bad format encountered while reading precompiled file"); | |
5291 | return 0; | |
5292 | } | |
5293 | if (!strncmp (prec, "define", 6)) { | |
5294 | HASHNODE *hp; | |
5295 | ||
5296 | prec += 6; | |
25cbb59e | 5297 | mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR); |
8d9bfdc5 | 5298 | |
b0bbbd85 | 5299 | if (mdef.defn == 0) |
ad0c9fa1 | 5300 | abort (); |
b0bbbd85 RS |
5301 | |
5302 | if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL | |
5303 | || (hp->type != T_MACRO && hp->type != T_CONST) | |
5304 | || (hp->type == T_MACRO | |
5305 | && !compare_defs (mdef.defn, hp->value.defn) | |
5306 | && (mdef.defn->length != 2 | |
5307 | || mdef.defn->expansion[0] != '\n' | |
5308 | || mdef.defn->expansion[1] != ' '))) | |
5309 | return 0; | |
5310 | } else if (!strncmp (prec, "undef", 5)) { | |
5311 | char *name; | |
5312 | int len; | |
5313 | ||
5314 | prec += 5; | |
d0691cfb | 5315 | while (is_hor_space[(U_CHAR) *prec]) |
b0bbbd85 RS |
5316 | prec++; |
5317 | name = prec; | |
d0691cfb | 5318 | while (is_idchar[(U_CHAR) *prec]) |
b0bbbd85 RS |
5319 | prec++; |
5320 | len = prec - name; | |
5321 | ||
25cbb59e | 5322 | if (lookup ((U_CHAR *) name, len, -1)) |
b0bbbd85 RS |
5323 | return 0; |
5324 | } else { | |
5325 | error ("Bad format encountered while reading precompiled file"); | |
5326 | return 0; | |
5327 | } | |
5328 | prec = lineend + 1; | |
5329 | } | |
5330 | /* They all passed successfully */ | |
5331 | return 1; | |
5332 | } | |
5333 | ||
5334 | /* Process the main body of a precompiled file. BUF points to the | |
5335 | string section of the file, following the preconditions. LIMIT is one | |
5336 | character past the end. NAME is the name of the file being read | |
5337 | in. OP is the main output buffer */ | |
5338 | static void | |
5339 | pcfinclude (buf, limit, name, op) | |
5340 | U_CHAR *buf, *limit, *name; | |
5341 | FILE_BUF *op; | |
5342 | { | |
5343 | FILE_BUF tmpbuf; | |
5344 | int nstrings; | |
5345 | U_CHAR *cp = buf; | |
5346 | ||
5347 | /* First in the file comes 4 bytes indicating the number of strings, */ | |
5348 | /* in network byte order. (MSB first). */ | |
5349 | nstrings = *cp++; | |
5350 | nstrings = (nstrings << 8) | *cp++; | |
5351 | nstrings = (nstrings << 8) | *cp++; | |
5352 | nstrings = (nstrings << 8) | *cp++; | |
5353 | ||
5354 | /* Looping over each string... */ | |
5355 | while (nstrings--) { | |
5356 | U_CHAR *string_start; | |
5357 | U_CHAR *endofthiskey; | |
5358 | STRINGDEF *str; | |
5359 | int nkeys; | |
5360 | ||
5361 | /* Each string starts with a STRINGDEF structure (str), followed */ | |
5362 | /* by the text of the string (string_start) */ | |
5363 | ||
5364 | /* First skip to a longword boundary */ | |
6842690e | 5365 | /* ??? Why a 4-byte boundary? On all machines? */ |
845e4228 RS |
5366 | /* NOTE: This works correctly even if HOST_WIDE_INT |
5367 | is narrower than a pointer. | |
5368 | Do not try risky measures here to get another type to use! | |
39d05dae | 5369 | Do not include stddef.h--it will fail! */ |
845e4228 RS |
5370 | if ((HOST_WIDE_INT) cp & 3) |
5371 | cp += 4 - ((HOST_WIDE_INT) cp & 3); | |
b0bbbd85 RS |
5372 | |
5373 | /* Now get the string. */ | |
25cbb59e | 5374 | str = (STRINGDEF *) (GENERIC_PTR) cp; |
b0bbbd85 RS |
5375 | string_start = cp += sizeof (STRINGDEF); |
5376 | ||
5377 | for (; *cp; cp++) /* skip the string */ | |
5378 | ; | |
5379 | ||
5380 | /* We need to macro expand the string here to ensure that the | |
5381 | proper definition environment is in place. If it were only | |
5382 | expanded when we find out it is needed, macros necessary for | |
5383 | its proper expansion might have had their definitions changed. */ | |
5384 | tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0); | |
5385 | /* Lineno is already set in the precompiled file */ | |
5386 | str->contents = tmpbuf.buf; | |
5387 | str->len = tmpbuf.length; | |
5388 | str->writeflag = 0; | |
5389 | str->filename = name; | |
5390 | str->output_mark = outbuf.bufp - outbuf.buf; | |
5391 | ||
5392 | str->chain = 0; | |
5393 | *stringlist_tailp = str; | |
5394 | stringlist_tailp = &str->chain; | |
5395 | ||
5396 | /* Next comes a fourbyte number indicating the number of keys */ | |
5397 | /* for this string. */ | |
5398 | nkeys = *cp++; | |
5399 | nkeys = (nkeys << 8) | *cp++; | |
5400 | nkeys = (nkeys << 8) | *cp++; | |
5401 | nkeys = (nkeys << 8) | *cp++; | |
5402 | ||
5403 | /* If this number is -1, then the string is mandatory. */ | |
5404 | if (nkeys == -1) | |
5405 | str->writeflag = 1; | |
5406 | else | |
2aa7ec37 | 5407 | /* Otherwise, for each key, */ |
b0bbbd85 | 5408 | for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) { |
25cbb59e | 5409 | KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp; |
b0bbbd85 RS |
5410 | HASHNODE *hp; |
5411 | ||
5412 | /* It starts with a KEYDEF structure */ | |
5413 | cp += sizeof (KEYDEF); | |
5414 | ||
5415 | /* Find the end of the key. At the end of this for loop we | |
5416 | advance CP to the start of the next key using this variable. */ | |
25cbb59e | 5417 | endofthiskey = cp + strlen ((char *) cp); |
b0bbbd85 RS |
5418 | kp->str = str; |
5419 | ||
5420 | /* Expand the key, and enter it into the hash table. */ | |
5421 | tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0); | |
5422 | tmpbuf.bufp = tmpbuf.buf; | |
5423 | ||
5424 | while (is_hor_space[*tmpbuf.bufp]) | |
5425 | tmpbuf.bufp++; | |
5426 | if (!is_idstart[*tmpbuf.bufp] | |
5427 | || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) { | |
5428 | str->writeflag = 1; | |
5429 | continue; | |
5430 | } | |
5431 | ||
5432 | hp = lookup (tmpbuf.bufp, -1, -1); | |
5433 | if (hp == NULL) { | |
5434 | kp->chain = 0; | |
91dbf5e7 | 5435 | install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1); |
b0bbbd85 RS |
5436 | } |
5437 | else if (hp->type == T_PCSTRING) { | |
5438 | kp->chain = hp->value.keydef; | |
5439 | hp->value.keydef = kp; | |
5440 | } | |
5441 | else | |
5442 | str->writeflag = 1; | |
5443 | } | |
5444 | } | |
adcfa681 | 5445 | /* This output_line_directive serves to switch us back to the current |
b0bbbd85 | 5446 | input file in case some of these strings get output (which will |
adcfa681 RK |
5447 | result in line directives for the header file being output). */ |
5448 | output_line_directive (&instack[indepth], op, 0, enter_file); | |
b0bbbd85 RS |
5449 | } |
5450 | ||
5451 | /* Called from rescan when it hits a key for strings. Mark them all */ | |
5452 | /* used and clean up. */ | |
5453 | static void | |
5454 | pcstring_used (hp) | |
5455 | HASHNODE *hp; | |
5456 | { | |
7633af95 | 5457 | KEYDEF *kp; |
b0bbbd85 RS |
5458 | |
5459 | for (kp = hp->value.keydef; kp; kp = kp->chain) | |
5460 | kp->str->writeflag = 1; | |
5461 | delete_macro (hp); | |
5462 | } | |
5463 | ||
5464 | /* Write the output, interspersing precompiled strings in their */ | |
5465 | /* appropriate places. */ | |
5466 | static void | |
5467 | write_output () | |
5468 | { | |
5469 | STRINGDEF *next_string; | |
5470 | U_CHAR *cur_buf_loc; | |
adcfa681 RK |
5471 | int line_directive_len = 80; |
5472 | char *line_directive = xmalloc (line_directive_len); | |
b0bbbd85 RS |
5473 | int len; |
5474 | ||
5475 | /* In each run through the loop, either cur_buf_loc == */ | |
5476 | /* next_string_loc, in which case we print a series of strings, or */ | |
5477 | /* it is less than next_string_loc, in which case we write some of */ | |
5478 | /* the buffer. */ | |
5479 | cur_buf_loc = outbuf.buf; | |
5480 | next_string = stringlist; | |
5481 | ||
5482 | while (cur_buf_loc < outbuf.bufp || next_string) { | |
5483 | if (next_string | |
5484 | && cur_buf_loc - outbuf.buf == next_string->output_mark) { | |
5485 | if (next_string->writeflag) { | |
25cbb59e | 5486 | len = 4 * strlen ((char *) next_string->filename) + 32; |
adcfa681 RK |
5487 | while (len > line_directive_len) |
5488 | line_directive = xrealloc (line_directive, | |
5489 | line_directive_len *= 2); | |
5490 | sprintf (line_directive, "\n# %d ", next_string->lineno); | |
5491 | strcpy (quote_string (line_directive + strlen (line_directive), | |
25cbb59e | 5492 | (char *) next_string->filename), |
f7531123 | 5493 | "\n"); |
adcfa681 | 5494 | safe_write (fileno (stdout), line_directive, strlen (line_directive)); |
25cbb59e RK |
5495 | safe_write (fileno (stdout), |
5496 | (char *) next_string->contents, next_string->len); | |
b0bbbd85 RS |
5497 | } |
5498 | next_string = next_string->chain; | |
5499 | } | |
5500 | else { | |
5501 | len = (next_string | |
5502 | ? (next_string->output_mark | |
5503 | - (cur_buf_loc - outbuf.buf)) | |
5504 | : outbuf.bufp - cur_buf_loc); | |
5505 | ||
25cbb59e | 5506 | safe_write (fileno (stdout), (char *) cur_buf_loc, len); |
b0bbbd85 RS |
5507 | cur_buf_loc += len; |
5508 | } | |
5509 | } | |
adcfa681 | 5510 | free (line_directive); |
b0bbbd85 RS |
5511 | } |
5512 | ||
5513 | /* Pass a directive through to the output file. | |
2aa7ec37 | 5514 | BUF points to the contents of the directive, as a contiguous string. |
b0bbbd85 RS |
5515 | LIMIT points to the first character past the end of the directive. |
5516 | KEYWORD is the keyword-table entry for the directive. */ | |
5517 | ||
5518 | static void | |
5519 | pass_thru_directive (buf, limit, op, keyword) | |
5520 | U_CHAR *buf, *limit; | |
5521 | FILE_BUF *op; | |
5522 | struct directive *keyword; | |
5523 | { | |
5524 | register unsigned keyword_length = keyword->length; | |
5525 | ||
5526 | check_expand (op, 1 + keyword_length + (limit - buf)); | |
5527 | *op->bufp++ = '#'; | |
4c9a05bc | 5528 | bcopy (keyword->name, (char *) op->bufp, keyword_length); |
b0bbbd85 RS |
5529 | op->bufp += keyword_length; |
5530 | if (limit != buf && buf[0] != ' ') | |
5531 | *op->bufp++ = ' '; | |
4c9a05bc | 5532 | bcopy ((char *) buf, (char *) op->bufp, limit - buf); |
b0bbbd85 | 5533 | op->bufp += (limit - buf); |
ae34b95d | 5534 | #if 0 |
b0bbbd85 | 5535 | *op->bufp++ = '\n'; |
ae34b95d RS |
5536 | /* Count the line we have just made in the output, |
5537 | to get in sync properly. */ | |
5538 | op->lineno++; | |
5539 | #endif | |
b0bbbd85 RS |
5540 | } |
5541 | \f | |
5542 | /* The arglist structure is built by do_define to tell | |
5543 | collect_definition where the argument names begin. That | |
5544 | is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist | |
5545 | would contain pointers to the strings x, y, and z. | |
5546 | Collect_definition would then build a DEFINITION node, | |
5547 | with reflist nodes pointing to the places x, y, and z had | |
5548 | appeared. So the arglist is just convenience data passed | |
5549 | between these two routines. It is not kept around after | |
5550 | the current #define has been processed and entered into the | |
5551 | hash table. */ | |
5552 | ||
5553 | struct arglist { | |
5554 | struct arglist *next; | |
5555 | U_CHAR *name; | |
5556 | int length; | |
5557 | int argno; | |
5ff1a832 | 5558 | char rest_args; |
b0bbbd85 RS |
5559 | }; |
5560 | ||
5561 | /* Create a DEFINITION node from a #define directive. Arguments are | |
5562 | as for do_define. */ | |
5563 | static MACRODEF | |
5564 | create_definition (buf, limit, op) | |
5565 | U_CHAR *buf, *limit; | |
5566 | FILE_BUF *op; | |
5567 | { | |
5568 | U_CHAR *bp; /* temp ptr into input buffer */ | |
5569 | U_CHAR *symname; /* remember where symbol name starts */ | |
5570 | int sym_length; /* and how long it is */ | |
5571 | int line = instack[indepth].lineno; | |
5572 | char *file = instack[indepth].nominal_fname; | |
5ff1a832 | 5573 | int rest_args = 0; |
b0bbbd85 RS |
5574 | |
5575 | DEFINITION *defn; | |
5576 | int arglengths = 0; /* Accumulate lengths of arg names | |
5577 | plus number of args. */ | |
5578 | MACRODEF mdef; | |
5579 | ||
5580 | bp = buf; | |
5581 | ||
5582 | while (is_hor_space[*bp]) | |
5583 | bp++; | |
5584 | ||
5585 | symname = bp; /* remember where it starts */ | |
5586 | sym_length = check_macro_name (bp, "macro"); | |
5587 | bp += sym_length; | |
5588 | ||
5589 | /* Lossage will occur if identifiers or control keywords are broken | |
5590 | across lines using backslash. This is not the right place to take | |
5591 | care of that. */ | |
5592 | ||
5593 | if (*bp == '(') { | |
5594 | struct arglist *arg_ptrs = NULL; | |
5595 | int argno = 0; | |
5596 | ||
5597 | bp++; /* skip '(' */ | |
5598 | SKIP_WHITE_SPACE (bp); | |
5599 | ||
5600 | /* Loop over macro argument names. */ | |
5601 | while (*bp != ')') { | |
5602 | struct arglist *temp; | |
5603 | ||
5604 | temp = (struct arglist *) alloca (sizeof (struct arglist)); | |
5605 | temp->name = bp; | |
5606 | temp->next = arg_ptrs; | |
5607 | temp->argno = argno++; | |
5ff1a832 | 5608 | temp->rest_args = 0; |
b0bbbd85 RS |
5609 | arg_ptrs = temp; |
5610 | ||
5ff1a832 RS |
5611 | if (rest_args) |
5612 | pedwarn ("another parameter follows `%s'", | |
5613 | rest_extension); | |
b0bbbd85 | 5614 | |
5ff1a832 RS |
5615 | if (!is_idstart[*bp]) |
5616 | pedwarn ("invalid character in macro parameter name"); | |
5617 | ||
b0bbbd85 RS |
5618 | /* Find the end of the arg name. */ |
5619 | while (is_idchar[*bp]) { | |
5620 | bp++; | |
5ff1a832 RS |
5621 | /* do we have a "special" rest-args extension here? */ |
5622 | if (limit - bp > REST_EXTENSION_LENGTH && | |
25cbb59e | 5623 | bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) { |
5ff1a832 RS |
5624 | rest_args = 1; |
5625 | temp->rest_args = 1; | |
5626 | break; | |
5627 | } | |
b0bbbd85 RS |
5628 | } |
5629 | temp->length = bp - temp->name; | |
5ff1a832 RS |
5630 | if (rest_args == 1) |
5631 | bp += REST_EXTENSION_LENGTH; | |
b0bbbd85 RS |
5632 | arglengths += temp->length + 2; |
5633 | SKIP_WHITE_SPACE (bp); | |
5634 | if (temp->length == 0 || (*bp != ',' && *bp != ')')) { | |
5635 | error ("badly punctuated parameter list in `#define'"); | |
5636 | goto nope; | |
5637 | } | |
5638 | if (*bp == ',') { | |
5639 | bp++; | |
5640 | SKIP_WHITE_SPACE (bp); | |
94d681a0 JW |
5641 | /* A comma at this point can only be followed by an identifier. */ |
5642 | if (!is_idstart[*bp]) { | |
5643 | error ("badly punctuated parameter list in `#define'"); | |
5644 | goto nope; | |
5645 | } | |
b0bbbd85 RS |
5646 | } |
5647 | if (bp >= limit) { | |
5648 | error ("unterminated parameter list in `#define'"); | |
5649 | goto nope; | |
5650 | } | |
5651 | { | |
5652 | struct arglist *otemp; | |
5653 | ||
5654 | for (otemp = temp->next; otemp != NULL; otemp = otemp->next) | |
5655 | if (temp->length == otemp->length && | |
25cbb59e RK |
5656 | bcmp (temp->name, otemp->name, temp->length) == 0) { |
5657 | error ("duplicate argument name `%.*s' in `#define'", | |
5658 | temp->length, temp->name); | |
b0bbbd85 RS |
5659 | goto nope; |
5660 | } | |
5661 | } | |
5662 | } | |
5663 | ||
5664 | ++bp; /* skip paren */ | |
c03413c7 | 5665 | SKIP_WHITE_SPACE (bp); |
b0bbbd85 RS |
5666 | /* now everything from bp before limit is the definition. */ |
5667 | defn = collect_expansion (bp, limit, argno, arg_ptrs); | |
5ff1a832 | 5668 | defn->rest_args = rest_args; |
b0bbbd85 RS |
5669 | |
5670 | /* Now set defn->args.argnames to the result of concatenating | |
5671 | the argument names in reverse order | |
5672 | with comma-space between them. */ | |
5673 | defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1); | |
5674 | { | |
5675 | struct arglist *temp; | |
5676 | int i = 0; | |
5677 | for (temp = arg_ptrs; temp; temp = temp->next) { | |
5678 | bcopy (temp->name, &defn->args.argnames[i], temp->length); | |
5679 | i += temp->length; | |
5680 | if (temp->next != 0) { | |
5681 | defn->args.argnames[i++] = ','; | |
5682 | defn->args.argnames[i++] = ' '; | |
5683 | } | |
5684 | } | |
5685 | defn->args.argnames[i] = 0; | |
5686 | } | |
5687 | } else { | |
9966b391 RK |
5688 | /* Simple expansion or empty definition. */ |
5689 | ||
fbcd3360 RK |
5690 | if (bp < limit) |
5691 | { | |
c03413c7 RK |
5692 | if (is_hor_space[*bp]) { |
5693 | bp++; | |
5694 | SKIP_WHITE_SPACE (bp); | |
5695 | } else { | |
5696 | switch (*bp) { | |
fbcd3360 RK |
5697 | case '!': case '"': case '#': case '%': case '&': case '\'': |
5698 | case ')': case '*': case '+': case ',': case '-': case '.': | |
5699 | case '/': case ':': case ';': case '<': case '=': case '>': | |
5700 | case '?': case '[': case '\\': case ']': case '^': case '{': | |
5701 | case '|': case '}': case '~': | |
5702 | warning ("missing white space after `#define %.*s'", | |
5703 | sym_length, symname); | |
5704 | break; | |
5705 | ||
5706 | default: | |
5707 | pedwarn ("missing white space after `#define %.*s'", | |
5708 | sym_length, symname); | |
5709 | break; | |
5710 | } | |
c03413c7 | 5711 | } |
fbcd3360 | 5712 | } |
9966b391 | 5713 | /* Now everything from bp before limit is the definition. */ |
8d9bfdc5 | 5714 | defn = collect_expansion (bp, limit, -1, NULL_PTR); |
b0bbbd85 RS |
5715 | defn->args.argnames = (U_CHAR *) ""; |
5716 | } | |
5717 | ||
5718 | defn->line = line; | |
5719 | defn->file = file; | |
5720 | ||
5721 | /* OP is null if this is a predefinition */ | |
5722 | defn->predefined = !op; | |
5723 | mdef.defn = defn; | |
5724 | mdef.symnam = symname; | |
5725 | mdef.symlen = sym_length; | |
5726 | ||
5727 | return mdef; | |
5728 | ||
5729 | nope: | |
5730 | mdef.defn = 0; | |
5731 | return mdef; | |
5732 | } | |
5733 | ||
adcfa681 RK |
5734 | /* Process a #define directive. |
5735 | BUF points to the contents of the #define directive, as a contiguous string. | |
b0bbbd85 RS |
5736 | LIMIT points to the first character past the end of the definition. |
5737 | KEYWORD is the keyword-table entry for #define. */ | |
5738 | ||
5739 | static int | |
5740 | do_define (buf, limit, op, keyword) | |
5741 | U_CHAR *buf, *limit; | |
5742 | FILE_BUF *op; | |
5743 | struct directive *keyword; | |
5744 | { | |
5745 | int hashcode; | |
5746 | MACRODEF mdef; | |
5747 | ||
adcfa681 | 5748 | /* If this is a precompiler run (with -pcp) pass thru #define directives. */ |
b0bbbd85 RS |
5749 | if (pcp_outfile && op) |
5750 | pass_thru_directive (buf, limit, op, keyword); | |
5751 | ||
5752 | mdef = create_definition (buf, limit, op); | |
5753 | if (mdef.defn == 0) | |
5754 | goto nope; | |
5755 | ||
5756 | hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE); | |
5757 | ||
5758 | { | |
5759 | HASHNODE *hp; | |
5760 | if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) { | |
5761 | int ok = 0; | |
5762 | /* Redefining a precompiled key is ok. */ | |
5763 | if (hp->type == T_PCSTRING) | |
5764 | ok = 1; | |
5765 | /* Redefining a macro is ok if the definitions are the same. */ | |
5766 | else if (hp->type == T_MACRO) | |
5767 | ok = ! compare_defs (mdef.defn, hp->value.defn); | |
5768 | /* Redefining a constant is ok with -D. */ | |
5769 | else if (hp->type == T_CONST) | |
5770 | ok = ! done_initializing; | |
5771 | /* Print the warning if it's not ok. */ | |
5772 | if (!ok) { | |
b0bbbd85 RS |
5773 | /* If we are passing through #define and #undef directives, do |
5774 | that for this re-definition now. */ | |
5775 | if (debug_output && op) | |
5776 | pass_thru_directive (buf, limit, op, keyword); | |
5777 | ||
25cbb59e | 5778 | pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam); |
b0bbbd85 RS |
5779 | if (hp->type == T_MACRO) |
5780 | pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line, | |
5781 | "this is the location of the previous definition"); | |
5782 | } | |
5783 | /* Replace the old definition. */ | |
5784 | hp->type = T_MACRO; | |
5785 | hp->value.defn = mdef.defn; | |
5786 | } else { | |
5787 | /* If we are passing through #define and #undef directives, do | |
5788 | that for this new definition now. */ | |
5789 | if (debug_output && op) | |
5790 | pass_thru_directive (buf, limit, op, keyword); | |
91dbf5e7 | 5791 | install (mdef.symnam, mdef.symlen, T_MACRO, |
47b2881e | 5792 | (char *) mdef.defn, hashcode); |
b0bbbd85 RS |
5793 | } |
5794 | } | |
5795 | ||
5796 | return 0; | |
5797 | ||
5798 | nope: | |
5799 | ||
5800 | return 1; | |
5801 | } | |
5802 | \f | |
5803 | /* Check a purported macro name SYMNAME, and yield its length. | |
5804 | USAGE is the kind of name this is intended for. */ | |
5805 | ||
5806 | static int | |
5807 | check_macro_name (symname, usage) | |
5808 | U_CHAR *symname; | |
5809 | char *usage; | |
5810 | { | |
5811 | U_CHAR *p; | |
5812 | int sym_length; | |
5813 | ||
5814 | for (p = symname; is_idchar[*p]; p++) | |
5815 | ; | |
5816 | sym_length = p - symname; | |
5817 | if (sym_length == 0) | |
5818 | error ("invalid %s name", usage); | |
25cbb59e RK |
5819 | else if (!is_idstart[*symname] |
5820 | || (sym_length == 7 && ! bcmp (symname, "defined", 7))) | |
5821 | error ("invalid %s name `%.*s'", usage, sym_length, symname); | |
b0bbbd85 RS |
5822 | return sym_length; |
5823 | } | |
5824 | ||
5825 | /* | |
5826 | * return zero if two DEFINITIONs are isomorphic | |
5827 | */ | |
5828 | static int | |
5829 | compare_defs (d1, d2) | |
5830 | DEFINITION *d1, *d2; | |
5831 | { | |
5832 | register struct reflist *a1, *a2; | |
5833 | register U_CHAR *p1 = d1->expansion; | |
5834 | register U_CHAR *p2 = d2->expansion; | |
5835 | int first = 1; | |
5836 | ||
5837 | if (d1->nargs != d2->nargs) | |
5838 | return 1; | |
5839 | if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames)) | |
5840 | return 1; | |
5841 | for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2; | |
5842 | a1 = a1->next, a2 = a2->next) { | |
25cbb59e | 5843 | if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars)) |
b0bbbd85 RS |
5844 | || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0)) |
5845 | || a1->argno != a2->argno | |
5846 | || a1->stringify != a2->stringify | |
5847 | || a1->raw_before != a2->raw_before | |
5848 | || a1->raw_after != a2->raw_after) | |
5849 | return 1; | |
5850 | first = 0; | |
5851 | p1 += a1->nchars; | |
5852 | p2 += a2->nchars; | |
5853 | } | |
5854 | if (a1 != a2) | |
5855 | return 1; | |
5856 | if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion), | |
5857 | p2, d2->length - (p2 - d2->expansion), 1)) | |
5858 | return 1; | |
5859 | return 0; | |
5860 | } | |
5861 | ||
5862 | /* Return 1 if two parts of two macro definitions are effectively different. | |
5863 | One of the parts starts at BEG1 and has LEN1 chars; | |
5864 | the other has LEN2 chars at BEG2. | |
5865 | Any sequence of whitespace matches any other sequence of whitespace. | |
5866 | FIRST means these parts are the first of a macro definition; | |
5867 | so ignore leading whitespace entirely. | |
5868 | LAST means these parts are the last of a macro definition; | |
5869 | so ignore trailing whitespace entirely. */ | |
5870 | ||
5871 | static int | |
5872 | comp_def_part (first, beg1, len1, beg2, len2, last) | |
5873 | int first; | |
5874 | U_CHAR *beg1, *beg2; | |
5875 | int len1, len2; | |
5876 | int last; | |
5877 | { | |
5878 | register U_CHAR *end1 = beg1 + len1; | |
5879 | register U_CHAR *end2 = beg2 + len2; | |
5880 | if (first) { | |
5881 | while (beg1 != end1 && is_space[*beg1]) beg1++; | |
5882 | while (beg2 != end2 && is_space[*beg2]) beg2++; | |
5883 | } | |
5884 | if (last) { | |
5885 | while (beg1 != end1 && is_space[end1[-1]]) end1--; | |
5886 | while (beg2 != end2 && is_space[end2[-1]]) end2--; | |
5887 | } | |
5888 | while (beg1 != end1 && beg2 != end2) { | |
5889 | if (is_space[*beg1] && is_space[*beg2]) { | |
5890 | while (beg1 != end1 && is_space[*beg1]) beg1++; | |
5891 | while (beg2 != end2 && is_space[*beg2]) beg2++; | |
5892 | } else if (*beg1 == *beg2) { | |
5893 | beg1++; beg2++; | |
5894 | } else break; | |
5895 | } | |
5896 | return (beg1 != end1) || (beg2 != end2); | |
5897 | } | |
5898 | \f | |
5899 | /* Read a replacement list for a macro with parameters. | |
5900 | Build the DEFINITION structure. | |
5901 | Reads characters of text starting at BUF until END. | |
5902 | ARGLIST specifies the formal parameters to look for | |
5903 | in the text of the definition; NARGS is the number of args | |
5904 | in that list, or -1 for a macro name that wants no argument list. | |
5905 | MACRONAME is the macro name itself (so we can avoid recursive expansion) | |
5906 | and NAMELEN is its length in characters. | |
5907 | ||
c9263446 RK |
5908 | Note that comments, backslash-newlines, and leading white space |
5909 | have already been deleted from the argument. */ | |
b0bbbd85 | 5910 | |
c9263446 | 5911 | /* If there is no trailing whitespace, a Newline Space is added at the end |
b0bbbd85 RS |
5912 | to prevent concatenation that would be contrary to the standard. */ |
5913 | ||
5914 | static DEFINITION * | |
5915 | collect_expansion (buf, end, nargs, arglist) | |
5916 | U_CHAR *buf, *end; | |
5917 | int nargs; | |
5918 | struct arglist *arglist; | |
5919 | { | |
5920 | DEFINITION *defn; | |
5921 | register U_CHAR *p, *limit, *lastp, *exp_p; | |
5922 | struct reflist *endpat = NULL; | |
5923 | /* Pointer to first nonspace after last ## seen. */ | |
5924 | U_CHAR *concat = 0; | |
5925 | /* Pointer to first nonspace after last single-# seen. */ | |
5926 | U_CHAR *stringify = 0; | |
5bdc1512 RK |
5927 | /* How those tokens were spelled. */ |
5928 | enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN; | |
5929 | enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN; | |
b0bbbd85 RS |
5930 | int maxsize; |
5931 | int expected_delimiter = '\0'; | |
5932 | ||
5933 | /* Scan thru the replacement list, ignoring comments and quoted | |
5934 | strings, picking up on the macro calls. It does a linear search | |
5935 | thru the arg list on every potential symbol. Profiling might say | |
5936 | that something smarter should happen. */ | |
5937 | ||
5938 | if (end < buf) | |
5939 | abort (); | |
5940 | ||
5941 | /* Find the beginning of the trailing whitespace. */ | |
b0bbbd85 RS |
5942 | limit = end; |
5943 | p = buf; | |
5944 | while (p < limit && is_space[limit[-1]]) limit--; | |
b0bbbd85 RS |
5945 | |
5946 | /* Allocate space for the text in the macro definition. | |
c9263446 | 5947 | Each input char may or may not need 1 byte, |
b0bbbd85 | 5948 | so this is an upper bound. |
c9263446 | 5949 | The extra 3 are for invented trailing newline-marker and final null. */ |
b0bbbd85 | 5950 | maxsize = (sizeof (DEFINITION) |
b0bbbd85 RS |
5951 | + (limit - p) + 3); |
5952 | defn = (DEFINITION *) xcalloc (1, maxsize); | |
5953 | ||
5954 | defn->nargs = nargs; | |
5955 | exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION); | |
5956 | lastp = exp_p; | |
5957 | ||
91dbf5e7 RK |
5958 | if (p[0] == '#' |
5959 | ? p[1] == '#' | |
5960 | : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') { | |
b0bbbd85 | 5961 | error ("`##' at start of macro definition"); |
91dbf5e7 | 5962 | p += p[0] == '#' ? 2 : 4; |
b0bbbd85 RS |
5963 | } |
5964 | ||
5965 | /* Process the main body of the definition. */ | |
5966 | while (p < limit) { | |
5967 | int skipped_arg = 0; | |
5968 | register U_CHAR c = *p++; | |
5969 | ||
5970 | *exp_p++ = c; | |
5971 | ||
5972 | if (!traditional) { | |
5973 | switch (c) { | |
5974 | case '\'': | |
5975 | case '\"': | |
5976 | if (expected_delimiter != '\0') { | |
5977 | if (c == expected_delimiter) | |
5978 | expected_delimiter = '\0'; | |
5979 | } else | |
5980 | expected_delimiter = c; | |
5981 | break; | |
5982 | ||
b0bbbd85 | 5983 | case '\\': |
01bffe73 | 5984 | if (p < limit && expected_delimiter) { |
b0bbbd85 RS |
5985 | /* In a string, backslash goes through |
5986 | and makes next char ordinary. */ | |
5987 | *exp_p++ = *p++; | |
5988 | } | |
5989 | break; | |
5990 | ||
91dbf5e7 RK |
5991 | case '%': |
5992 | if (!expected_delimiter && *p == ':') { | |
5993 | /* %: is not a digraph if preceded by an odd number of '<'s. */ | |
5994 | U_CHAR *p0 = p - 1; | |
5995 | while (buf < p0 && p0[-1] == '<') | |
5996 | p0--; | |
5997 | if ((p - p0) & 1) { | |
5998 | /* Treat %:%: as ## and %: as #. */ | |
5999 | if (p[1] == '%' && p[2] == ':') { | |
6000 | p += 2; | |
5bdc1512 | 6001 | goto sharp_sharp_token; |
91dbf5e7 RK |
6002 | } |
6003 | if (nargs >= 0) { | |
6004 | p++; | |
5bdc1512 | 6005 | goto sharp_token; |
91dbf5e7 RK |
6006 | } |
6007 | } | |
6008 | } | |
6009 | break; | |
6010 | ||
b0bbbd85 RS |
6011 | case '#': |
6012 | /* # is ordinary inside a string. */ | |
6013 | if (expected_delimiter) | |
6014 | break; | |
91dbf5e7 | 6015 | if (*p == '#') { |
5bdc1512 | 6016 | sharp_sharp_token: |
b0bbbd85 RS |
6017 | /* ##: concatenate preceding and following tokens. */ |
6018 | /* Take out the first #, discard preceding whitespace. */ | |
6019 | exp_p--; | |
6020 | while (exp_p > lastp && is_hor_space[exp_p[-1]]) | |
6021 | --exp_p; | |
6022 | /* Skip the second #. */ | |
6023 | p++; | |
5bdc1512 RK |
6024 | concat_sharp_token_type = c; |
6025 | if (is_hor_space[*p]) { | |
6026 | concat_sharp_token_type++; | |
6027 | p++; | |
6028 | SKIP_WHITE_SPACE (p); | |
6029 | } | |
b0bbbd85 RS |
6030 | concat = p; |
6031 | if (p == limit) | |
6032 | error ("`##' at end of macro definition"); | |
2fc33352 | 6033 | } else if (nargs >= 0) { |
b0bbbd85 RS |
6034 | /* Single #: stringify following argument ref. |
6035 | Don't leave the # in the expansion. */ | |
5bdc1512 | 6036 | sharp_token: |
b0bbbd85 | 6037 | exp_p--; |
5bdc1512 RK |
6038 | stringify_sharp_token_type = c; |
6039 | if (is_hor_space[*p]) { | |
6040 | stringify_sharp_token_type++; | |
6041 | p++; | |
6042 | SKIP_WHITE_SPACE (p); | |
6043 | } | |
91dbf5e7 | 6044 | if (! is_idstart[*p] || nargs == 0) |
b0bbbd85 | 6045 | error ("`#' operator is not followed by a macro argument name"); |
5bdc1512 | 6046 | else |
b0bbbd85 RS |
6047 | stringify = p; |
6048 | } | |
6049 | break; | |
6050 | } | |
6051 | } else { | |
6052 | /* In -traditional mode, recognize arguments inside strings and | |
6053 | and character constants, and ignore special properties of #. | |
6054 | Arguments inside strings are considered "stringified", but no | |
6055 | extra quote marks are supplied. */ | |
6056 | switch (c) { | |
6057 | case '\'': | |
6058 | case '\"': | |
6059 | if (expected_delimiter != '\0') { | |
6060 | if (c == expected_delimiter) | |
6061 | expected_delimiter = '\0'; | |
6062 | } else | |
6063 | expected_delimiter = c; | |
6064 | break; | |
6065 | ||
6066 | case '\\': | |
6067 | /* Backslash quotes delimiters and itself, but not macro args. */ | |
6068 | if (expected_delimiter != 0 && p < limit | |
6069 | && (*p == expected_delimiter || *p == '\\')) { | |
6070 | *exp_p++ = *p++; | |
6071 | continue; | |
6072 | } | |
6073 | break; | |
6074 | ||
6075 | case '/': | |
6076 | if (expected_delimiter != '\0') /* No comments inside strings. */ | |
6077 | break; | |
6078 | if (*p == '*') { | |
6079 | /* If we find a comment that wasn't removed by handle_directive, | |
6080 | this must be -traditional. So replace the comment with | |
6081 | nothing at all. */ | |
6082 | exp_p--; | |
6083 | p += 1; | |
6084 | while (p < limit && !(p[-2] == '*' && p[-1] == '/')) | |
6085 | p++; | |
6086 | #if 0 | |
6087 | /* Mark this as a concatenation-point, as if it had been ##. */ | |
6088 | concat = p; | |
6089 | #endif | |
6090 | } | |
6091 | break; | |
6092 | } | |
6093 | } | |
6094 | ||
6095 | /* Handle the start of a symbol. */ | |
6096 | if (is_idchar[c] && nargs > 0) { | |
6097 | U_CHAR *id_beg = p - 1; | |
6098 | int id_len; | |
6099 | ||
6100 | --exp_p; | |
6101 | while (p != limit && is_idchar[*p]) p++; | |
6102 | id_len = p - id_beg; | |
6103 | ||
6104 | if (is_idstart[c]) { | |
6105 | register struct arglist *arg; | |
6106 | ||
6107 | for (arg = arglist; arg != NULL; arg = arg->next) { | |
6108 | struct reflist *tpat; | |
6109 | ||
6110 | if (arg->name[0] == c | |
6111 | && arg->length == id_len | |
25cbb59e | 6112 | && bcmp (arg->name, id_beg, id_len) == 0) { |
c29a4cbc RK |
6113 | enum sharp_token_type tpat_stringify; |
6114 | if (expected_delimiter) { | |
6115 | if (warn_stringify) { | |
6116 | if (traditional) { | |
6117 | warning ("macro argument `%.*s' is stringified.", | |
6118 | id_len, arg->name); | |
6119 | } else { | |
6120 | warning ("macro arg `%.*s' would be stringified with -traditional.", | |
6121 | id_len, arg->name); | |
6122 | } | |
b0bbbd85 | 6123 | } |
c29a4cbc RK |
6124 | /* If ANSI, don't actually substitute inside a string. */ |
6125 | if (!traditional) | |
6126 | break; | |
6127 | tpat_stringify = SHARP_TOKEN; | |
6128 | } else { | |
6129 | tpat_stringify | |
6130 | = (stringify == id_beg | |
6131 | ? stringify_sharp_token_type : NO_SHARP_TOKEN); | |
b0bbbd85 | 6132 | } |
b0bbbd85 RS |
6133 | /* make a pat node for this arg and append it to the end of |
6134 | the pat list */ | |
6135 | tpat = (struct reflist *) xmalloc (sizeof (struct reflist)); | |
6136 | tpat->next = NULL; | |
5bdc1512 RK |
6137 | tpat->raw_before |
6138 | = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN; | |
6139 | tpat->raw_after = NO_SHARP_TOKEN; | |
5ff1a832 | 6140 | tpat->rest_args = arg->rest_args; |
c29a4cbc | 6141 | tpat->stringify = tpat_stringify; |
b0bbbd85 RS |
6142 | |
6143 | if (endpat == NULL) | |
6144 | defn->pattern = tpat; | |
6145 | else | |
6146 | endpat->next = tpat; | |
6147 | endpat = tpat; | |
6148 | ||
6149 | tpat->argno = arg->argno; | |
6150 | tpat->nchars = exp_p - lastp; | |
6151 | { | |
6152 | register U_CHAR *p1 = p; | |
6153 | SKIP_WHITE_SPACE (p1); | |
91dbf5e7 RK |
6154 | if (p1[0]=='#' |
6155 | ? p1[1]=='#' | |
6156 | : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':') | |
5bdc1512 | 6157 | tpat->raw_after = p1[0] + (p != p1); |
b0bbbd85 RS |
6158 | } |
6159 | lastp = exp_p; /* place to start copying from next time */ | |
6160 | skipped_arg = 1; | |
6161 | break; | |
6162 | } | |
6163 | } | |
6164 | } | |
6165 | ||
6166 | /* If this was not a macro arg, copy it into the expansion. */ | |
6167 | if (! skipped_arg) { | |
6168 | register U_CHAR *lim1 = p; | |
6169 | p = id_beg; | |
6170 | while (p != lim1) | |
6171 | *exp_p++ = *p++; | |
6172 | if (stringify == id_beg) | |
6173 | error ("`#' operator should be followed by a macro argument name"); | |
6174 | } | |
6175 | } | |
6176 | } | |
6177 | ||
d52a8965 MM |
6178 | if (!traditional && expected_delimiter == 0) { |
6179 | /* There is no trailing whitespace, so invent some in ANSI mode. | |
6180 | But not if "inside a string" (which in ANSI mode | |
6181 | happens only for -D option). */ | |
6182 | *exp_p++ = '\n'; | |
6183 | *exp_p++ = ' '; | |
6184 | } | |
6185 | ||
b0bbbd85 RS |
6186 | *exp_p = '\0'; |
6187 | ||
6188 | defn->length = exp_p - defn->expansion; | |
6189 | ||
6190 | /* Crash now if we overrun the allocated size. */ | |
6191 | if (defn->length + 1 > maxsize) | |
6192 | abort (); | |
6193 | ||
6194 | #if 0 | |
6195 | /* This isn't worth the time it takes. */ | |
6196 | /* give back excess storage */ | |
6197 | defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1); | |
6198 | #endif | |
6199 | ||
6200 | return defn; | |
6201 | } | |
6202 | \f | |
6203 | static int | |
6204 | do_assert (buf, limit, op, keyword) | |
6205 | U_CHAR *buf, *limit; | |
6206 | FILE_BUF *op; | |
6207 | struct directive *keyword; | |
6208 | { | |
6209 | U_CHAR *bp; /* temp ptr into input buffer */ | |
6210 | U_CHAR *symname; /* remember where symbol name starts */ | |
6211 | int sym_length; /* and how long it is */ | |
6212 | struct arglist *tokens = NULL; | |
6213 | ||
6214 | if (pedantic && done_initializing && !instack[indepth].system_header_p) | |
6215 | pedwarn ("ANSI C does not allow `#assert'"); | |
6216 | ||
6217 | bp = buf; | |
6218 | ||
6219 | while (is_hor_space[*bp]) | |
6220 | bp++; | |
6221 | ||
6222 | symname = bp; /* remember where it starts */ | |
6223 | sym_length = check_macro_name (bp, "assertion"); | |
6224 | bp += sym_length; | |
6225 | /* #define doesn't do this, but we should. */ | |
6226 | SKIP_WHITE_SPACE (bp); | |
6227 | ||
6228 | /* Lossage will occur if identifiers or control tokens are broken | |
6229 | across lines using backslash. This is not the right place to take | |
6230 | care of that. */ | |
6231 | ||
6232 | if (*bp != '(') { | |
6233 | error ("missing token-sequence in `#assert'"); | |
6234 | return 1; | |
6235 | } | |
6236 | ||
6237 | { | |
6238 | int error_flag = 0; | |
6239 | ||
6240 | bp++; /* skip '(' */ | |
6241 | SKIP_WHITE_SPACE (bp); | |
6242 | ||
6243 | tokens = read_token_list (&bp, limit, &error_flag); | |
6244 | if (error_flag) | |
6245 | return 1; | |
6246 | if (tokens == 0) { | |
6247 | error ("empty token-sequence in `#assert'"); | |
6248 | return 1; | |
6249 | } | |
6250 | ||
6251 | ++bp; /* skip paren */ | |
6252 | SKIP_WHITE_SPACE (bp); | |
6253 | } | |
6254 | ||
6255 | /* If this name isn't already an assertion name, make it one. | |
6256 | Error if it was already in use in some other way. */ | |
6257 | ||
6258 | { | |
6259 | ASSERTION_HASHNODE *hp; | |
6260 | int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE); | |
6261 | struct tokenlist_list *value | |
6262 | = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list)); | |
6263 | ||
6264 | hp = assertion_lookup (symname, sym_length, hashcode); | |
6265 | if (hp == NULL) { | |
25cbb59e | 6266 | if (sym_length == 7 && ! bcmp (symname, "defined", 7)) |
b0bbbd85 RS |
6267 | error ("`defined' redefined as assertion"); |
6268 | hp = assertion_install (symname, sym_length, hashcode); | |
6269 | } | |
6270 | ||
6271 | /* Add the spec'd token-sequence to the list of such. */ | |
6272 | value->tokens = tokens; | |
6273 | value->next = hp->value; | |
6274 | hp->value = value; | |
6275 | } | |
6276 | ||
6277 | return 0; | |
6278 | } | |
6279 | \f | |
6280 | static int | |
6281 | do_unassert (buf, limit, op, keyword) | |
6282 | U_CHAR *buf, *limit; | |
6283 | FILE_BUF *op; | |
6284 | struct directive *keyword; | |
6285 | { | |
6286 | U_CHAR *bp; /* temp ptr into input buffer */ | |
6287 | U_CHAR *symname; /* remember where symbol name starts */ | |
6288 | int sym_length; /* and how long it is */ | |
6289 | ||
6290 | struct arglist *tokens = NULL; | |
6291 | int tokens_specified = 0; | |
6292 | ||
6293 | if (pedantic && done_initializing && !instack[indepth].system_header_p) | |
6294 | pedwarn ("ANSI C does not allow `#unassert'"); | |
6295 | ||
6296 | bp = buf; | |
6297 | ||
6298 | while (is_hor_space[*bp]) | |
6299 | bp++; | |
6300 | ||
6301 | symname = bp; /* remember where it starts */ | |
6302 | sym_length = check_macro_name (bp, "assertion"); | |
6303 | bp += sym_length; | |
6304 | /* #define doesn't do this, but we should. */ | |
6305 | SKIP_WHITE_SPACE (bp); | |
6306 | ||
6307 | /* Lossage will occur if identifiers or control tokens are broken | |
6308 | across lines using backslash. This is not the right place to take | |
6309 | care of that. */ | |
6310 | ||
6311 | if (*bp == '(') { | |
6312 | int error_flag = 0; | |
6313 | ||
6314 | bp++; /* skip '(' */ | |
6315 | SKIP_WHITE_SPACE (bp); | |
6316 | ||
6317 | tokens = read_token_list (&bp, limit, &error_flag); | |
6318 | if (error_flag) | |
6319 | return 1; | |
6320 | if (tokens == 0) { | |
6321 | error ("empty token list in `#unassert'"); | |
6322 | return 1; | |
6323 | } | |
6324 | ||
6325 | tokens_specified = 1; | |
6326 | ||
6327 | ++bp; /* skip paren */ | |
6328 | SKIP_WHITE_SPACE (bp); | |
6329 | } | |
6330 | ||
6331 | { | |
6332 | ASSERTION_HASHNODE *hp; | |
6333 | int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE); | |
6334 | struct tokenlist_list *tail, *prev; | |
6335 | ||
6336 | hp = assertion_lookup (symname, sym_length, hashcode); | |
6337 | if (hp == NULL) | |
6338 | return 1; | |
6339 | ||
6340 | /* If no token list was specified, then eliminate this assertion | |
6341 | entirely. */ | |
6342 | if (! tokens_specified) { | |
6343 | struct tokenlist_list *next; | |
6344 | for (tail = hp->value; tail; tail = next) { | |
6345 | next = tail->next; | |
6346 | free_token_list (tail->tokens); | |
6347 | free (tail); | |
6348 | } | |
6349 | delete_assertion (hp); | |
6350 | } else { | |
6351 | /* If a list of tokens was given, then delete any matching list. */ | |
6352 | ||
6353 | tail = hp->value; | |
6354 | prev = 0; | |
6355 | while (tail) { | |
6356 | struct tokenlist_list *next = tail->next; | |
6357 | if (compare_token_lists (tail->tokens, tokens)) { | |
6358 | if (prev) | |
6359 | prev->next = next; | |
6360 | else | |
6361 | hp->value = tail->next; | |
6362 | free_token_list (tail->tokens); | |
6363 | free (tail); | |
6364 | } else { | |
6365 | prev = tail; | |
6366 | } | |
6367 | tail = next; | |
6368 | } | |
6369 | } | |
6370 | } | |
6371 | ||
6372 | return 0; | |
6373 | } | |
6374 | \f | |
6375 | /* Test whether there is an assertion named NAME | |
6376 | and optionally whether it has an asserted token list TOKENS. | |
6377 | NAME is not null terminated; its length is SYM_LENGTH. | |
6378 | If TOKENS_SPECIFIED is 0, then don't check for any token list. */ | |
6379 | ||
6380 | int | |
6381 | check_assertion (name, sym_length, tokens_specified, tokens) | |
6382 | U_CHAR *name; | |
6383 | int sym_length; | |
6384 | int tokens_specified; | |
6385 | struct arglist *tokens; | |
6386 | { | |
6387 | ASSERTION_HASHNODE *hp; | |
6388 | int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE); | |
6389 | ||
6390 | if (pedantic && !instack[indepth].system_header_p) | |
6391 | pedwarn ("ANSI C does not allow testing assertions"); | |
6392 | ||
6393 | hp = assertion_lookup (name, sym_length, hashcode); | |
6394 | if (hp == NULL) | |
6395 | /* It is not an assertion; just return false. */ | |
6396 | return 0; | |
6397 | ||
6398 | /* If no token list was specified, then value is 1. */ | |
6399 | if (! tokens_specified) | |
6400 | return 1; | |
6401 | ||
6402 | { | |
6403 | struct tokenlist_list *tail; | |
6404 | ||
6405 | tail = hp->value; | |
6406 | ||
6407 | /* If a list of tokens was given, | |
6408 | then succeed if the assertion records a matching list. */ | |
6409 | ||
6410 | while (tail) { | |
6411 | if (compare_token_lists (tail->tokens, tokens)) | |
6412 | return 1; | |
6413 | tail = tail->next; | |
6414 | } | |
6415 | ||
6416 | /* Fail if the assertion has no matching list. */ | |
6417 | return 0; | |
6418 | } | |
6419 | } | |
6420 | ||
6421 | /* Compare two lists of tokens for equality including order of tokens. */ | |
6422 | ||
6423 | static int | |
6424 | compare_token_lists (l1, l2) | |
6425 | struct arglist *l1, *l2; | |
6426 | { | |
6427 | while (l1 && l2) { | |
6428 | if (l1->length != l2->length) | |
6429 | return 0; | |
25cbb59e | 6430 | if (bcmp (l1->name, l2->name, l1->length)) |
b0bbbd85 RS |
6431 | return 0; |
6432 | l1 = l1->next; | |
6433 | l2 = l2->next; | |
6434 | } | |
6435 | ||
6436 | /* Succeed if both lists end at the same time. */ | |
6437 | return l1 == l2; | |
6438 | } | |
6439 | \f | |
6440 | /* Read a space-separated list of tokens ending in a close parenthesis. | |
6441 | Return a list of strings, in the order they were written. | |
6442 | (In case of error, return 0 and store -1 in *ERROR_FLAG.) | |
6443 | Parse the text starting at *BPP, and update *BPP. | |
6444 | Don't parse beyond LIMIT. */ | |
6445 | ||
6446 | static struct arglist * | |
6447 | read_token_list (bpp, limit, error_flag) | |
6448 | U_CHAR **bpp; | |
6449 | U_CHAR *limit; | |
6450 | int *error_flag; | |
6451 | { | |
6452 | struct arglist *token_ptrs = 0; | |
6453 | U_CHAR *bp = *bpp; | |
6454 | int depth = 1; | |
6455 | ||
6456 | *error_flag = 0; | |
6457 | ||
6458 | /* Loop over the assertion value tokens. */ | |
6459 | while (depth > 0) { | |
6460 | struct arglist *temp; | |
6461 | int eofp = 0; | |
6462 | U_CHAR *beg = bp; | |
6463 | ||
6464 | /* Find the end of the token. */ | |
6465 | if (*bp == '(') { | |
6466 | bp++; | |
6467 | depth++; | |
6468 | } else if (*bp == ')') { | |
6469 | depth--; | |
6470 | if (depth == 0) | |
6471 | break; | |
6472 | bp++; | |
6473 | } else if (*bp == '"' || *bp == '\'') | |
8d9bfdc5 | 6474 | bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp); |
b0bbbd85 RS |
6475 | else |
6476 | while (! is_hor_space[*bp] && *bp != '(' && *bp != ')' | |
6477 | && *bp != '"' && *bp != '\'' && bp != limit) | |
6478 | bp++; | |
6479 | ||
6480 | temp = (struct arglist *) xmalloc (sizeof (struct arglist)); | |
6481 | temp->name = (U_CHAR *) xmalloc (bp - beg + 1); | |
4c9a05bc | 6482 | bcopy ((char *) beg, (char *) temp->name, bp - beg); |
b0bbbd85 RS |
6483 | temp->name[bp - beg] = 0; |
6484 | temp->next = token_ptrs; | |
6485 | token_ptrs = temp; | |
6486 | temp->length = bp - beg; | |
6487 | ||
6488 | SKIP_WHITE_SPACE (bp); | |
6489 | ||
6490 | if (bp >= limit) { | |
6491 | error ("unterminated token sequence in `#assert' or `#unassert'"); | |
6492 | *error_flag = -1; | |
6493 | return 0; | |
6494 | } | |
6495 | } | |
6496 | *bpp = bp; | |
6497 | ||
6498 | /* We accumulated the names in reverse order. | |
6499 | Now reverse them to get the proper order. */ | |
6500 | { | |
6501 | register struct arglist *prev = 0, *this, *next; | |
6502 | for (this = token_ptrs; this; this = next) { | |
6503 | next = this->next; | |
6504 | this->next = prev; | |
6505 | prev = this; | |
6506 | } | |
6507 | return prev; | |
6508 | } | |
6509 | } | |
6510 | ||
6511 | static void | |
6512 | free_token_list (tokens) | |
6513 | struct arglist *tokens; | |
6514 | { | |
6515 | while (tokens) { | |
6516 | struct arglist *next = tokens->next; | |
6517 | free (tokens->name); | |
6518 | free (tokens); | |
6519 | tokens = next; | |
6520 | } | |
6521 | } | |
6522 | \f | |
6523 | /* | |
6524 | * Install a name in the assertion hash table. | |
6525 | * | |
6526 | * If LEN is >= 0, it is the length of the name. | |
6527 | * Otherwise, compute the length by scanning the entire name. | |
6528 | * | |
6529 | * If HASH is >= 0, it is the precomputed hash code. | |
6530 | * Otherwise, compute the hash code. | |
6531 | */ | |
6532 | static ASSERTION_HASHNODE * | |
6533 | assertion_install (name, len, hash) | |
6534 | U_CHAR *name; | |
6535 | int len; | |
6536 | int hash; | |
6537 | { | |
6538 | register ASSERTION_HASHNODE *hp; | |
6539 | register int i, bucket; | |
6540 | register U_CHAR *p, *q; | |
6541 | ||
6542 | i = sizeof (ASSERTION_HASHNODE) + len + 1; | |
6543 | hp = (ASSERTION_HASHNODE *) xmalloc (i); | |
6544 | bucket = hash; | |
6545 | hp->bucket_hdr = &assertion_hashtab[bucket]; | |
6546 | hp->next = assertion_hashtab[bucket]; | |
6547 | assertion_hashtab[bucket] = hp; | |
6548 | hp->prev = NULL; | |
6549 | if (hp->next != NULL) | |
6550 | hp->next->prev = hp; | |
6551 | hp->length = len; | |
6552 | hp->value = 0; | |
6553 | hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE); | |
6554 | p = hp->name; | |
6555 | q = name; | |
6556 | for (i = 0; i < len; i++) | |
6557 | *p++ = *q++; | |
6558 | hp->name[len] = 0; | |
6559 | return hp; | |
6560 | } | |
6561 | ||
6562 | /* | |
6563 | * find the most recent hash node for name name (ending with first | |
6564 | * non-identifier char) installed by install | |
6565 | * | |
6566 | * If LEN is >= 0, it is the length of the name. | |
6567 | * Otherwise, compute the length by scanning the entire name. | |
6568 | * | |
6569 | * If HASH is >= 0, it is the precomputed hash code. | |
6570 | * Otherwise, compute the hash code. | |
6571 | */ | |
6572 | static ASSERTION_HASHNODE * | |
6573 | assertion_lookup (name, len, hash) | |
6574 | U_CHAR *name; | |
6575 | int len; | |
6576 | int hash; | |
6577 | { | |
b0bbbd85 RS |
6578 | register ASSERTION_HASHNODE *bucket; |
6579 | ||
6580 | bucket = assertion_hashtab[hash]; | |
6581 | while (bucket) { | |
25cbb59e | 6582 | if (bucket->length == len && bcmp (bucket->name, name, len) == 0) |
b0bbbd85 RS |
6583 | return bucket; |
6584 | bucket = bucket->next; | |
6585 | } | |
6586 | return NULL; | |
6587 | } | |
6588 | ||
6589 | static void | |
6590 | delete_assertion (hp) | |
6591 | ASSERTION_HASHNODE *hp; | |
6592 | { | |
6593 | ||
6594 | if (hp->prev != NULL) | |
6595 | hp->prev->next = hp->next; | |
6596 | if (hp->next != NULL) | |
6597 | hp->next->prev = hp->prev; | |
6598 | ||
6599 | /* make sure that the bucket chain header that | |
6600 | the deleted guy was on points to the right thing afterwards. */ | |
6601 | if (hp == *hp->bucket_hdr) | |
6602 | *hp->bucket_hdr = hp->next; | |
6603 | ||
6604 | free (hp); | |
6605 | } | |
6606 | \f | |
6607 | /* | |
adcfa681 | 6608 | * interpret #line directive. Remembers previously seen fnames |
b0bbbd85 RS |
6609 | * in its very own hash table. |
6610 | */ | |
6611 | #define FNAME_HASHSIZE 37 | |
6612 | ||
6613 | static int | |
6614 | do_line (buf, limit, op, keyword) | |
6615 | U_CHAR *buf, *limit; | |
6616 | FILE_BUF *op; | |
6617 | struct directive *keyword; | |
6618 | { | |
6619 | register U_CHAR *bp; | |
6620 | FILE_BUF *ip = &instack[indepth]; | |
6621 | FILE_BUF tem; | |
6622 | int new_lineno; | |
6623 | enum file_change_code file_change = same_file; | |
6624 | ||
6625 | /* Expand any macros. */ | |
6626 | tem = expand_to_temp_buffer (buf, limit, 0, 0); | |
6627 | ||
6628 | /* Point to macroexpanded line, which is null-terminated now. */ | |
6629 | bp = tem.buf; | |
6630 | SKIP_WHITE_SPACE (bp); | |
6631 | ||
6632 | if (!isdigit (*bp)) { | |
adcfa681 | 6633 | error ("invalid format `#line' directive"); |
b0bbbd85 RS |
6634 | return 0; |
6635 | } | |
6636 | ||
6637 | /* The Newline at the end of this line remains to be processed. | |
6638 | To put the next line at the specified line number, | |
6639 | we must store a line number now that is one less. */ | |
25cbb59e | 6640 | new_lineno = atoi ((char *) bp) - 1; |
b0bbbd85 | 6641 | |
bdc680a2 JW |
6642 | /* NEW_LINENO is one less than the actual line number here. */ |
6643 | if (pedantic && new_lineno < 0) | |
adcfa681 | 6644 | pedwarn ("line number out of range in `#line' directive"); |
bdc680a2 | 6645 | |
b0bbbd85 RS |
6646 | /* skip over the line number. */ |
6647 | while (isdigit (*bp)) | |
6648 | bp++; | |
6649 | ||
6650 | #if 0 /* #line 10"foo.c" is supposed to be allowed. */ | |
6651 | if (*bp && !is_space[*bp]) { | |
adcfa681 | 6652 | error ("invalid format `#line' directive"); |
b0bbbd85 RS |
6653 | return; |
6654 | } | |
6655 | #endif | |
6656 | ||
6657 | SKIP_WHITE_SPACE (bp); | |
6658 | ||
6659 | if (*bp == '\"') { | |
6660 | static HASHNODE *fname_table[FNAME_HASHSIZE]; | |
6661 | HASHNODE *hp, **hash_bucket; | |
f7531123 | 6662 | U_CHAR *fname, *p; |
b0bbbd85 RS |
6663 | int fname_length; |
6664 | ||
6665 | fname = ++bp; | |
6666 | ||
f7531123 PE |
6667 | /* Turn the file name, which is a character string literal, |
6668 | into a null-terminated string. Do this in place. */ | |
6669 | p = bp; | |
6670 | for (;;) | |
6671 | switch ((*p++ = *bp++)) { | |
6672 | case '\0': | |
adcfa681 | 6673 | error ("invalid format `#line' directive"); |
f7531123 | 6674 | return 0; |
b0bbbd85 | 6675 | |
f7531123 PE |
6676 | case '\\': |
6677 | { | |
6678 | char *bpc = (char *) bp; | |
6679 | int c = parse_escape (&bpc); | |
6680 | bp = (U_CHAR *) bpc; | |
6681 | if (c < 0) | |
6682 | p--; | |
6683 | else | |
6684 | p[-1] = c; | |
6685 | } | |
6686 | break; | |
6687 | ||
6688 | case '\"': | |
6689 | p[-1] = 0; | |
6690 | goto fname_done; | |
6691 | } | |
6692 | fname_done: | |
6693 | fname_length = p - fname; | |
b0bbbd85 | 6694 | |
b0bbbd85 RS |
6695 | SKIP_WHITE_SPACE (bp); |
6696 | if (*bp) { | |
bdc680a2 | 6697 | if (pedantic) |
adcfa681 | 6698 | pedwarn ("garbage at end of `#line' directive"); |
b0bbbd85 RS |
6699 | if (*bp == '1') |
6700 | file_change = enter_file; | |
6701 | else if (*bp == '2') | |
6702 | file_change = leave_file; | |
2aa7ec37 RS |
6703 | else if (*bp == '3') |
6704 | ip->system_header_p = 1; | |
65715dea RS |
6705 | else if (*bp == '4') |
6706 | ip->system_header_p = 2; | |
b0bbbd85 | 6707 | else { |
adcfa681 | 6708 | error ("invalid format `#line' directive"); |
b0bbbd85 RS |
6709 | return 0; |
6710 | } | |
6711 | ||
6712 | bp++; | |
6713 | SKIP_WHITE_SPACE (bp); | |
2aa7ec37 RS |
6714 | if (*bp == '3') { |
6715 | ip->system_header_p = 1; | |
6716 | bp++; | |
6717 | SKIP_WHITE_SPACE (bp); | |
6718 | } | |
65715dea RS |
6719 | if (*bp == '4') { |
6720 | ip->system_header_p = 2; | |
6721 | bp++; | |
6722 | SKIP_WHITE_SPACE (bp); | |
6723 | } | |
b0bbbd85 | 6724 | if (*bp) { |
adcfa681 | 6725 | error ("invalid format `#line' directive"); |
b0bbbd85 RS |
6726 | return 0; |
6727 | } | |
6728 | } | |
6729 | ||
6730 | hash_bucket = | |
6731 | &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)]; | |
6732 | for (hp = *hash_bucket; hp != NULL; hp = hp->next) | |
6733 | if (hp->length == fname_length && | |
25cbb59e | 6734 | bcmp (hp->value.cpval, fname, fname_length) == 0) { |
b0bbbd85 RS |
6735 | ip->nominal_fname = hp->value.cpval; |
6736 | break; | |
6737 | } | |
6738 | if (hp == 0) { | |
6739 | /* Didn't find it; cons up a new one. */ | |
6740 | hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1); | |
6741 | hp->next = *hash_bucket; | |
6742 | *hash_bucket = hp; | |
6743 | ||
6744 | hp->length = fname_length; | |
6745 | ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE); | |
6746 | bcopy (fname, hp->value.cpval, fname_length); | |
6747 | } | |
6748 | } else if (*bp) { | |
adcfa681 | 6749 | error ("invalid format `#line' directive"); |
b0bbbd85 RS |
6750 | return 0; |
6751 | } | |
6752 | ||
6753 | ip->lineno = new_lineno; | |
adcfa681 | 6754 | output_line_directive (ip, op, 0, file_change); |
b0bbbd85 RS |
6755 | check_expand (op, ip->length - (ip->bufp - ip->buf)); |
6756 | return 0; | |
6757 | } | |
6758 | ||
6759 | /* | |
6760 | * remove the definition of a symbol from the symbol table. | |
6761 | * according to un*x /lib/cpp, it is not an error to undef | |
6762 | * something that has no definitions, so it isn't one here either. | |
6763 | */ | |
6764 | ||
6765 | static int | |
6766 | do_undef (buf, limit, op, keyword) | |
6767 | U_CHAR *buf, *limit; | |
6768 | FILE_BUF *op; | |
6769 | struct directive *keyword; | |
6770 | { | |
6771 | int sym_length; | |
6772 | HASHNODE *hp; | |
6773 | U_CHAR *orig_buf = buf; | |
6774 | ||
adcfa681 | 6775 | /* If this is a precompiler run (with -pcp) pass thru #undef directives. */ |
b0bbbd85 RS |
6776 | if (pcp_outfile && op) |
6777 | pass_thru_directive (buf, limit, op, keyword); | |
6778 | ||
6779 | SKIP_WHITE_SPACE (buf); | |
6780 | sym_length = check_macro_name (buf, "macro"); | |
6781 | ||
6782 | while ((hp = lookup (buf, sym_length, -1)) != NULL) { | |
6783 | /* If we are generating additional info for debugging (with -g) we | |
adcfa681 | 6784 | need to pass through all effective #undef directives. */ |
b0bbbd85 RS |
6785 | if (debug_output && op) |
6786 | pass_thru_directive (orig_buf, limit, op, keyword); | |
6787 | if (hp->type != T_MACRO) | |
6788 | warning ("undefining `%s'", hp->name); | |
6789 | delete_macro (hp); | |
6790 | } | |
6791 | ||
6792 | if (pedantic) { | |
6793 | buf += sym_length; | |
6794 | SKIP_WHITE_SPACE (buf); | |
6795 | if (buf != limit) | |
6796 | pedwarn ("garbage after `#undef' directive"); | |
6797 | } | |
6798 | return 0; | |
6799 | } | |
6800 | \f | |
6801 | /* | |
9b034961 RK |
6802 | * Report an error detected by the program we are processing. |
6803 | * Use the text of the line in the error message. | |
ad0c9fa1 | 6804 | * (We use error because it prints the filename & line#.) |
b0bbbd85 RS |
6805 | */ |
6806 | ||
6807 | static int | |
6808 | do_error (buf, limit, op, keyword) | |
6809 | U_CHAR *buf, *limit; | |
6810 | FILE_BUF *op; | |
6811 | struct directive *keyword; | |
6812 | { | |
6813 | int length = limit - buf; | |
d0691cfb | 6814 | U_CHAR *copy = (U_CHAR *) xmalloc (length + 1); |
4c9a05bc | 6815 | bcopy ((char *) buf, (char *) copy, length); |
b0bbbd85 RS |
6816 | copy[length] = 0; |
6817 | SKIP_WHITE_SPACE (copy); | |
6818 | error ("#error %s", copy); | |
b0bbbd85 RS |
6819 | return 0; |
6820 | } | |
6821 | ||
6822 | /* | |
6823 | * Report a warning detected by the program we are processing. | |
6824 | * Use the text of the line in the warning message, then continue. | |
ad0c9fa1 | 6825 | * (We use error because it prints the filename & line#.) |
b0bbbd85 RS |
6826 | */ |
6827 | ||
6828 | static int | |
6829 | do_warning (buf, limit, op, keyword) | |
6830 | U_CHAR *buf, *limit; | |
6831 | FILE_BUF *op; | |
6832 | struct directive *keyword; | |
6833 | { | |
6834 | int length = limit - buf; | |
d0691cfb | 6835 | U_CHAR *copy = (U_CHAR *) xmalloc (length + 1); |
4c9a05bc | 6836 | bcopy ((char *) buf, (char *) copy, length); |
b0bbbd85 RS |
6837 | copy[length] = 0; |
6838 | SKIP_WHITE_SPACE (copy); | |
7cb2a67c | 6839 | warning ("#warning %s", copy); |
b0bbbd85 RS |
6840 | return 0; |
6841 | } | |
6842 | ||
6843 | /* Remember the name of the current file being read from so that we can | |
6844 | avoid ever including it again. */ | |
6845 | ||
25cbb59e | 6846 | static void |
b0bbbd85 RS |
6847 | do_once () |
6848 | { | |
6849 | int i; | |
6850 | FILE_BUF *ip = NULL; | |
6851 | ||
6852 | for (i = indepth; i >= 0; i--) | |
6853 | if (instack[i].fname != NULL) { | |
6854 | ip = &instack[i]; | |
6855 | break; | |
6856 | } | |
6857 | ||
6858 | if (ip != NULL) { | |
6859 | struct file_name_list *new; | |
6860 | ||
6861 | new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
6862 | new->next = dont_repeat_files; | |
6863 | dont_repeat_files = new; | |
6864 | new->fname = savestring (ip->fname); | |
6865 | new->control_macro = 0; | |
6e7f952e | 6866 | new->got_name_map = 0; |
d2a22862 | 6867 | new->c_system_include_path = 0; |
b0bbbd85 | 6868 | } |
b0bbbd85 RS |
6869 | } |
6870 | ||
6871 | /* #ident has already been copied to the output file, so just ignore it. */ | |
6872 | ||
6873 | static int | |
25cbb59e | 6874 | do_ident (buf, limit, op, keyword) |
b0bbbd85 | 6875 | U_CHAR *buf, *limit; |
25cbb59e RK |
6876 | FILE_BUF *op; |
6877 | struct directive *keyword; | |
b0bbbd85 | 6878 | { |
a3fb124a RS |
6879 | FILE_BUF trybuf; |
6880 | int len; | |
a3fb124a | 6881 | |
b0bbbd85 RS |
6882 | /* Allow #ident in system headers, since that's not user's fault. */ |
6883 | if (pedantic && !instack[indepth].system_header_p) | |
6884 | pedwarn ("ANSI C does not allow `#ident'"); | |
a3fb124a RS |
6885 | |
6886 | trybuf = expand_to_temp_buffer (buf, limit, 0, 0); | |
6887 | buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1); | |
4c9a05bc | 6888 | bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf); |
a3fb124a RS |
6889 | limit = buf + (trybuf.bufp - trybuf.buf); |
6890 | len = (limit - buf); | |
6891 | free (trybuf.buf); | |
6892 | ||
6893 | /* Output directive name. */ | |
b415f25e | 6894 | check_expand (op, 7); |
4c9a05bc | 6895 | bcopy ("#ident ", (char *) op->bufp, 7); |
a3fb124a RS |
6896 | op->bufp += 7; |
6897 | ||
6898 | /* Output the expanded argument line. */ | |
6899 | check_expand (op, len); | |
4c9a05bc | 6900 | bcopy ((char *) buf, (char *) op->bufp, len); |
a3fb124a RS |
6901 | op->bufp += len; |
6902 | ||
b0bbbd85 RS |
6903 | return 0; |
6904 | } | |
6905 | ||
6906 | /* #pragma and its argument line have already been copied to the output file. | |
789d0ee5 | 6907 | Just check for some recognized pragmas that need validation here. */ |
b0bbbd85 RS |
6908 | |
6909 | static int | |
25cbb59e | 6910 | do_pragma (buf, limit, op, keyword) |
b0bbbd85 | 6911 | U_CHAR *buf, *limit; |
25cbb59e RK |
6912 | FILE_BUF *op; |
6913 | struct directive *keyword; | |
b0bbbd85 | 6914 | { |
c03413c7 | 6915 | SKIP_WHITE_SPACE (buf); |
25cbb59e | 6916 | if (!strncmp ((char *) buf, "once", 4)) { |
71c4681d RS |
6917 | /* Allow #pragma once in system headers, since that's not the user's |
6918 | fault. */ | |
6919 | if (!instack[indepth].system_header_p) | |
6920 | warning ("`#pragma once' is obsolete"); | |
b0bbbd85 RS |
6921 | do_once (); |
6922 | } | |
789d0ee5 | 6923 | |
25cbb59e | 6924 | if (!strncmp ((char *) buf, "implementation", 14)) { |
789d0ee5 RS |
6925 | /* Be quiet about `#pragma implementation' for a file only if it hasn't |
6926 | been included yet. */ | |
6927 | struct file_name_list *ptr; | |
f786509d | 6928 | U_CHAR *p = buf + 14, *fname, *inc_fname; |
789d0ee5 RS |
6929 | SKIP_WHITE_SPACE (p); |
6930 | if (*p == '\n' || *p != '\"') | |
6931 | return 0; | |
6932 | ||
6933 | fname = p + 1; | |
25cbb59e | 6934 | if ((p = (U_CHAR *) index ((char *) fname, '\"'))) |
789d0ee5 RS |
6935 | *p = '\0'; |
6936 | ||
6937 | for (ptr = all_include_files; ptr; ptr = ptr->next) { | |
8a6c1538 | 6938 | inc_fname = (U_CHAR *) rindex (ptr->fname, '/'); |
a91ae5fb | 6939 | inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname; |
25cbb59e | 6940 | if (inc_fname && !strcmp ((char *) inc_fname, (char *) fname)) |
f786509d | 6941 | warning ("`#pragma implementation' for `%s' appears after file is included", |
789d0ee5 RS |
6942 | fname); |
6943 | } | |
6944 | } | |
6945 | ||
b0bbbd85 RS |
6946 | return 0; |
6947 | } | |
6948 | ||
6949 | #if 0 | |
6950 | /* This was a fun hack, but #pragma seems to start to be useful. | |
6951 | By failing to recognize it, we pass it through unchanged to cc1. */ | |
6952 | ||
6953 | /* | |
6954 | * the behavior of the #pragma directive is implementation defined. | |
6955 | * this implementation defines it as follows. | |
6956 | */ | |
6957 | ||
6958 | static int | |
6959 | do_pragma () | |
6960 | { | |
6961 | close (0); | |
6962 | if (open ("/dev/tty", O_RDONLY, 0666) != 0) | |
6963 | goto nope; | |
6964 | close (1); | |
6965 | if (open ("/dev/tty", O_WRONLY, 0666) != 1) | |
6966 | goto nope; | |
6967 | execl ("/usr/games/hack", "#pragma", 0); | |
6968 | execl ("/usr/games/rogue", "#pragma", 0); | |
6969 | execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0); | |
6970 | execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0); | |
6971 | nope: | |
6972 | fatal ("You are in a maze of twisty compiler features, all different"); | |
6973 | } | |
6974 | #endif | |
6975 | ||
25cbb59e RK |
6976 | #ifdef SCCS_DIRECTIVE |
6977 | ||
b0bbbd85 RS |
6978 | /* Just ignore #sccs, on systems where we define it at all. */ |
6979 | ||
6980 | static int | |
25cbb59e RK |
6981 | do_sccs (buf, limit, op, keyword) |
6982 | U_CHAR *buf, *limit; | |
6983 | FILE_BUF *op; | |
6984 | struct directive *keyword; | |
b0bbbd85 RS |
6985 | { |
6986 | if (pedantic) | |
6987 | pedwarn ("ANSI C does not allow `#sccs'"); | |
6988 | return 0; | |
6989 | } | |
25cbb59e RK |
6990 | |
6991 | #endif /* defined (SCCS_DIRECTIVE) */ | |
b0bbbd85 RS |
6992 | \f |
6993 | /* | |
adcfa681 | 6994 | * handle #if directive by |
b0bbbd85 RS |
6995 | * 1) inserting special `defined' keyword into the hash table |
6996 | * that gets turned into 0 or 1 by special_symbol (thus, | |
6997 | * if the luser has a symbol called `defined' already, it won't | |
adcfa681 | 6998 | * work inside the #if directive) |
b0bbbd85 RS |
6999 | * 2) rescan the input into a temporary output buffer |
7000 | * 3) pass the output buffer to the yacc parser and collect a value | |
7001 | * 4) clean up the mess left from steps 1 and 2. | |
7002 | * 5) call conditional_skip to skip til the next #endif (etc.), | |
7003 | * or not, depending on the value from step 3. | |
7004 | */ | |
7005 | ||
7006 | static int | |
7007 | do_if (buf, limit, op, keyword) | |
7008 | U_CHAR *buf, *limit; | |
7009 | FILE_BUF *op; | |
7010 | struct directive *keyword; | |
7011 | { | |
39ffdc1e | 7012 | HOST_WIDE_INT value; |
b0bbbd85 RS |
7013 | FILE_BUF *ip = &instack[indepth]; |
7014 | ||
7015 | value = eval_if_expression (buf, limit - buf); | |
bbd4b75b | 7016 | conditional_skip (ip, value == 0, T_IF, NULL_PTR, op); |
b0bbbd85 RS |
7017 | return 0; |
7018 | } | |
7019 | ||
7020 | /* | |
7021 | * handle a #elif directive by not changing if_stack either. | |
7022 | * see the comment above do_else. | |
7023 | */ | |
7024 | ||
7025 | static int | |
7026 | do_elif (buf, limit, op, keyword) | |
7027 | U_CHAR *buf, *limit; | |
7028 | FILE_BUF *op; | |
7029 | struct directive *keyword; | |
7030 | { | |
39ffdc1e | 7031 | HOST_WIDE_INT value; |
b0bbbd85 RS |
7032 | FILE_BUF *ip = &instack[indepth]; |
7033 | ||
7034 | if (if_stack == instack[indepth].if_stack) { | |
7035 | error ("`#elif' not within a conditional"); | |
7036 | return 0; | |
7037 | } else { | |
7038 | if (if_stack->type != T_IF && if_stack->type != T_ELIF) { | |
7039 | error ("`#elif' after `#else'"); | |
7040 | fprintf (stderr, " (matches line %d", if_stack->lineno); | |
7041 | if (if_stack->fname != NULL && ip->fname != NULL && | |
7042 | strcmp (if_stack->fname, ip->nominal_fname) != 0) | |
7043 | fprintf (stderr, ", file %s", if_stack->fname); | |
7044 | fprintf (stderr, ")\n"); | |
7045 | } | |
7046 | if_stack->type = T_ELIF; | |
7047 | } | |
7048 | ||
7049 | if (if_stack->if_succeeded) | |
bbd4b75b | 7050 | skip_if_group (ip, 0, op); |
b0bbbd85 RS |
7051 | else { |
7052 | value = eval_if_expression (buf, limit - buf); | |
7053 | if (value == 0) | |
bbd4b75b | 7054 | skip_if_group (ip, 0, op); |
b0bbbd85 RS |
7055 | else { |
7056 | ++if_stack->if_succeeded; /* continue processing input */ | |
adcfa681 | 7057 | output_line_directive (ip, op, 1, same_file); |
b0bbbd85 RS |
7058 | } |
7059 | } | |
7060 | return 0; | |
7061 | } | |
7062 | ||
7063 | /* | |
7064 | * evaluate a #if expression in BUF, of length LENGTH, | |
7065 | * then parse the result as a C expression and return the value as an int. | |
7066 | */ | |
39ffdc1e | 7067 | static HOST_WIDE_INT |
b0bbbd85 RS |
7068 | eval_if_expression (buf, length) |
7069 | U_CHAR *buf; | |
7070 | int length; | |
7071 | { | |
7072 | FILE_BUF temp_obuf; | |
7073 | HASHNODE *save_defined; | |
39ffdc1e | 7074 | HOST_WIDE_INT value; |
b0bbbd85 | 7075 | |
25cbb59e RK |
7076 | save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED, |
7077 | NULL_PTR, -1); | |
b0bbbd85 RS |
7078 | pcp_inside_if = 1; |
7079 | temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1); | |
7080 | pcp_inside_if = 0; | |
7081 | delete_macro (save_defined); /* clean up special symbol */ | |
7082 | ||
25cbb59e | 7083 | value = parse_c_expression ((char *) temp_obuf.buf); |
b0bbbd85 RS |
7084 | |
7085 | free (temp_obuf.buf); | |
7086 | ||
7087 | return value; | |
7088 | } | |
7089 | ||
7090 | /* | |
7091 | * routine to handle ifdef/ifndef. Try to look up the symbol, | |
7092 | * then do or don't skip to the #endif/#else/#elif depending | |
7093 | * on what directive is actually being processed. | |
7094 | */ | |
7095 | ||
7096 | static int | |
7097 | do_xifdef (buf, limit, op, keyword) | |
7098 | U_CHAR *buf, *limit; | |
7099 | FILE_BUF *op; | |
7100 | struct directive *keyword; | |
7101 | { | |
7102 | int skip; | |
7103 | FILE_BUF *ip = &instack[indepth]; | |
7104 | U_CHAR *end; | |
7105 | int start_of_file = 0; | |
7106 | U_CHAR *control_macro = 0; | |
7107 | ||
7108 | /* Detect a #ifndef at start of file (not counting comments). */ | |
7109 | if (ip->fname != 0 && keyword->type == T_IFNDEF) { | |
7110 | U_CHAR *p = ip->buf; | |
7111 | while (p != directive_start) { | |
d0691cfb | 7112 | U_CHAR c = *p++; |
49df5f37 RS |
7113 | if (is_space[c]) |
7114 | ; | |
dec5c86b RK |
7115 | /* Make no special provision for backslash-newline here; this is |
7116 | slower if backslash-newlines are present, but it's correct, | |
7117 | and it's not worth it to tune for the rare backslash-newline. */ | |
7118 | else if (c == '/' | |
23f15b0c | 7119 | && (*p == '*' || (cplusplus_comments && *p == '/'))) { |
49df5f37 | 7120 | /* Skip this comment. */ |
2af5e9e2 | 7121 | int junk = 0; |
49df5f37 RS |
7122 | U_CHAR *save_bufp = ip->bufp; |
7123 | ip->bufp = p + 1; | |
7124 | p = skip_to_end_of_comment (ip, &junk, 1); | |
7125 | ip->bufp = save_bufp; | |
7126 | } else { | |
b0bbbd85 RS |
7127 | goto fail; |
7128 | } | |
7129 | } | |
7130 | /* If we get here, this conditional is the beginning of the file. */ | |
7131 | start_of_file = 1; | |
7132 | fail: ; | |
7133 | } | |
7134 | ||
7135 | /* Discard leading and trailing whitespace. */ | |
7136 | SKIP_WHITE_SPACE (buf); | |
7137 | while (limit != buf && is_hor_space[limit[-1]]) limit--; | |
7138 | ||
7139 | /* Find the end of the identifier at the beginning. */ | |
7140 | for (end = buf; is_idchar[*end]; end++); | |
7141 | ||
7142 | if (end == buf) { | |
7143 | skip = (keyword->type == T_IFDEF); | |
7144 | if (! traditional) | |
7145 | pedwarn (end == limit ? "`#%s' with no argument" | |
7146 | : "`#%s' argument starts with punctuation", | |
7147 | keyword->name); | |
7148 | } else { | |
7149 | HASHNODE *hp; | |
7150 | ||
7151 | if (pedantic && buf[0] >= '0' && buf[0] <= '9') | |
7152 | pedwarn ("`#%s' argument starts with a digit", keyword->name); | |
7153 | else if (end != limit && !traditional) | |
7154 | pedwarn ("garbage at end of `#%s' argument", keyword->name); | |
7155 | ||
7156 | hp = lookup (buf, end-buf, -1); | |
7157 | ||
7158 | if (pcp_outfile) { | |
7159 | /* Output a precondition for this macro. */ | |
4f845465 RK |
7160 | if (hp && |
7161 | (hp->type == T_CONST | |
7162 | || (hp->type == T_MACRO && hp->value.defn->predefined))) | |
ad0c9fa1 | 7163 | fprintf (pcp_outfile, "#define %s\n", hp->name); |
b0bbbd85 RS |
7164 | else { |
7165 | U_CHAR *cp = buf; | |
ad0c9fa1 | 7166 | fprintf (pcp_outfile, "#undef "); |
b0bbbd85 RS |
7167 | while (is_idchar[*cp]) /* Ick! */ |
7168 | fputc (*cp++, pcp_outfile); | |
7169 | putc ('\n', pcp_outfile); | |
7170 | } | |
7171 | } | |
7172 | ||
7173 | skip = (hp == NULL) ^ (keyword->type == T_IFNDEF); | |
7174 | if (start_of_file && !skip) { | |
7175 | control_macro = (U_CHAR *) xmalloc (end - buf + 1); | |
4c9a05bc | 7176 | bcopy ((char *) buf, (char *) control_macro, end - buf); |
b0bbbd85 RS |
7177 | control_macro[end - buf] = 0; |
7178 | } | |
7179 | } | |
7180 | ||
bbd4b75b | 7181 | conditional_skip (ip, skip, T_IF, control_macro, op); |
b0bbbd85 RS |
7182 | return 0; |
7183 | } | |
7184 | ||
7185 | /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead. | |
7186 | If this is a #ifndef starting at the beginning of a file, | |
7187 | CONTROL_MACRO is the macro name tested by the #ifndef. | |
7188 | Otherwise, CONTROL_MACRO is 0. */ | |
7189 | ||
7190 | static void | |
bbd4b75b | 7191 | conditional_skip (ip, skip, type, control_macro, op) |
b0bbbd85 RS |
7192 | FILE_BUF *ip; |
7193 | int skip; | |
7194 | enum node_type type; | |
7195 | U_CHAR *control_macro; | |
bbd4b75b | 7196 | FILE_BUF *op; |
b0bbbd85 RS |
7197 | { |
7198 | IF_STACK_FRAME *temp; | |
7199 | ||
7200 | temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
7201 | temp->fname = ip->nominal_fname; | |
7202 | temp->lineno = ip->lineno; | |
7203 | temp->next = if_stack; | |
7204 | temp->control_macro = control_macro; | |
7205 | if_stack = temp; | |
7206 | ||
7207 | if_stack->type = type; | |
7208 | ||
7209 | if (skip != 0) { | |
bbd4b75b | 7210 | skip_if_group (ip, 0, op); |
b0bbbd85 RS |
7211 | return; |
7212 | } else { | |
7213 | ++if_stack->if_succeeded; | |
adcfa681 | 7214 | output_line_directive (ip, &outbuf, 1, same_file); |
b0bbbd85 RS |
7215 | } |
7216 | } | |
7217 | ||
7218 | /* | |
7219 | * skip to #endif, #else, or #elif. adjust line numbers, etc. | |
7220 | * leaves input ptr at the sharp sign found. | |
7221 | * If ANY is nonzero, return at next directive of any sort. | |
7222 | */ | |
7223 | static void | |
bbd4b75b | 7224 | skip_if_group (ip, any, op) |
b0bbbd85 RS |
7225 | FILE_BUF *ip; |
7226 | int any; | |
bbd4b75b | 7227 | FILE_BUF *op; |
b0bbbd85 RS |
7228 | { |
7229 | register U_CHAR *bp = ip->bufp, *cp; | |
7230 | register U_CHAR *endb = ip->buf + ip->length; | |
7231 | struct directive *kt; | |
7232 | IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */ | |
7233 | U_CHAR *beg_of_line = bp; | |
7234 | register int ident_length; | |
7235 | U_CHAR *ident, *after_ident; | |
bbd4b75b RS |
7236 | /* Save info about where the group starts. */ |
7237 | U_CHAR *beg_of_group = bp; | |
7238 | int beg_lineno = ip->lineno; | |
7239 | ||
7240 | if (output_conditionals && op != 0) { | |
7241 | char *ptr = "#failed\n"; | |
7242 | int len = strlen (ptr); | |
7243 | ||
7244 | if (op->bufp > op->buf && op->bufp[-1] != '\n') | |
7245 | { | |
7246 | *op->bufp++ = '\n'; | |
7247 | op->lineno++; | |
7248 | } | |
7249 | check_expand (op, len); | |
7250 | bcopy (ptr, (char *) op->bufp, len); | |
7251 | op->bufp += len; | |
7252 | op->lineno++; | |
adcfa681 | 7253 | output_line_directive (ip, op, 1, 0); |
bbd4b75b | 7254 | } |
b0bbbd85 RS |
7255 | |
7256 | while (bp < endb) { | |
7257 | switch (*bp++) { | |
7258 | case '/': /* possible comment */ | |
7259 | if (*bp == '\\' && bp[1] == '\n') | |
7260 | newline_fix (bp); | |
7261 | if (*bp == '*' | |
eda5fa7b | 7262 | || (cplusplus_comments && *bp == '/')) { |
b0bbbd85 | 7263 | ip->bufp = ++bp; |
2aa7ec37 | 7264 | bp = skip_to_end_of_comment (ip, &ip->lineno, 0); |
b0bbbd85 RS |
7265 | } |
7266 | break; | |
7267 | case '\"': | |
7268 | case '\'': | |
8d9bfdc5 RK |
7269 | bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, |
7270 | NULL_PTR, NULL_PTR); | |
b0bbbd85 RS |
7271 | break; |
7272 | case '\\': | |
7273 | /* Char after backslash loses its special meaning. */ | |
7274 | if (bp < endb) { | |
7275 | if (*bp == '\n') | |
7276 | ++ip->lineno; /* But do update the line-count. */ | |
7277 | bp++; | |
7278 | } | |
7279 | break; | |
7280 | case '\n': | |
7281 | ++ip->lineno; | |
7282 | beg_of_line = bp; | |
7283 | break; | |
91dbf5e7 RK |
7284 | case '%': |
7285 | if (beg_of_line == 0 || traditional) | |
7286 | break; | |
b0bbbd85 | 7287 | ip->bufp = bp - 1; |
91dbf5e7 RK |
7288 | while (bp[0] == '\\' && bp[1] == '\n') |
7289 | bp += 2; | |
7290 | if (*bp == ':') | |
5bdc1512 | 7291 | goto sharp_token; |
91dbf5e7 RK |
7292 | break; |
7293 | case '#': | |
b0bbbd85 RS |
7294 | /* # keyword: a # must be first nonblank char on the line */ |
7295 | if (beg_of_line == 0) | |
7296 | break; | |
91dbf5e7 | 7297 | ip->bufp = bp - 1; |
5bdc1512 | 7298 | sharp_token: |
b0bbbd85 RS |
7299 | /* Scan from start of line, skipping whitespace, comments |
7300 | and backslash-newlines, and see if we reach this #. | |
7301 | If not, this # is not special. */ | |
7302 | bp = beg_of_line; | |
78af79ab | 7303 | /* If -traditional, require # to be at beginning of line. */ |
91dbf5e7 | 7304 | if (!traditional) { |
78af79ab RK |
7305 | while (1) { |
7306 | if (is_hor_space[*bp]) | |
b0bbbd85 | 7307 | bp++; |
78af79ab RK |
7308 | else if (*bp == '\\' && bp[1] == '\n') |
7309 | bp += 2; | |
7310 | else if (*bp == '/' && bp[1] == '*') { | |
7311 | bp += 2; | |
7312 | while (!(*bp == '*' && bp[1] == '/')) | |
7313 | bp++; | |
7314 | bp += 2; | |
78af79ab | 7315 | } |
80512db7 JW |
7316 | /* There is no point in trying to deal with C++ // comments here, |
7317 | because if there is one, then this # must be part of the | |
7318 | comment and we would never reach here. */ | |
78af79ab RK |
7319 | else break; |
7320 | } | |
91dbf5e7 | 7321 | } |
b0bbbd85 RS |
7322 | if (bp != ip->bufp) { |
7323 | bp = ip->bufp + 1; /* Reset bp to after the #. */ | |
7324 | break; | |
7325 | } | |
7326 | ||
7327 | bp = ip->bufp + 1; /* Point after the '#' */ | |
91dbf5e7 RK |
7328 | if (ip->bufp[0] == '%') { |
7329 | /* Skip past the ':' again. */ | |
7330 | while (*bp == '\\') { | |
7331 | ip->lineno++; | |
7332 | bp += 2; | |
7333 | } | |
7334 | bp++; | |
7335 | } | |
b0bbbd85 RS |
7336 | |
7337 | /* Skip whitespace and \-newline. */ | |
7338 | while (1) { | |
7339 | if (is_hor_space[*bp]) | |
7340 | bp++; | |
7341 | else if (*bp == '\\' && bp[1] == '\n') | |
7342 | bp += 2; | |
7343 | else if (*bp == '/' && bp[1] == '*') { | |
7344 | bp += 2; | |
7345 | while (!(*bp == '*' && bp[1] == '/')) { | |
7346 | if (*bp == '\n') | |
7347 | ip->lineno++; | |
7348 | bp++; | |
7349 | } | |
7350 | bp += 2; | |
eda5fa7b | 7351 | } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') { |
b0bbbd85 | 7352 | bp += 2; |
80512db7 JW |
7353 | while (bp[-1] == '\\' || *bp != '\n') { |
7354 | if (*bp == '\n') | |
7355 | ip->lineno++; | |
7356 | bp++; | |
7357 | } | |
b0bbbd85 RS |
7358 | } |
7359 | else break; | |
7360 | } | |
7361 | ||
7362 | cp = bp; | |
7363 | ||
7364 | /* Now find end of directive name. | |
7365 | If we encounter a backslash-newline, exchange it with any following | |
7366 | symbol-constituents so that we end up with a contiguous name. */ | |
7367 | ||
7368 | while (1) { | |
7369 | if (is_idchar[*bp]) | |
7370 | bp++; | |
7371 | else { | |
7372 | if (*bp == '\\' && bp[1] == '\n') | |
7373 | name_newline_fix (bp); | |
7374 | if (is_idchar[*bp]) | |
7375 | bp++; | |
7376 | else break; | |
7377 | } | |
7378 | } | |
7379 | ident_length = bp - cp; | |
7380 | ident = cp; | |
7381 | after_ident = bp; | |
7382 | ||
7383 | /* A line of just `#' becomes blank. */ | |
7384 | ||
7385 | if (ident_length == 0 && *after_ident == '\n') { | |
7386 | continue; | |
7387 | } | |
7388 | ||
7389 | if (ident_length == 0 || !is_idstart[*ident]) { | |
7390 | U_CHAR *p = ident; | |
7391 | while (is_idchar[*p]) { | |
7392 | if (*p < '0' || *p > '9') | |
7393 | break; | |
7394 | p++; | |
7395 | } | |
7396 | /* Handle # followed by a line number. */ | |
7397 | if (p != ident && !is_idchar[*p]) { | |
7398 | if (pedantic) | |
7399 | pedwarn ("`#' followed by integer"); | |
7400 | continue; | |
7401 | } | |
7402 | ||
7403 | /* Avoid error for `###' and similar cases unless -pedantic. */ | |
7404 | if (p == ident) { | |
7405 | while (*p == '#' || is_hor_space[*p]) p++; | |
7406 | if (*p == '\n') { | |
7407 | if (pedantic && !lang_asm) | |
adcfa681 | 7408 | pedwarn ("invalid preprocessing directive"); |
b0bbbd85 RS |
7409 | continue; |
7410 | } | |
7411 | } | |
7412 | ||
7413 | if (!lang_asm && pedantic) | |
adcfa681 | 7414 | pedwarn ("invalid preprocessing directive name"); |
b0bbbd85 RS |
7415 | continue; |
7416 | } | |
7417 | ||
7418 | for (kt = directive_table; kt->length >= 0; kt++) { | |
7419 | IF_STACK_FRAME *temp; | |
7420 | if (ident_length == kt->length | |
25cbb59e | 7421 | && bcmp (cp, kt->name, kt->length) == 0) { |
b0bbbd85 RS |
7422 | /* If we are asked to return on next directive, do so now. */ |
7423 | if (any) | |
bbd4b75b | 7424 | goto done; |
b0bbbd85 RS |
7425 | |
7426 | switch (kt->type) { | |
7427 | case T_IF: | |
7428 | case T_IFDEF: | |
7429 | case T_IFNDEF: | |
7430 | temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
7431 | temp->next = if_stack; | |
7432 | if_stack = temp; | |
7433 | temp->lineno = ip->lineno; | |
7434 | temp->fname = ip->nominal_fname; | |
7435 | temp->type = kt->type; | |
7436 | break; | |
7437 | case T_ELSE: | |
7438 | case T_ENDIF: | |
7439 | if (pedantic && if_stack != save_if_stack) | |
7440 | validate_else (bp); | |
7441 | case T_ELIF: | |
7442 | if (if_stack == instack[indepth].if_stack) { | |
7443 | error ("`#%s' not within a conditional", kt->name); | |
7444 | break; | |
7445 | } | |
7446 | else if (if_stack == save_if_stack) | |
bbd4b75b | 7447 | goto done; /* found what we came for */ |
b0bbbd85 RS |
7448 | |
7449 | if (kt->type != T_ENDIF) { | |
7450 | if (if_stack->type == T_ELSE) | |
7451 | error ("`#else' or `#elif' after `#else'"); | |
7452 | if_stack->type = kt->type; | |
7453 | break; | |
7454 | } | |
7455 | ||
7456 | temp = if_stack; | |
7457 | if_stack = if_stack->next; | |
7458 | free (temp); | |
7459 | break; | |
25cbb59e RK |
7460 | |
7461 | default: | |
7462 | break; | |
b0bbbd85 RS |
7463 | } |
7464 | break; | |
7465 | } | |
7466 | } | |
7467 | /* Don't let erroneous code go by. */ | |
7468 | if (kt->length < 0 && !lang_asm && pedantic) | |
adcfa681 | 7469 | pedwarn ("invalid preprocessing directive name"); |
b0bbbd85 RS |
7470 | } |
7471 | } | |
bbd4b75b | 7472 | |
b0bbbd85 RS |
7473 | ip->bufp = bp; |
7474 | /* after this returns, rescan will exit because ip->bufp | |
7475 | now points to the end of the buffer. | |
7476 | rescan is responsible for the error message also. */ | |
bbd4b75b RS |
7477 | |
7478 | done: | |
7479 | if (output_conditionals && op != 0) { | |
7480 | char *ptr = "#endfailed\n"; | |
7481 | int len = strlen (ptr); | |
7482 | ||
7483 | if (op->bufp > op->buf && op->bufp[-1] != '\n') | |
7484 | { | |
7485 | *op->bufp++ = '\n'; | |
7486 | op->lineno++; | |
7487 | } | |
7488 | check_expand (op, beg_of_line - beg_of_group); | |
7489 | bcopy ((char *) beg_of_group, (char *) op->bufp, | |
7490 | beg_of_line - beg_of_group); | |
7491 | op->bufp += beg_of_line - beg_of_group; | |
7492 | op->lineno += ip->lineno - beg_lineno; | |
7493 | check_expand (op, len); | |
7494 | bcopy (ptr, (char *) op->bufp, len); | |
7495 | op->bufp += len; | |
7496 | op->lineno++; | |
7497 | } | |
b0bbbd85 RS |
7498 | } |
7499 | ||
7500 | /* | |
7501 | * handle a #else directive. Do this by just continuing processing | |
7502 | * without changing if_stack ; this is so that the error message | |
7503 | * for missing #endif's etc. will point to the original #if. It | |
7504 | * is possible that something different would be better. | |
7505 | */ | |
7506 | ||
7507 | static int | |
7508 | do_else (buf, limit, op, keyword) | |
7509 | U_CHAR *buf, *limit; | |
7510 | FILE_BUF *op; | |
7511 | struct directive *keyword; | |
7512 | { | |
7513 | FILE_BUF *ip = &instack[indepth]; | |
7514 | ||
7515 | if (pedantic) { | |
7516 | SKIP_WHITE_SPACE (buf); | |
7517 | if (buf != limit) | |
7518 | pedwarn ("text following `#else' violates ANSI standard"); | |
7519 | } | |
7520 | ||
7521 | if (if_stack == instack[indepth].if_stack) { | |
7522 | error ("`#else' not within a conditional"); | |
7523 | return 0; | |
7524 | } else { | |
7525 | /* #ifndef can't have its special treatment for containing the whole file | |
7526 | if it has a #else clause. */ | |
7527 | if_stack->control_macro = 0; | |
7528 | ||
7529 | if (if_stack->type != T_IF && if_stack->type != T_ELIF) { | |
7530 | error ("`#else' after `#else'"); | |
7531 | fprintf (stderr, " (matches line %d", if_stack->lineno); | |
7532 | if (strcmp (if_stack->fname, ip->nominal_fname) != 0) | |
7533 | fprintf (stderr, ", file %s", if_stack->fname); | |
7534 | fprintf (stderr, ")\n"); | |
7535 | } | |
7536 | if_stack->type = T_ELSE; | |
7537 | } | |
7538 | ||
7539 | if (if_stack->if_succeeded) | |
bbd4b75b | 7540 | skip_if_group (ip, 0, op); |
b0bbbd85 RS |
7541 | else { |
7542 | ++if_stack->if_succeeded; /* continue processing input */ | |
adcfa681 | 7543 | output_line_directive (ip, op, 1, same_file); |
b0bbbd85 RS |
7544 | } |
7545 | return 0; | |
7546 | } | |
7547 | ||
7548 | /* | |
adcfa681 | 7549 | * unstack after #endif directive |
b0bbbd85 RS |
7550 | */ |
7551 | ||
7552 | static int | |
7553 | do_endif (buf, limit, op, keyword) | |
7554 | U_CHAR *buf, *limit; | |
7555 | FILE_BUF *op; | |
7556 | struct directive *keyword; | |
7557 | { | |
7558 | if (pedantic) { | |
7559 | SKIP_WHITE_SPACE (buf); | |
7560 | if (buf != limit) | |
7561 | pedwarn ("text following `#endif' violates ANSI standard"); | |
7562 | } | |
7563 | ||
7564 | if (if_stack == instack[indepth].if_stack) | |
7565 | error ("unbalanced `#endif'"); | |
7566 | else { | |
7567 | IF_STACK_FRAME *temp = if_stack; | |
7568 | if_stack = if_stack->next; | |
7569 | if (temp->control_macro != 0) { | |
7570 | /* This #endif matched a #ifndef at the start of the file. | |
7571 | See if it is at the end of the file. */ | |
7572 | FILE_BUF *ip = &instack[indepth]; | |
7573 | U_CHAR *p = ip->bufp; | |
7574 | U_CHAR *ep = ip->buf + ip->length; | |
7575 | ||
7576 | while (p != ep) { | |
7577 | U_CHAR c = *p++; | |
c03413c7 | 7578 | if (!is_space[c]) { |
dec5c86b | 7579 | if (c == '/' |
23f15b0c | 7580 | && (*p == '*' || (cplusplus_comments && *p == '/'))) { |
b0bbbd85 | 7581 | /* Skip this comment. */ |
2af5e9e2 | 7582 | int junk = 0; |
b0bbbd85 RS |
7583 | U_CHAR *save_bufp = ip->bufp; |
7584 | ip->bufp = p + 1; | |
2aa7ec37 | 7585 | p = skip_to_end_of_comment (ip, &junk, 1); |
b0bbbd85 | 7586 | ip->bufp = save_bufp; |
c03413c7 RK |
7587 | } else |
7588 | goto fail; | |
b0bbbd85 RS |
7589 | } |
7590 | } | |
7591 | /* If we get here, this #endif ends a #ifndef | |
7592 | that contains all of the file (aside from whitespace). | |
7593 | Arrange not to include the file again | |
42e2194b RK |
7594 | if the macro that was tested is defined. |
7595 | ||
7596 | Do not do this for the top-level file in a -include or any | |
7597 | file in a -imacros. */ | |
7598 | if (indepth != 0 | |
7599 | && ! (indepth == 1 && no_record_file) | |
7600 | && ! (no_record_file && no_output)) | |
b0bbbd85 RS |
7601 | record_control_macro (ip->fname, temp->control_macro); |
7602 | fail: ; | |
7603 | } | |
7604 | free (temp); | |
adcfa681 | 7605 | output_line_directive (&instack[indepth], op, 1, same_file); |
b0bbbd85 RS |
7606 | } |
7607 | return 0; | |
7608 | } | |
7609 | ||
7610 | /* When an #else or #endif is found while skipping failed conditional, | |
7611 | if -pedantic was specified, this is called to warn about text after | |
adcfa681 | 7612 | the directive name. P points to the first char after the directive name. */ |
b0bbbd85 RS |
7613 | |
7614 | static void | |
7615 | validate_else (p) | |
7616 | register U_CHAR *p; | |
7617 | { | |
7618 | /* Advance P over whitespace and comments. */ | |
7619 | while (1) { | |
7620 | if (*p == '\\' && p[1] == '\n') | |
7621 | p += 2; | |
7622 | if (is_hor_space[*p]) | |
7623 | p++; | |
7624 | else if (*p == '/') { | |
7625 | if (p[1] == '\\' && p[2] == '\n') | |
7626 | newline_fix (p + 1); | |
7627 | if (p[1] == '*') { | |
7628 | p += 2; | |
7629 | /* Don't bother warning about unterminated comments | |
7630 | since that will happen later. Just be sure to exit. */ | |
7631 | while (*p) { | |
7632 | if (p[1] == '\\' && p[2] == '\n') | |
7633 | newline_fix (p + 1); | |
7634 | if (*p == '*' && p[1] == '/') { | |
7635 | p += 2; | |
7636 | break; | |
7637 | } | |
7638 | p++; | |
7639 | } | |
7640 | } | |
eda5fa7b | 7641 | else if (cplusplus_comments && p[1] == '/') { |
b0bbbd85 | 7642 | p += 2; |
80512db7 JW |
7643 | while (*p && (*p != '\n' || p[-1] == '\\')) |
7644 | p++; | |
b0bbbd85 RS |
7645 | } |
7646 | } else break; | |
7647 | } | |
7648 | if (*p && *p != '\n') | |
7649 | pedwarn ("text following `#else' or `#endif' violates ANSI standard"); | |
7650 | } | |
7651 | \f | |
2aa7ec37 RS |
7652 | /* Skip a comment, assuming the input ptr immediately follows the |
7653 | initial slash-star. Bump *LINE_COUNTER for each newline. | |
7654 | (The canonical line counter is &ip->lineno.) | |
7655 | Don't use this routine (or the next one) if bumping the line | |
7656 | counter is not sufficient to deal with newlines in the string. | |
7657 | ||
7658 | If NOWARN is nonzero, don't warn about slash-star inside a comment. | |
7659 | This feature is useful when processing a comment that is going to be | |
7660 | processed or was processed at another point in the preprocessor, | |
e24d9a31 DE |
7661 | to avoid a duplicate warning. Likewise for unterminated comment errors. */ |
7662 | ||
b0bbbd85 | 7663 | static U_CHAR * |
2aa7ec37 | 7664 | skip_to_end_of_comment (ip, line_counter, nowarn) |
b0bbbd85 RS |
7665 | register FILE_BUF *ip; |
7666 | int *line_counter; /* place to remember newlines, or NULL */ | |
2aa7ec37 | 7667 | int nowarn; |
b0bbbd85 RS |
7668 | { |
7669 | register U_CHAR *limit = ip->buf + ip->length; | |
7670 | register U_CHAR *bp = ip->bufp; | |
7671 | FILE_BUF *op = &outbuf; /* JF */ | |
7672 | int output = put_out_comments && !line_counter; | |
e24d9a31 | 7673 | int start_line = line_counter ? *line_counter : 0; |
b0bbbd85 RS |
7674 | |
7675 | /* JF this line_counter stuff is a crock to make sure the | |
7676 | comment is only put out once, no matter how many times | |
7677 | the comment is skipped. It almost works */ | |
7678 | if (output) { | |
7679 | *op->bufp++ = '/'; | |
7680 | *op->bufp++ = '*'; | |
7681 | } | |
eda5fa7b | 7682 | if (cplusplus_comments && bp[-1] == '/') { |
b0bbbd85 | 7683 | if (output) { |
80512db7 JW |
7684 | while (bp < limit) { |
7685 | *op->bufp++ = *bp; | |
7686 | if (*bp == '\n' && bp[-1] != '\\') | |
b0bbbd85 | 7687 | break; |
80512db7 JW |
7688 | if (*bp == '\n') { |
7689 | ++*line_counter; | |
7690 | ++op->lineno; | |
b0bbbd85 | 7691 | } |
80512db7 JW |
7692 | bp++; |
7693 | } | |
b0bbbd85 RS |
7694 | op->bufp[-1] = '*'; |
7695 | *op->bufp++ = '/'; | |
7696 | *op->bufp++ = '\n'; | |
7697 | } else { | |
7698 | while (bp < limit) { | |
80512db7 | 7699 | if (bp[-1] != '\\' && *bp == '\n') { |
b0bbbd85 | 7700 | break; |
80512db7 JW |
7701 | } else { |
7702 | if (*bp == '\n' && line_counter) | |
7703 | ++*line_counter; | |
7704 | bp++; | |
b0bbbd85 RS |
7705 | } |
7706 | } | |
7707 | } | |
7708 | ip->bufp = bp; | |
7709 | return bp; | |
7710 | } | |
7711 | while (bp < limit) { | |
7712 | if (output) | |
7713 | *op->bufp++ = *bp; | |
7714 | switch (*bp++) { | |
7715 | case '/': | |
2aa7ec37 | 7716 | if (warn_comments && !nowarn && bp < limit && *bp == '*') |
b0bbbd85 RS |
7717 | warning ("`/*' within comment"); |
7718 | break; | |
7719 | case '\n': | |
e24d9a31 DE |
7720 | /* If this is the end of the file, we have an unterminated comment. |
7721 | Don't swallow the newline. We are guaranteed that there will be a | |
7722 | trailing newline and various pieces assume it's there. */ | |
7723 | if (bp == limit) | |
7724 | { | |
7725 | --bp; | |
7726 | --limit; | |
7727 | break; | |
7728 | } | |
b0bbbd85 RS |
7729 | if (line_counter != NULL) |
7730 | ++*line_counter; | |
7731 | if (output) | |
7732 | ++op->lineno; | |
7733 | break; | |
7734 | case '*': | |
7735 | if (*bp == '\\' && bp[1] == '\n') | |
7736 | newline_fix (bp); | |
7737 | if (*bp == '/') { | |
7738 | if (output) | |
7739 | *op->bufp++ = '/'; | |
7740 | ip->bufp = ++bp; | |
7741 | return bp; | |
7742 | } | |
7743 | break; | |
7744 | } | |
7745 | } | |
e24d9a31 DE |
7746 | |
7747 | if (!nowarn) | |
7748 | error_with_line (line_for_error (start_line), "unterminated comment"); | |
b0bbbd85 RS |
7749 | ip->bufp = bp; |
7750 | return bp; | |
7751 | } | |
7752 | ||
7753 | /* | |
7754 | * Skip over a quoted string. BP points to the opening quote. | |
7755 | * Returns a pointer after the closing quote. Don't go past LIMIT. | |
7756 | * START_LINE is the line number of the starting point (but it need | |
7757 | * not be valid if the starting point is inside a macro expansion). | |
7758 | * | |
7759 | * The input stack state is not changed. | |
7760 | * | |
7761 | * If COUNT_NEWLINES is nonzero, it points to an int to increment | |
7762 | * for each newline passed. | |
7763 | * | |
7764 | * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it | |
7765 | * if we pass a backslash-newline. | |
7766 | * | |
7767 | * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. | |
7768 | */ | |
7769 | static U_CHAR * | |
7770 | skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp) | |
7771 | register U_CHAR *bp; | |
7772 | register U_CHAR *limit; | |
7773 | int start_line; | |
7774 | int *count_newlines; | |
7775 | int *backslash_newlines_p; | |
7776 | int *eofp; | |
7777 | { | |
7778 | register U_CHAR c, match; | |
7779 | ||
7780 | match = *bp++; | |
7781 | while (1) { | |
7782 | if (bp >= limit) { | |
7783 | error_with_line (line_for_error (start_line), | |
7784 | "unterminated string or character constant"); | |
5b01bc66 JW |
7785 | error_with_line (multiline_string_line, |
7786 | "possible real start of unterminated constant"); | |
7787 | multiline_string_line = 0; | |
b0bbbd85 RS |
7788 | if (eofp) |
7789 | *eofp = 1; | |
7790 | break; | |
7791 | } | |
7792 | c = *bp++; | |
7793 | if (c == '\\') { | |
7794 | while (*bp == '\\' && bp[1] == '\n') { | |
7795 | if (backslash_newlines_p) | |
7796 | *backslash_newlines_p = 1; | |
7797 | if (count_newlines) | |
7798 | ++*count_newlines; | |
7799 | bp += 2; | |
7800 | } | |
7801 | if (*bp == '\n' && count_newlines) { | |
7802 | if (backslash_newlines_p) | |
7803 | *backslash_newlines_p = 1; | |
7804 | ++*count_newlines; | |
7805 | } | |
7806 | bp++; | |
7807 | } else if (c == '\n') { | |
7808 | if (traditional) { | |
3826a3da | 7809 | /* Unterminated strings and character constants are 'valid'. */ |
b0bbbd85 RS |
7810 | bp--; /* Don't consume the newline. */ |
7811 | if (eofp) | |
7812 | *eofp = 1; | |
7813 | break; | |
7814 | } | |
5b01bc66 | 7815 | if (pedantic || match == '\'') { |
b0bbbd85 | 7816 | error_with_line (line_for_error (start_line), |
5b01bc66 | 7817 | "unterminated string or character constant"); |
b0bbbd85 RS |
7818 | bp--; |
7819 | if (eofp) | |
7820 | *eofp = 1; | |
7821 | break; | |
7822 | } | |
b0bbbd85 RS |
7823 | /* If not traditional, then allow newlines inside strings. */ |
7824 | if (count_newlines) | |
7825 | ++*count_newlines; | |
5b01bc66 JW |
7826 | if (multiline_string_line == 0) |
7827 | multiline_string_line = start_line; | |
b0bbbd85 RS |
7828 | } else if (c == match) |
7829 | break; | |
7830 | } | |
7831 | return bp; | |
7832 | } | |
7833 | ||
f7531123 PE |
7834 | /* Place into DST a quoted string representing the string SRC. |
7835 | Return the address of DST's terminating null. */ | |
7836 | static char * | |
bb2f42b1 PE |
7837 | quote_string (dst, src) |
7838 | char *dst, *src; | |
7839 | { | |
f7531123 | 7840 | U_CHAR c; |
bb2f42b1 | 7841 | |
f7531123 PE |
7842 | *dst++ = '\"'; |
7843 | for (;;) | |
bb2f42b1 PE |
7844 | switch ((c = *src++)) |
7845 | { | |
f7531123 PE |
7846 | default: |
7847 | if (isprint (c)) | |
7848 | *dst++ = c; | |
7849 | else | |
7850 | { | |
7851 | sprintf (dst, "\\%03o", c); | |
7852 | dst += 4; | |
7853 | } | |
7854 | break; | |
7855 | ||
bb2f42b1 PE |
7856 | case '\"': |
7857 | case '\\': | |
7858 | *dst++ = '\\'; | |
f7531123 | 7859 | *dst++ = c; |
bb2f42b1 PE |
7860 | break; |
7861 | ||
7862 | case '\0': | |
7863 | *dst++ = '\"'; | |
7864 | *dst = '\0'; | |
f7531123 | 7865 | return dst; |
bb2f42b1 PE |
7866 | } |
7867 | } | |
7868 | ||
b0bbbd85 RS |
7869 | /* Skip across a group of balanced parens, starting from IP->bufp. |
7870 | IP->bufp is updated. Use this with IP->bufp pointing at an open-paren. | |
7871 | ||
7872 | This does not handle newlines, because it's used for the arg of #if, | |
2aa7ec37 | 7873 | where there aren't any newlines. Also, backslash-newline can't appear. */ |
b0bbbd85 RS |
7874 | |
7875 | static U_CHAR * | |
7876 | skip_paren_group (ip) | |
7877 | register FILE_BUF *ip; | |
7878 | { | |
7879 | U_CHAR *limit = ip->buf + ip->length; | |
7880 | U_CHAR *p = ip->bufp; | |
7881 | int depth = 0; | |
7882 | int lines_dummy = 0; | |
7883 | ||
7884 | while (p != limit) { | |
7885 | int c = *p++; | |
7886 | switch (c) { | |
7887 | case '(': | |
7888 | depth++; | |
7889 | break; | |
7890 | ||
7891 | case ')': | |
7892 | depth--; | |
7893 | if (depth == 0) | |
7894 | return ip->bufp = p; | |
7895 | break; | |
7896 | ||
7897 | case '/': | |
7898 | if (*p == '*') { | |
7899 | ip->bufp = p; | |
2aa7ec37 | 7900 | p = skip_to_end_of_comment (ip, &lines_dummy, 0); |
b0bbbd85 RS |
7901 | p = ip->bufp; |
7902 | } | |
7903 | ||
7904 | case '"': | |
7905 | case '\'': | |
7906 | { | |
7907 | int eofp = 0; | |
8d9bfdc5 | 7908 | p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp); |
b0bbbd85 RS |
7909 | if (eofp) |
7910 | return ip->bufp = p; | |
7911 | } | |
7912 | break; | |
7913 | } | |
7914 | } | |
7915 | ||
7916 | ip->bufp = p; | |
7917 | return p; | |
7918 | } | |
7919 | \f | |
7920 | /* | |
adcfa681 | 7921 | * write out a #line directive, for instance, after an #include file. |
b0bbbd85 RS |
7922 | * If CONDITIONAL is nonzero, we can omit the #line if it would |
7923 | * appear to be a no-op, and we can output a few newlines instead | |
7924 | * if we want to increase the line number by a small amount. | |
7925 | * FILE_CHANGE says whether we are entering a file, leaving, or neither. | |
7926 | */ | |
7927 | ||
7928 | static void | |
adcfa681 | 7929 | output_line_directive (ip, op, conditional, file_change) |
b0bbbd85 RS |
7930 | FILE_BUF *ip, *op; |
7931 | int conditional; | |
7932 | enum file_change_code file_change; | |
7933 | { | |
7934 | int len; | |
adcfa681 | 7935 | char *line_directive_buf, *line_end; |
b0bbbd85 | 7936 | |
adcfa681 | 7937 | if (no_line_directives |
b0bbbd85 RS |
7938 | || ip->fname == NULL |
7939 | || no_output) { | |
7940 | op->lineno = ip->lineno; | |
7941 | return; | |
7942 | } | |
7943 | ||
7944 | if (conditional) { | |
7945 | if (ip->lineno == op->lineno) | |
7946 | return; | |
7947 | ||
7948 | /* If the inherited line number is a little too small, | |
adcfa681 | 7949 | output some newlines instead of a #line directive. */ |
b0bbbd85 RS |
7950 | if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) { |
7951 | check_expand (op, 10); | |
7952 | while (ip->lineno > op->lineno) { | |
7953 | *op->bufp++ = '\n'; | |
7954 | op->lineno++; | |
7955 | } | |
7956 | return; | |
7957 | } | |
7958 | } | |
7959 | ||
2aa7ec37 RS |
7960 | /* Don't output a line number of 0 if we can help it. */ |
7961 | if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length | |
7962 | && *ip->bufp == '\n') { | |
7963 | ip->lineno++; | |
7964 | ip->bufp++; | |
7965 | } | |
7966 | ||
adcfa681 RK |
7967 | line_directive_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100); |
7968 | sprintf (line_directive_buf, "# %d ", ip->lineno); | |
7969 | line_end = quote_string (line_directive_buf + strlen (line_directive_buf), | |
f7531123 PE |
7970 | ip->nominal_fname); |
7971 | if (file_change != same_file) { | |
7972 | *line_end++ = ' '; | |
7973 | *line_end++ = file_change == enter_file ? '1' : '2'; | |
7974 | } | |
b0bbbd85 | 7975 | /* Tell cc1 if following text comes from a system header file. */ |
f7531123 PE |
7976 | if (ip->system_header_p) { |
7977 | *line_end++ = ' '; | |
7978 | *line_end++ = '3'; | |
7979 | } | |
0031ac57 | 7980 | #ifndef NO_IMPLICIT_EXTERN_C |
d2a22862 | 7981 | /* Tell cc1plus if following text should be treated as C. */ |
65715dea | 7982 | if (ip->system_header_p == 2 && cplusplus) { |
d2a22862 RS |
7983 | *line_end++ = ' '; |
7984 | *line_end++ = '4'; | |
7985 | } | |
0031ac57 | 7986 | #endif |
f7531123 | 7987 | *line_end++ = '\n'; |
adcfa681 | 7988 | len = line_end - line_directive_buf; |
b0bbbd85 RS |
7989 | check_expand (op, len + 1); |
7990 | if (op->bufp > op->buf && op->bufp[-1] != '\n') | |
7991 | *op->bufp++ = '\n'; | |
adcfa681 | 7992 | bcopy ((char *) line_directive_buf, (char *) op->bufp, len); |
b0bbbd85 RS |
7993 | op->bufp += len; |
7994 | op->lineno = ip->lineno; | |
7995 | } | |
7996 | \f | |
7997 | /* This structure represents one parsed argument in a macro call. | |
7998 | `raw' points to the argument text as written (`raw_length' is its length). | |
7999 | `expanded' points to the argument's macro-expansion | |
8000 | (its length is `expand_length'). | |
8001 | `stringified_length' is the length the argument would have | |
8002 | if stringified. | |
8003 | `use_count' is the number of times this macro arg is substituted | |
8004 | into the macro. If the actual use count exceeds 10, | |
8005 | the value stored is 10. | |
8006 | `free1' and `free2', if nonzero, point to blocks to be freed | |
8007 | when the macro argument data is no longer needed. */ | |
8008 | ||
8009 | struct argdata { | |
8010 | U_CHAR *raw, *expanded; | |
8011 | int raw_length, expand_length; | |
8012 | int stringified_length; | |
8013 | U_CHAR *free1, *free2; | |
8014 | char newlines; | |
8015 | char comments; | |
8016 | char use_count; | |
8017 | }; | |
8018 | ||
8019 | /* Expand a macro call. | |
8020 | HP points to the symbol that is the macro being called. | |
8021 | Put the result of expansion onto the input stack | |
8022 | so that subsequent input by our caller will use it. | |
8023 | ||
8024 | If macro wants arguments, caller has already verified that | |
8025 | an argument list follows; arguments come from the input stack. */ | |
8026 | ||
8027 | static void | |
8028 | macroexpand (hp, op) | |
8029 | HASHNODE *hp; | |
8030 | FILE_BUF *op; | |
8031 | { | |
8032 | int nargs; | |
8033 | DEFINITION *defn = hp->value.defn; | |
8034 | register U_CHAR *xbuf; | |
8035 | int xbuf_len; | |
8036 | int start_line = instack[indepth].lineno; | |
5ff1a832 | 8037 | int rest_args, rest_zero; |
b0bbbd85 RS |
8038 | |
8039 | CHECK_DEPTH (return;); | |
8040 | ||
8041 | /* it might not actually be a macro. */ | |
8042 | if (hp->type != T_MACRO) { | |
8043 | special_symbol (hp, op); | |
8044 | return; | |
8045 | } | |
8046 | ||
8047 | /* This macro is being used inside a #if, which means it must be */ | |
8048 | /* recorded as a precondition. */ | |
8049 | if (pcp_inside_if && pcp_outfile && defn->predefined) | |
8050 | dump_single_macro (hp, pcp_outfile); | |
8051 | ||
8052 | nargs = defn->nargs; | |
8053 | ||
8054 | if (nargs >= 0) { | |
8055 | register int i; | |
8056 | struct argdata *args; | |
8057 | char *parse_error = 0; | |
8058 | ||
8059 | args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata)); | |
8060 | ||
8061 | for (i = 0; i < nargs; i++) { | |
2b1a049f RS |
8062 | args[i].raw = (U_CHAR *) ""; |
8063 | args[i].expanded = 0; | |
b0bbbd85 RS |
8064 | args[i].raw_length = args[i].expand_length |
8065 | = args[i].stringified_length = 0; | |
8066 | args[i].free1 = args[i].free2 = 0; | |
8067 | args[i].use_count = 0; | |
8068 | } | |
8069 | ||
8070 | /* Parse all the macro args that are supplied. I counts them. | |
8071 | The first NARGS args are stored in ARGS. | |
5ff1a832 RS |
8072 | The rest are discarded. |
8073 | If rest_args is set then we assume macarg absorbed the rest of the args. | |
8074 | */ | |
b0bbbd85 | 8075 | i = 0; |
5ff1a832 | 8076 | rest_args = 0; |
b0bbbd85 RS |
8077 | do { |
8078 | /* Discard the open-parenthesis or comma before the next arg. */ | |
8079 | ++instack[indepth].bufp; | |
5ff1a832 RS |
8080 | if (rest_args) |
8081 | continue; | |
8082 | if (i < nargs || (nargs == 0 && i == 0)) { | |
f72aed24 | 8083 | /* if we are working on last arg which absorbs rest of args... */ |
5ff1a832 RS |
8084 | if (i == nargs - 1 && defn->rest_args) |
8085 | rest_args = 1; | |
8086 | parse_error = macarg (&args[i], rest_args); | |
8087 | } | |
8088 | else | |
8d9bfdc5 | 8089 | parse_error = macarg (NULL_PTR, 0); |
b0bbbd85 RS |
8090 | if (parse_error) { |
8091 | error_with_line (line_for_error (start_line), parse_error); | |
8092 | break; | |
8093 | } | |
8094 | i++; | |
8095 | } while (*instack[indepth].bufp != ')'); | |
8096 | ||
8097 | /* If we got one arg but it was just whitespace, call that 0 args. */ | |
8098 | if (i == 1) { | |
8099 | register U_CHAR *bp = args[0].raw; | |
8100 | register U_CHAR *lim = bp + args[0].raw_length; | |
148597b9 RS |
8101 | /* cpp.texi says for foo ( ) we provide one argument. |
8102 | However, if foo wants just 0 arguments, treat this as 0. */ | |
8103 | if (nargs == 0) | |
8104 | while (bp != lim && is_space[*bp]) bp++; | |
b0bbbd85 RS |
8105 | if (bp == lim) |
8106 | i = 0; | |
8107 | } | |
8108 | ||
7dcdbecb JW |
8109 | /* Don't output an error message if we have already output one for |
8110 | a parse error above. */ | |
5ff1a832 | 8111 | rest_zero = 0; |
7dcdbecb JW |
8112 | if (nargs == 0 && i > 0) { |
8113 | if (! parse_error) | |
8114 | error ("arguments given to macro `%s'", hp->name); | |
8115 | } else if (i < nargs) { | |
b0bbbd85 RS |
8116 | /* traditional C allows foo() if foo wants one argument. */ |
8117 | if (nargs == 1 && i == 0 && traditional) | |
8118 | ; | |
5ff1a832 RS |
8119 | /* the rest args token is allowed to absorb 0 tokens */ |
8120 | else if (i == nargs - 1 && defn->rest_args) | |
8121 | rest_zero = 1; | |
7dcdbecb JW |
8122 | else if (parse_error) |
8123 | ; | |
b0bbbd85 RS |
8124 | else if (i == 0) |
8125 | error ("macro `%s' used without args", hp->name); | |
8126 | else if (i == 1) | |
8127 | error ("macro `%s' used with just one arg", hp->name); | |
8128 | else | |
8129 | error ("macro `%s' used with only %d args", hp->name, i); | |
7dcdbecb JW |
8130 | } else if (i > nargs) { |
8131 | if (! parse_error) | |
8132 | error ("macro `%s' used with too many (%d) args", hp->name, i); | |
8133 | } | |
b0bbbd85 RS |
8134 | |
8135 | /* Swallow the closeparen. */ | |
8136 | ++instack[indepth].bufp; | |
8137 | ||
8138 | /* If macro wants zero args, we parsed the arglist for checking only. | |
8139 | Read directly from the macro definition. */ | |
8140 | if (nargs == 0) { | |
8141 | xbuf = defn->expansion; | |
8142 | xbuf_len = defn->length; | |
8143 | } else { | |
8144 | register U_CHAR *exp = defn->expansion; | |
8145 | register int offset; /* offset in expansion, | |
8146 | copied a piece at a time */ | |
8147 | register int totlen; /* total amount of exp buffer filled so far */ | |
8148 | ||
5ff1a832 | 8149 | register struct reflist *ap, *last_ap; |
b0bbbd85 RS |
8150 | |
8151 | /* Macro really takes args. Compute the expansion of this call. */ | |
8152 | ||
8153 | /* Compute length in characters of the macro's expansion. | |
8154 | Also count number of times each arg is used. */ | |
8155 | xbuf_len = defn->length; | |
8156 | for (ap = defn->pattern; ap != NULL; ap = ap->next) { | |
8157 | if (ap->stringify) | |
8158 | xbuf_len += args[ap->argno].stringified_length; | |
8159 | else if (ap->raw_before || ap->raw_after || traditional) | |
d0691cfb RS |
8160 | /* Add 4 for two newline-space markers to prevent |
8161 | token concatenation. */ | |
8162 | xbuf_len += args[ap->argno].raw_length + 4; | |
2b1a049f RS |
8163 | else { |
8164 | /* We have an ordinary (expanded) occurrence of the arg. | |
8165 | So compute its expansion, if we have not already. */ | |
8166 | if (args[ap->argno].expanded == 0) { | |
8167 | FILE_BUF obuf; | |
8168 | obuf = expand_to_temp_buffer (args[ap->argno].raw, | |
8169 | args[ap->argno].raw + args[ap->argno].raw_length, | |
8170 | 1, 0); | |
8171 | ||
8172 | args[ap->argno].expanded = obuf.buf; | |
8173 | args[ap->argno].expand_length = obuf.length; | |
8174 | args[ap->argno].free2 = obuf.buf; | |
8175 | } | |
b0bbbd85 | 8176 | |
d0691cfb RS |
8177 | /* Add 4 for two newline-space markers to prevent |
8178 | token concatenation. */ | |
8179 | xbuf_len += args[ap->argno].expand_length + 4; | |
2b1a049f | 8180 | } |
b0bbbd85 RS |
8181 | if (args[ap->argno].use_count < 10) |
8182 | args[ap->argno].use_count++; | |
8183 | } | |
8184 | ||
8185 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
8186 | ||
8187 | /* Generate in XBUF the complete expansion | |
8188 | with arguments substituted in. | |
8189 | TOTLEN is the total size generated so far. | |
8190 | OFFSET is the index in the definition | |
8191 | of where we are copying from. */ | |
8192 | offset = totlen = 0; | |
5ff1a832 RS |
8193 | for (last_ap = NULL, ap = defn->pattern; ap != NULL; |
8194 | last_ap = ap, ap = ap->next) { | |
b0bbbd85 | 8195 | register struct argdata *arg = &args[ap->argno]; |
d0691cfb | 8196 | int count_before = totlen; |
b0bbbd85 | 8197 | |
d0691cfb | 8198 | /* Add chars to XBUF. */ |
5ff1a832 | 8199 | for (i = 0; i < ap->nchars; i++, offset++) |
d0691cfb RS |
8200 | xbuf[totlen++] = exp[offset]; |
8201 | ||
8202 | /* If followed by an empty rest arg with concatenation, | |
8203 | delete the last run of nonwhite chars. */ | |
8204 | if (rest_zero && totlen > count_before | |
8205 | && ((ap->rest_args && ap->raw_before) | |
8206 | || (last_ap != NULL && last_ap->rest_args | |
8207 | && last_ap->raw_after))) { | |
8208 | /* Delete final whitespace. */ | |
8209 | while (totlen > count_before && is_space[xbuf[totlen - 1]]) { | |
8210 | totlen--; | |
8211 | } | |
8212 | ||
8213 | /* Delete the nonwhites before them. */ | |
8214 | while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) { | |
8215 | totlen--; | |
8216 | } | |
8217 | } | |
b0bbbd85 RS |
8218 | |
8219 | if (ap->stringify != 0) { | |
8220 | int arglen = arg->raw_length; | |
8221 | int escaped = 0; | |
8222 | int in_string = 0; | |
8223 | int c; | |
8224 | i = 0; | |
8225 | while (i < arglen | |
8226 | && (c = arg->raw[i], is_space[c])) | |
8227 | i++; | |
8228 | while (i < arglen | |
8229 | && (c = arg->raw[arglen - 1], is_space[c])) | |
8230 | arglen--; | |
8231 | if (!traditional) | |
8232 | xbuf[totlen++] = '\"'; /* insert beginning quote */ | |
8233 | for (; i < arglen; i++) { | |
8234 | c = arg->raw[i]; | |
8235 | ||
8236 | /* Special markers Newline Space | |
8237 | generate nothing for a stringified argument. */ | |
8238 | if (c == '\n' && arg->raw[i+1] != '\n') { | |
8239 | i++; | |
8240 | continue; | |
8241 | } | |
8242 | ||
8243 | /* Internal sequences of whitespace are replaced by one space | |
8244 | except within an string or char token. */ | |
8245 | if (! in_string | |
8246 | && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) { | |
8247 | while (1) { | |
8248 | /* Note that Newline Space does occur within whitespace | |
8249 | sequences; consider it part of the sequence. */ | |
8250 | if (c == '\n' && is_space[arg->raw[i+1]]) | |
8251 | i += 2; | |
8252 | else if (c != '\n' && is_space[c]) | |
8253 | i++; | |
8254 | else break; | |
8255 | c = arg->raw[i]; | |
8256 | } | |
8257 | i--; | |
8258 | c = ' '; | |
8259 | } | |
8260 | ||
8261 | if (escaped) | |
8262 | escaped = 0; | |
8263 | else { | |
8264 | if (c == '\\') | |
8265 | escaped = 1; | |
8266 | if (in_string) { | |
8267 | if (c == in_string) | |
8268 | in_string = 0; | |
8269 | } else if (c == '\"' || c == '\'') | |
8270 | in_string = c; | |
8271 | } | |
8272 | ||
8273 | /* Escape these chars */ | |
8274 | if (c == '\"' || (in_string && c == '\\')) | |
8275 | xbuf[totlen++] = '\\'; | |
8276 | if (isprint (c)) | |
8277 | xbuf[totlen++] = c; | |
8278 | else { | |
8279 | sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c); | |
8280 | totlen += 4; | |
8281 | } | |
8282 | } | |
8283 | if (!traditional) | |
8284 | xbuf[totlen++] = '\"'; /* insert ending quote */ | |
8285 | } else if (ap->raw_before || ap->raw_after || traditional) { | |
8286 | U_CHAR *p1 = arg->raw; | |
8287 | U_CHAR *l1 = p1 + arg->raw_length; | |
8288 | if (ap->raw_before) { | |
8289 | while (p1 != l1 && is_space[*p1]) p1++; | |
8290 | while (p1 != l1 && is_idchar[*p1]) | |
8291 | xbuf[totlen++] = *p1++; | |
8292 | /* Delete any no-reexpansion marker that follows | |
8293 | an identifier at the beginning of the argument | |
8294 | if the argument is concatenated with what precedes it. */ | |
8295 | if (p1[0] == '\n' && p1[1] == '-') | |
8296 | p1 += 2; | |
d0691cfb RS |
8297 | } else if (!traditional) { |
8298 | /* Ordinary expanded use of the argument. | |
8299 | Put in newline-space markers to prevent token pasting. */ | |
8300 | xbuf[totlen++] = '\n'; | |
8301 | xbuf[totlen++] = ' '; | |
b0bbbd85 RS |
8302 | } |
8303 | if (ap->raw_after) { | |
8304 | /* Arg is concatenated after: delete trailing whitespace, | |
8305 | whitespace markers, and no-reexpansion markers. */ | |
8306 | while (p1 != l1) { | |
8307 | if (is_space[l1[-1]]) l1--; | |
8308 | else if (l1[-1] == '-') { | |
8309 | U_CHAR *p2 = l1 - 1; | |
8310 | /* If a `-' is preceded by an odd number of newlines then it | |
8311 | and the last newline are a no-reexpansion marker. */ | |
8312 | while (p2 != p1 && p2[-1] == '\n') p2--; | |
8313 | if ((l1 - 1 - p2) & 1) { | |
8314 | l1 -= 2; | |
8315 | } | |
8316 | else break; | |
8317 | } | |
8318 | else break; | |
8319 | } | |
8320 | } | |
d0691cfb | 8321 | |
4c9a05bc | 8322 | bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1); |
b0bbbd85 | 8323 | totlen += l1 - p1; |
d0691cfb RS |
8324 | if (!traditional && !ap->raw_after) { |
8325 | /* Ordinary expanded use of the argument. | |
8326 | Put in newline-space markers to prevent token pasting. */ | |
8327 | xbuf[totlen++] = '\n'; | |
8328 | xbuf[totlen++] = ' '; | |
8329 | } | |
b0bbbd85 | 8330 | } else { |
d0691cfb RS |
8331 | /* Ordinary expanded use of the argument. |
8332 | Put in newline-space markers to prevent token pasting. */ | |
8333 | if (!traditional) { | |
8334 | xbuf[totlen++] = '\n'; | |
8335 | xbuf[totlen++] = ' '; | |
8336 | } | |
4c9a05bc RK |
8337 | bcopy ((char *) arg->expanded, (char *) (xbuf + totlen), |
8338 | arg->expand_length); | |
b0bbbd85 | 8339 | totlen += arg->expand_length; |
d0691cfb RS |
8340 | if (!traditional) { |
8341 | xbuf[totlen++] = '\n'; | |
8342 | xbuf[totlen++] = ' '; | |
8343 | } | |
b0bbbd85 RS |
8344 | /* If a macro argument with newlines is used multiple times, |
8345 | then only expand the newlines once. This avoids creating output | |
8346 | lines which don't correspond to any input line, which confuses | |
8347 | gdb and gcov. */ | |
8348 | if (arg->use_count > 1 && arg->newlines > 0) { | |
de5f1a5a | 8349 | /* Don't bother doing change_newlines for subsequent |
b0bbbd85 RS |
8350 | uses of arg. */ |
8351 | arg->use_count = 1; | |
8352 | arg->expand_length | |
de5f1a5a | 8353 | = change_newlines (arg->expanded, arg->expand_length); |
b0bbbd85 RS |
8354 | } |
8355 | } | |
8356 | ||
8357 | if (totlen > xbuf_len) | |
8358 | abort (); | |
8359 | } | |
8360 | ||
8361 | /* if there is anything left of the definition | |
8362 | after handling the arg list, copy that in too. */ | |
8363 | ||
5ff1a832 RS |
8364 | for (i = offset; i < defn->length; i++) { |
8365 | /* if we've reached the end of the macro */ | |
8366 | if (exp[i] == ')') | |
8367 | rest_zero = 0; | |
8368 | if (! (rest_zero && last_ap != NULL && last_ap->rest_args | |
8369 | && last_ap->raw_after)) | |
8370 | xbuf[totlen++] = exp[i]; | |
8371 | } | |
b0bbbd85 RS |
8372 | |
8373 | xbuf[totlen] = 0; | |
8374 | xbuf_len = totlen; | |
8375 | ||
8376 | for (i = 0; i < nargs; i++) { | |
8377 | if (args[i].free1 != 0) | |
8378 | free (args[i].free1); | |
8379 | if (args[i].free2 != 0) | |
8380 | free (args[i].free2); | |
8381 | } | |
8382 | } | |
8383 | } else { | |
8384 | xbuf = defn->expansion; | |
8385 | xbuf_len = defn->length; | |
8386 | } | |
8387 | ||
8388 | /* Now put the expansion on the input stack | |
8389 | so our caller will commence reading from it. */ | |
8390 | { | |
8391 | register FILE_BUF *ip2; | |
8392 | ||
8393 | ip2 = &instack[++indepth]; | |
8394 | ||
8395 | ip2->fname = 0; | |
8396 | ip2->nominal_fname = 0; | |
3375e662 JW |
8397 | /* This may not be exactly correct, but will give much better error |
8398 | messages for nested macro calls than using a line number of zero. */ | |
8399 | ip2->lineno = start_line; | |
b0bbbd85 RS |
8400 | ip2->buf = xbuf; |
8401 | ip2->length = xbuf_len; | |
8402 | ip2->bufp = xbuf; | |
8403 | ip2->free_ptr = (nargs > 0) ? xbuf : 0; | |
8404 | ip2->macro = hp; | |
8405 | ip2->if_stack = if_stack; | |
8406 | ip2->system_header_p = 0; | |
8407 | ||
8408 | /* Recursive macro use sometimes works traditionally. | |
ad0c9fa1 RS |
8409 | #define foo(x,y) bar (x (y,0), y) |
8410 | foo (foo, baz) */ | |
b0bbbd85 RS |
8411 | |
8412 | if (!traditional) | |
8413 | hp->type = T_DISABLED; | |
8414 | } | |
8415 | } | |
8416 | \f | |
8417 | /* | |
8418 | * Parse a macro argument and store the info on it into *ARGPTR. | |
5ff1a832 | 8419 | * REST_ARGS is passed to macarg1 to make it absorb the rest of the args. |
b0bbbd85 RS |
8420 | * Return nonzero to indicate a syntax error. |
8421 | */ | |
8422 | ||
8423 | static char * | |
5ff1a832 | 8424 | macarg (argptr, rest_args) |
b0bbbd85 | 8425 | register struct argdata *argptr; |
5ff1a832 | 8426 | int rest_args; |
b0bbbd85 RS |
8427 | { |
8428 | FILE_BUF *ip = &instack[indepth]; | |
8429 | int paren = 0; | |
8430 | int newlines = 0; | |
8431 | int comments = 0; | |
8432 | ||
8433 | /* Try to parse as much of the argument as exists at this | |
8434 | input stack level. */ | |
8435 | U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, | |
5ff1a832 | 8436 | &paren, &newlines, &comments, rest_args); |
b0bbbd85 RS |
8437 | |
8438 | /* If we find the end of the argument at this level, | |
8439 | set up *ARGPTR to point at it in the input stack. */ | |
8440 | if (!(ip->fname != 0 && (newlines != 0 || comments != 0)) | |
8441 | && bp != ip->buf + ip->length) { | |
8442 | if (argptr != 0) { | |
8443 | argptr->raw = ip->bufp; | |
8444 | argptr->raw_length = bp - ip->bufp; | |
8445 | argptr->newlines = newlines; | |
8446 | } | |
8447 | ip->bufp = bp; | |
8448 | } else { | |
8449 | /* This input stack level ends before the macro argument does. | |
8450 | We must pop levels and keep parsing. | |
8451 | Therefore, we must allocate a temporary buffer and copy | |
8452 | the macro argument into it. */ | |
8453 | int bufsize = bp - ip->bufp; | |
8454 | int extra = newlines; | |
8455 | U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1); | |
8456 | int final_start = 0; | |
8457 | ||
4c9a05bc | 8458 | bcopy ((char *) ip->bufp, (char *) buffer, bufsize); |
b0bbbd85 RS |
8459 | ip->bufp = bp; |
8460 | ip->lineno += newlines; | |
8461 | ||
8462 | while (bp == ip->buf + ip->length) { | |
8463 | if (instack[indepth].macro == 0) { | |
8464 | free (buffer); | |
8465 | return "unterminated macro call"; | |
8466 | } | |
8467 | ip->macro->type = T_MACRO; | |
8468 | if (ip->free_ptr) | |
8469 | free (ip->free_ptr); | |
8470 | ip = &instack[--indepth]; | |
8471 | newlines = 0; | |
8472 | comments = 0; | |
8473 | bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren, | |
5ff1a832 | 8474 | &newlines, &comments, rest_args); |
b0bbbd85 RS |
8475 | final_start = bufsize; |
8476 | bufsize += bp - ip->bufp; | |
8477 | extra += newlines; | |
8478 | buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1); | |
4c9a05bc RK |
8479 | bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)), |
8480 | bp - ip->bufp); | |
b0bbbd85 RS |
8481 | ip->bufp = bp; |
8482 | ip->lineno += newlines; | |
8483 | } | |
8484 | ||
8485 | /* Now, if arg is actually wanted, record its raw form, | |
8486 | discarding comments and duplicating newlines in whatever | |
8487 | part of it did not come from a macro expansion. | |
8488 | EXTRA space has been preallocated for duplicating the newlines. | |
8489 | FINAL_START is the index of the start of that part. */ | |
8490 | if (argptr != 0) { | |
8491 | argptr->raw = buffer; | |
8492 | argptr->raw_length = bufsize; | |
8493 | argptr->free1 = buffer; | |
8494 | argptr->newlines = newlines; | |
8495 | argptr->comments = comments; | |
8496 | if ((newlines || comments) && ip->fname != 0) | |
8497 | argptr->raw_length | |
8498 | = final_start + | |
8499 | discard_comments (argptr->raw + final_start, | |
8500 | argptr->raw_length - final_start, | |
8501 | newlines); | |
8502 | argptr->raw[argptr->raw_length] = 0; | |
8503 | if (argptr->raw_length > bufsize + extra) | |
8504 | abort (); | |
8505 | } | |
8506 | } | |
8507 | ||
8508 | /* If we are not discarding this argument, | |
8509 | macroexpand it and compute its length as stringified. | |
8510 | All this info goes into *ARGPTR. */ | |
8511 | ||
8512 | if (argptr != 0) { | |
b0bbbd85 RS |
8513 | register U_CHAR *buf, *lim; |
8514 | register int totlen; | |
8515 | ||
b0bbbd85 RS |
8516 | buf = argptr->raw; |
8517 | lim = buf + argptr->raw_length; | |
8518 | ||
8519 | while (buf != lim && is_space[*buf]) | |
8520 | buf++; | |
8521 | while (buf != lim && is_space[lim[-1]]) | |
8522 | lim--; | |
8523 | totlen = traditional ? 0 : 2; /* Count opening and closing quote. */ | |
8524 | while (buf != lim) { | |
8525 | register U_CHAR c = *buf++; | |
8526 | totlen++; | |
8527 | /* Internal sequences of whitespace are replaced by one space | |
8528 | in most cases, but not always. So count all the whitespace | |
8529 | in case we need to keep it all. */ | |
8530 | #if 0 | |
8531 | if (is_space[c]) | |
8532 | SKIP_ALL_WHITE_SPACE (buf); | |
8533 | else | |
8534 | #endif | |
8535 | if (c == '\"' || c == '\\') /* escape these chars */ | |
8536 | totlen++; | |
8537 | else if (!isprint (c)) | |
8538 | totlen += 3; | |
8539 | } | |
8540 | argptr->stringified_length = totlen; | |
8541 | } | |
8542 | return 0; | |
8543 | } | |
8544 | \f | |
8545 | /* Scan text from START (inclusive) up to LIMIT (exclusive), | |
8546 | counting parens in *DEPTHPTR, | |
8547 | and return if reach LIMIT | |
8548 | or before a `)' that would make *DEPTHPTR negative | |
8549 | or before a comma when *DEPTHPTR is zero. | |
8550 | Single and double quotes are matched and termination | |
8551 | is inhibited within them. Comments also inhibit it. | |
8552 | Value returned is pointer to stopping place. | |
8553 | ||
8554 | Increment *NEWLINES each time a newline is passed. | |
5ff1a832 | 8555 | REST_ARGS notifies macarg1 that it should absorb the rest of the args. |
b0bbbd85 RS |
8556 | Set *COMMENTS to 1 if a comment is seen. */ |
8557 | ||
8558 | static U_CHAR * | |
5ff1a832 | 8559 | macarg1 (start, limit, depthptr, newlines, comments, rest_args) |
b0bbbd85 RS |
8560 | U_CHAR *start; |
8561 | register U_CHAR *limit; | |
8562 | int *depthptr, *newlines, *comments; | |
5ff1a832 | 8563 | int rest_args; |
b0bbbd85 RS |
8564 | { |
8565 | register U_CHAR *bp = start; | |
8566 | ||
8567 | while (bp < limit) { | |
8568 | switch (*bp) { | |
8569 | case '(': | |
8570 | (*depthptr)++; | |
8571 | break; | |
8572 | case ')': | |
8573 | if (--(*depthptr) < 0) | |
8574 | return bp; | |
8575 | break; | |
8576 | case '\\': | |
8577 | /* Traditionally, backslash makes following char not special. */ | |
8578 | if (bp + 1 < limit && traditional) | |
8579 | { | |
8580 | bp++; | |
8581 | /* But count source lines anyway. */ | |
8582 | if (*bp == '\n') | |
8583 | ++*newlines; | |
8584 | } | |
8585 | break; | |
8586 | case '\n': | |
8587 | ++*newlines; | |
8588 | break; | |
8589 | case '/': | |
8590 | if (bp[1] == '\\' && bp[2] == '\n') | |
8591 | newline_fix (bp + 1); | |
eda5fa7b | 8592 | if (cplusplus_comments && bp[1] == '/') { |
b0bbbd85 RS |
8593 | *comments = 1; |
8594 | bp += 2; | |
80512db7 JW |
8595 | while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) { |
8596 | if (*bp == '\n') ++*newlines; | |
8597 | bp++; | |
8598 | } | |
a8cdcc1f JW |
8599 | /* Now count the newline that we are about to skip. */ |
8600 | ++*newlines; | |
b0bbbd85 RS |
8601 | break; |
8602 | } | |
8603 | if (bp[1] != '*' || bp + 1 >= limit) | |
8604 | break; | |
8605 | *comments = 1; | |
8606 | bp += 2; | |
8607 | while (bp + 1 < limit) { | |
8608 | if (bp[0] == '*' | |
8609 | && bp[1] == '\\' && bp[2] == '\n') | |
8610 | newline_fix (bp + 1); | |
8611 | if (bp[0] == '*' && bp[1] == '/') | |
8612 | break; | |
8613 | if (*bp == '\n') ++*newlines; | |
8614 | bp++; | |
8615 | } | |
8616 | break; | |
8617 | case '\'': | |
8618 | case '\"': | |
8619 | { | |
8620 | int quotec; | |
8621 | for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) { | |
8622 | if (*bp == '\\') { | |
8623 | bp++; | |
8624 | if (*bp == '\n') | |
8625 | ++*newlines; | |
8626 | while (*bp == '\\' && bp[1] == '\n') { | |
8627 | bp += 2; | |
8628 | } | |
8629 | } else if (*bp == '\n') { | |
8630 | ++*newlines; | |
8631 | if (quotec == '\'') | |
8632 | break; | |
8633 | } | |
8634 | } | |
8635 | } | |
8636 | break; | |
8637 | case ',': | |
5ff1a832 RS |
8638 | /* if we've returned to lowest level and we aren't absorbing all args */ |
8639 | if ((*depthptr) == 0 && rest_args == 0) | |
b0bbbd85 RS |
8640 | return bp; |
8641 | break; | |
8642 | } | |
8643 | bp++; | |
8644 | } | |
8645 | ||
8646 | return bp; | |
8647 | } | |
8648 | \f | |
8649 | /* Discard comments and duplicate newlines | |
8650 | in the string of length LENGTH at START, | |
8651 | except inside of string constants. | |
8652 | The string is copied into itself with its beginning staying fixed. | |
8653 | ||
8654 | NEWLINES is the number of newlines that must be duplicated. | |
8655 | We assume that that much extra space is available past the end | |
8656 | of the string. */ | |
8657 | ||
8658 | static int | |
8659 | discard_comments (start, length, newlines) | |
8660 | U_CHAR *start; | |
8661 | int length; | |
8662 | int newlines; | |
8663 | { | |
8664 | register U_CHAR *ibp; | |
8665 | register U_CHAR *obp; | |
8666 | register U_CHAR *limit; | |
8667 | register int c; | |
8668 | ||
8669 | /* If we have newlines to duplicate, copy everything | |
8670 | that many characters up. Then, in the second part, | |
8671 | we will have room to insert the newlines | |
8672 | while copying down. | |
8673 | NEWLINES may actually be too large, because it counts | |
8674 | newlines in string constants, and we don't duplicate those. | |
8675 | But that does no harm. */ | |
8676 | if (newlines > 0) { | |
8677 | ibp = start + length; | |
8678 | obp = ibp + newlines; | |
8679 | limit = start; | |
8680 | while (limit != ibp) | |
8681 | *--obp = *--ibp; | |
8682 | } | |
8683 | ||
8684 | ibp = start + newlines; | |
8685 | limit = start + length + newlines; | |
8686 | obp = start; | |
8687 | ||
8688 | while (ibp < limit) { | |
8689 | *obp++ = c = *ibp++; | |
8690 | switch (c) { | |
8691 | case '\n': | |
8692 | /* Duplicate the newline. */ | |
8693 | *obp++ = '\n'; | |
8694 | break; | |
8695 | ||
8696 | case '\\': | |
8697 | if (*ibp == '\n') { | |
8698 | obp--; | |
8699 | ibp++; | |
8700 | } | |
8701 | break; | |
8702 | ||
8703 | case '/': | |
8704 | if (*ibp == '\\' && ibp[1] == '\n') | |
8705 | newline_fix (ibp); | |
8706 | /* Delete any comment. */ | |
eda5fa7b | 8707 | if (cplusplus_comments && ibp[0] == '/') { |
18e2b1c0 JW |
8708 | /* Comments are equivalent to spaces. */ |
8709 | obp[-1] = ' '; | |
b0bbbd85 | 8710 | ibp++; |
80512db7 JW |
8711 | while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\')) |
8712 | ibp++; | |
b0bbbd85 RS |
8713 | break; |
8714 | } | |
8715 | if (ibp[0] != '*' || ibp + 1 >= limit) | |
8716 | break; | |
b6d90143 RK |
8717 | /* Comments are equivalent to spaces. |
8718 | For -traditional, a comment is equivalent to nothing. */ | |
8719 | if (traditional) | |
8720 | obp--; | |
8721 | else | |
8722 | obp[-1] = ' '; | |
b0bbbd85 RS |
8723 | ibp++; |
8724 | while (ibp + 1 < limit) { | |
8725 | if (ibp[0] == '*' | |
8726 | && ibp[1] == '\\' && ibp[2] == '\n') | |
8727 | newline_fix (ibp + 1); | |
8728 | if (ibp[0] == '*' && ibp[1] == '/') | |
8729 | break; | |
8730 | ibp++; | |
8731 | } | |
8732 | ibp += 2; | |
8733 | break; | |
8734 | ||
8735 | case '\'': | |
8736 | case '\"': | |
8737 | /* Notice and skip strings, so that we don't | |
8738 | think that comments start inside them, | |
8739 | and so we don't duplicate newlines in them. */ | |
8740 | { | |
8741 | int quotec = c; | |
8742 | while (ibp < limit) { | |
8743 | *obp++ = c = *ibp++; | |
8744 | if (c == quotec) | |
8745 | break; | |
8746 | if (c == '\n' && quotec == '\'') | |
8747 | break; | |
8748 | if (c == '\\' && ibp < limit) { | |
8749 | while (*ibp == '\\' && ibp[1] == '\n') | |
8750 | ibp += 2; | |
8751 | *obp++ = *ibp++; | |
8752 | } | |
8753 | } | |
8754 | } | |
8755 | break; | |
8756 | } | |
8757 | } | |
8758 | ||
8759 | return obp - start; | |
8760 | } | |
8761 | \f | |
de5f1a5a RS |
8762 | /* Turn newlines to spaces in the string of length LENGTH at START, |
8763 | except inside of string constants. | |
8764 | The string is copied into itself with its beginning staying fixed. */ | |
b0bbbd85 RS |
8765 | |
8766 | static int | |
de5f1a5a | 8767 | change_newlines (start, length) |
b0bbbd85 RS |
8768 | U_CHAR *start; |
8769 | int length; | |
8770 | { | |
8771 | register U_CHAR *ibp; | |
8772 | register U_CHAR *obp; | |
8773 | register U_CHAR *limit; | |
8774 | register int c; | |
8775 | ||
8776 | ibp = start; | |
8777 | limit = start + length; | |
8778 | obp = start; | |
8779 | ||
8780 | while (ibp < limit) { | |
8781 | *obp++ = c = *ibp++; | |
8782 | switch (c) { | |
8783 | case '\n': | |
8784 | /* If this is a NEWLINE NEWLINE, then this is a real newline in the | |
de5f1a5a RS |
8785 | string. Skip past the newline and its duplicate. |
8786 | Put a space in the output. */ | |
b0bbbd85 RS |
8787 | if (*ibp == '\n') |
8788 | { | |
8789 | ibp++; | |
8790 | obp--; | |
de5f1a5a | 8791 | *obp++ = ' '; |
b0bbbd85 RS |
8792 | } |
8793 | break; | |
8794 | ||
8795 | case '\'': | |
8796 | case '\"': | |
8797 | /* Notice and skip strings, so that we don't delete newlines in them. */ | |
8798 | { | |
8799 | int quotec = c; | |
8800 | while (ibp < limit) { | |
8801 | *obp++ = c = *ibp++; | |
8802 | if (c == quotec) | |
8803 | break; | |
8804 | if (c == '\n' && quotec == '\'') | |
8805 | break; | |
8806 | } | |
8807 | } | |
8808 | break; | |
8809 | } | |
8810 | } | |
8811 | ||
8812 | return obp - start; | |
8813 | } | |
8814 | \f | |
02cc38b5 RK |
8815 | /* |
8816 | * my_strerror - return the descriptive text associated with an `errno' code. | |
8817 | */ | |
8818 | ||
8819 | char * | |
8820 | my_strerror (errnum) | |
8821 | int errnum; | |
8822 | { | |
8823 | char *result; | |
8824 | ||
8825 | #ifndef VMS | |
8826 | #ifndef HAVE_STRERROR | |
8827 | result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0); | |
8828 | #else | |
8829 | result = strerror (errnum); | |
8830 | #endif | |
8831 | #else /* VMS */ | |
8832 | /* VAXCRTL's strerror() takes an optional second argument, which only | |
8833 | matters when the first argument is EVMSERR. However, it's simplest | |
8834 | just to pass it unconditionally. `vaxc$errno' is declared in | |
8835 | <errno.h>, and maintained by the library in parallel with `errno'. | |
8836 | We assume that caller's `errnum' either matches the last setting of | |
8837 | `errno' by the library or else does not have the value `EVMSERR'. */ | |
8838 | ||
8839 | result = strerror (errnum, vaxc$errno); | |
8840 | #endif | |
8841 | ||
8842 | if (!result) | |
8843 | result = "undocumented I/O error"; | |
8844 | ||
8845 | return result; | |
8846 | } | |
8847 | ||
b0bbbd85 RS |
8848 | /* |
8849 | * error - print error message and increment count of errors. | |
8850 | */ | |
8851 | ||
8852 | void | |
25cbb59e RK |
8853 | error (PRINTF_ALIST (msg)) |
8854 | PRINTF_DCL (msg) | |
8855 | { | |
8856 | va_list args; | |
8857 | ||
8858 | VA_START (args, msg); | |
8859 | verror (msg, args); | |
8860 | va_end (args); | |
8861 | } | |
8862 | ||
8863 | static void | |
8864 | verror (msg, args) | |
b0bbbd85 | 8865 | char *msg; |
25cbb59e | 8866 | va_list args; |
b0bbbd85 RS |
8867 | { |
8868 | int i; | |
8869 | FILE_BUF *ip = NULL; | |
8870 | ||
8871 | print_containing_files (); | |
8872 | ||
8873 | for (i = indepth; i >= 0; i--) | |
8874 | if (instack[i].fname != NULL) { | |
8875 | ip = &instack[i]; | |
8876 | break; | |
8877 | } | |
8878 | ||
8879 | if (ip != NULL) | |
8880 | fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno); | |
25cbb59e | 8881 | vfprintf (stderr, msg, args); |
b0bbbd85 RS |
8882 | fprintf (stderr, "\n"); |
8883 | errors++; | |
8884 | } | |
8885 | ||
8886 | /* Error including a message from `errno'. */ | |
8887 | ||
8888 | static void | |
8889 | error_from_errno (name) | |
8890 | char *name; | |
8891 | { | |
8892 | int i; | |
8893 | FILE_BUF *ip = NULL; | |
8894 | ||
8895 | print_containing_files (); | |
8896 | ||
8897 | for (i = indepth; i >= 0; i--) | |
8898 | if (instack[i].fname != NULL) { | |
8899 | ip = &instack[i]; | |
8900 | break; | |
8901 | } | |
8902 | ||
8903 | if (ip != NULL) | |
8904 | fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno); | |
8905 | ||
02cc38b5 | 8906 | fprintf (stderr, "%s: %s\n", name, my_strerror (errno)); |
b0bbbd85 RS |
8907 | |
8908 | errors++; | |
8909 | } | |
8910 | ||
8911 | /* Print error message but don't count it. */ | |
8912 | ||
8913 | void | |
25cbb59e RK |
8914 | warning (PRINTF_ALIST (msg)) |
8915 | PRINTF_DCL (msg) | |
8916 | { | |
8917 | va_list args; | |
8918 | ||
8919 | VA_START (args, msg); | |
8920 | vwarning (msg, args); | |
8921 | va_end (args); | |
8922 | } | |
8923 | ||
8924 | static void | |
8925 | vwarning (msg, args) | |
b0bbbd85 | 8926 | char *msg; |
25cbb59e | 8927 | va_list args; |
b0bbbd85 RS |
8928 | { |
8929 | int i; | |
8930 | FILE_BUF *ip = NULL; | |
8931 | ||
8932 | if (inhibit_warnings) | |
8933 | return; | |
8934 | ||
8935 | if (warnings_are_errors) | |
8936 | errors++; | |
8937 | ||
8938 | print_containing_files (); | |
8939 | ||
8940 | for (i = indepth; i >= 0; i--) | |
8941 | if (instack[i].fname != NULL) { | |
8942 | ip = &instack[i]; | |
8943 | break; | |
8944 | } | |
8945 | ||
8946 | if (ip != NULL) | |
8947 | fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno); | |
8948 | fprintf (stderr, "warning: "); | |
25cbb59e | 8949 | vfprintf (stderr, msg, args); |
b0bbbd85 RS |
8950 | fprintf (stderr, "\n"); |
8951 | } | |
8952 | ||
8953 | static void | |
25cbb59e RK |
8954 | #if defined (__STDC__) && defined (HAVE_VPRINTF) |
8955 | error_with_line (int line, PRINTF_ALIST (msg)) | |
8956 | #else | |
8957 | error_with_line (line, PRINTF_ALIST (msg)) | |
8958 | int line; | |
8959 | PRINTF_DCL (msg) | |
8960 | #endif | |
8961 | { | |
8962 | va_list args; | |
8963 | ||
8964 | VA_START (args, msg); | |
8965 | verror_with_line (line, msg, args); | |
8966 | va_end (args); | |
8967 | } | |
8968 | ||
8969 | static void | |
8970 | verror_with_line (line, msg, args) | |
b0bbbd85 RS |
8971 | int line; |
8972 | char *msg; | |
25cbb59e | 8973 | va_list args; |
b0bbbd85 RS |
8974 | { |
8975 | int i; | |
8976 | FILE_BUF *ip = NULL; | |
8977 | ||
8978 | print_containing_files (); | |
8979 | ||
8980 | for (i = indepth; i >= 0; i--) | |
8981 | if (instack[i].fname != NULL) { | |
8982 | ip = &instack[i]; | |
8983 | break; | |
8984 | } | |
8985 | ||
8986 | if (ip != NULL) | |
8987 | fprintf (stderr, "%s:%d: ", ip->nominal_fname, line); | |
25cbb59e | 8988 | vfprintf (stderr, msg, args); |
b0bbbd85 RS |
8989 | fprintf (stderr, "\n"); |
8990 | errors++; | |
8991 | } | |
8992 | ||
27a5574b | 8993 | static void |
25cbb59e | 8994 | vwarning_with_line (line, msg, args) |
27a5574b RS |
8995 | int line; |
8996 | char *msg; | |
25cbb59e | 8997 | va_list args; |
27a5574b RS |
8998 | { |
8999 | int i; | |
9000 | FILE_BUF *ip = NULL; | |
9001 | ||
9002 | if (inhibit_warnings) | |
9003 | return; | |
9004 | ||
9005 | if (warnings_are_errors) | |
9006 | errors++; | |
9007 | ||
9008 | print_containing_files (); | |
9009 | ||
9010 | for (i = indepth; i >= 0; i--) | |
9011 | if (instack[i].fname != NULL) { | |
9012 | ip = &instack[i]; | |
9013 | break; | |
9014 | } | |
9015 | ||
9016 | if (ip != NULL) | |
9017 | fprintf (stderr, "%s:%d: ", ip->nominal_fname, line); | |
9018 | fprintf (stderr, "warning: "); | |
25cbb59e | 9019 | vfprintf (stderr, msg, args); |
27a5574b RS |
9020 | fprintf (stderr, "\n"); |
9021 | } | |
9022 | ||
b0bbbd85 RS |
9023 | /* print an error message and maybe count it. */ |
9024 | ||
9025 | void | |
25cbb59e RK |
9026 | pedwarn (PRINTF_ALIST (msg)) |
9027 | PRINTF_DCL (msg) | |
b0bbbd85 | 9028 | { |
25cbb59e RK |
9029 | va_list args; |
9030 | ||
9031 | VA_START (args, msg); | |
b0bbbd85 | 9032 | if (pedantic_errors) |
25cbb59e | 9033 | verror (msg, args); |
b0bbbd85 | 9034 | else |
25cbb59e RK |
9035 | vwarning (msg, args); |
9036 | va_end (args); | |
b0bbbd85 RS |
9037 | } |
9038 | ||
27a5574b | 9039 | void |
25cbb59e RK |
9040 | #if defined (__STDC__) && defined (HAVE_VPRINTF) |
9041 | pedwarn_with_line (int line, PRINTF_ALIST (msg)) | |
9042 | #else | |
9043 | pedwarn_with_line (line, PRINTF_ALIST (msg)) | |
27a5574b | 9044 | int line; |
25cbb59e RK |
9045 | PRINTF_DCL (msg) |
9046 | #endif | |
27a5574b | 9047 | { |
25cbb59e RK |
9048 | va_list args; |
9049 | ||
9050 | VA_START (args, msg); | |
27a5574b | 9051 | if (pedantic_errors) |
25cbb59e | 9052 | verror_with_line (line, msg, args); |
27a5574b | 9053 | else |
25cbb59e RK |
9054 | vwarning_with_line (line, msg, args); |
9055 | va_end (args); | |
27a5574b RS |
9056 | } |
9057 | ||
b0bbbd85 RS |
9058 | /* Report a warning (or an error if pedantic_errors) |
9059 | giving specified file name and line number, not current. */ | |
9060 | ||
9061 | static void | |
25cbb59e RK |
9062 | #if defined (__STDC__) && defined (HAVE_VPRINTF) |
9063 | pedwarn_with_file_and_line (char *file, int line, PRINTF_ALIST (msg)) | |
9064 | #else | |
9065 | pedwarn_with_file_and_line (file, line, PRINTF_ALIST (msg)) | |
b0bbbd85 RS |
9066 | char *file; |
9067 | int line; | |
25cbb59e RK |
9068 | PRINTF_DCL (msg) |
9069 | #endif | |
b0bbbd85 | 9070 | { |
25cbb59e RK |
9071 | va_list args; |
9072 | ||
b0bbbd85 RS |
9073 | if (!pedantic_errors && inhibit_warnings) |
9074 | return; | |
9075 | if (file != NULL) | |
9076 | fprintf (stderr, "%s:%d: ", file, line); | |
27a5574b | 9077 | if (pedantic_errors) |
b0bbbd85 RS |
9078 | errors++; |
9079 | if (!pedantic_errors) | |
9080 | fprintf (stderr, "warning: "); | |
25cbb59e RK |
9081 | VA_START (args, msg); |
9082 | vfprintf (stderr, msg, args); | |
9083 | va_end (args); | |
b0bbbd85 RS |
9084 | fprintf (stderr, "\n"); |
9085 | } | |
9086 | \f | |
9087 | /* Print the file names and line numbers of the #include | |
adcfa681 | 9088 | directives which led to the current file. */ |
b0bbbd85 RS |
9089 | |
9090 | static void | |
9091 | print_containing_files () | |
9092 | { | |
9093 | FILE_BUF *ip = NULL; | |
9094 | int i; | |
9095 | int first = 1; | |
9096 | ||
9097 | /* If stack of files hasn't changed since we last printed | |
9098 | this info, don't repeat it. */ | |
9099 | if (last_error_tick == input_file_stack_tick) | |
9100 | return; | |
9101 | ||
9102 | for (i = indepth; i >= 0; i--) | |
9103 | if (instack[i].fname != NULL) { | |
9104 | ip = &instack[i]; | |
9105 | break; | |
9106 | } | |
9107 | ||
9108 | /* Give up if we don't find a source file. */ | |
9109 | if (ip == NULL) | |
9110 | return; | |
9111 | ||
9112 | /* Find the other, outer source files. */ | |
9113 | for (i--; i >= 0; i--) | |
9114 | if (instack[i].fname != NULL) { | |
9115 | ip = &instack[i]; | |
9116 | if (first) { | |
9117 | first = 0; | |
9118 | fprintf (stderr, "In file included"); | |
9119 | } else { | |
6b46c090 | 9120 | fprintf (stderr, ",\n "); |
b0bbbd85 RS |
9121 | } |
9122 | ||
9123 | fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno); | |
9124 | } | |
9125 | if (! first) | |
9126 | fprintf (stderr, ":\n"); | |
9127 | ||
9128 | /* Record we have printed the status as of this time. */ | |
9129 | last_error_tick = input_file_stack_tick; | |
9130 | } | |
9131 | \f | |
9132 | /* Return the line at which an error occurred. | |
9133 | The error is not necessarily associated with the current spot | |
9134 | in the input stack, so LINE says where. LINE will have been | |
9135 | copied from ip->lineno for the current input level. | |
9136 | If the current level is for a file, we return LINE. | |
9137 | But if the current level is not for a file, LINE is meaningless. | |
9138 | In that case, we return the lineno of the innermost file. */ | |
9139 | ||
9140 | static int | |
9141 | line_for_error (line) | |
9142 | int line; | |
9143 | { | |
9144 | int i; | |
9145 | int line1 = line; | |
9146 | ||
9147 | for (i = indepth; i >= 0; ) { | |
9148 | if (instack[i].fname != 0) | |
9149 | return line1; | |
9150 | i--; | |
9151 | if (i < 0) | |
9152 | return 0; | |
9153 | line1 = instack[i].lineno; | |
9154 | } | |
9155 | abort (); | |
9156 | /*NOTREACHED*/ | |
9157 | return 0; | |
9158 | } | |
9159 | ||
9160 | /* | |
9161 | * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger. | |
9162 | * | |
9163 | * As things stand, nothing is ever placed in the output buffer to be | |
9164 | * removed again except when it's KNOWN to be part of an identifier, | |
9165 | * so flushing and moving down everything left, instead of expanding, | |
9166 | * should work ok. | |
9167 | */ | |
9168 | ||
9169 | /* You might think void was cleaner for the return type, | |
9170 | but that would get type mismatch in check_expand in strict ANSI. */ | |
9171 | static int | |
9172 | grow_outbuf (obuf, needed) | |
9173 | register FILE_BUF *obuf; | |
9174 | register int needed; | |
9175 | { | |
9176 | register U_CHAR *p; | |
9177 | int minsize; | |
9178 | ||
9179 | if (obuf->length - (obuf->bufp - obuf->buf) > needed) | |
9180 | return 0; | |
9181 | ||
9182 | /* Make it at least twice as big as it is now. */ | |
9183 | obuf->length *= 2; | |
9184 | /* Make it have at least 150% of the free space we will need. */ | |
9185 | minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf); | |
9186 | if (minsize > obuf->length) | |
9187 | obuf->length = minsize; | |
9188 | ||
9189 | if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL) | |
9190 | memory_full (); | |
9191 | ||
9192 | obuf->bufp = p + (obuf->bufp - obuf->buf); | |
9193 | obuf->buf = p; | |
9194 | ||
9195 | return 0; | |
9196 | } | |
9197 | \f | |
9198 | /* Symbol table for macro names and special symbols */ | |
9199 | ||
9200 | /* | |
9201 | * install a name in the main hash table, even if it is already there. | |
9202 | * name stops with first non alphanumeric, except leading '#'. | |
9203 | * caller must check against redefinition if that is desired. | |
9204 | * delete_macro () removes things installed by install () in fifo order. | |
9205 | * this is important because of the `defined' special symbol used | |
9206 | * in #if, and also if pushdef/popdef directives are ever implemented. | |
9207 | * | |
9208 | * If LEN is >= 0, it is the length of the name. | |
9209 | * Otherwise, compute the length by scanning the entire name. | |
9210 | * | |
9211 | * If HASH is >= 0, it is the precomputed hash code. | |
9212 | * Otherwise, compute the hash code. | |
9213 | */ | |
9214 | static HASHNODE * | |
91dbf5e7 | 9215 | install (name, len, type, value, hash) |
b0bbbd85 RS |
9216 | U_CHAR *name; |
9217 | int len; | |
9218 | enum node_type type; | |
47b2881e | 9219 | char *value; |
b0bbbd85 | 9220 | int hash; |
b0bbbd85 RS |
9221 | { |
9222 | register HASHNODE *hp; | |
9223 | register int i, bucket; | |
9224 | register U_CHAR *p, *q; | |
9225 | ||
9226 | if (len < 0) { | |
9227 | p = name; | |
9228 | while (is_idchar[*p]) | |
9229 | p++; | |
9230 | len = p - name; | |
9231 | } | |
9232 | ||
9233 | if (hash < 0) | |
9234 | hash = hashf (name, len, HASHSIZE); | |
9235 | ||
9236 | i = sizeof (HASHNODE) + len + 1; | |
9237 | hp = (HASHNODE *) xmalloc (i); | |
9238 | bucket = hash; | |
9239 | hp->bucket_hdr = &hashtab[bucket]; | |
9240 | hp->next = hashtab[bucket]; | |
9241 | hashtab[bucket] = hp; | |
9242 | hp->prev = NULL; | |
9243 | if (hp->next != NULL) | |
9244 | hp->next->prev = hp; | |
9245 | hp->type = type; | |
9246 | hp->length = len; | |
91dbf5e7 | 9247 | hp->value.cpval = value; |
b0bbbd85 RS |
9248 | hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE); |
9249 | p = hp->name; | |
9250 | q = name; | |
9251 | for (i = 0; i < len; i++) | |
9252 | *p++ = *q++; | |
9253 | hp->name[len] = 0; | |
9254 | return hp; | |
9255 | } | |
9256 | ||
9257 | /* | |
9258 | * find the most recent hash node for name name (ending with first | |
9259 | * non-identifier char) installed by install | |
9260 | * | |
9261 | * If LEN is >= 0, it is the length of the name. | |
9262 | * Otherwise, compute the length by scanning the entire name. | |
9263 | * | |
9264 | * If HASH is >= 0, it is the precomputed hash code. | |
9265 | * Otherwise, compute the hash code. | |
9266 | */ | |
9267 | HASHNODE * | |
9268 | lookup (name, len, hash) | |
9269 | U_CHAR *name; | |
9270 | int len; | |
9271 | int hash; | |
9272 | { | |
9273 | register U_CHAR *bp; | |
9274 | register HASHNODE *bucket; | |
9275 | ||
9276 | if (len < 0) { | |
9277 | for (bp = name; is_idchar[*bp]; bp++) ; | |
9278 | len = bp - name; | |
9279 | } | |
9280 | ||
9281 | if (hash < 0) | |
9282 | hash = hashf (name, len, HASHSIZE); | |
9283 | ||
9284 | bucket = hashtab[hash]; | |
9285 | while (bucket) { | |
25cbb59e | 9286 | if (bucket->length == len && bcmp (bucket->name, name, len) == 0) |
b0bbbd85 RS |
9287 | return bucket; |
9288 | bucket = bucket->next; | |
9289 | } | |
9290 | return NULL; | |
9291 | } | |
9292 | ||
9293 | /* | |
9294 | * Delete a hash node. Some weirdness to free junk from macros. | |
9295 | * More such weirdness will have to be added if you define more hash | |
9296 | * types that need it. | |
9297 | */ | |
9298 | ||
9299 | /* Note that the DEFINITION of a macro is removed from the hash table | |
9300 | but its storage is not freed. This would be a storage leak | |
9301 | except that it is not reasonable to keep undefining and redefining | |
9302 | large numbers of macros many times. | |
9303 | In any case, this is necessary, because a macro can be #undef'd | |
9304 | in the middle of reading the arguments to a call to it. | |
9305 | If #undef freed the DEFINITION, that would crash. */ | |
9306 | ||
9307 | static void | |
9308 | delete_macro (hp) | |
9309 | HASHNODE *hp; | |
9310 | { | |
9311 | ||
9312 | if (hp->prev != NULL) | |
9313 | hp->prev->next = hp->next; | |
9314 | if (hp->next != NULL) | |
9315 | hp->next->prev = hp->prev; | |
9316 | ||
9317 | /* make sure that the bucket chain header that | |
9318 | the deleted guy was on points to the right thing afterwards. */ | |
9319 | if (hp == *hp->bucket_hdr) | |
9320 | *hp->bucket_hdr = hp->next; | |
9321 | ||
9322 | #if 0 | |
9323 | if (hp->type == T_MACRO) { | |
9324 | DEFINITION *d = hp->value.defn; | |
9325 | struct reflist *ap, *nextap; | |
9326 | ||
9327 | for (ap = d->pattern; ap != NULL; ap = nextap) { | |
9328 | nextap = ap->next; | |
9329 | free (ap); | |
9330 | } | |
9331 | free (d); | |
9332 | } | |
9333 | #endif | |
9334 | free (hp); | |
9335 | } | |
9336 | ||
9337 | /* | |
9338 | * return hash function on name. must be compatible with the one | |
9339 | * computed a step at a time, elsewhere | |
9340 | */ | |
9341 | static int | |
9342 | hashf (name, len, hashsize) | |
9343 | register U_CHAR *name; | |
9344 | register int len; | |
9345 | int hashsize; | |
9346 | { | |
9347 | register int r = 0; | |
9348 | ||
9349 | while (len--) | |
9350 | r = HASHSTEP (r, *name++); | |
9351 | ||
9352 | return MAKE_POS (r) % hashsize; | |
9353 | } | |
9354 | \f | |
9355 | ||
9356 | /* Dump the definition of a single macro HP to OF. */ | |
9357 | static void | |
9358 | dump_single_macro (hp, of) | |
9359 | register HASHNODE *hp; | |
9360 | FILE *of; | |
9361 | { | |
9362 | register DEFINITION *defn = hp->value.defn; | |
9363 | struct reflist *ap; | |
9364 | int offset; | |
9365 | int concat; | |
9366 | ||
9367 | ||
9368 | /* Print the definition of the macro HP. */ | |
9369 | ||
9370 | fprintf (of, "#define %s", hp->name); | |
9371 | ||
9372 | if (defn->nargs >= 0) { | |
9373 | int i; | |
9374 | ||
9375 | fprintf (of, "("); | |
9376 | for (i = 0; i < defn->nargs; i++) { | |
9377 | dump_arg_n (defn, i, of); | |
9378 | if (i + 1 < defn->nargs) | |
9379 | fprintf (of, ", "); | |
9380 | } | |
9381 | fprintf (of, ")"); | |
9382 | } | |
9383 | ||
9384 | fprintf (of, " "); | |
9385 | ||
9386 | offset = 0; | |
9387 | concat = 0; | |
9388 | for (ap = defn->pattern; ap != NULL; ap = ap->next) { | |
9389 | dump_defn_1 (defn->expansion, offset, ap->nchars, of); | |
b0bbbd85 | 9390 | offset += ap->nchars; |
f46b6be4 RK |
9391 | if (!traditional) { |
9392 | if (ap->nchars != 0) | |
9393 | concat = 0; | |
5bdc1512 RK |
9394 | if (ap->stringify) { |
9395 | switch (ap->stringify) { | |
9396 | case SHARP_TOKEN: fprintf (of, "#"); break; | |
9397 | case WHITE_SHARP_TOKEN: fprintf (of, "# "); break; | |
03285371 RK |
9398 | case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break; |
9399 | case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break; | |
25cbb59e | 9400 | default: abort (); |
5bdc1512 RK |
9401 | } |
9402 | } | |
9403 | if (ap->raw_before) { | |
9404 | if (concat) { | |
9405 | switch (ap->raw_before) { | |
9406 | case WHITE_SHARP_TOKEN: | |
9407 | case WHITE_PERCENT_COLON_TOKEN: | |
9408 | fprintf (of, " "); | |
9409 | break; | |
25cbb59e RK |
9410 | default: |
9411 | break; | |
5bdc1512 RK |
9412 | } |
9413 | } else { | |
9414 | switch (ap->raw_before) { | |
9415 | case SHARP_TOKEN: fprintf (of, "##"); break; | |
9416 | case WHITE_SHARP_TOKEN: fprintf (of, "## "); break; | |
03285371 RK |
9417 | case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break; |
9418 | case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break; | |
25cbb59e | 9419 | default: abort (); |
5bdc1512 RK |
9420 | } |
9421 | } | |
9422 | } | |
f46b6be4 RK |
9423 | concat = 0; |
9424 | } | |
b0bbbd85 | 9425 | dump_arg_n (defn, ap->argno, of); |
f46b6be4 | 9426 | if (!traditional && ap->raw_after) { |
5bdc1512 RK |
9427 | switch (ap->raw_after) { |
9428 | case SHARP_TOKEN: fprintf (of, "##"); break; | |
9429 | case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break; | |
03285371 RK |
9430 | case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break; |
9431 | case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break; | |
25cbb59e | 9432 | default: abort (); |
5bdc1512 | 9433 | } |
b0bbbd85 RS |
9434 | concat = 1; |
9435 | } | |
9436 | } | |
9437 | dump_defn_1 (defn->expansion, offset, defn->length - offset, of); | |
9438 | fprintf (of, "\n"); | |
9439 | } | |
9440 | ||
9441 | /* Dump all macro definitions as #defines to stdout. */ | |
9442 | ||
9443 | static void | |
9444 | dump_all_macros () | |
9445 | { | |
9446 | int bucket; | |
9447 | ||
9448 | for (bucket = 0; bucket < HASHSIZE; bucket++) { | |
9449 | register HASHNODE *hp; | |
9450 | ||
9451 | for (hp = hashtab[bucket]; hp; hp= hp->next) { | |
9452 | if (hp->type == T_MACRO) | |
9453 | dump_single_macro (hp, stdout); | |
9454 | } | |
9455 | } | |
9456 | } | |
9457 | ||
9458 | /* Output to OF a substring of a macro definition. | |
9459 | BASE is the beginning of the definition. | |
9460 | Output characters START thru LENGTH. | |
f46b6be4 | 9461 | Unless traditional, discard newlines outside of strings, thus |
b0bbbd85 RS |
9462 | converting funny-space markers to ordinary spaces. */ |
9463 | ||
9464 | static void | |
9465 | dump_defn_1 (base, start, length, of) | |
9466 | U_CHAR *base; | |
9467 | int start; | |
9468 | int length; | |
9469 | FILE *of; | |
9470 | { | |
9471 | U_CHAR *p = base + start; | |
9472 | U_CHAR *limit = base + start + length; | |
9473 | ||
f46b6be4 RK |
9474 | if (traditional) |
9475 | fwrite (p, sizeof (*p), length, of); | |
9476 | else { | |
9477 | while (p < limit) { | |
9478 | if (*p == '\"' || *p =='\'') { | |
9479 | U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR, | |
9480 | NULL_PTR, NULL_PTR); | |
9481 | fwrite (p, sizeof (*p), p1 - p, of); | |
9482 | p = p1; | |
9483 | } else { | |
9484 | if (*p != '\n') | |
9485 | putc (*p, of); | |
9486 | p++; | |
9487 | } | |
b0bbbd85 | 9488 | } |
b0bbbd85 RS |
9489 | } |
9490 | } | |
9491 | ||
9492 | /* Print the name of argument number ARGNUM of macro definition DEFN | |
9493 | to OF. | |
9494 | Recall that DEFN->args.argnames contains all the arg names | |
9495 | concatenated in reverse order with comma-space in between. */ | |
9496 | ||
9497 | static void | |
9498 | dump_arg_n (defn, argnum, of) | |
9499 | DEFINITION *defn; | |
9500 | int argnum; | |
9501 | FILE *of; | |
9502 | { | |
9503 | register U_CHAR *p = defn->args.argnames; | |
9504 | while (argnum + 1 < defn->nargs) { | |
25cbb59e | 9505 | p = (U_CHAR *) index ((char *) p, ' ') + 1; |
b0bbbd85 RS |
9506 | argnum++; |
9507 | } | |
9508 | ||
9509 | while (*p && *p != ',') { | |
9510 | putc (*p, of); | |
9511 | p++; | |
9512 | } | |
9513 | } | |
9514 | \f | |
9515 | /* Initialize syntactic classifications of characters. */ | |
9516 | ||
9517 | static void | |
9518 | initialize_char_syntax () | |
9519 | { | |
9520 | register int i; | |
9521 | ||
9522 | /* | |
9523 | * Set up is_idchar and is_idstart tables. These should be | |
9524 | * faster than saying (is_alpha (c) || c == '_'), etc. | |
9525 | * Set up these things before calling any routines tthat | |
9526 | * refer to them. | |
9527 | */ | |
9528 | for (i = 'a'; i <= 'z'; i++) { | |
9529 | is_idchar[i - 'a' + 'A'] = 1; | |
9530 | is_idchar[i] = 1; | |
9531 | is_idstart[i - 'a' + 'A'] = 1; | |
9532 | is_idstart[i] = 1; | |
9533 | } | |
9534 | for (i = '0'; i <= '9'; i++) | |
9535 | is_idchar[i] = 1; | |
9536 | is_idchar['_'] = 1; | |
9537 | is_idstart['_'] = 1; | |
9538 | is_idchar['$'] = dollars_in_ident; | |
9539 | is_idstart['$'] = dollars_in_ident; | |
9540 | ||
9541 | /* horizontal space table */ | |
9542 | is_hor_space[' '] = 1; | |
9543 | is_hor_space['\t'] = 1; | |
9544 | is_hor_space['\v'] = 1; | |
9545 | is_hor_space['\f'] = 1; | |
9546 | is_hor_space['\r'] = 1; | |
9547 | ||
9548 | is_space[' '] = 1; | |
9549 | is_space['\t'] = 1; | |
9550 | is_space['\v'] = 1; | |
9551 | is_space['\f'] = 1; | |
9552 | is_space['\n'] = 1; | |
9553 | is_space['\r'] = 1; | |
c03413c7 RK |
9554 | |
9555 | char_name['\v'] = "vertical tab"; | |
9556 | char_name['\f'] = "formfeed"; | |
9557 | char_name['\r'] = "carriage return"; | |
b0bbbd85 RS |
9558 | } |
9559 | ||
9560 | /* Initialize the built-in macros. */ | |
9561 | ||
9562 | static void | |
9563 | initialize_builtins (inp, outp) | |
9564 | FILE_BUF *inp; | |
9565 | FILE_BUF *outp; | |
9566 | { | |
25cbb59e RK |
9567 | install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1); |
9568 | install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1); | |
9569 | install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1); | |
9570 | install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1); | |
9571 | install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1); | |
9572 | install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1); | |
8da4cd09 | 9573 | #ifndef NO_BUILTIN_SIZE_TYPE |
25cbb59e | 9574 | install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1); |
8da4cd09 JW |
9575 | #endif |
9576 | #ifndef NO_BUILTIN_PTRDIFF_TYPE | |
25cbb59e | 9577 | install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1); |
8da4cd09 | 9578 | #endif |
25cbb59e RK |
9579 | install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1); |
9580 | install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, | |
9581 | NULL_PTR, -1); | |
9582 | install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, | |
9583 | NULL_PTR, -1); | |
9584 | install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1); | |
91dbf5e7 | 9585 | if (!traditional) { |
25cbb59e RK |
9586 | install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1); |
9587 | install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1); | |
91dbf5e7 | 9588 | } |
b0bbbd85 | 9589 | if (objc) |
25cbb59e | 9590 | install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1); |
b0bbbd85 RS |
9591 | /* This is supplied using a -D by the compiler driver |
9592 | so that it is present only when truly compiling with GNU C. */ | |
25cbb59e | 9593 | /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */ |
b0bbbd85 RS |
9594 | |
9595 | if (debug_output) | |
9596 | { | |
9597 | char directive[2048]; | |
25cbb59e | 9598 | U_CHAR *udirective = (U_CHAR *) directive; |
b0bbbd85 | 9599 | register struct directive *dp = &directive_table[0]; |
bfa30b22 | 9600 | struct tm *timebuf = timestamp (); |
b0bbbd85 | 9601 | |
ae34b95d | 9602 | sprintf (directive, " __BASE_FILE__ \"%s\"\n", |
b0bbbd85 | 9603 | instack[0].nominal_fname); |
adcfa681 | 9604 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9605 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9606 | outp, dp); | |
b0bbbd85 | 9607 | |
ae34b95d | 9608 | sprintf (directive, " __VERSION__ \"%s\"\n", version_string); |
adcfa681 | 9609 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9610 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9611 | outp, dp); | |
b0bbbd85 | 9612 | |
9e7270cd | 9613 | #ifndef NO_BUILTIN_SIZE_TYPE |
ae34b95d | 9614 | sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE); |
adcfa681 | 9615 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9616 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9617 | outp, dp); | |
9e7270cd | 9618 | #endif |
b0bbbd85 | 9619 | |
9e7270cd | 9620 | #ifndef NO_BUILTIN_PTRDIFF_TYPE |
ae34b95d | 9621 | sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE); |
adcfa681 | 9622 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9623 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9624 | outp, dp); | |
9e7270cd | 9625 | #endif |
b0bbbd85 | 9626 | |
767d412c | 9627 | sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type); |
adcfa681 | 9628 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9629 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9630 | outp, dp); | |
b0bbbd85 | 9631 | |
ae34b95d | 9632 | sprintf (directive, " __DATE__ \"%s %2d %4d\"\n", |
b0bbbd85 RS |
9633 | monthnames[timebuf->tm_mon], |
9634 | timebuf->tm_mday, timebuf->tm_year + 1900); | |
adcfa681 | 9635 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9636 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9637 | outp, dp); | |
b0bbbd85 | 9638 | |
ae34b95d | 9639 | sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n", |
b0bbbd85 | 9640 | timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec); |
adcfa681 | 9641 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e RK |
9642 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
9643 | outp, dp); | |
b0bbbd85 RS |
9644 | |
9645 | if (!traditional) | |
9646 | { | |
9647 | sprintf (directive, " __STDC__ 1"); | |
adcfa681 | 9648 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e | 9649 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
b0bbbd85 RS |
9650 | outp, dp); |
9651 | } | |
9652 | if (objc) | |
9653 | { | |
9654 | sprintf (directive, " __OBJC__ 1"); | |
adcfa681 | 9655 | output_line_directive (inp, outp, 0, same_file); |
25cbb59e | 9656 | pass_thru_directive (udirective, &udirective[strlen (directive)], |
b0bbbd85 RS |
9657 | outp, dp); |
9658 | } | |
9659 | } | |
9660 | } | |
9661 | \f | |
9662 | /* | |
9663 | * process a given definition string, for initialization | |
9664 | * If STR is just an identifier, define it with value 1. | |
9665 | * If STR has anything after the identifier, then it should | |
9666 | * be identifier=definition. | |
9667 | */ | |
9668 | ||
9669 | static void | |
9670 | make_definition (str, op) | |
25cbb59e | 9671 | char *str; |
b0bbbd85 RS |
9672 | FILE_BUF *op; |
9673 | { | |
9674 | FILE_BUF *ip; | |
9675 | struct directive *kt; | |
9676 | U_CHAR *buf, *p; | |
9677 | ||
25cbb59e | 9678 | p = buf = (U_CHAR *) str; |
b0bbbd85 RS |
9679 | if (!is_idstart[*p]) { |
9680 | error ("malformed option `-D %s'", str); | |
9681 | return; | |
9682 | } | |
9683 | while (is_idchar[*++p]) | |
9684 | ; | |
cac9d91e RK |
9685 | if (*p == '(') { |
9686 | while (is_idchar[*++p] || *p == ',' || is_hor_space[*p]) | |
9687 | ; | |
9688 | if (*p++ != ')') | |
25cbb59e | 9689 | p = (U_CHAR *) str; /* Error */ |
cac9d91e | 9690 | } |
b0bbbd85 RS |
9691 | if (*p == 0) { |
9692 | buf = (U_CHAR *) alloca (p - buf + 4); | |
9693 | strcpy ((char *)buf, str); | |
9694 | strcat ((char *)buf, " 1"); | |
9695 | } else if (*p != '=') { | |
9696 | error ("malformed option `-D %s'", str); | |
9697 | return; | |
9698 | } else { | |
9699 | U_CHAR *q; | |
9700 | /* Copy the entire option so we can modify it. */ | |
9701 | buf = (U_CHAR *) alloca (2 * strlen (str) + 1); | |
25cbb59e | 9702 | strncpy ((char *) buf, str, p - (U_CHAR *) str); |
b0bbbd85 | 9703 | /* Change the = to a space. */ |
25cbb59e | 9704 | buf[p - (U_CHAR *) str] = ' '; |
b0bbbd85 RS |
9705 | /* Scan for any backslash-newline and remove it. */ |
9706 | p++; | |
25cbb59e | 9707 | q = &buf[p - (U_CHAR *) str]; |
b0bbbd85 | 9708 | while (*p) { |
1a6e3d52 RK |
9709 | if (*p == '\"' || *p == '\'') { |
9710 | int unterminated = 0; | |
25cbb59e | 9711 | U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0, |
1a6e3d52 RK |
9712 | NULL_PTR, NULL_PTR, &unterminated); |
9713 | if (unterminated) | |
9714 | return; | |
9715 | while (p != p1) | |
9716 | if (*p == '\\' && p[1] == '\n') | |
9717 | p += 2; | |
9718 | else | |
9719 | *q++ = *p++; | |
9720 | } else if (*p == '\\' && p[1] == '\n') | |
b0bbbd85 RS |
9721 | p += 2; |
9722 | /* Change newline chars into newline-markers. */ | |
9723 | else if (*p == '\n') | |
9724 | { | |
9725 | *q++ = '\n'; | |
9726 | *q++ = '\n'; | |
9727 | p++; | |
9728 | } | |
9729 | else | |
9730 | *q++ = *p++; | |
9731 | } | |
9732 | *q = 0; | |
9733 | } | |
9734 | ||
9735 | ip = &instack[++indepth]; | |
9736 | ip->nominal_fname = ip->fname = "*Initialization*"; | |
9737 | ||
9738 | ip->buf = ip->bufp = buf; | |
25cbb59e | 9739 | ip->length = strlen ((char *) buf); |
b0bbbd85 RS |
9740 | ip->lineno = 1; |
9741 | ip->macro = 0; | |
9742 | ip->free_ptr = 0; | |
9743 | ip->if_stack = if_stack; | |
9744 | ip->system_header_p = 0; | |
9745 | ||
9746 | for (kt = directive_table; kt->type != T_DEFINE; kt++) | |
9747 | ; | |
9748 | ||
2b1a049f | 9749 | /* Pass NULL instead of OP, since this is a "predefined" macro. */ |
25cbb59e | 9750 | do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt); |
b0bbbd85 RS |
9751 | --indepth; |
9752 | } | |
9753 | ||
9754 | /* JF, this does the work for the -U option */ | |
9755 | ||
9756 | static void | |
9757 | make_undef (str, op) | |
25cbb59e | 9758 | char *str; |
b0bbbd85 RS |
9759 | FILE_BUF *op; |
9760 | { | |
9761 | FILE_BUF *ip; | |
9762 | struct directive *kt; | |
9763 | ||
9764 | ip = &instack[++indepth]; | |
9765 | ip->nominal_fname = ip->fname = "*undef*"; | |
9766 | ||
25cbb59e | 9767 | ip->buf = ip->bufp = (U_CHAR *) str; |
b0bbbd85 RS |
9768 | ip->length = strlen (str); |
9769 | ip->lineno = 1; | |
9770 | ip->macro = 0; | |
9771 | ip->free_ptr = 0; | |
9772 | ip->if_stack = if_stack; | |
9773 | ip->system_header_p = 0; | |
9774 | ||
9775 | for (kt = directive_table; kt->type != T_UNDEF; kt++) | |
9776 | ; | |
9777 | ||
25cbb59e | 9778 | do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt); |
b0bbbd85 RS |
9779 | --indepth; |
9780 | } | |
9781 | \f | |
9782 | /* Process the string STR as if it appeared as the body of a #assert. | |
9783 | OPTION is the option name for which STR was the argument. */ | |
9784 | ||
9785 | static void | |
9786 | make_assertion (option, str) | |
9787 | char *option; | |
25cbb59e | 9788 | char *str; |
b0bbbd85 RS |
9789 | { |
9790 | FILE_BUF *ip; | |
9791 | struct directive *kt; | |
9792 | U_CHAR *buf, *p, *q; | |
9793 | ||
9794 | /* Copy the entire option so we can modify it. */ | |
9795 | buf = (U_CHAR *) alloca (strlen (str) + 1); | |
9796 | strcpy ((char *) buf, str); | |
9797 | /* Scan for any backslash-newline and remove it. */ | |
9798 | p = q = buf; | |
9799 | while (*p) { | |
9800 | if (*p == '\\' && p[1] == '\n') | |
9801 | p += 2; | |
9802 | else | |
9803 | *q++ = *p++; | |
9804 | } | |
9805 | *q = 0; | |
9806 | ||
9807 | p = buf; | |
9808 | if (!is_idstart[*p]) { | |
9809 | error ("malformed option `%s %s'", option, str); | |
9810 | return; | |
9811 | } | |
9812 | while (is_idchar[*++p]) | |
9813 | ; | |
c03413c7 | 9814 | SKIP_WHITE_SPACE (p); |
b0bbbd85 RS |
9815 | if (! (*p == 0 || *p == '(')) { |
9816 | error ("malformed option `%s %s'", option, str); | |
9817 | return; | |
9818 | } | |
9819 | ||
9820 | ip = &instack[++indepth]; | |
9821 | ip->nominal_fname = ip->fname = "*Initialization*"; | |
9822 | ||
9823 | ip->buf = ip->bufp = buf; | |
25cbb59e | 9824 | ip->length = strlen ((char *) buf); |
b0bbbd85 RS |
9825 | ip->lineno = 1; |
9826 | ip->macro = 0; | |
9827 | ip->free_ptr = 0; | |
9828 | ip->if_stack = if_stack; | |
9829 | ip->system_header_p = 0; | |
9830 | ||
9831 | for (kt = directive_table; kt->type != T_ASSERT; kt++) | |
9832 | ; | |
9833 | ||
9834 | /* pass NULL as output ptr to do_define since we KNOW it never | |
9835 | does any output.... */ | |
25cbb59e | 9836 | do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt); |
b0bbbd85 RS |
9837 | --indepth; |
9838 | } | |
9839 | \f | |
6489924b RS |
9840 | /* Append a chain of `struct file_name_list's |
9841 | to the end of the main include chain. | |
9842 | FIRST is the beginning of the chain to append, and LAST is the end. */ | |
9843 | ||
9844 | static void | |
9845 | append_include_chain (first, last) | |
9846 | struct file_name_list *first, *last; | |
9847 | { | |
9848 | struct file_name_list *dir; | |
9849 | ||
9850 | if (!first || !last) | |
9851 | return; | |
9852 | ||
9853 | if (include == 0) | |
9854 | include = first; | |
9855 | else | |
9856 | last_include->next = first; | |
9857 | ||
9858 | if (first_bracket_include == 0) | |
56430040 | 9859 | first_bracket_include = first; |
6489924b RS |
9860 | |
9861 | for (dir = first; ; dir = dir->next) { | |
c01b03e8 | 9862 | int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE; |
6489924b RS |
9863 | if (len > max_include_len) |
9864 | max_include_len = len; | |
9865 | if (dir == last) | |
9866 | break; | |
9867 | } | |
9868 | ||
9869 | last->next = NULL; | |
9870 | last_include = last; | |
9871 | } | |
9872 | \f | |
b0bbbd85 RS |
9873 | /* Add output to `deps_buffer' for the -M switch. |
9874 | STRING points to the text to be output. | |
ba6aa38e | 9875 | SPACER is ':' for targets, ' ' for dependencies. */ |
b0bbbd85 RS |
9876 | |
9877 | static void | |
389bb508 | 9878 | deps_output (string, spacer) |
b0bbbd85 | 9879 | char *string; |
389bb508 | 9880 | int spacer; |
b0bbbd85 | 9881 | { |
389bb508 RK |
9882 | int size = strlen (string); |
9883 | ||
b0bbbd85 | 9884 | if (size == 0) |
389bb508 | 9885 | return; |
b0bbbd85 RS |
9886 | |
9887 | #ifndef MAX_OUTPUT_COLUMNS | |
389bb508 | 9888 | #define MAX_OUTPUT_COLUMNS 72 |
b0bbbd85 | 9889 | #endif |
ba6aa38e RK |
9890 | if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size |
9891 | && 1 < deps_column) { | |
9892 | bcopy (" \\\n ", &deps_buffer[deps_size], 4); | |
9893 | deps_size += 4; | |
9894 | deps_column = 1; | |
9895 | if (spacer == ' ') | |
9896 | spacer = 0; | |
b0bbbd85 RS |
9897 | } |
9898 | ||
389bb508 RK |
9899 | if (deps_size + size + 8 > deps_allocated_size) { |
9900 | deps_allocated_size = (deps_size + size + 50) * 2; | |
25cbb59e | 9901 | deps_buffer = xrealloc (deps_buffer, deps_allocated_size); |
b0bbbd85 | 9902 | } |
ba6aa38e | 9903 | if (spacer == ' ') { |
389bb508 | 9904 | deps_buffer[deps_size++] = ' '; |
ba6aa38e RK |
9905 | deps_column++; |
9906 | } | |
b0bbbd85 RS |
9907 | bcopy (string, &deps_buffer[deps_size], size); |
9908 | deps_size += size; | |
9909 | deps_column += size; | |
ba6aa38e | 9910 | if (spacer == ':') { |
389bb508 | 9911 | deps_buffer[deps_size++] = ':'; |
ba6aa38e RK |
9912 | deps_column++; |
9913 | } | |
b0bbbd85 RS |
9914 | deps_buffer[deps_size] = 0; |
9915 | } | |
b0bbbd85 RS |
9916 | \f |
9917 | static void | |
25cbb59e RK |
9918 | fatal (PRINTF_ALIST (msg)) |
9919 | PRINTF_DCL (msg) | |
b0bbbd85 | 9920 | { |
25cbb59e RK |
9921 | va_list args; |
9922 | ||
b0bbbd85 | 9923 | fprintf (stderr, "%s: ", progname); |
25cbb59e RK |
9924 | VA_START (args, msg); |
9925 | vfprintf (stderr, msg, args); | |
9926 | va_end (args); | |
b0bbbd85 RS |
9927 | fprintf (stderr, "\n"); |
9928 | exit (FAILURE_EXIT_CODE); | |
9929 | } | |
9930 | ||
9931 | /* More 'friendly' abort that prints the line and file. | |
9932 | config.h can #define abort fancy_abort if you like that sort of thing. */ | |
9933 | ||
9934 | void | |
9935 | fancy_abort () | |
9936 | { | |
9937 | fatal ("Internal gcc abort."); | |
9938 | } | |
9939 | ||
9940 | static void | |
9941 | perror_with_name (name) | |
9942 | char *name; | |
9943 | { | |
9944 | fprintf (stderr, "%s: ", progname); | |
02cc38b5 | 9945 | fprintf (stderr, "%s: %s\n", name, my_strerror (errno)); |
b0bbbd85 RS |
9946 | errors++; |
9947 | } | |
9948 | ||
9949 | static void | |
9950 | pfatal_with_name (name) | |
9951 | char *name; | |
9952 | { | |
9953 | perror_with_name (name); | |
9954 | #ifdef VMS | |
9955 | exit (vaxc$errno); | |
9956 | #else | |
9957 | exit (FAILURE_EXIT_CODE); | |
9958 | #endif | |
9959 | } | |
9960 | ||
9e263fc4 JW |
9961 | /* Handler for SIGPIPE. */ |
9962 | ||
9963 | static void | |
9964 | pipe_closed (signo) | |
9965 | /* If this is missing, some compilers complain. */ | |
9966 | int signo; | |
9967 | { | |
9968 | fatal ("output pipe has been closed"); | |
9969 | } | |
b0bbbd85 RS |
9970 | \f |
9971 | static void | |
9972 | memory_full () | |
9973 | { | |
9974 | fatal ("Memory exhausted."); | |
9975 | } | |
9976 | ||
9977 | ||
25cbb59e | 9978 | GENERIC_PTR |
b0bbbd85 | 9979 | xmalloc (size) |
25cbb59e | 9980 | size_t size; |
b0bbbd85 | 9981 | { |
25cbb59e RK |
9982 | register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size); |
9983 | if (!ptr) | |
9984 | memory_full (); | |
9985 | return ptr; | |
b0bbbd85 RS |
9986 | } |
9987 | ||
25cbb59e | 9988 | static GENERIC_PTR |
b0bbbd85 | 9989 | xrealloc (old, size) |
25cbb59e RK |
9990 | GENERIC_PTR old; |
9991 | size_t size; | |
b0bbbd85 | 9992 | { |
25cbb59e RK |
9993 | register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size); |
9994 | if (!ptr) | |
9995 | memory_full (); | |
9996 | return ptr; | |
b0bbbd85 RS |
9997 | } |
9998 | ||
25cbb59e | 9999 | static GENERIC_PTR |
b0bbbd85 | 10000 | xcalloc (number, size) |
25cbb59e | 10001 | size_t number, size; |
b0bbbd85 | 10002 | { |
25cbb59e RK |
10003 | register size_t total = number * size; |
10004 | register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total); | |
10005 | if (!ptr) | |
10006 | memory_full (); | |
10007 | bzero (ptr, total); | |
10008 | return ptr; | |
b0bbbd85 RS |
10009 | } |
10010 | ||
10011 | static char * | |
10012 | savestring (input) | |
10013 | char *input; | |
10014 | { | |
25cbb59e | 10015 | size_t size = strlen (input); |
b0bbbd85 RS |
10016 | char *output = xmalloc (size + 1); |
10017 | strcpy (output, input); | |
10018 | return output; | |
10019 | } | |
10020 | \f | |
10021 | /* Get the file-mode and data size of the file open on FD | |
10022 | and store them in *MODE_POINTER and *SIZE_POINTER. */ | |
10023 | ||
10024 | static int | |
10025 | file_size_and_mode (fd, mode_pointer, size_pointer) | |
10026 | int fd; | |
10027 | int *mode_pointer; | |
10028 | long int *size_pointer; | |
10029 | { | |
10030 | struct stat sbuf; | |
10031 | ||
10032 | if (fstat (fd, &sbuf) < 0) return (-1); | |
10033 | if (mode_pointer) *mode_pointer = sbuf.st_mode; | |
10034 | if (size_pointer) *size_pointer = sbuf.st_size; | |
10035 | return 0; | |
10036 | } | |
334eb142 RS |
10037 | |
10038 | static void | |
10039 | output_dots (fd, depth) | |
10040 | FILE* fd; | |
10041 | int depth; | |
10042 | { | |
10043 | while (depth > 0) { | |
10044 | putc ('.', fd); | |
10045 | depth--; | |
10046 | } | |
10047 | } | |
10048 | ||
b0bbbd85 RS |
10049 | \f |
10050 | #ifdef VMS | |
10051 | ||
10052 | /* Under VMS we need to fix up the "include" specification | |
10053 | filename so that everything following the 1st slash is | |
10054 | changed into its correct VMS file specification. */ | |
10055 | ||
10056 | static void | |
10057 | hack_vms_include_specification (fname) | |
10058 | char *fname; | |
10059 | { | |
10060 | register char *cp, *cp1, *cp2; | |
10061 | int f, check_filename_before_returning, no_prefix_seen; | |
10062 | char Local[512]; | |
10063 | ||
10064 | check_filename_before_returning = 0; | |
10065 | no_prefix_seen = 0; | |
10066 | ||
10067 | /* Ignore leading "./"s */ | |
10068 | while (fname[0] == '.' && fname[1] == '/') { | |
10069 | strcpy (fname, fname+2); | |
10070 | no_prefix_seen = 1; /* mark this for later */ | |
10071 | } | |
10072 | /* Look for the boundary between the VMS and UNIX filespecs */ | |
10073 | cp = rindex (fname, ']'); /* Look for end of dirspec. */ | |
10074 | if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto */ | |
10075 | if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */ | |
10076 | if (cp) { | |
10077 | cp++; | |
10078 | } else { | |
10079 | cp = index (fname, '/'); /* Look for the "/" */ | |
10080 | } | |
10081 | ||
fe701b40 RK |
10082 | /* |
10083 | * Check if we have a vax-c style '#include filename' | |
10084 | * and add the missing .h | |
10085 | */ | |
10086 | if (cp == 0) { | |
10087 | if (index(fname,'.') == 0) | |
10088 | strcat(fname, ".h"); | |
10089 | } else { | |
10090 | if (index(cp,'.') == 0) | |
10091 | strcat(cp, ".h"); | |
10092 | } | |
10093 | ||
b0bbbd85 RS |
10094 | cp2 = Local; /* initialize */ |
10095 | ||
10096 | /* We are trying to do a number of things here. First of all, we are | |
10097 | trying to hammer the filenames into a standard format, such that later | |
10098 | processing can handle them. | |
10099 | ||
10100 | If the file name contains something like [dir.], then it recognizes this | |
10101 | as a root, and strips the ".]". Later processing will add whatever is | |
10102 | needed to get things working properly. | |
10103 | ||
10104 | If no device is specified, then the first directory name is taken to be | |
10105 | a device name (or a rooted logical). */ | |
10106 | ||
10107 | /* See if we found that 1st slash */ | |
10108 | if (cp == 0) return; /* Nothing to do!!! */ | |
10109 | if (*cp != '/') return; /* Nothing to do!!! */ | |
10110 | /* Point to the UNIX filename part (which needs to be fixed!) */ | |
10111 | cp1 = cp+1; | |
10112 | /* If the directory spec is not rooted, we can just copy | |
10113 | the UNIX filename part and we are done */ | |
10114 | if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) { | |
10115 | if (cp[-2] != '.') { | |
10116 | /* | |
2aa7ec37 | 10117 | * The VMS part ends in a `]', and the preceding character is not a `.'. |
b0bbbd85 RS |
10118 | * We strip the `]', and then splice the two parts of the name in the |
10119 | * usual way. Given the default locations for include files in cccp.c, | |
10120 | * we will only use this code if the user specifies alternate locations | |
10121 | * with the /include (-I) switch on the command line. */ | |
10122 | cp -= 1; /* Strip "]" */ | |
10123 | cp1--; /* backspace */ | |
10124 | } else { | |
10125 | /* | |
10126 | * The VMS part has a ".]" at the end, and this will not do. Later | |
10127 | * processing will add a second directory spec, and this would be a syntax | |
10128 | * error. Thus we strip the ".]", and thus merge the directory specs. | |
10129 | * We also backspace cp1, so that it points to a '/'. This inhibits the | |
10130 | * generation of the 000000 root directory spec (which does not belong here | |
10131 | * in this case). | |
10132 | */ | |
10133 | cp -= 2; /* Strip ".]" */ | |
10134 | cp1--; }; /* backspace */ | |
10135 | } else { | |
10136 | ||
10137 | /* We drop in here if there is no VMS style directory specification yet. | |
10138 | * If there is no device specification either, we make the first dir a | |
10139 | * device and try that. If we do not do this, then we will be essentially | |
10140 | * searching the users default directory (as if they did a #include "asdf.h"). | |
10141 | * | |
10142 | * Then all we need to do is to push a '[' into the output string. Later | |
10143 | * processing will fill this in, and close the bracket. | |
10144 | */ | |
ad0c9fa1 | 10145 | if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec. take first dir */ |
b0bbbd85 RS |
10146 | *cp2++ = '['; /* Open the directory specification */ |
10147 | } | |
10148 | ||
10149 | /* at this point we assume that we have the device spec, and (at least | |
10150 | the opening "[" for a directory specification. We may have directories | |
10151 | specified already */ | |
10152 | ||
10153 | /* If there are no other slashes then the filename will be | |
10154 | in the "root" directory. Otherwise, we need to add | |
10155 | directory specifications. */ | |
10156 | if (index (cp1, '/') == 0) { | |
10157 | /* Just add "000000]" as the directory string */ | |
10158 | strcpy (cp2, "000000]"); | |
10159 | cp2 += strlen (cp2); | |
10160 | check_filename_before_returning = 1; /* we might need to fool with this later */ | |
10161 | } else { | |
10162 | /* As long as there are still subdirectories to add, do them. */ | |
10163 | while (index (cp1, '/') != 0) { | |
10164 | /* If this token is "." we can ignore it */ | |
10165 | if ((cp1[0] == '.') && (cp1[1] == '/')) { | |
10166 | cp1 += 2; | |
10167 | continue; | |
10168 | } | |
10169 | /* Add a subdirectory spec. Do not duplicate "." */ | |
10170 | if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<') | |
10171 | *cp2++ = '.'; | |
10172 | /* If this is ".." then the spec becomes "-" */ | |
10173 | if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) { | |
10174 | /* Add "-" and skip the ".." */ | |
10175 | *cp2++ = '-'; | |
10176 | cp1 += 3; | |
10177 | continue; | |
10178 | } | |
10179 | /* Copy the subdirectory */ | |
10180 | while (*cp1 != '/') *cp2++= *cp1++; | |
10181 | cp1++; /* Skip the "/" */ | |
10182 | } | |
10183 | /* Close the directory specification */ | |
ad0c9fa1 | 10184 | if (cp2[-1] == '.') /* no trailing periods */ |
b0bbbd85 RS |
10185 | cp2--; |
10186 | *cp2++ = ']'; | |
10187 | } | |
10188 | /* Now add the filename */ | |
10189 | while (*cp1) *cp2++ = *cp1++; | |
10190 | *cp2 = 0; | |
10191 | /* Now append it to the original VMS spec. */ | |
10192 | strcpy (cp, Local); | |
10193 | ||
10194 | /* If we put a [000000] in the filename, try to open it first. If this fails, | |
10195 | remove the [000000], and return that name. This provides flexibility | |
10196 | to the user in that they can use both rooted and non-rooted logical names | |
10197 | to point to the location of the file. */ | |
10198 | ||
10199 | if (check_filename_before_returning && no_prefix_seen) { | |
10200 | f = open (fname, O_RDONLY, 0666); | |
10201 | if (f >= 0) { | |
10202 | /* The file name is OK as it is, so return it as is. */ | |
10203 | close (f); | |
10204 | return; | |
10205 | } | |
10206 | /* The filename did not work. Try to remove the [000000] from the name, | |
10207 | and return it. */ | |
10208 | cp = index (fname, '['); | |
10209 | cp2 = index (fname, ']') + 1; | |
10210 | strcpy (cp, cp2); /* this gets rid of it */ | |
10211 | } | |
10212 | return; | |
10213 | } | |
10214 | #endif /* VMS */ | |
10215 | \f | |
10216 | #ifdef VMS | |
10217 | ||
10218 | /* These are the read/write replacement routines for | |
10219 | VAX-11 "C". They make read/write behave enough | |
10220 | like their UNIX counterparts that CCCP will work */ | |
10221 | ||
10222 | static int | |
10223 | read (fd, buf, size) | |
10224 | int fd; | |
10225 | char *buf; | |
10226 | int size; | |
10227 | { | |
10228 | #undef read /* Get back the REAL read routine */ | |
10229 | register int i; | |
10230 | register int total = 0; | |
10231 | ||
10232 | /* Read until the buffer is exhausted */ | |
10233 | while (size > 0) { | |
10234 | /* Limit each read to 32KB */ | |
10235 | i = (size > (32*1024)) ? (32*1024) : size; | |
10236 | i = read (fd, buf, i); | |
10237 | if (i <= 0) { | |
10238 | if (i == 0) return (total); | |
ad0c9fa1 | 10239 | return (i); |
b0bbbd85 RS |
10240 | } |
10241 | /* Account for this read */ | |
10242 | total += i; | |
10243 | buf += i; | |
10244 | size -= i; | |
10245 | } | |
10246 | return (total); | |
10247 | } | |
10248 | ||
10249 | static int | |
10250 | write (fd, buf, size) | |
10251 | int fd; | |
10252 | char *buf; | |
10253 | int size; | |
10254 | { | |
10255 | #undef write /* Get back the REAL write routine */ | |
10256 | int i; | |
10257 | int j; | |
10258 | ||
10259 | /* Limit individual writes to 32Kb */ | |
10260 | i = size; | |
10261 | while (i > 0) { | |
10262 | j = (i > (32*1024)) ? (32*1024) : i; | |
10263 | if (write (fd, buf, j) < 0) return (-1); | |
10264 | /* Account for the data written */ | |
10265 | buf += j; | |
10266 | i -= j; | |
10267 | } | |
10268 | return (size); | |
10269 | } | |
10270 | ||
10271 | /* The following wrapper functions supply additional arguments to the VMS | |
10272 | I/O routines to optimize performance with file handling. The arguments | |
10273 | are: | |
10274 | "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer). | |
10275 | "deq=64" - When extending the file, extend it in chunks of 32Kbytes. | |
10276 | "fop=tef"- Truncate unused portions of file when closing file. | |
10277 | "shr=nil"- Disallow file sharing while file is open. | |
10278 | */ | |
10279 | ||
10280 | static FILE * | |
10281 | freopen (fname, type, oldfile) | |
10282 | char *fname; | |
10283 | char *type; | |
10284 | FILE *oldfile; | |
10285 | { | |
10286 | #undef freopen /* Get back the REAL fopen routine */ | |
10287 | if (strcmp (type, "w") == 0) | |
10288 | return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil"); | |
10289 | return freopen (fname, type, oldfile, "mbc=16"); | |
10290 | } | |
10291 | ||
10292 | static FILE * | |
10293 | fopen (fname, type) | |
10294 | char *fname; | |
10295 | char *type; | |
10296 | { | |
10297 | #undef fopen /* Get back the REAL fopen routine */ | |
37c9f674 RK |
10298 | /* The gcc-vms-1.42 distribution's header files prototype fopen with two |
10299 | fixed arguments, which matches ANSI's specification but not VAXCRTL's | |
10300 | pre-ANSI implmentation. This hack circumvents the mismatch problem. */ | |
10301 | FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen; | |
10302 | ||
10303 | if (*type == 'w') | |
10304 | return (*vmslib_fopen) (fname, type, "mbc=32", | |
10305 | "deq=64", "fop=tef", "shr=nil"); | |
10306 | else | |
10307 | return (*vmslib_fopen) (fname, type, "mbc=32"); | |
b0bbbd85 RS |
10308 | } |
10309 | ||
10310 | static int | |
10311 | open (fname, flags, prot) | |
10312 | char *fname; | |
10313 | int flags; | |
10314 | int prot; | |
10315 | { | |
10316 | #undef open /* Get back the REAL open routine */ | |
10317 | return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef"); | |
10318 | } | |
10319 | ||
106d7d7d RS |
10320 | /* Avoid run-time library bug, where copying M out of N+M characters with |
10321 | N >= 65535 results in VAXCRTL's strncat falling into an infinite loop. | |
bd8cb5e2 | 10322 | gcc-cpp exercises this particular bug. [Fixed in V5.5-2's VAXCRTL.] */ |
106d7d7d RS |
10323 | |
10324 | static char * | |
10325 | strncat (dst, src, cnt) | |
10326 | char *dst; | |
10327 | const char *src; | |
10328 | unsigned cnt; | |
10329 | { | |
10330 | register char *d = dst, *s = (char *) src; | |
10331 | register int n = cnt; /* convert to _signed_ type */ | |
10332 | ||
10333 | while (*d) d++; /* advance to end */ | |
10334 | while (--n >= 0) | |
10335 | if (!(*d++ = *s++)) break; | |
10336 | if (n < 0) *d = '\0'; | |
10337 | return dst; | |
10338 | } | |
bd8cb5e2 RK |
10339 | \f |
10340 | /* more VMS hackery */ | |
10341 | #include <fab.h> | |
10342 | #include <nam.h> | |
10343 | ||
10344 | extern unsigned long sys$parse(), sys$search(); | |
10345 | ||
10346 | /* Work around another library bug. If a file is located via a searchlist, | |
10347 | and if the device it's on is not the same device as the one specified | |
10348 | in the first element of that searchlist, then both stat() and fstat() | |
10349 | will fail to return info about it. `errno' will be set to EVMSERR, and | |
10350 | `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()! | |
10351 | We can get around this by fully parsing the filename and then passing | |
10352 | that absolute name to stat(). | |
10353 | ||
10354 | Without this fix, we can end up failing to find header files, which is | |
10355 | bad enough, but then compounding the problem by reporting the reason for | |
10356 | failure as "normal successful completion." */ | |
10357 | ||
10358 | static int | |
10359 | fstat (fd, statbuf) | |
10360 | int fd; | |
10361 | struct stat *statbuf; | |
10362 | { | |
10363 | #undef fstat | |
10364 | int result = fstat (fd, statbuf); | |
10365 | ||
10366 | if (result < 0) | |
10367 | { | |
10368 | FILE *fp; | |
10369 | char nambuf[NAM$C_MAXRSS+1]; | |
10370 | ||
10371 | if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0) | |
10372 | result = stat (nambuf, statbuf); | |
10373 | /* No fclose(fp) here; that would close(fd) as well. */ | |
10374 | } | |
10375 | ||
10376 | return result; | |
10377 | } | |
10378 | ||
10379 | static int | |
10380 | stat (name, statbuf) | |
10381 | const char *name; | |
10382 | struct stat *statbuf; | |
10383 | { | |
10384 | #undef stat | |
10385 | int result = stat (name, statbuf); | |
10386 | ||
10387 | if (result < 0) | |
10388 | { | |
10389 | struct FAB fab; | |
10390 | struct NAM nam; | |
10391 | char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for sys$parse */ | |
10392 | res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for sys$search */ | |
10393 | ||
10394 | fab = cc$rms_fab; | |
10395 | fab.fab$l_fna = (char *) name; | |
10396 | fab.fab$b_fns = (unsigned char) strlen (name); | |
10397 | fab.fab$l_nam = (void *) &nam; | |
10398 | nam = cc$rms_nam; | |
10399 | nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1; | |
10400 | nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1; | |
10401 | nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL; | |
10402 | if (sys$parse (&fab) & 1) | |
10403 | { | |
10404 | if (sys$search (&fab) & 1) | |
10405 | { | |
10406 | res_nam[nam.nam$b_rsl] = '\0'; | |
10407 | result = stat (res_nam, statbuf); | |
10408 | } | |
10409 | /* Clean up searchlist context cached by the system. */ | |
10410 | nam.nam$b_nop = NAM$M_SYNCHK; | |
10411 | fab.fab$l_fna = 0, fab.fab$b_fns = 0; | |
10412 | (void) sys$parse (&fab); | |
10413 | } | |
10414 | } | |
10415 | ||
10416 | return result; | |
10417 | } | |
b0bbbd85 | 10418 | #endif /* VMS */ |