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