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