]> gcc.gnu.org Git - gcc.git/blob - gcc/cppinit.c
cpphash.h: ISvspace, is_vspace, is_nvspace: New.
[gcc.git] / gcc / cppinit.c
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "output.h"
27 #include "prefix.h"
28 #include "intl.h"
29 #include "version.h"
30 #include "mkdeps.h"
31 #include "cppdefault.h"
32
33 /* Predefined symbols, built-in macros, and the default include path. */
34
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 #endif
38
39 /* Windows does not natively support inodes, and neither does MSDOS.
40 Cygwin's emulation can generate non-unique inodes, so don't use it.
41 VMS has non-numeric inodes. */
42 #ifdef VMS
43 #define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a)))
44 #elif (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
45 #define INO_T_EQ(a, b) 0
46 #else
47 #define INO_T_EQ(a, b) ((a) == (b))
48 #endif
49
50 /* Internal structures and prototypes. */
51
52 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
53 switch. There are four lists: one for -D and -U, one for -A, one
54 for -include, one for -imacros. `undef' is set for -U, clear for
55 -D, ignored for the others.
56 (Future: add an equivalent of -U for -A) */
57
58 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
59 struct pending_option
60 {
61 struct pending_option *next;
62 const char *arg;
63 cl_directive_handler handler;
64 };
65
66 /* The `pending' structure accumulates all the options that are not
67 actually processed until we hit cpp_start_read. It consists of
68 several lists, one for each type of option. We keep both head and
69 tail pointers for quick insertion. */
70 struct cpp_pending
71 {
72 struct pending_option *directive_head, *directive_tail;
73
74 struct file_name_list *quote_head, *quote_tail;
75 struct file_name_list *brack_head, *brack_tail;
76 struct file_name_list *systm_head, *systm_tail;
77 struct file_name_list *after_head, *after_tail;
78
79 struct pending_option *imacros_head, *imacros_tail;
80 struct pending_option *include_head, *include_tail;
81 };
82
83 #ifdef __STDC__
84 #define APPEND(pend, list, elt) \
85 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86 else (pend)->list##_tail->next = (elt); \
87 (pend)->list##_tail = (elt); \
88 } while (0)
89 #else
90 #define APPEND(pend, list, elt) \
91 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92 else (pend)->list/**/_tail->next = (elt); \
93 (pend)->list/**/_tail = (elt); \
94 } while (0)
95 #endif
96
97 static void print_help PARAMS ((void));
98 static void path_include PARAMS ((cpp_reader *,
99 struct cpp_pending *,
100 char *, int));
101 static void initialize_builtins PARAMS ((cpp_reader *));
102 static void append_include_chain PARAMS ((cpp_reader *,
103 struct cpp_pending *,
104 char *, int, int));
105 static void merge_include_chains PARAMS ((cpp_reader *));
106
107 static void initialize_dependency_output PARAMS ((cpp_reader *));
108 static void initialize_standard_includes PARAMS ((cpp_reader *));
109 static void new_pending_directive PARAMS ((struct cpp_pending *,
110 const char *,
111 cl_directive_handler));
112 #ifdef HOST_EBCDIC
113 static int opt_comp PARAMS ((const void *, const void *));
114 #endif
115 static int parse_option PARAMS ((const char *));
116 static int handle_option PARAMS ((cpp_reader *, int, char **));
117
118 /* Fourth argument to append_include_chain: chain to use */
119 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
120
121 /* If we have designated initializers (GCC >2.7) this table can be
122 initialized, constant data. Otherwise, it has to be filled in at
123 runtime. */
124
125 #if (GCC_VERSION >= 2007)
126 #define init_IStable() /* nothing */
127 #define ISTABLE __extension__ const unsigned char _cpp_IStable[256] = {
128 #define END };
129 #define s(p, v) [p] = v,
130 #else
131 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
132 static void init_IStable PARAMS ((void)) { \
133 unsigned char *x = _cpp_IStable;
134 #define END }
135 #define s(p, v) x[p] = v;
136 #endif
137
138 #define A(x) s(x, ISidnum|ISidstart)
139 #define N(x) s(x, ISidnum|ISnumstart)
140 #define H(x) s(x, IShspace|ISspace)
141 #define V(x) s(x, ISvspace|ISspace)
142 #define S(x) s(x, ISspace)
143
144 ISTABLE
145 A('_')
146
147 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
148 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
149 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
150
151 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
152 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
153 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
154
155 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
156
157 H(' ') H('\t')
158
159 V('\n') V('\r')
160
161 S('\0') S('\v') S('\f')
162 END
163
164 #undef A
165 #undef N
166 #undef H
167 #undef V
168 #undef S
169 #undef s
170 #undef ISTABLE
171 #undef END
172
173 /* Given a colon-separated list of file names PATH,
174 add all the names to the search path for include files. */
175
176 static void
177 path_include (pfile, pend, list, path)
178 cpp_reader *pfile;
179 struct cpp_pending *pend;
180 char *list;
181 int path;
182 {
183 char *p, *q, *name;
184
185 p = list;
186
187 do
188 {
189 /* Find the end of this name. */
190 q = p;
191 while (*q != 0 && *q != PATH_SEPARATOR) q++;
192 if (q == p)
193 {
194 /* An empty name in the path stands for the current directory. */
195 name = (char *) xmalloc (2);
196 name[0] = '.';
197 name[1] = 0;
198 }
199 else
200 {
201 /* Otherwise use the directory that is named. */
202 name = (char *) xmalloc (q - p + 1);
203 memcpy (name, p, q - p);
204 name[q - p] = 0;
205 }
206
207 append_include_chain (pfile, pend, name, path, 0);
208
209 /* Advance past this name. */
210 if (*q == 0)
211 break;
212 p = q + 1;
213 }
214 while (1);
215 }
216
217 /* Append DIR to include path PATH. DIR must be permanently allocated
218 and writable. */
219 static void
220 append_include_chain (pfile, pend, dir, path, cxx_aware)
221 cpp_reader *pfile;
222 struct cpp_pending *pend;
223 char *dir;
224 int path;
225 int cxx_aware;
226 {
227 struct file_name_list *new;
228 struct stat st;
229 unsigned int len;
230
231 _cpp_simplify_pathname (dir);
232 if (stat (dir, &st))
233 {
234 /* Dirs that don't exist are silently ignored. */
235 if (errno != ENOENT)
236 cpp_notice_from_errno (pfile, dir);
237 else if (CPP_OPTION (pfile, verbose))
238 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
239 return;
240 }
241
242 if (!S_ISDIR (st.st_mode))
243 {
244 cpp_notice (pfile, "%s: Not a directory", dir);
245 return;
246 }
247
248 len = strlen (dir);
249 if (len > pfile->max_include_len)
250 pfile->max_include_len = len;
251
252 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
253 new->name = dir;
254 new->nlen = len;
255 new->ino = st.st_ino;
256 new->dev = st.st_dev;
257 if (path == SYSTEM)
258 new->sysp = cxx_aware ? 1 : 2;
259 else
260 new->sysp = 0;
261 new->name_map = NULL;
262 new->next = NULL;
263 new->alloc = NULL;
264
265 switch (path)
266 {
267 case QUOTE: APPEND (pend, quote, new); break;
268 case BRACKET: APPEND (pend, brack, new); break;
269 case SYSTEM: APPEND (pend, systm, new); break;
270 case AFTER: APPEND (pend, after, new); break;
271 }
272 }
273
274 /* Merge the four include chains together in the order quote, bracket,
275 system, after. Remove duplicate dirs (as determined by
276 INO_T_EQ()). The system_include and after_include chains are never
277 referred to again after this function; all access is through the
278 bracket_include path.
279
280 For the future: Check if the directory is empty (but
281 how?) and possibly preload the include hash. */
282
283 static void
284 merge_include_chains (pfile)
285 cpp_reader *pfile;
286 {
287 struct file_name_list *prev, *cur, *other;
288 struct file_name_list *quote, *brack, *systm, *after;
289 struct file_name_list *qtail, *btail, *stail, *atail;
290
291 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
292
293 qtail = pend->quote_tail;
294 btail = pend->brack_tail;
295 stail = pend->systm_tail;
296 atail = pend->after_tail;
297
298 quote = pend->quote_head;
299 brack = pend->brack_head;
300 systm = pend->systm_head;
301 after = pend->after_head;
302
303 /* Paste together bracket, system, and after include chains. */
304 if (stail)
305 stail->next = after;
306 else
307 systm = after;
308 if (btail)
309 btail->next = systm;
310 else
311 brack = systm;
312
313 /* This is a bit tricky.
314 First we drop dupes from the quote-include list.
315 Then we drop dupes from the bracket-include list.
316 Finally, if qtail and brack are the same directory,
317 we cut out qtail.
318
319 We can't just merge the lists and then uniquify them because
320 then we may lose directories from the <> search path that should
321 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
322 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
323 -Ibar -I- -Ifoo -Iquux.
324
325 Note that this algorithm is quadratic in the number of -I switches,
326 which is acceptable since there aren't usually that many of them. */
327
328 for (cur = quote, prev = NULL; cur; cur = cur->next)
329 {
330 for (other = quote; other != cur; other = other->next)
331 if (INO_T_EQ (cur->ino, other->ino)
332 && cur->dev == other->dev)
333 {
334 if (CPP_OPTION (pfile, verbose))
335 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
336 cur->name);
337
338 prev->next = cur->next;
339 free (cur->name);
340 free (cur);
341 cur = prev;
342 break;
343 }
344 prev = cur;
345 }
346 qtail = prev;
347
348 for (cur = brack; cur; cur = cur->next)
349 {
350 for (other = brack; other != cur; other = other->next)
351 if (INO_T_EQ (cur->ino, other->ino)
352 && cur->dev == other->dev)
353 {
354 if (CPP_OPTION (pfile, verbose))
355 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
356 cur->name);
357
358 prev->next = cur->next;
359 free (cur->name);
360 free (cur);
361 cur = prev;
362 break;
363 }
364 prev = cur;
365 }
366
367 if (quote)
368 {
369 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
370 {
371 if (quote == qtail)
372 {
373 if (CPP_OPTION (pfile, verbose))
374 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
375 quote->name);
376
377 free (quote->name);
378 free (quote);
379 quote = brack;
380 }
381 else
382 {
383 cur = quote;
384 while (cur->next != qtail)
385 cur = cur->next;
386 cur->next = brack;
387 if (CPP_OPTION (pfile, verbose))
388 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
389 qtail->name);
390
391 free (qtail->name);
392 free (qtail);
393 }
394 }
395 else
396 qtail->next = brack;
397 }
398 else
399 quote = brack;
400
401 CPP_OPTION (pfile, quote_include) = quote;
402 CPP_OPTION (pfile, bracket_include) = brack;
403 }
404
405
406 /* Initialize a cpp_reader structure. */
407 void
408 cpp_reader_init (pfile)
409 cpp_reader *pfile;
410 {
411 memset ((char *) pfile, 0, sizeof (cpp_reader));
412
413 pfile->token_buffer_size = 200;
414 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
415 CPP_SET_WRITTEN (pfile, 0);
416
417 CPP_OPTION (pfile, dollars_in_ident) = 1;
418 CPP_OPTION (pfile, cplusplus_comments) = 1;
419 CPP_OPTION (pfile, warn_import) = 1;
420 CPP_OPTION (pfile, warn_paste) = 1;
421 CPP_OPTION (pfile, discard_comments) = 1;
422 CPP_OPTION (pfile, show_column) = 1;
423 CPP_OPTION (pfile, tabstop) = 8;
424
425 CPP_OPTION (pfile, pending) =
426 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
427
428 _cpp_init_macros (pfile);
429 _cpp_init_stacks (pfile);
430 _cpp_init_includes (pfile);
431 }
432
433 /* Initialize a cpp_printer structure. As a side effect, open the
434 output file. */
435 cpp_printer *
436 cpp_printer_init (pfile, print)
437 cpp_reader *pfile;
438 cpp_printer *print;
439 {
440 memset (print, '\0', sizeof (cpp_printer));
441 if (CPP_OPTION (pfile, out_fname) == NULL)
442 CPP_OPTION (pfile, out_fname) = "";
443
444 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
445 print->outf = stdout;
446 else
447 {
448 print->outf = fopen (CPP_OPTION (pfile, out_fname), "w");
449 if (! print->outf)
450 {
451 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
452 return NULL;
453 }
454 }
455 return print;
456 }
457
458 /* Free resources used by PFILE.
459 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
460 void
461 cpp_cleanup (pfile)
462 cpp_reader *pfile;
463 {
464 while (CPP_BUFFER (pfile) != NULL)
465 cpp_pop_buffer (pfile);
466
467 if (pfile->token_buffer)
468 {
469 free (pfile->token_buffer);
470 pfile->token_buffer = NULL;
471 }
472
473 if (pfile->deps)
474 deps_free (pfile->deps);
475
476 if (pfile->spec_nodes)
477 free (pfile->spec_nodes);
478
479 _cpp_free_temp_tokens (pfile);
480 _cpp_cleanup_includes (pfile);
481 _cpp_cleanup_stacks (pfile);
482 _cpp_cleanup_macros (pfile);
483 }
484
485
486 /* This structure defines one built-in macro. A node of type TYPE will
487 be entered in the macro hash table under the name NAME, with value
488 VALUE (if any). Two values are not compile time constants, so we tag
489 them in the FLAGS field instead:
490 VERS value is the global version_string, quoted
491 ULP value is the global user_label_prefix
492 */
493
494 struct builtin
495 {
496 const U_CHAR *name;
497 const char *value;
498 unsigned short type;
499 unsigned short flags;
500 unsigned int len;
501 };
502 #define VERS 0x01
503 #define ULP 0x02
504
505 #define B(n, t) { U n, 0, t, 0, sizeof n - 1 }
506 #define C(n, v) { U n, v, T_MACRO, 0, sizeof n - 1 }
507 #define X(n, f) { U n, 0, T_MACRO, f, sizeof n - 1 }
508 static const struct builtin builtin_array[] =
509 {
510 B("__TIME__", T_TIME),
511 B("__DATE__", T_DATE),
512 B("__FILE__", T_FILE),
513 B("__BASE_FILE__", T_BASE_FILE),
514 B("__LINE__", T_SPECLINE),
515 B("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL),
516 B("__STDC__", T_STDC),
517
518 X("__VERSION__", VERS),
519 X("__USER_LABEL_PREFIX__", ULP),
520 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
521 C("__HAVE_BUILTIN_SETJMP__", "1"),
522 #ifndef NO_BUILTIN_SIZE_TYPE
523 C("__SIZE_TYPE__", SIZE_TYPE),
524 #endif
525 #ifndef NO_BUILTIN_PTRDIFF_TYPE
526 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
527 #endif
528 #ifndef NO_BUILTIN_WCHAR_TYPE
529 C("__WCHAR_TYPE__", WCHAR_TYPE),
530 #endif
531 };
532 #undef B
533 #undef C
534 #undef X
535 #define builtin_array_end \
536 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
537
538 /* Subroutine of cpp_start_read; reads the builtins table above and
539 enters the macros into the hash table. */
540 static void
541 initialize_builtins (pfile)
542 cpp_reader *pfile;
543 {
544 const struct builtin *b;
545 for(b = builtin_array; b < builtin_array_end; b++)
546 {
547 if (b->type == T_MACRO)
548 {
549 const char *val;
550 char *str;
551
552 if (b->flags & VERS)
553 {
554 /* Allocate enough space for 'name="value"\0'. */
555 str = xmalloc (b->len + strlen (version_string) + 4);
556 sprintf (str, "%s=\"%s\"", b->name, version_string);
557 }
558 else
559 {
560 if (b->flags & ULP)
561 val = user_label_prefix;
562 else
563 val = b->value;
564
565 /* Allocate enough space for "name=value\0". */
566 str = xmalloc (b->len + strlen (val) + 2);
567 sprintf(str, "%s=%s", b->name, val);
568 }
569 cpp_define (pfile, str);
570 }
571 else
572 {
573 cpp_hashnode *hp;
574
575 if (b->type == T_STDC && CPP_TRADITIONAL (pfile))
576 continue;
577
578 hp = cpp_lookup (pfile, b->name, b->len);
579 hp->type = b->type;
580 }
581 }
582 }
583 #undef VERS
584 #undef ULP
585 #undef builtin_array_end
586
587 /* Another subroutine of cpp_start_read. This one sets up to do
588 dependency-file output. */
589 static void
590 initialize_dependency_output (pfile)
591 cpp_reader *pfile;
592 {
593 char *spec, *s, *output_file;
594
595 /* Either of two environment variables can specify output of deps.
596 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
597 where OUTPUT_FILE is the file to write deps info to
598 and DEPS_TARGET is the target to mention in the deps. */
599
600 if (CPP_OPTION (pfile, print_deps) == 0)
601 {
602 spec = getenv ("DEPENDENCIES_OUTPUT");
603 if (spec)
604 CPP_OPTION (pfile, print_deps) = 1;
605 else
606 {
607 spec = getenv ("SUNPRO_DEPENDENCIES");
608 if (spec)
609 CPP_OPTION (pfile, print_deps) = 2;
610 else
611 return;
612 }
613
614 /* Find the space before the DEPS_TARGET, if there is one. */
615 s = strchr (spec, ' ');
616 if (s)
617 {
618 CPP_OPTION (pfile, deps_target) = s + 1;
619 output_file = (char *) xmalloc (s - spec + 1);
620 memcpy (output_file, spec, s - spec);
621 output_file[s - spec] = 0;
622 }
623 else
624 {
625 CPP_OPTION (pfile, deps_target) = 0;
626 output_file = spec;
627 }
628
629 CPP_OPTION (pfile, deps_file) = output_file;
630 CPP_OPTION (pfile, print_deps_append) = 1;
631 }
632
633 pfile->deps = deps_init ();
634
635 /* Print the expected object file name as the target of this Make-rule. */
636 if (CPP_OPTION (pfile, deps_target))
637 deps_add_target (pfile->deps, CPP_OPTION (pfile, deps_target));
638 else if (*CPP_OPTION (pfile, in_fname) == 0)
639 deps_add_target (pfile->deps, "-");
640 else
641 deps_calc_target (pfile->deps, CPP_OPTION (pfile, in_fname));
642
643 if (CPP_OPTION (pfile, in_fname))
644 deps_add_dep (pfile->deps, CPP_OPTION (pfile, in_fname));
645 }
646
647 /* And another subroutine. This one sets up the standard include path. */
648 static void
649 initialize_standard_includes (pfile)
650 cpp_reader *pfile;
651 {
652 char *path;
653 const struct default_include *p;
654 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
655
656 /* Several environment variables may add to the include search path.
657 CPATH specifies an additional list of directories to be searched
658 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
659 etc. specify an additional list of directories to be searched as
660 if specified with -isystem, for the language indicated. */
661
662 GET_ENV_PATH_LIST (path, "CPATH");
663 if (path != 0 && *path != 0)
664 path_include (pfile, CPP_OPTION (pfile, pending), path, BRACKET);
665
666 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
667 {
668 case 0:
669 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
670 break;
671 case 1:
672 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
673 break;
674 case 2:
675 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
676 break;
677 case 3:
678 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
679 break;
680 }
681 if (path != 0 && *path != 0)
682 path_include (pfile, CPP_OPTION (pfile, pending), path, SYSTEM);
683
684 /* Search "translated" versions of GNU directories.
685 These have /usr/local/lib/gcc... replaced by specd_prefix. */
686 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
687 {
688 /* Remove the `include' from /usr/local/lib/gcc.../include.
689 GCC_INCLUDE_DIR will always end in /include. */
690 int default_len = cpp_GCC_INCLUDE_DIR_len;
691 char *default_prefix = (char *) alloca (default_len + 1);
692 int specd_len = strlen (specd_prefix);
693
694 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
695 default_prefix[default_len] = '\0';
696
697 for (p = cpp_include_defaults; p->fname; p++)
698 {
699 /* Some standard dirs are only for C++. */
700 if (!p->cplusplus
701 || (CPP_OPTION (pfile, cplusplus)
702 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
703 {
704 /* Does this dir start with the prefix? */
705 if (!strncmp (p->fname, default_prefix, default_len))
706 {
707 /* Yes; change prefix and add to search list. */
708 int flen = strlen (p->fname);
709 int this_len = specd_len + flen - default_len;
710 char *str = (char *) xmalloc (this_len + 1);
711 memcpy (str, specd_prefix, specd_len);
712 memcpy (str + specd_len,
713 p->fname + default_len,
714 flen - default_len + 1);
715
716 append_include_chain (pfile, CPP_OPTION (pfile, pending),
717 str, SYSTEM, p->cxx_aware);
718 }
719 }
720 }
721 }
722
723 /* Search ordinary names for GNU include directories. */
724 for (p = cpp_include_defaults; p->fname; p++)
725 {
726 /* Some standard dirs are only for C++. */
727 if (!p->cplusplus
728 || (CPP_OPTION (pfile, cplusplus)
729 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
730 {
731 /* XXX Potential memory leak! */
732 char *str = xstrdup (update_path (p->fname, p->component));
733 append_include_chain (pfile, CPP_OPTION (pfile, pending),
734 str, SYSTEM, p->cxx_aware);
735 }
736 }
737 }
738
739 /* This is called after options have been processed.
740 * Check options for consistency, and setup for processing input
741 * from the file named FNAME. (Use standard input if FNAME==NULL.)
742 * Return 1 on success, 0 on failure.
743 */
744
745 int
746 cpp_start_read (pfile, print, fname)
747 cpp_reader *pfile;
748 cpp_printer *print;
749 const char *fname;
750 {
751 struct pending_option *p, *q;
752
753 /* -MG doesn't select the form of output and must be specified with one of
754 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
755 inhibit compilation. */
756 if (CPP_OPTION (pfile, print_deps_missing_files)
757 && (CPP_OPTION (pfile, print_deps) == 0
758 || !CPP_OPTION (pfile, no_output)))
759 {
760 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
761 return 0;
762 }
763
764 /* Chill should not be used with -trigraphs. */
765 if (CPP_OPTION (pfile, chill) && CPP_OPTION (pfile, trigraphs))
766 {
767 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
768 CPP_OPTION (pfile, trigraphs) = 0;
769 }
770
771 /* -Wtraditional is not useful in C++ mode. */
772 if (CPP_OPTION (pfile, cplusplus))
773 CPP_OPTION (pfile, warn_traditional) = 0;
774
775 /* Do not warn about illegal token pasting if -traditional,
776 -lang-fortran, or -lang-asm. */
777 if (CPP_OPTION (pfile, traditional)
778 || CPP_OPTION (pfile, lang_fortran)
779 || CPP_OPTION (pfile, lang_asm))
780 CPP_OPTION (pfile, warn_paste) = 0;
781
782 /* Set this if it hasn't been set already. */
783 if (user_label_prefix == NULL)
784 user_label_prefix = USER_LABEL_PREFIX;
785
786 /* Figure out if we need to save function macro parameter spellings.
787 We don't use CPP_PEDANTIC() here because that depends on whether
788 or not the current file is a system header, and there is no
789 current file yet. */
790 pfile->save_parameter_spellings =
791 CPP_OPTION (pfile, pedantic)
792 || CPP_OPTION (pfile, debug_output)
793 || CPP_OPTION (pfile, dump_macros) == dump_definitions
794 || CPP_OPTION (pfile, dump_macros) == dump_only;
795
796 /* Set up the IStable. This doesn't do anything if we were compiled
797 with a compiler that supports C99 designated initializers. */
798 init_IStable ();
799
800 /* Set up the tables used by read_and_prescan. */
801 _cpp_init_input_buffer (pfile);
802
803 /* Set up the include search path now. */
804 if (! CPP_OPTION (pfile, no_standard_includes))
805 initialize_standard_includes (pfile);
806
807 merge_include_chains (pfile);
808
809 /* With -v, print the list of dirs to search. */
810 if (CPP_OPTION (pfile, verbose))
811 {
812 struct file_name_list *l;
813 fprintf (stderr, _("#include \"...\" search starts here:\n"));
814 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
815 {
816 if (l == CPP_OPTION (pfile, bracket_include))
817 fprintf (stderr, _("#include <...> search starts here:\n"));
818 fprintf (stderr, " %s\n", l->name);
819 }
820 fprintf (stderr, _("End of search list.\n"));
821 }
822
823 /* Open the main input file. This must be done early, so we have a
824 buffer to stand on. */
825 if (CPP_OPTION (pfile, in_fname) == NULL
826 || *CPP_OPTION (pfile, in_fname) == 0)
827 {
828 CPP_OPTION (pfile, in_fname) = fname;
829 if (CPP_OPTION (pfile, in_fname) == NULL)
830 CPP_OPTION (pfile, in_fname) = "";
831 }
832 if (CPP_OPTION (pfile, out_fname) == NULL)
833 CPP_OPTION (pfile, out_fname) = "";
834
835 if (!cpp_read_file (pfile, fname))
836 return 0;
837
838 initialize_dependency_output (pfile);
839
840 /* -D and friends may produce output, which should be identified
841 as line 0. */
842
843 CPP_BUFFER (pfile)->lineno = 0;
844 if (print)
845 {
846 print->last_fname = CPP_BUFFER (pfile)->nominal_fname;
847 print->last_id = pfile->include_depth;
848 print->written = CPP_WRITTEN (pfile);
849 print->lineno = 0;
850 }
851
852 /* Install __LINE__, etc. */
853 initialize_builtins (pfile);
854
855 /* Do -U's, -D's and -A's in the order they were seen. */
856 p = CPP_OPTION (pfile, pending)->directive_head;
857 while (p)
858 {
859 (*p->handler) (pfile, p->arg);
860 q = p->next;
861 free (p);
862 p = q;
863 }
864 pfile->done_initializing = 1;
865
866 /* Now flush any output recorded during initialization, and advance
867 to line 1 of the main input file. */
868 CPP_BUFFER (pfile)->lineno = 1;
869
870 if (print && ! CPP_OPTION (pfile, no_output))
871 cpp_output_tokens (pfile, print, 1);
872
873 /* The -imacros files can be scanned now, but the -include files
874 have to be pushed onto the include stack and processed later,
875 in the main loop calling cpp_get_token. */
876
877 p = CPP_OPTION (pfile, pending)->imacros_head;
878 while (p)
879 {
880 if (cpp_read_file (pfile, p->arg))
881 cpp_scan_buffer_nooutput (pfile);
882 q = p->next;
883 free (p);
884 p = q;
885 }
886
887 p = CPP_OPTION (pfile, pending)->include_head;
888 while (p)
889 {
890 if (cpp_read_file (pfile, p->arg)
891 && print && ! CPP_OPTION (pfile, no_output))
892 cpp_output_tokens (pfile, print, 1); /* record entry to file */
893 q = p->next;
894 free (p);
895 p = q;
896 }
897
898 free (CPP_OPTION (pfile, pending));
899 CPP_OPTION (pfile, pending) = NULL;
900
901 return 1;
902 }
903
904 /* This is called at the end of preprocessing. It pops the
905 last buffer and writes dependency output. It should also
906 clear macro definitions, such that you could call cpp_start_read
907 with a new filename to restart processing. */
908 void
909 cpp_finish (pfile, print)
910 cpp_reader *pfile;
911 cpp_printer *print;
912 {
913 if (CPP_BUFFER (pfile))
914 {
915 cpp_ice (pfile, "buffers still stacked in cpp_finish");
916 while (CPP_BUFFER (pfile))
917 cpp_pop_buffer (pfile);
918 }
919
920 /* Don't write the deps file if preprocessing has failed. */
921 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
922 {
923 /* Stream on which to print the dependency information. */
924 FILE *deps_stream = 0;
925 const char *deps_mode
926 = CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
927 if (CPP_OPTION (pfile, deps_file) == 0)
928 deps_stream = stdout;
929 else
930 {
931 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
932 if (deps_stream == 0)
933 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
934 }
935 if (deps_stream)
936 {
937 deps_write (pfile->deps, deps_stream, 72);
938 if (CPP_OPTION (pfile, deps_file))
939 {
940 if (ferror (deps_stream) || fclose (deps_stream) != 0)
941 cpp_fatal (pfile, "I/O error on output");
942 }
943 }
944 }
945
946 if (CPP_OPTION (pfile, dump_macros) == dump_only)
947 _cpp_dump_macro_hash (pfile);
948
949 /* Flush any pending output. */
950 if (print)
951 {
952 cpp_output_tokens (pfile, print, print->lineno);
953 if (ferror (print->outf) || fclose (print->outf))
954 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
955 }
956
957 /* Report on headers that could use multiple include guards. */
958 if (CPP_OPTION (pfile, print_include_names))
959 _cpp_report_missing_guards (pfile);
960 }
961
962 static void
963 new_pending_directive (pend, text, handler)
964 struct cpp_pending *pend;
965 const char *text;
966 cl_directive_handler handler;
967 {
968 struct pending_option *o = (struct pending_option *)
969 xmalloc (sizeof (struct pending_option));
970
971 o->arg = text;
972 o->next = NULL;
973 o->handler = handler;
974 APPEND (pend, directive, o);
975 }
976
977 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
978 I.e. a const string initializer with parens around it. That is
979 what N_("string") resolves to, so we make no_* be macros instead. */
980 #define no_arg N_("Argument missing after %s")
981 #define no_ass N_("Assertion missing after %s")
982 #define no_dir N_("Directory name missing after %s")
983 #define no_fil N_("File name missing after %s")
984 #define no_mac N_("Macro name missing after %s")
985 #define no_pth N_("Path name missing after %s")
986 #define no_num N_("Number missing after %s")
987
988 /* This is the list of all command line options, with the leading
989 "-" removed. It must be sorted in ASCII collating order. */
990 #define COMMAND_LINE_OPTIONS \
991 DEF_OPT("", 0, OPT_stdin_stdout) \
992 DEF_OPT("$", 0, OPT_dollar) \
993 DEF_OPT("+", 0, OPT_plus) \
994 DEF_OPT("-help", 0, OPT__help) \
995 DEF_OPT("-version", 0, OPT__version) \
996 DEF_OPT("A", no_ass, OPT_A) \
997 DEF_OPT("C", 0, OPT_C) \
998 DEF_OPT("D", no_mac, OPT_D) \
999 DEF_OPT("H", 0, OPT_H) \
1000 DEF_OPT("I", no_dir, OPT_I) \
1001 DEF_OPT("M", 0, OPT_M) \
1002 DEF_OPT("MD", no_fil, OPT_MD) \
1003 DEF_OPT("MG", 0, OPT_MG) \
1004 DEF_OPT("MM", 0, OPT_MM) \
1005 DEF_OPT("MMD", no_fil, OPT_MMD) \
1006 DEF_OPT("P", 0, OPT_P) \
1007 DEF_OPT("U", no_mac, OPT_U) \
1008 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1009 DEF_OPT("d", no_arg, OPT_d) \
1010 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1011 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1012 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1013 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1014 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1015 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1016 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1017 DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \
1018 DEF_OPT("h", 0, OPT_h) \
1019 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1020 DEF_OPT("imacros", no_fil, OPT_imacros) \
1021 DEF_OPT("include", no_fil, OPT_include) \
1022 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1023 DEF_OPT("isystem", no_dir, OPT_isystem) \
1024 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1025 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1026 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1027 DEF_OPT("lang-c", 0, OPT_lang_c) \
1028 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1029 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1030 DEF_OPT("lang-chill", 0, OPT_lang_chill) \
1031 DEF_OPT("lang-fortran", 0, OPT_lang_fortran) \
1032 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1033 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1034 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1035 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1036 DEF_OPT("o", no_fil, OPT_o) \
1037 DEF_OPT("pedantic", 0, OPT_pedantic) \
1038 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1039 DEF_OPT("remap", 0, OPT_remap) \
1040 DEF_OPT("std=c89", 0, OPT_std_c89) \
1041 DEF_OPT("std=c99", 0, OPT_std_c99) \
1042 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1043 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1044 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1045 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1046 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1047 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1048 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1049 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1050 DEF_OPT("traditional", 0, OPT_traditional) \
1051 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1052 DEF_OPT("v", 0, OPT_v) \
1053 DEF_OPT("w", 0, OPT_w)
1054
1055 #define DEF_OPT(text, msg, code) code,
1056 enum opt_code
1057 {
1058 COMMAND_LINE_OPTIONS
1059 N_OPTS
1060 };
1061 #undef DEF_OPT
1062
1063 struct cl_option
1064 {
1065 const char *opt_text;
1066 const char *msg;
1067 size_t opt_len;
1068 enum opt_code opt_code;
1069 };
1070
1071 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1072 #ifdef HOST_EBCDIC
1073 static struct cl_option cl_options[] =
1074 #else
1075 static const struct cl_option cl_options[] =
1076 #endif
1077 {
1078 COMMAND_LINE_OPTIONS
1079 };
1080 #undef DEF_OPT
1081 #undef COMMAND_LINE_OPTIONS
1082
1083 /* Perform a binary search to find which, if any, option the given
1084 command-line matches. Returns its index in the option array,
1085 negative on failure. Complications arise since some options can be
1086 suffixed with an argument, and multiple complete matches can occur,
1087 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1088 accept options beginning with -g and -W that we do not recognise,
1089 but not to swallow any subsequent command line argument; these are
1090 handled as special cases in cpp_handle_option */
1091 static int
1092 parse_option (input)
1093 const char *input;
1094 {
1095 unsigned int md, mn, mx;
1096 size_t opt_len;
1097 int comp;
1098
1099 mn = 0;
1100 mx = N_OPTS;
1101
1102 while (mx > mn)
1103 {
1104 md = (mn + mx) / 2;
1105
1106 opt_len = cl_options[md].opt_len;
1107 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1108
1109 if (comp > 0)
1110 mn = md + 1;
1111 else if (comp < 0)
1112 mx = md;
1113 else
1114 {
1115 if (input[opt_len] == '\0')
1116 return md;
1117 /* We were passed more text. If the option takes an argument,
1118 we may match a later option or we may have been passed the
1119 argument. The longest possible option match succeeds.
1120 If the option takes no arguments we have not matched and
1121 continue the search (e.g. input="stdc++" match was "stdc") */
1122 mn = md + 1;
1123 if (cl_options[md].msg)
1124 {
1125 /* Scan forwards. If we get an exact match, return it.
1126 Otherwise, return the longest option-accepting match.
1127 This loops no more than twice with current options */
1128 mx = md;
1129 for (; mn < N_OPTS; mn++)
1130 {
1131 opt_len = cl_options[mn].opt_len;
1132 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1133 break;
1134 if (input[opt_len] == '\0')
1135 return mn;
1136 if (cl_options[mn].msg)
1137 mx = mn;
1138 }
1139 return mx;
1140 }
1141 }
1142 }
1143
1144 return -1;
1145 }
1146
1147 /* Handle one command-line option in (argc, argv).
1148 Can be called multiple times, to handle multiple sets of options.
1149 Returns number of strings consumed. */
1150
1151 static int
1152 handle_option (pfile, argc, argv)
1153 cpp_reader *pfile;
1154 int argc;
1155 char **argv;
1156 {
1157 int i = 0;
1158 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1159
1160 if (argv[i][0] != '-')
1161 {
1162 if (CPP_OPTION (pfile, out_fname) != NULL)
1163 cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info",
1164 progname);
1165 else if (CPP_OPTION (pfile, in_fname) != NULL)
1166 CPP_OPTION (pfile, out_fname) = argv[i];
1167 else
1168 CPP_OPTION (pfile, in_fname) = argv[i];
1169 }
1170 else
1171 {
1172 enum opt_code opt_code;
1173 int opt_index;
1174 const char *arg = 0;
1175
1176 /* Skip over '-' */
1177 opt_index = parse_option (&argv[i][1]);
1178 if (opt_index < 0)
1179 return i;
1180
1181 opt_code = cl_options[opt_index].opt_code;
1182 if (cl_options[opt_index].msg)
1183 {
1184 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1185
1186 /* Yuk. Special case for -g and -W as they must not swallow
1187 up any following argument. If this becomes common, add
1188 another field to the cl_options table */
1189 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
1190 {
1191 arg = argv[++i];
1192 if (!arg)
1193 {
1194 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1195 return argc;
1196 }
1197 }
1198 }
1199
1200 switch (opt_code)
1201 {
1202 case N_OPTS: /* shut GCC up */
1203 break;
1204 case OPT_fleading_underscore:
1205 user_label_prefix = "_";
1206 break;
1207 case OPT_fno_leading_underscore:
1208 user_label_prefix = "";
1209 break;
1210 case OPT_fpreprocessed:
1211 CPP_OPTION (pfile, preprocessed) = 1;
1212 break;
1213 case OPT_fno_preprocessed:
1214 CPP_OPTION (pfile, preprocessed) = 0;
1215 break;
1216 case OPT_fshow_column:
1217 CPP_OPTION (pfile, show_column) = 1;
1218 break;
1219 case OPT_fno_show_column:
1220 CPP_OPTION (pfile, show_column) = 0;
1221 break;
1222 case OPT_ftabstop:
1223 /* Silently ignore empty string, non-longs and silly values. */
1224 if (arg[0] != '\0')
1225 {
1226 char *endptr;
1227 long tabstop = strtol (arg, &endptr, 10);
1228 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1229 CPP_OPTION (pfile, tabstop) = tabstop;
1230 }
1231 break;
1232 case OPT_w:
1233 CPP_OPTION (pfile, inhibit_warnings) = 1;
1234 break;
1235 case OPT_g: /* Silently ignore anything but -g3 */
1236 if (!strcmp(&argv[i][2], "3"))
1237 CPP_OPTION (pfile, debug_output) = 1;
1238 break;
1239 case OPT_h:
1240 case OPT__help:
1241 print_help ();
1242 exit (0); /* XXX */
1243 break;
1244 case OPT__version:
1245 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1246 exit (0); /* XXX */
1247 break;
1248 case OPT_C:
1249 CPP_OPTION (pfile, discard_comments) = 0;
1250 break;
1251 case OPT_P:
1252 CPP_OPTION (pfile, no_line_commands) = 1;
1253 break;
1254 case OPT_dollar: /* Don't include $ in identifiers. */
1255 CPP_OPTION (pfile, dollars_in_ident) = 0;
1256 break;
1257 case OPT_H:
1258 CPP_OPTION (pfile, print_include_names) = 1;
1259 break;
1260 case OPT_D:
1261 new_pending_directive (pend, arg, cpp_define);
1262 break;
1263 case OPT_pedantic_errors:
1264 CPP_OPTION (pfile, pedantic_errors) = 1;
1265 /* fall through */
1266 case OPT_pedantic:
1267 CPP_OPTION (pfile, pedantic) = 1;
1268 break;
1269 case OPT_traditional:
1270 CPP_OPTION (pfile, traditional) = 1;
1271 CPP_OPTION (pfile, cplusplus_comments) = 0;
1272 CPP_OPTION (pfile, trigraphs) = 0;
1273 CPP_OPTION (pfile, warn_trigraphs) = 0;
1274 break;
1275 case OPT_trigraphs:
1276 CPP_OPTION (pfile, trigraphs) = 1;
1277 break;
1278 case OPT_plus:
1279 CPP_OPTION (pfile, cplusplus) = 1;
1280 CPP_OPTION (pfile, cplusplus_comments) = 1;
1281 break;
1282 case OPT_remap:
1283 CPP_OPTION (pfile, remap) = 1;
1284 break;
1285 case OPT_iprefix:
1286 CPP_OPTION (pfile, include_prefix) = arg;
1287 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1288 break;
1289 case OPT_lang_c:
1290 CPP_OPTION (pfile, cplusplus) = 0;
1291 CPP_OPTION (pfile, cplusplus_comments) = 1;
1292 CPP_OPTION (pfile, c89) = 0;
1293 CPP_OPTION (pfile, c99) = 1;
1294 CPP_OPTION (pfile, objc) = 0;
1295 break;
1296 case OPT_lang_c89:
1297 CPP_OPTION (pfile, cplusplus) = 0;
1298 CPP_OPTION (pfile, cplusplus_comments) = 0;
1299 CPP_OPTION (pfile, c89) = 1;
1300 CPP_OPTION (pfile, c99) = 0;
1301 CPP_OPTION (pfile, objc) = 0;
1302 CPP_OPTION (pfile, trigraphs) = 1;
1303 new_pending_directive (pend, "__STRICT_ANSI__", cpp_define);
1304 break;
1305 case OPT_lang_cplusplus:
1306 CPP_OPTION (pfile, cplusplus) = 1;
1307 CPP_OPTION (pfile, cplusplus_comments) = 1;
1308 CPP_OPTION (pfile, c89) = 0;
1309 CPP_OPTION (pfile, c99) = 0;
1310 CPP_OPTION (pfile, objc) = 0;
1311 new_pending_directive (pend, "__cplusplus", cpp_define);
1312 break;
1313 case OPT_lang_objcplusplus:
1314 CPP_OPTION (pfile, cplusplus) = 1;
1315 new_pending_directive (pend, "__cplusplus", cpp_define);
1316 /* fall through */
1317 case OPT_lang_objc:
1318 CPP_OPTION (pfile, cplusplus_comments) = 1;
1319 CPP_OPTION (pfile, c89) = 0;
1320 CPP_OPTION (pfile, c99) = 0;
1321 CPP_OPTION (pfile, objc) = 1;
1322 new_pending_directive (pend, "__OBJC__", cpp_define);
1323 break;
1324 case OPT_lang_asm:
1325 CPP_OPTION (pfile, lang_asm) = 1;
1326 CPP_OPTION (pfile, dollars_in_ident) = 0;
1327 new_pending_directive (pend, "__ASSEMBLER__", cpp_define);
1328 break;
1329 case OPT_lang_fortran:
1330 CPP_OPTION (pfile, lang_fortran) = 1;
1331 CPP_OPTION (pfile, traditional) = 1;
1332 CPP_OPTION (pfile, cplusplus_comments) = 0;
1333 new_pending_directive (pend, "_LANGUAGE_FORTRAN", cpp_define);
1334 break;
1335 case OPT_lang_chill:
1336 CPP_OPTION (pfile, objc) = 0;
1337 CPP_OPTION (pfile, cplusplus) = 0;
1338 CPP_OPTION (pfile, chill) = 1;
1339 CPP_OPTION (pfile, traditional) = 1;
1340 break;
1341 case OPT_nostdinc:
1342 /* -nostdinc causes no default include directories.
1343 You must specify all include-file directories with -I. */
1344 CPP_OPTION (pfile, no_standard_includes) = 1;
1345 break;
1346 case OPT_nostdincplusplus:
1347 /* -nostdinc++ causes no default C++-specific include directories. */
1348 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1349 break;
1350 case OPT_std_gnu89:
1351 CPP_OPTION (pfile, cplusplus) = 0;
1352 CPP_OPTION (pfile, cplusplus_comments) = 1;
1353 CPP_OPTION (pfile, c89) = 1;
1354 CPP_OPTION (pfile, c99) = 0;
1355 CPP_OPTION (pfile, objc) = 0;
1356 break;
1357 case OPT_std_gnu9x:
1358 case OPT_std_gnu99:
1359 CPP_OPTION (pfile, cplusplus) = 0;
1360 CPP_OPTION (pfile, cplusplus_comments) = 1;
1361 CPP_OPTION (pfile, c89) = 0;
1362 CPP_OPTION (pfile, c99) = 1;
1363 CPP_OPTION (pfile, objc) = 0;
1364 new_pending_directive (CPP_OPTION (pfile, pending),
1365 "__STDC_VERSION__=199901L", cpp_define);
1366 break;
1367 case OPT_std_iso9899_199409:
1368 new_pending_directive (CPP_OPTION (pfile, pending),
1369 "__STDC_VERSION__=199409L", cpp_define);
1370 /* Fall through */
1371 case OPT_std_iso9899_1990:
1372 case OPT_std_c89:
1373 CPP_OPTION (pfile, cplusplus) = 0;
1374 CPP_OPTION (pfile, cplusplus_comments) = 0;
1375 CPP_OPTION (pfile, c89) = 1;
1376 CPP_OPTION (pfile, c99) = 0;
1377 CPP_OPTION (pfile, objc) = 0;
1378 CPP_OPTION (pfile, trigraphs) = 1;
1379 new_pending_directive (CPP_OPTION (pfile, pending),
1380 "__STRICT_ANSI__", cpp_define);
1381 break;
1382 case OPT_std_iso9899_199x:
1383 case OPT_std_iso9899_1999:
1384 case OPT_std_c9x:
1385 case OPT_std_c99:
1386 CPP_OPTION (pfile, cplusplus) = 0;
1387 CPP_OPTION (pfile, cplusplus_comments) = 1;
1388 CPP_OPTION (pfile, c89) = 0;
1389 CPP_OPTION (pfile, c99) = 1;
1390 CPP_OPTION (pfile, objc) = 0;
1391 CPP_OPTION (pfile, trigraphs) = 1;
1392 new_pending_directive (CPP_OPTION (pfile, pending),
1393 "__STRICT_ANSI__", cpp_define);
1394 new_pending_directive (CPP_OPTION (pfile, pending),
1395 "__STDC_VERSION__=199901L", cpp_define);
1396 break;
1397 case OPT_o:
1398 if (CPP_OPTION (pfile, out_fname) != NULL)
1399 {
1400 cpp_fatal (pfile, "Output filename specified twice");
1401 return argc;
1402 }
1403 CPP_OPTION (pfile, out_fname) = arg;
1404 if (!strcmp (CPP_OPTION (pfile, out_fname), "-"))
1405 CPP_OPTION (pfile, out_fname) = "";
1406 break;
1407 case OPT_v:
1408 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1409 #ifdef TARGET_VERSION
1410 TARGET_VERSION;
1411 #endif
1412 fputc ('\n', stderr);
1413 CPP_OPTION (pfile, verbose) = 1;
1414 break;
1415 case OPT_stdin_stdout:
1416 /* JF handle '-' as file name meaning stdin or stdout */
1417 if (CPP_OPTION (pfile, in_fname) == NULL)
1418 CPP_OPTION (pfile, in_fname) = "";
1419 else if (CPP_OPTION (pfile, out_fname) == NULL)
1420 CPP_OPTION (pfile, out_fname) = "";
1421 break;
1422 case OPT_d:
1423 /* Args to -d specify what parts of macros to dump.
1424 Silently ignore unrecognised options; they may
1425 be aimed at the compiler proper. */
1426 {
1427 char c;
1428
1429 while ((c = *arg++) != '\0')
1430 switch (c)
1431 {
1432 case 'M':
1433 CPP_OPTION (pfile, dump_macros) = dump_only;
1434 CPP_OPTION (pfile, no_output) = 1;
1435 break;
1436 case 'N':
1437 CPP_OPTION (pfile, dump_macros) = dump_names;
1438 break;
1439 case 'D':
1440 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1441 break;
1442 case 'I':
1443 CPP_OPTION (pfile, dump_includes) = 1;
1444 break;
1445 }
1446 }
1447 break;
1448 /* The style of the choices here is a bit mixed.
1449 The chosen scheme is a hybrid of keeping all options in one string
1450 and specifying each option in a separate argument:
1451 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1452 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1453 -M[M][G][D file]. This is awkward to handle in specs, and is not
1454 as extensible. */
1455 /* ??? -MG must be specified in addition to one of -M or -MM.
1456 This can be relaxed in the future without breaking anything.
1457 The converse isn't true. */
1458
1459 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1460 case OPT_MG:
1461 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1462 break;
1463 case OPT_M:
1464 case OPT_MD:
1465 case OPT_MM:
1466 case OPT_MMD:
1467 if (opt_code == OPT_M || opt_code == OPT_MD)
1468 CPP_OPTION (pfile, print_deps) = 2;
1469 else
1470 CPP_OPTION (pfile, print_deps) = 1;
1471
1472 /* For -MD and -MMD options, write deps on file named by next arg */
1473 /* For -M and -MM, write deps on standard output
1474 and suppress the usual output. */
1475 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1476 CPP_OPTION (pfile, deps_file) = arg;
1477 else
1478 CPP_OPTION (pfile, no_output) = 1;
1479 break;
1480 case OPT_A:
1481 if (arg[0] == '-')
1482 {
1483 /* -A with an argument beginning with '-' acts as
1484 #unassert on whatever immediately follows the '-'.
1485 If "-" is the whole argument, we eliminate all
1486 predefined macros and assertions, including those
1487 that were specified earlier on the command line.
1488 That way we can get rid of any that were passed
1489 automatically in from GCC. */
1490
1491 if (arg[1] == '\0')
1492 {
1493 struct pending_option *o1, *o2;
1494
1495 o1 = CPP_OPTION (pfile, pending)->directive_head;
1496 while (o1)
1497 {
1498 o2 = o1->next;
1499 free (o1);
1500 o1 = o2;
1501 }
1502 CPP_OPTION (pfile, pending)->directive_head = NULL;
1503 CPP_OPTION (pfile, pending)->directive_tail = NULL;
1504 }
1505 else
1506 new_pending_directive (CPP_OPTION (pfile, pending),
1507 arg + 1, cpp_unassert);
1508 }
1509 else
1510 new_pending_directive (CPP_OPTION (pfile, pending),
1511 arg, cpp_assert);
1512 break;
1513 case OPT_U:
1514 new_pending_directive (CPP_OPTION (pfile, pending), arg, cpp_undef);
1515 break;
1516 case OPT_I: /* Add directory to path for includes. */
1517 if (!strcmp (arg, "-"))
1518 {
1519 /* -I- means:
1520 Use the preceding -I directories for #include "..."
1521 but not #include <...>.
1522 Don't search the directory of the present file
1523 for #include "...". (Note that -I. -I- is not the same as
1524 the default setup; -I. uses the compiler's working dir.) */
1525 if (! CPP_OPTION (pfile, ignore_srcdir))
1526 {
1527 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1528 pend->quote_head = pend->brack_head;
1529 pend->quote_tail = pend->brack_tail;
1530 pend->brack_head = 0;
1531 pend->brack_tail = 0;
1532 CPP_OPTION (pfile, ignore_srcdir) = 1;
1533 }
1534 else
1535 {
1536 cpp_fatal (pfile, "-I- specified twice");
1537 return argc;
1538 }
1539 }
1540 else
1541 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1542 xstrdup (arg), BRACKET, 0);
1543 break;
1544 case OPT_isystem:
1545 /* Add directory to beginning of system include path, as a system
1546 include directory. */
1547 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1548 xstrdup (arg), SYSTEM, 0);
1549 break;
1550 case OPT_include:
1551 {
1552 struct pending_option *o = (struct pending_option *)
1553 xmalloc (sizeof (struct pending_option));
1554 o->arg = arg;
1555
1556 /* This list has to be built in reverse order so that
1557 when cpp_start_read pushes all the -include files onto
1558 the buffer stack, they will be scanned in forward order. */
1559 o->next = CPP_OPTION (pfile, pending)->include_head;
1560 CPP_OPTION (pfile, pending)->include_head = o;
1561 }
1562 break;
1563 case OPT_imacros:
1564 {
1565 struct pending_option *o = (struct pending_option *)
1566 xmalloc (sizeof (struct pending_option));
1567 o->arg = arg;
1568 o->next = NULL;
1569
1570 APPEND (CPP_OPTION (pfile, pending), imacros, o);
1571 }
1572 break;
1573 case OPT_iwithprefix:
1574 /* Add directory to end of path for includes,
1575 with the default prefix at the front of its name. */
1576 /* fall through */
1577 case OPT_iwithprefixbefore:
1578 /* Add directory to main path for includes,
1579 with the default prefix at the front of its name. */
1580 {
1581 char *fname;
1582 int len;
1583
1584 len = strlen (arg);
1585
1586 if (CPP_OPTION (pfile, include_prefix) != 0)
1587 {
1588 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1589 fname = xmalloc (ipl + len + 1);
1590 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1591 memcpy (fname + ipl, arg, len + 1);
1592 }
1593 else if (cpp_GCC_INCLUDE_DIR_len)
1594 {
1595 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1596 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1597 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1598 }
1599 else
1600 fname = xstrdup (arg);
1601
1602 append_include_chain (pfile, CPP_OPTION (pfile, pending), fname,
1603 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1604 }
1605 break;
1606 case OPT_idirafter:
1607 /* Add directory to end of path for includes. */
1608 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1609 xstrdup (arg), AFTER, 0);
1610 break;
1611 case OPT_W:
1612 /* Silently ignore unrecognised options */
1613 if (!strcmp (argv[i], "-Wall"))
1614 {
1615 CPP_OPTION (pfile, warn_trigraphs) = 1;
1616 CPP_OPTION (pfile, warn_comments) = 1;
1617 }
1618 else if (!strcmp (argv[i], "-Wtraditional"))
1619 CPP_OPTION (pfile, warn_traditional) = 1;
1620 else if (!strcmp (argv[i], "-Wtrigraphs"))
1621 CPP_OPTION (pfile, warn_trigraphs) = 1;
1622 else if (!strcmp (argv[i], "-Wcomment"))
1623 CPP_OPTION (pfile, warn_comments) = 1;
1624 else if (!strcmp (argv[i], "-Wcomments"))
1625 CPP_OPTION (pfile, warn_comments) = 1;
1626 else if (!strcmp (argv[i], "-Wundef"))
1627 CPP_OPTION (pfile, warn_undef) = 1;
1628 else if (!strcmp (argv[i], "-Wimport"))
1629 CPP_OPTION (pfile, warn_import) = 1;
1630 else if (!strcmp (argv[i], "-Wpaste"))
1631 CPP_OPTION (pfile, warn_paste) = 1;
1632 else if (!strcmp (argv[i], "-Werror"))
1633 CPP_OPTION (pfile, warnings_are_errors) = 1;
1634 else if (!strcmp (argv[i], "-Wno-traditional"))
1635 CPP_OPTION (pfile, warn_traditional) = 0;
1636 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1637 CPP_OPTION (pfile, warn_trigraphs) = 0;
1638 else if (!strcmp (argv[i], "-Wno-comment"))
1639 CPP_OPTION (pfile, warn_comments) = 0;
1640 else if (!strcmp (argv[i], "-Wno-comments"))
1641 CPP_OPTION (pfile, warn_comments) = 0;
1642 else if (!strcmp (argv[i], "-Wno-undef"))
1643 CPP_OPTION (pfile, warn_undef) = 0;
1644 else if (!strcmp (argv[i], "-Wno-import"))
1645 CPP_OPTION (pfile, warn_import) = 0;
1646 else if (!strcmp (argv[i], "-Wno-paste"))
1647 CPP_OPTION (pfile, warn_paste) = 0;
1648 else if (!strcmp (argv[i], "-Wno-error"))
1649 CPP_OPTION (pfile, warnings_are_errors) = 0;
1650 break;
1651 }
1652 }
1653 return i + 1;
1654 }
1655
1656 #ifdef HOST_EBCDIC
1657 static int
1658 opt_comp (const void *p1, const void *p2)
1659 {
1660 return strcmp (((struct cl_option *)p1)->opt_text,
1661 ((struct cl_option *)p2)->opt_text);
1662 }
1663 #endif
1664
1665 /* Handle command-line options in (argc, argv).
1666 Can be called multiple times, to handle multiple sets of options.
1667 Returns if an unrecognized option is seen.
1668 Returns number of strings consumed. */
1669 int
1670 cpp_handle_options (pfile, argc, argv)
1671 cpp_reader *pfile;
1672 int argc;
1673 char **argv;
1674 {
1675 int i;
1676 int strings_processed;
1677
1678 #ifdef HOST_EBCDIC
1679 static int opts_sorted = 0;
1680
1681 if (!opts_sorted)
1682 {
1683 opts_sorted = 1;
1684 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1685 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1686 }
1687 #endif
1688
1689 for (i = 0; i < argc; i += strings_processed)
1690 {
1691 strings_processed = handle_option (pfile, argc - i, argv + i);
1692 if (strings_processed == 0)
1693 break;
1694 }
1695 return i;
1696 }
1697
1698 static void
1699 print_help ()
1700 {
1701 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1702 /* To keep the lines from getting too long for some compilers, limit
1703 to about 500 characters (6 lines) per chunk. */
1704 fputs (_("\
1705 Switches:\n\
1706 -include <file> Include the contents of <file> before other files\n\
1707 -imacros <file> Accept definition of macros in <file>\n\
1708 -iprefix <path> Specify <path> as a prefix for next two options\n\
1709 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1710 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1711 -isystem <dir> Add <dir> to the start of the system include path\n\
1712 "), stdout);
1713 fputs (_("\
1714 -idirafter <dir> Add <dir> to the end of the system include path\n\
1715 -I <dir> Add <dir> to the end of the main include path\n\
1716 -I- Fine-grained include path control; see info docs\n\
1717 -nostdinc Do not search system include directories\n\
1718 (dirs specified with -isystem will still be used)\n\
1719 -nostdinc++ Do not search system include directories for C++\n\
1720 -o <file> Put output into <file>\n\
1721 "), stdout);
1722 fputs (_("\
1723 -pedantic Issue all warnings demanded by strict ISO C\n\
1724 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1725 -traditional Follow K&R pre-processor behaviour\n\
1726 -trigraphs Support ISO C trigraphs\n\
1727 -lang-c Assume that the input sources are in C\n\
1728 -lang-c89 Assume that the input sources are in C89\n\
1729 -lang-c++ Assume that the input sources are in C++\n\
1730 "), stdout);
1731 fputs (_("\
1732 -lang-objc Assume that the input sources are in ObjectiveC\n\
1733 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1734 -lang-asm Assume that the input sources are in assembler\n\
1735 -lang-fortran Assume that the input sources are in Fortran\n\
1736 -lang-chill Assume that the input sources are in Chill\n\
1737 "), stdout);
1738 fputs (_("\
1739 -std=<std name> Specify the conformance standard; one of:\n\
1740 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1741 iso9899:199409, iso9899:1999\n\
1742 -+ Allow parsing of C++ style features\n\
1743 -w Inhibit warning messages\n\
1744 -Wtrigraphs Warn if trigraphs are encountered\n\
1745 -Wno-trigraphs Do not warn about trigraphs\n\
1746 -Wcomment{s} Warn if one comment starts inside another\n\
1747 "), stdout);
1748 fputs (_("\
1749 -Wno-comment{s} Do not warn about comments\n\
1750 -Wtraditional Warn if a macro argument is/would be turned into\n\
1751 a string if -traditional is specified\n\
1752 -Wno-traditional Do not warn about stringification\n\
1753 -Wundef Warn if an undefined macro is used by #if\n\
1754 -Wno-undef Do not warn about testing undefined macros\n\
1755 -Wimport Warn about the use of the #import directive\n\
1756 "), stdout);
1757 fputs (_("\
1758 -Wno-import Do not warn about the use of #import\n\
1759 -Werror Treat all warnings as errors\n\
1760 -Wno-error Do not treat warnings as errors\n\
1761 -Wall Enable all preprocessor warnings\n\
1762 -M Generate make dependencies\n\
1763 -MM As -M, but ignore system header files\n\
1764 "), stdout);
1765 fputs (_("\
1766 -MD As -M, but put output in a .d file\n\
1767 -MMD As -MD, but ignore system header files\n\
1768 -MG Treat missing header file as generated files\n\
1769 -g3 Include #define and #undef directives in the output\n\
1770 -D<macro> Define a <macro> with string '1' as its value\n\
1771 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1772 "), stdout);
1773 fputs (_("\
1774 -A<question> (<answer>) Assert the <answer> to <question>\n\
1775 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1776 -U<macro> Undefine <macro> \n\
1777 -v Display the version number\n\
1778 -H Print the name of header files as they are used\n\
1779 -C Do not discard comments\n\
1780 "), stdout);
1781 fputs (_("\
1782 -dM Display a list of macro definitions active at end\n\
1783 -dD Preserve macro definitions in output\n\
1784 -dN As -dD except that only the names are preserved\n\
1785 -dI Include #include directives in the output\n\
1786 -ftabstop=<number> Distance between tab stops for column reporting\n\
1787 -P Do not generate #line directives\n\
1788 -$ Do not allow '$' in identifiers\n\
1789 "), stdout);
1790 fputs (_("\
1791 -remap Remap file names when including files.\n\
1792 --version Display version information\n\
1793 -h or --help Display this information\n\
1794 "), stdout);
1795 }
This page took 0.138174 seconds and 5 git commands to generate.