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