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