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