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