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