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