]> gcc.gnu.org Git - gcc.git/blame - gcc/cppinit.c
* cppinit.c (remove_dup_nonsys_dirs): Fix warning and return value.
[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"
49e6c08e 28#include "mkdeps.h"
60893f43 29#include "cppdefault.h"
6de1e2a9 30
88ae23e7
ZW
31/* Windows does not natively support inodes, and neither does MSDOS.
32 Cygwin's emulation can generate non-unique inodes, so don't use it.
ec5c56db 33 VMS has non-numeric inodes. */
88ae23e7 34#ifdef VMS
ca47c89e
DR
35# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
36# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
88ae23e7 37#else
3943e756 38# if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
ca47c89e 39# define INO_T_EQ(A, B) 0
3943e756 40# else
ca47c89e 41# define INO_T_EQ(A, B) ((A) == (B))
3943e756 42# endif
ca47c89e 43# define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
88ae23e7
ZW
44#endif
45
4a58aab6 46/* Internal structures and prototypes. */
6de1e2a9 47
4a58aab6
NB
48/* A `struct pending_option' remembers one -D, -A, -U, -include, or
49 -imacros switch. */
8be1ddca 50typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
0b22d65c 51struct pending_option
6de1e2a9 52{
0b22d65c 53 struct pending_option *next;
7ceb3598 54 const char *arg;
40eac643 55 cl_directive_handler handler;
6de1e2a9 56};
0b22d65c 57
88ae23e7 58/* The `pending' structure accumulates all the options that are not
f5e99456 59 actually processed until we hit cpp_read_main_file. It consists of
88ae23e7 60 several lists, one for each type of option. We keep both head and
4a58aab6 61 tail pointers for quick insertion. */
88ae23e7
ZW
62struct cpp_pending
63{
40eac643 64 struct pending_option *directive_head, *directive_tail;
88ae23e7 65
591e15a1
NB
66 struct search_path *quote_head, *quote_tail;
67 struct search_path *brack_head, *brack_tail;
68 struct search_path *systm_head, *systm_tail;
69 struct search_path *after_head, *after_tail;
88ae23e7
ZW
70
71 struct pending_option *imacros_head, *imacros_tail;
72 struct pending_option *include_head, *include_tail;
73};
74
0b22d65c
ZW
75#ifdef __STDC__
76#define APPEND(pend, list, elt) \
77 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
78 else (pend)->list##_tail->next = (elt); \
79 (pend)->list##_tail = (elt); \
80 } while (0)
81#else
82#define APPEND(pend, list, elt) \
83 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
84 else (pend)->list/**/_tail->next = (elt); \
85 (pend)->list/**/_tail = (elt); \
86 } while (0)
87#endif
6de1e2a9 88
0b22d65c 89static void path_include PARAMS ((cpp_reader *,
0b22d65c 90 char *, int));
674c3b40 91static void init_library PARAMS ((void));
7ca3d2b1 92static void init_builtins PARAMS ((cpp_reader *));
17645b15 93static void mark_named_operators PARAMS ((cpp_reader *));
0b22d65c 94static void append_include_chain PARAMS ((cpp_reader *,
c45da1ca 95 char *, int, int));
002ee64f 96static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
48209ce5
JDA
97 struct search_path *,
98 struct search_path **));
99static struct search_path * remove_dup_nonsys_dirs PARAMS ((cpp_reader *,
100 struct search_path **,
591e15a1 101 struct search_path *));
002ee64f 102static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
48209ce5 103 struct search_path **));
ae79697b 104static void merge_include_chains PARAMS ((cpp_reader *));
d7bc7a98
NB
105static bool push_include PARAMS ((cpp_reader *,
106 struct pending_option *));
107static void free_chain PARAMS ((struct pending_option *));
7ca3d2b1 108static void init_standard_includes PARAMS ((cpp_reader *));
f5e99456 109static void read_original_filename PARAMS ((cpp_reader *));
2c0accc9 110static void new_pending_directive PARAMS ((struct cpp_pending *,
40eac643
NB
111 const char *,
112 cl_directive_handler));
e23c0ba3 113static int parse_option PARAMS ((const char *));
f4ff5a69 114static void post_options PARAMS ((cpp_reader *));
6de1e2a9 115
cb773845
ZW
116/* Fourth argument to append_include_chain: chain to use.
117 Note it's never asked to append to the quote chain. */
118enum { BRACKET = 0, SYSTEM, AFTER };
6de1e2a9 119
61d0346d
NB
120/* If we have designated initializers (GCC >2.7) these tables can be
121 initialized, constant data. Otherwise, they have to be filled in at
12cf91fe 122 runtime. */
61d0346d 123#if HAVE_DESIGNATED_INITIALIZERS
a9ae4483 124
4a58aab6 125#define init_trigraph_map() /* Nothing. */
61d0346d 126#define TRIGRAPH_MAP \
562a5c27 127__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
61d0346d 128
a9ae4483 129#define END };
455d2586 130#define s(p, v) [p] = v,
61d0346d 131
a9ae4483 132#else
61d0346d 133
562a5c27 134#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
61d0346d
NB
135 static void init_trigraph_map PARAMS ((void)) { \
136 unsigned char *x = _cpp_trigraph_map;
137
ae79697b 138#define END }
455d2586 139#define s(p, v) x[p] = v;
61d0346d 140
a9ae4483 141#endif
6de1e2a9 142
61d0346d
NB
143TRIGRAPH_MAP
144 s('=', '#') s(')', ']') s('!', '|')
145 s('(', '[') s('\'', '^') s('>', '}')
146 s('/', '\\') s('<', '{') s('-', '~')
147END
148
a9ae4483 149#undef s
455d2586 150#undef END
61d0346d 151#undef TRIGRAPH_MAP
6de1e2a9
ZW
152
153/* Given a colon-separated list of file names PATH,
154 add all the names to the search path for include files. */
6de1e2a9 155static void
e33f6253 156path_include (pfile, list, path)
6de1e2a9 157 cpp_reader *pfile;
0b22d65c
ZW
158 char *list;
159 int path;
6de1e2a9 160{
0b22d65c 161 char *p, *q, *name;
6de1e2a9 162
0b22d65c 163 p = list;
6de1e2a9 164
0b22d65c
ZW
165 do
166 {
6de1e2a9 167 /* Find the end of this name. */
0b22d65c 168 q = p;
6de1e2a9 169 while (*q != 0 && *q != PATH_SEPARATOR) q++;
0b22d65c
ZW
170 if (q == p)
171 {
172 /* An empty name in the path stands for the current directory. */
173 name = (char *) xmalloc (2);
174 name[0] = '.';
175 name[1] = 0;
176 }
177 else
178 {
179 /* Otherwise use the directory that is named. */
180 name = (char *) xmalloc (q - p + 1);
181 memcpy (name, p, q - p);
182 name[q - p] = 0;
183 }
6de1e2a9 184
463f1b2b 185 append_include_chain (pfile, name, path, path == SYSTEM);
6de1e2a9
ZW
186
187 /* Advance past this name. */
0b22d65c 188 if (*q == 0)
6de1e2a9 189 break;
0b22d65c
ZW
190 p = q + 1;
191 }
192 while (1);
193}
194
bef985f3 195/* Append DIR to include path PATH. DIR must be allocated on the
5d8ebbd8
NB
196 heap; this routine takes responsibility for freeing it. CXX_AWARE
197 is non-zero if the header contains extern "C" guards for C++,
198 otherwise it is zero. */
0b22d65c 199static void
e33f6253 200append_include_chain (pfile, dir, path, cxx_aware)
0b22d65c 201 cpp_reader *pfile;
0b22d65c
ZW
202 char *dir;
203 int path;
39e5db1a 204 int cxx_aware;
0b22d65c 205{
e33f6253 206 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
591e15a1 207 struct search_path *new;
0b22d65c
ZW
208 struct stat st;
209 unsigned int len;
210
f9200da2 211 if (*dir == '\0')
bef985f3
NB
212 {
213 free (dir);
214 dir = xstrdup (".");
215 }
b0699dad 216 _cpp_simplify_pathname (dir);
bef985f3 217
0b22d65c
ZW
218 if (stat (dir, &st))
219 {
f9200da2 220 /* Dirs that don't exist are silently ignored. */
0b22d65c 221 if (errno != ENOENT)
ebef4e8c 222 cpp_errno (pfile, DL_ERROR, dir);
ae79697b 223 else if (CPP_OPTION (pfile, verbose))
041c3194 224 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
bef985f3 225 free (dir);
0b22d65c
ZW
226 return;
227 }
228
229 if (!S_ISDIR (st.st_mode))
230 {
ebef4e8c 231 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
bef985f3 232 free (dir);
0b22d65c
ZW
233 return;
234 }
235
236 len = strlen (dir);
237 if (len > pfile->max_include_len)
238 pfile->max_include_len = len;
ae79697b 239
591e15a1 240 new = (struct search_path *) xmalloc (sizeof (struct search_path));
0b22d65c 241 new->name = dir;
591e15a1 242 new->len = len;
ca47c89e 243 INO_T_COPY (new->ino, st.st_ino);
0b22d65c 244 new->dev = st.st_dev;
4737b274
CF
245 /* Both systm and after include file lists should be treated as system
246 include files since these two lists are really just a concatenation
ec5c56db 247 of one "system" list. */
4737b274 248 if (path == SYSTEM || path == AFTER)
c45da1ca
ZW
249 new->sysp = cxx_aware ? 1 : 2;
250 else
251 new->sysp = 0;
0b22d65c 252 new->name_map = NULL;
503cb436 253 new->next = NULL;
0b22d65c
ZW
254
255 switch (path)
256 {
0b22d65c
ZW
257 case BRACKET: APPEND (pend, brack, new); break;
258 case SYSTEM: APPEND (pend, systm, new); break;
259 case AFTER: APPEND (pend, after, new); break;
6de1e2a9
ZW
260 }
261}
262
dd69c71b 263/* Handle a duplicated include path. PREV is the link in the chain
48209ce5
JDA
264 before the duplicate, or NULL if the duplicate is at the head of
265 the chain. The duplicate is removed from the chain and freed.
266 Returns PREV. */
002ee64f 267static struct search_path *
48209ce5 268remove_dup_dir (pfile, prev, head_ptr)
dd69c71b 269 cpp_reader *pfile;
591e15a1 270 struct search_path *prev;
48209ce5 271 struct search_path **head_ptr;
dd69c71b 272{
48209ce5
JDA
273 struct search_path *cur;
274
275 if (prev != NULL)
276 {
277 cur = prev->next;
278 prev->next = cur->next;
279 }
280 else
281 {
282 cur = *head_ptr;
283 *head_ptr = cur->next;
284 }
dd69c71b
NB
285
286 if (CPP_OPTION (pfile, verbose))
287 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
288
591e15a1 289 free ((PTR) cur->name);
dd69c71b
NB
290 free (cur);
291
292 return prev;
293}
294
48209ce5
JDA
295/* Remove duplicate non-system directories for which there is an equivalent
296 system directory latter in the chain. The range for removal is between
297 *HEAD_PTR and END. Returns the directory before END, or NULL if none.
298 This algorithm is quadratic in the number system directories, which is
299 acceptable since there aren't usually that many of them. */
300static struct search_path *
301remove_dup_nonsys_dirs (pfile, head_ptr, end)
302 cpp_reader *pfile;
303 struct search_path **head_ptr;
304 struct search_path *end;
305{
d873d827
JDA
306 int sysdir = 0;
307 struct search_path *prev = NULL, *cur, *other;
48209ce5
JDA
308
309 for (cur = *head_ptr; cur; cur = cur->next)
310 {
311 if (cur->sysp)
312 {
d873d827 313 sysdir = 1;
48209ce5
JDA
314 for (other = *head_ptr, prev = NULL;
315 other != end;
316 other = other ? other->next : *head_ptr)
317 {
318 if (!other->sysp
319 && INO_T_EQ (cur->ino, other->ino)
320 && cur->dev == other->dev)
321 {
322 other = remove_dup_dir (pfile, prev, head_ptr);
323 if (CPP_OPTION (pfile, verbose))
324 fprintf (stderr,
325 _(" as it is a non-system directory that duplicates a system directory\n"));
326 }
327 prev = other;
328 }
329 }
330 }
331
d873d827
JDA
332 if (!sysdir)
333 for (cur = *head_ptr; cur != end; cur = cur->next)
334 prev = cur;
335
48209ce5
JDA
336 return prev;
337}
338
dd69c71b
NB
339/* Remove duplicate directories from a chain. Returns the tail of the
340 chain, or NULL if the chain is empty. This algorithm is quadratic
341 in the number of -I switches, which is acceptable since there
342 aren't usually that many of them. */
002ee64f 343static struct search_path *
48209ce5 344remove_dup_dirs (pfile, head_ptr)
dd69c71b 345 cpp_reader *pfile;
48209ce5 346 struct search_path **head_ptr;
dd69c71b 347{
591e15a1 348 struct search_path *prev = NULL, *cur, *other;
dd69c71b 349
48209ce5 350 for (cur = *head_ptr; cur; cur = cur->next)
dd69c71b 351 {
48209ce5 352 for (other = *head_ptr; other != cur; other = other->next)
df383483 353 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
dd69c71b 354 {
48209ce5 355 cur = remove_dup_dir (pfile, prev, head_ptr);
dd69c71b
NB
356 break;
357 }
358 prev = cur;
359 }
360
361 return prev;
362}
363
88ae23e7
ZW
364/* Merge the four include chains together in the order quote, bracket,
365 system, after. Remove duplicate dirs (as determined by
366 INO_T_EQ()). The system_include and after_include chains are never
367 referred to again after this function; all access is through the
5d8ebbd8 368 bracket_include path. */
88ae23e7 369static void
ae79697b
ZW
370merge_include_chains (pfile)
371 cpp_reader *pfile;
88ae23e7 372{
591e15a1 373 struct search_path *quote, *brack, *systm, *qtail;
88ae23e7 374
ae79697b 375 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
88ae23e7 376
ae79697b
ZW
377 quote = pend->quote_head;
378 brack = pend->brack_head;
379 systm = pend->systm_head;
dd69c71b 380 qtail = pend->quote_tail;
88ae23e7 381
dd69c71b
NB
382 /* Paste together bracket, system, and after include chains. */
383 if (systm)
384 pend->systm_tail->next = pend->after_head;
88ae23e7 385 else
dd69c71b
NB
386 systm = pend->after_head;
387
388 if (brack)
389 pend->brack_tail->next = systm;
88ae23e7
ZW
390 else
391 brack = systm;
392
48209ce5
JDA
393 /* This is a bit tricky. First we drop non-system dupes of system
394 directories from the merged bracket-include list. Next we drop
395 dupes from the bracket and quote include lists. Then we drop
396 non-system dupes from the merged quote-include list. Finally,
397 if qtail and brack are the same directory, we cut out brack and
398 move brack up to point to qtail.
88ae23e7
ZW
399
400 We can't just merge the lists and then uniquify them because
401 then we may lose directories from the <> search path that should
48209ce5 402 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
88ae23e7 403 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
dd69c71b 404 -Ibar -I- -Ifoo -Iquux. */
88ae23e7 405
48209ce5
JDA
406 remove_dup_nonsys_dirs (pfile, &brack, systm);
407 remove_dup_dirs (pfile, &brack);
88ae23e7
ZW
408
409 if (quote)
410 {
48209ce5 411 qtail = remove_dup_dirs (pfile, &quote);
dd69c71b
NB
412 qtail->next = brack;
413
48209ce5
JDA
414 qtail = remove_dup_nonsys_dirs (pfile, &quote, brack);
415
dd69c71b 416 /* If brack == qtail, remove brack as it's simpler. */
48209ce5 417 if (qtail && brack && INO_T_EQ (qtail->ino, brack->ino)
afb58288 418 && qtail->dev == brack->dev)
48209ce5 419 brack = remove_dup_dir (pfile, qtail, &quote);
88ae23e7
ZW
420 }
421 else
bef985f3 422 quote = brack;
88ae23e7 423
ae79697b
ZW
424 CPP_OPTION (pfile, quote_include) = quote;
425 CPP_OPTION (pfile, bracket_include) = brack;
88ae23e7
ZW
426}
427
5d8ebbd8
NB
428/* A set of booleans indicating what CPP features each source language
429 requires. */
a01eb545
ZW
430struct lang_flags
431{
432 char c99;
a01eb545
ZW
433 char cplusplus;
434 char extended_numbers;
58551c23 435 char std;
a01eb545
ZW
436 char dollars_in_ident;
437 char cplusplus_comments;
438 char digraphs;
439};
440
441/* ??? Enable $ in identifiers in assembly? */
442static const struct lang_flags lang_defaults[] =
58551c23 443{ /* c99 c++ xnum std dollar c++comm digr */
bdcae02b
NB
444 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
445 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
446 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
447 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
448 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
449 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
450 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
451 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
a01eb545
ZW
452};
453
5d8ebbd8 454/* Sets internal flags correctly for a given language. */
f749a36b
NB
455void
456cpp_set_lang (pfile, lang)
dd07b884
NB
457 cpp_reader *pfile;
458 enum c_lang lang;
459{
a01eb545 460 const struct lang_flags *l = &lang_defaults[(int) lang];
df383483 461
bdb05a7b 462 CPP_OPTION (pfile, lang) = lang;
dd07b884 463
a01eb545 464 CPP_OPTION (pfile, c99) = l->c99;
a01eb545
ZW
465 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
466 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
58551c23
NB
467 CPP_OPTION (pfile, std) = l->std;
468 CPP_OPTION (pfile, trigraphs) = l->std;
a01eb545
ZW
469 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
470 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
471 CPP_OPTION (pfile, digraphs) = l->digraphs;
dd07b884
NB
472}
473
7ca3d2b1
NB
474#ifdef HOST_EBCDIC
475static int opt_comp PARAMS ((const void *, const void *));
476
477/* Run-time sorting of options array. */
478static int
479opt_comp (p1, p2)
480 const void *p1, *p2;
481{
482 return strcmp (((struct cl_option *) p1)->opt_text,
483 ((struct cl_option *) p2)->opt_text);
484}
485#endif
cf44ea52 486
7ca3d2b1
NB
487/* init initializes library global state. It might not need to
488 do anything depending on the platform and compiler. */
cf44ea52 489static void
674c3b40 490init_library ()
cf44ea52 491{
7ca3d2b1
NB
492 static int initialized = 0;
493
494 if (! initialized)
495 {
496 initialized = 1;
497
cf44ea52 498#ifdef HOST_EBCDIC
7ca3d2b1
NB
499 /* For non-ASCII hosts, the cl_options array needs to be sorted at
500 runtime. */
501 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
cf44ea52
NB
502#endif
503
7ca3d2b1
NB
504 /* Set up the trigraph map. This doesn't need to do anything if
505 we were compiled with a compiler that supports C99 designated
506 initializers. */
507 init_trigraph_map ();
508 }
cf44ea52
NB
509}
510
ec5c56db 511/* Initialize a cpp_reader structure. */
cf44ea52 512cpp_reader *
f5e99456 513cpp_create_reader (lang)
dd07b884 514 enum c_lang lang;
6de1e2a9 515{
7ca3d2b1 516 cpp_reader *pfile;
93c80368 517
cf44ea52 518 /* Initialise this instance of the library if it hasn't been already. */
674c3b40 519 init_library ();
7ca3d2b1
NB
520
521 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
93c80368 522
f749a36b 523 cpp_set_lang (pfile, lang);
ae79697b 524 CPP_OPTION (pfile, warn_import) = 1;
a5a49440 525 CPP_OPTION (pfile, warn_multichar) = 1;
ae79697b 526 CPP_OPTION (pfile, discard_comments) = 1;
477cdac7 527 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
ae79697b 528 CPP_OPTION (pfile, show_column) = 1;
6ab3e7dd 529 CPP_OPTION (pfile, tabstop) = 8;
be768055 530 CPP_OPTION (pfile, operator_names) = 1;
909de5da 531 CPP_OPTION (pfile, warn_endif_labels) = 1;
f4ff5a69 532 CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
ae79697b
ZW
533
534 CPP_OPTION (pfile, pending) =
535 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
536
2443d4e1
NB
537 /* Default CPP arithmetic to something sensible for the host for the
538 benefit of dumb users like fix-header. */
c9220e3a 539 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
2443d4e1
NB
540 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
541 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
542 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
2a1dc0d8 543 CPP_OPTION (pfile, unsigned_char) = 0;
44a147ad 544 CPP_OPTION (pfile, unsigned_wchar) = 1;
4268e8bb 545
50410426
NB
546 /* Initialise the line map. Start at logical line 1, so we can use
547 a line number of zero for special states. */
d82fc108 548 init_line_maps (&pfile->line_maps);
1a76916c 549 pfile->line = 1;
d82fc108 550
4a58aab6 551 /* Initialize lexer state. */
93c80368
NB
552 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
553
4ed5bcfb 554 /* Set up static tokens. */
4ed5bcfb
NB
555 pfile->avoid_paste.type = CPP_PADDING;
556 pfile->avoid_paste.val.source = NULL;
557 pfile->eof.type = CPP_EOF;
558 pfile->eof.flags = 0;
93c80368 559
5fddcffc
NB
560 /* Create a token buffer for the lexer. */
561 _cpp_init_tokenrun (&pfile->base_run, 250);
562 pfile->cur_run = &pfile->base_run;
563 pfile->cur_token = pfile->base_run.base;
5fddcffc 564
93c80368
NB
565 /* Initialise the base context. */
566 pfile->context = &pfile->base_context;
567 pfile->base_context.macro = 0;
568 pfile->base_context.prev = pfile->base_context.next = 0;
569
8c3b2693
NB
570 /* Aligned and unaligned storage. */
571 pfile->a_buff = _cpp_get_buff (pfile, 0);
ece54d54 572 pfile->u_buff = _cpp_get_buff (pfile, 0);
93c80368 573
87ed109f
NB
574 /* The expression parser stack. */
575 _cpp_expand_op_stack (pfile);
576
2a967f3d
NB
577 /* Initialise the buffer obstack. */
578 gcc_obstack_init (&pfile->buffer_ob);
579
c71f835b 580 _cpp_init_includes (pfile);
cf44ea52
NB
581
582 return pfile;
f2d5f0cc
ZW
583}
584
400023a3 585/* Free resources used by PFILE. Accessing PFILE after this function
5d8ebbd8 586 returns leads to undefined behaviour. Returns the error count. */
76c3e73e 587void
400023a3 588cpp_destroy (pfile)
6de1e2a9
ZW
589 cpp_reader *pfile;
590{
591e15a1 591 struct search_path *dir, *dirn;
93c80368 592 cpp_context *context, *contextn;
50410426 593 tokenrun *run, *runn;
709e9e50 594
af0d16cd
NB
595 free_chain (CPP_OPTION (pfile, pending)->include_head);
596 free (CPP_OPTION (pfile, pending));
87ed109f 597 free (pfile->op_stack);
af0d16cd 598
38b24ee2 599 while (CPP_BUFFER (pfile) != NULL)
ef6e958a 600 _cpp_pop_buffer (pfile);
6de1e2a9 601
1a76916c
NB
602 if (pfile->out.base)
603 free (pfile->out.base);
004cb263 604
93c80368 605 if (pfile->macro_buffer)
4b49c365
AO
606 {
607 free ((PTR) pfile->macro_buffer);
608 pfile->macro_buffer = NULL;
609 pfile->macro_buffer_len = 0;
610 }
93c80368 611
f4ff5a69
NB
612 if (pfile->deps)
613 deps_free (pfile->deps);
2a967f3d 614 obstack_free (&pfile->buffer_ob, 0);
49e6c08e 615
2a967f3d 616 _cpp_destroy_hashtable (pfile);
bfb9dc7f 617 _cpp_cleanup_includes (pfile);
709e9e50 618
8c3b2693 619 _cpp_free_buff (pfile->a_buff);
ece54d54 620 _cpp_free_buff (pfile->u_buff);
b8af0ca5 621 _cpp_free_buff (pfile->free_buffs);
93c80368 622
50410426
NB
623 for (run = &pfile->base_run; run; run = runn)
624 {
625 runn = run->next;
626 free (run->base);
627 if (run != &pfile->base_run)
628 free (run);
629 }
630
93c80368 631 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
709e9e50 632 {
93c80368 633 dirn = dir->next;
591e15a1 634 free ((PTR) dir->name);
709e9e50
NB
635 free (dir);
636 }
93c80368
NB
637
638 for (context = pfile->base_context.next; context; context = contextn)
639 {
640 contextn = context->next;
641 free (context);
642 }
400023a3 643
d82fc108 644 free_line_maps (&pfile->line_maps);
400023a3 645 free (pfile);
6de1e2a9
ZW
646}
647
93c80368 648/* This structure defines one built-in identifier. A node will be
f24a153a
ZW
649 entered in the hash table under the name NAME, with value VALUE.
650
651 There are two tables of these. builtin_array holds all the
652 "builtin" macros: these are handled by builtin_macro() in
653 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
654 interest is that these macros require special code to compute their
655 expansions. The value is a "builtin_type" enumerator.
656
657 operator_array holds the C++ named operators. These are keywords
658 which act as aliases for punctuators. In C++, they cannot be
659 altered through #define, and #if recognizes them as operators. In
660 C, these are not entered into the hash table at all (but see
661 <iso646.h>). The value is a token-type enumerator. */
a9ae4483
ZW
662struct builtin
663{
562a5c27 664 const uchar *name;
93c80368 665 unsigned short len;
f24a153a 666 unsigned short value;
a9ae4483 667};
93c80368 668
f24a153a 669#define B(n, t) { DSC(n), t }
a9ae4483 670static const struct builtin builtin_array[] =
6de1e2a9 671{
93c80368
NB
672 B("__TIME__", BT_TIME),
673 B("__DATE__", BT_DATE),
674 B("__FILE__", BT_FILE),
675 B("__BASE_FILE__", BT_BASE_FILE),
676 B("__LINE__", BT_SPECLINE),
677 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
278c4662
NB
678 /* Keep builtins not used for -traditional-cpp at the end, and
679 update init_builtins() if any more are added. */
644eddaa 680 B("_Pragma", BT_PRAGMA),
618cdda7 681 B("__STDC__", BT_STDC),
f24a153a 682};
92936ecf 683
f24a153a
ZW
684static const struct builtin operator_array[] =
685{
686 B("and", CPP_AND_AND),
687 B("and_eq", CPP_AND_EQ),
688 B("bitand", CPP_AND),
689 B("bitor", CPP_OR),
690 B("compl", CPP_COMPL),
691 B("not", CPP_NOT),
692 B("not_eq", CPP_NOT_EQ),
693 B("or", CPP_OR_OR),
694 B("or_eq", CPP_OR_EQ),
695 B("xor", CPP_XOR),
696 B("xor_eq", CPP_XOR_EQ)
a9ae4483 697};
12cf91fe 698#undef B
a9ae4483 699
17645b15
NB
700/* Mark the C++ named operators in the hash table. */
701static void
702mark_named_operators (pfile)
703 cpp_reader *pfile;
704{
705 const struct builtin *b;
706
707 for (b = operator_array;
708 b < (operator_array + ARRAY_SIZE (operator_array));
709 b++)
710 {
711 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
712 hp->flags |= NODE_OPERATOR;
713 hp->value.operator = b->value;
714 }
715}
716
f5e99456 717/* Subroutine of cpp_read_main_file; reads the builtins table above and
5d8ebbd8 718 enters them, and language-specific macros, into the hash table. */
a9ae4483 719static void
7ca3d2b1 720init_builtins (pfile)
a9ae4483
ZW
721 cpp_reader *pfile;
722{
a9ae4483 723 const struct builtin *b;
278c4662 724 size_t n = ARRAY_SIZE (builtin_array);
771c4df3 725
278c4662
NB
726 if (CPP_OPTION (pfile, traditional))
727 n -= 2;
728
729 for(b = builtin_array; b < builtin_array + n; b++)
6de1e2a9 730 {
f24a153a
ZW
731 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
732 hp->type = NT_MACRO;
733 hp->flags |= NODE_BUILTIN | NODE_WARN;
734 hp->value.builtin = b->value;
6de1e2a9 735 }
c740cee2
NB
736
737 if (CPP_OPTION (pfile, cplusplus))
3d90d290 738 _cpp_define_builtin (pfile, "__cplusplus 1");
2a1dc0d8
ZW
739 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
740 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
0f7866e7 741 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
c740cee2
NB
742 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
743 else if (CPP_OPTION (pfile, c99))
744 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
745
0f7866e7
ZL
746 if (CPP_OPTION (pfile, objc))
747 _cpp_define_builtin (pfile, "__OBJC__ 1");
748
3d90d290
NB
749 if (pfile->cb.register_builtins)
750 (*pfile->cb.register_builtins) (pfile);
6de1e2a9
ZW
751}
752
c45da1ca
ZW
753/* And another subroutine. This one sets up the standard include path. */
754static void
7ca3d2b1 755init_standard_includes (pfile)
c45da1ca
ZW
756 cpp_reader *pfile;
757{
c45da1ca 758 char *path;
455d2586 759 const struct default_include *p;
ae79697b 760 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
c45da1ca
ZW
761
762 /* Several environment variables may add to the include search path.
763 CPATH specifies an additional list of directories to be searched
764 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
765 etc. specify an additional list of directories to be searched as
766 if specified with -isystem, for the language indicated. */
767
2f8dd115 768 GET_ENVIRONMENT (path, "CPATH");
c45da1ca 769 if (path != 0 && *path != 0)
e33f6253 770 path_include (pfile, path, BRACKET);
c45da1ca 771
ae79697b 772 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
c45da1ca
ZW
773 {
774 case 0:
2f8dd115 775 GET_ENVIRONMENT (path, "C_INCLUDE_PATH");
c45da1ca
ZW
776 break;
777 case 1:
2f8dd115 778 GET_ENVIRONMENT (path, "CPLUS_INCLUDE_PATH");
c45da1ca
ZW
779 break;
780 case 2:
2f8dd115 781 GET_ENVIRONMENT (path, "OBJC_INCLUDE_PATH");
c45da1ca
ZW
782 break;
783 case 3:
2f8dd115 784 GET_ENVIRONMENT (path, "OBJCPLUS_INCLUDE_PATH");
c45da1ca
ZW
785 break;
786 }
787 if (path != 0 && *path != 0)
e33f6253 788 path_include (pfile, path, SYSTEM);
c45da1ca
ZW
789
790 /* Search "translated" versions of GNU directories.
791 These have /usr/local/lib/gcc... replaced by specd_prefix. */
60893f43 792 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
c45da1ca 793 {
c45da1ca 794 /* Remove the `include' from /usr/local/lib/gcc.../include.
ec5c56db 795 GCC_INCLUDE_DIR will always end in /include. */
60893f43
ZW
796 int default_len = cpp_GCC_INCLUDE_DIR_len;
797 char *default_prefix = (char *) alloca (default_len + 1);
c45da1ca
ZW
798 int specd_len = strlen (specd_prefix);
799
60893f43 800 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
c45da1ca
ZW
801 default_prefix[default_len] = '\0';
802
60893f43 803 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
804 {
805 /* Some standard dirs are only for C++. */
806 if (!p->cplusplus
ae79697b
ZW
807 || (CPP_OPTION (pfile, cplusplus)
808 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca
ZW
809 {
810 /* Does this dir start with the prefix? */
61d0346d 811 if (!memcmp (p->fname, default_prefix, default_len))
c45da1ca
ZW
812 {
813 /* Yes; change prefix and add to search list. */
814 int flen = strlen (p->fname);
815 int this_len = specd_len + flen - default_len;
816 char *str = (char *) xmalloc (this_len + 1);
817 memcpy (str, specd_prefix, specd_len);
818 memcpy (str + specd_len,
819 p->fname + default_len,
820 flen - default_len + 1);
821
e33f6253 822 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
823 }
824 }
825 }
826 }
827
828 /* Search ordinary names for GNU include directories. */
60893f43 829 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
830 {
831 /* Some standard dirs are only for C++. */
832 if (!p->cplusplus
ae79697b
ZW
833 || (CPP_OPTION (pfile, cplusplus)
834 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca 835 {
51c04256 836 char *str = update_path (p->fname, p->component);
e33f6253 837 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
838 }
839 }
840}
841
5d8ebbd8 842/* Pushes a command line -imacro and -include file indicated by P onto
d7bc7a98
NB
843 the buffer stack. Returns non-zero if successful. */
844static bool
845push_include (pfile, p)
4a58aab6
NB
846 cpp_reader *pfile;
847 struct pending_option *p;
4a58aab6 848{
d7bc7a98 849 cpp_token header;
4a58aab6 850
d7bc7a98
NB
851 /* Later: maybe update this to use the #include "" search path
852 if cpp_read_file fails. */
853 header.type = CPP_STRING;
854 header.val.str.text = (const unsigned char *) p->arg;
855 header.val.str.len = strlen (p->arg);
856 /* Make the command line directive take up a line. */
50410426 857 pfile->line++;
d7bc7a98
NB
858
859 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
860}
861
862/* Frees a pending_option chain. */
863static void
864free_chain (head)
865 struct pending_option *head;
866{
867 struct pending_option *next;
868
869 while (head)
870 {
871 next = head->next;
872 free (head);
873 head = next;
4a58aab6
NB
874 }
875}
876
2443d4e1
NB
877/* Sanity-checks are dependent on command-line options, so it is
878 called as a subroutine of cpp_read_main_file (). */
879#if ENABLE_CHECKING
880static void sanity_checks PARAMS ((cpp_reader *));
881static void sanity_checks (pfile)
882 cpp_reader *pfile;
883{
884 cppchar_t test = 0;
c9220e3a 885 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
2443d4e1
NB
886
887 /* Sanity checks for assumptions about CPP arithmetic and target
888 type precisions made by cpplib. */
889 test--;
890 if (test < 1)
bdee42b1 891 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
2443d4e1 892
c9220e3a 893 if (CPP_OPTION (pfile, precision) > max_precision)
bdee42b1 894 cpp_error (pfile, DL_ICE,
68bd6dd6 895 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
c9220e3a
NB
896 (unsigned long) max_precision,
897 (unsigned long) CPP_OPTION (pfile, precision));
2443d4e1
NB
898
899 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
bdee42b1 900 cpp_error (pfile, DL_ICE,
2443d4e1
NB
901 "CPP arithmetic must be at least as precise as a target int");
902
903 if (CPP_OPTION (pfile, char_precision) < 8)
bdee42b1 904 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
2443d4e1
NB
905
906 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
bdee42b1 907 cpp_error (pfile, DL_ICE,
2443d4e1
NB
908 "target wchar_t is narrower than target char");
909
910 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
bdee42b1 911 cpp_error (pfile, DL_ICE,
2443d4e1
NB
912 "target int is narrower than target char");
913
c9220e3a
NB
914 /* This is assumed in eval_token() and could be fixed if necessary. */
915 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
916 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
917
2443d4e1 918 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
bdee42b1 919 cpp_error (pfile, DL_ICE,
68bd6dd6 920 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
c9220e3a
NB
921 (unsigned long) BITS_PER_CPPCHAR_T,
922 (unsigned long) CPP_OPTION (pfile, wchar_precision));
2443d4e1
NB
923}
924#else
925# define sanity_checks(PFILE)
926#endif
927
76c3e73e
NB
928/* Add a dependency target. Can be called any number of times before
929 cpp_read_main_file(). If no targets have been added before
930 cpp_read_main_file(), then the default target is used. */
931void
932cpp_add_dependency_target (pfile, target, quote)
933 cpp_reader *pfile;
934 const char *target;
935 int quote;
936{
937 if (!pfile->deps)
938 pfile->deps = deps_init ();
939
940 deps_add_target (pfile->deps, target, quote);
941}
942
f5e99456
NB
943/* This is called after options have been parsed, and partially
944 processed. Setup for processing input from the file named FNAME,
945 or stdin if it is the empty string. Return the original filename
946 on success (e.g. foo.i->foo.c), or NULL on failure. */
947const char *
948cpp_read_main_file (pfile, fname, table)
6de1e2a9 949 cpp_reader *pfile;
7ceb3598 950 const char *fname;
f5e99456 951 hash_table *table;
6de1e2a9 952{
2443d4e1
NB
953 sanity_checks (pfile);
954
f4ff5a69
NB
955 post_options (pfile);
956
f5e99456
NB
957 /* The front ends don't set up the hash table until they have
958 finished processing the command line options, so initializing the
959 hashtable is deferred until now. */
960 _cpp_init_hashtable (pfile, table);
961
c45da1ca 962 /* Set up the include search path now. */
ae79697b 963 if (! CPP_OPTION (pfile, no_standard_includes))
7ca3d2b1 964 init_standard_includes (pfile);
c45da1ca 965
ae79697b 966 merge_include_chains (pfile);
c45da1ca
ZW
967
968 /* With -v, print the list of dirs to search. */
ae79697b 969 if (CPP_OPTION (pfile, verbose))
c45da1ca 970 {
591e15a1 971 struct search_path *l;
c45da1ca 972 fprintf (stderr, _("#include \"...\" search starts here:\n"));
ae79697b 973 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
c45da1ca 974 {
ae79697b 975 if (l == CPP_OPTION (pfile, bracket_include))
c45da1ca
ZW
976 fprintf (stderr, _("#include <...> search starts here:\n"));
977 fprintf (stderr, " %s\n", l->name);
978 }
979 fprintf (stderr, _("End of search list.\n"));
980 }
981
f4ff5a69
NB
982 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
983 {
984 if (!pfile->deps)
985 pfile->deps = deps_init ();
986
987 /* Set the default target (if there is none already). */
988 deps_add_default_target (pfile->deps, fname);
989 }
96302433 990
f5e99456 991 /* Open the main input file. */
614c7d37 992 if (!_cpp_read_file (pfile, fname))
f5e99456 993 return NULL;
c45da1ca 994
f4ff5a69
NB
995 /* Set this here so the client can change the option if it wishes,
996 and after stacking the main file so we don't trace the main
997 file. */
5993019d
NB
998 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
999
f5e99456
NB
1000 /* For foo.i, read the original filename foo.c now, for the benefit
1001 of the front ends. */
1002 if (CPP_OPTION (pfile, preprocessed))
1003 read_original_filename (pfile);
1004
1005 return pfile->map->to_file;
1006}
1007
1008/* For preprocessed files, if the first tokens are of the form # NUM.
1009 handle the directive so we know the original file name. This will
1010 generate file_change callbacks, which the front ends must handle
1011 appropriately given their state of initialization. */
1012static void
1013read_original_filename (pfile)
1014 cpp_reader *pfile;
1015{
1016 const cpp_token *token, *token1;
1017
1018 /* Lex ahead; if the first tokens are of the form # NUM, then
1019 process the directive, otherwise back up. */
1020 token = _cpp_lex_direct (pfile);
1021 if (token->type == CPP_HASH)
1022 {
1023 token1 = _cpp_lex_direct (pfile);
1024 _cpp_backup_tokens (pfile, 1);
1025
1026 /* If it's a #line directive, handle it. */
1027 if (token1->type == CPP_NUMBER)
1028 {
1029 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1030 return;
1031 }
1032 }
1033
1034 /* Backup as if nothing happened. */
1035 _cpp_backup_tokens (pfile, 1);
1036}
1037
1038/* Handle pending command line options: -D, -U, -A, -imacros and
1039 -include. This should be called after debugging has been properly
1040 set up in the front ends. */
1041void
1042cpp_finish_options (pfile)
1043 cpp_reader *pfile;
1044{
17645b15
NB
1045 /* Mark named operators before handling command line macros. */
1046 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
1047 mark_named_operators (pfile);
1048
d7bc7a98
NB
1049 /* Install builtins and process command line macros etc. in the order
1050 they appeared, but only if not already preprocessed. */
05e81724 1051 if (! CPP_OPTION (pfile, preprocessed))
6de1e2a9 1052 {
d7bc7a98
NB
1053 struct pending_option *p;
1054
b0287a90 1055 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
cbc69f84 1056 init_builtins (pfile);
d7bc7a98 1057 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
cbc69f84
NB
1058 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1059 (*p->handler) (pfile, p->arg);
d7bc7a98 1060
af0d16cd
NB
1061 /* Scan -imacros files after -D, -U, but before -include.
1062 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1063 push -include files. */
1064 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1065 if (push_include (pfile, p))
1066 cpp_scan_nooutput (pfile);
1067
1068 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1069 _cpp_maybe_push_include_file (pfile);
6de1e2a9 1070 }
05e81724 1071
a69cbaac
NB
1072 pfile->first_unused_line = pfile->line;
1073
af0d16cd 1074 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
d7bc7a98 1075 free_chain (CPP_OPTION (pfile, pending)->directive_head);
d7bc7a98 1076}
6de1e2a9 1077
af0d16cd
NB
1078/* Push the next buffer on the stack given by -include, if any. */
1079void
1080_cpp_maybe_push_include_file (pfile)
d7bc7a98
NB
1081 cpp_reader *pfile;
1082{
af0d16cd 1083 if (pfile->next_include_file)
d7bc7a98 1084 {
af0d16cd 1085 struct pending_option *head = *pfile->next_include_file;
df383483 1086
af0d16cd
NB
1087 while (head && !push_include (pfile, head))
1088 head = head->next;
d7bc7a98 1089
af0d16cd
NB
1090 if (head)
1091 pfile->next_include_file = &head->next;
1092 else
d7bc7a98 1093 {
af0d16cd
NB
1094 /* All done; restore the line map from <command line>. */
1095 _cpp_do_file_change (pfile, LC_RENAME,
1096 pfile->line_maps.maps[0].to_file, 1, 0);
1097 /* Don't come back here again. */
1098 pfile->next_include_file = NULL;
d7bc7a98
NB
1099 }
1100 }
6de1e2a9
ZW
1101}
1102
76c3e73e
NB
1103/* This is called at the end of preprocessing. It pops the last
1104 buffer and writes dependency output, and returns the number of
1105 errors.
1106
1107 Maybe it should also reset state, such that you could call
1108 cpp_start_read with a new filename to restart processing. */
1109int
1110cpp_finish (pfile, deps_stream)
6de1e2a9 1111 cpp_reader *pfile;
76c3e73e 1112 FILE *deps_stream;
6de1e2a9 1113{
a69cbaac
NB
1114 /* Warn about unused macros before popping the final buffer. */
1115 if (CPP_OPTION (pfile, warn_unused_macros))
1116 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
1117
7364fdd8
NB
1118 /* cpplex.c leaves the final buffer on the stack. This it so that
1119 it returns an unending stream of CPP_EOFs to the client. If we
a1f300c0 1120 popped the buffer, we'd dereference a NULL buffer pointer and
7364fdd8
NB
1121 segfault. It's nice to allow the client to do worry-free excess
1122 cpp_get_token calls. */
1123 while (pfile->buffer)
1124 _cpp_pop_buffer (pfile);
c1212d2f 1125
76c3e73e 1126 /* Don't write the deps file if there are errors. */
f4ff5a69
NB
1127 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
1128 && deps_stream && pfile->errors == 0)
76c3e73e
NB
1129 {
1130 deps_write (pfile->deps, deps_stream, 72);
1131
f4ff5a69 1132 if (CPP_OPTION (pfile, deps.phony_targets))
76c3e73e
NB
1133 deps_phony_targets (pfile->deps, deps_stream);
1134 }
3caee4a8 1135
d4506961
ZW
1136 /* Report on headers that could use multiple include guards. */
1137 if (CPP_OPTION (pfile, print_include_names))
c71f835b 1138 _cpp_report_missing_guards (pfile);
76c3e73e
NB
1139
1140 return pfile->errors;
6de1e2a9
ZW
1141}
1142
5d8ebbd8 1143/* Add a directive to be handled later in the initialization phase. */
223dca6a 1144static void
ae79697b
ZW
1145new_pending_directive (pend, text, handler)
1146 struct cpp_pending *pend;
223dca6a 1147 const char *text;
40eac643 1148 cl_directive_handler handler;
223dca6a
RH
1149{
1150 struct pending_option *o = (struct pending_option *)
1151 xmalloc (sizeof (struct pending_option));
1152
7ceb3598 1153 o->arg = text;
223dca6a 1154 o->next = NULL;
40eac643 1155 o->handler = handler;
ae79697b 1156 APPEND (pend, directive, o);
223dca6a
RH
1157}
1158
ae79697b
ZW
1159/* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1160 I.e. a const string initializer with parens around it. That is
1161 what N_("string") resolves to, so we make no_* be macros instead. */
c725bd79
NB
1162#define no_ass N_("assertion missing after %s")
1163#define no_dir N_("directory name missing after %s")
1164#define no_fil N_("file name missing after %s")
1165#define no_mac N_("macro name missing after %s")
1166#define no_pth N_("path name missing after %s")
ae79697b
ZW
1167
1168/* This is the list of all command line options, with the leading
1169 "-" removed. It must be sorted in ASCII collating order. */
1170#define COMMAND_LINE_OPTIONS \
ae79697b 1171 DEF_OPT("A", no_ass, OPT_A) \
ae79697b 1172 DEF_OPT("D", no_mac, OPT_D) \
ae79697b 1173 DEF_OPT("I", no_dir, OPT_I) \
ae79697b 1174 DEF_OPT("U", no_mac, OPT_U) \
ae79697b
ZW
1175 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1176 DEF_OPT("imacros", no_fil, OPT_imacros) \
1177 DEF_OPT("include", no_fil, OPT_include) \
1178 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1179 DEF_OPT("isystem", no_dir, OPT_isystem) \
1180 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
b4a93904 1181 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
ae79697b
ZW
1182
1183#define DEF_OPT(text, msg, code) code,
e23c0ba3
ZW
1184enum opt_code
1185{
ae79697b 1186 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1187 N_OPTS
1188};
ae79697b 1189#undef DEF_OPT
e23c0ba3
ZW
1190
1191struct cl_option
1192{
1193 const char *opt_text;
1194 const char *msg;
1195 size_t opt_len;
1196 enum opt_code opt_code;
1197};
1198
ae79697b 1199#define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
e23c0ba3
ZW
1200#ifdef HOST_EBCDIC
1201static struct cl_option cl_options[] =
1202#else
1203static const struct cl_option cl_options[] =
1204#endif
1205{
ae79697b 1206 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1207};
1208#undef DEF_OPT
ae79697b 1209#undef COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1210
1211/* Perform a binary search to find which, if any, option the given
1212 command-line matches. Returns its index in the option array,
1213 negative on failure. Complications arise since some options can be
1214 suffixed with an argument, and multiple complete matches can occur,
09e77dee 1215 e.g. -pedantic and -pedantic-errors. */
e23c0ba3
ZW
1216static int
1217parse_option (input)
1218 const char *input;
1219{
1220 unsigned int md, mn, mx;
1221 size_t opt_len;
1222 int comp;
1223
1224 mn = 0;
1225 mx = N_OPTS;
1226
1227 while (mx > mn)
1228 {
1229 md = (mn + mx) / 2;
ae79697b 1230
e23c0ba3 1231 opt_len = cl_options[md].opt_len;
61d0346d 1232 comp = memcmp (input, cl_options[md].opt_text, opt_len);
ae79697b 1233
e23c0ba3
ZW
1234 if (comp > 0)
1235 mn = md + 1;
1236 else if (comp < 0)
1237 mx = md;
1238 else
1239 {
1240 if (input[opt_len] == '\0')
1241 return md;
1242 /* We were passed more text. If the option takes an argument,
1243 we may match a later option or we may have been passed the
1244 argument. The longest possible option match succeeds.
1245 If the option takes no arguments we have not matched and
4a58aab6 1246 continue the search (e.g. input="stdc++" match was "stdc"). */
e23c0ba3
ZW
1247 mn = md + 1;
1248 if (cl_options[md].msg)
1249 {
1250 /* Scan forwards. If we get an exact match, return it.
1251 Otherwise, return the longest option-accepting match.
4a58aab6 1252 This loops no more than twice with current options. */
e23c0ba3 1253 mx = md;
37b8524c 1254 for (; mn < (unsigned int) N_OPTS; mn++)
e23c0ba3
ZW
1255 {
1256 opt_len = cl_options[mn].opt_len;
61d0346d 1257 if (memcmp (input, cl_options[mn].opt_text, opt_len))
e23c0ba3
ZW
1258 break;
1259 if (input[opt_len] == '\0')
1260 return mn;
1261 if (cl_options[mn].msg)
1262 mx = mn;
1263 }
1264 return mx;
1265 }
1266 }
1267 }
1268
1269 return -1;
1270}
1271
6de1e2a9
ZW
1272/* Handle one command-line option in (argc, argv).
1273 Can be called multiple times, to handle multiple sets of options.
1274 Returns number of strings consumed. */
2c0accc9 1275int
09e77dee 1276cpp_handle_option (pfile, argc, argv)
6de1e2a9
ZW
1277 cpp_reader *pfile;
1278 int argc;
1279 char **argv;
1280{
6de1e2a9 1281 int i = 0;
f8f769ea 1282 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
6de1e2a9 1283
e23c0ba3
ZW
1284 {
1285 enum opt_code opt_code;
1286 int opt_index;
7ceb3598 1287 const char *arg = 0;
e23c0ba3 1288
4a58aab6 1289 /* Skip over '-'. */
e23c0ba3
ZW
1290 opt_index = parse_option (&argv[i][1]);
1291 if (opt_index < 0)
1292 return i;
1293
1294 opt_code = cl_options[opt_index].opt_code;
1295 if (cl_options[opt_index].msg)
1296 {
1297 arg = &argv[i][cl_options[opt_index].opt_len + 1];
09e77dee 1298 if (arg[0] == '\0')
e23c0ba3
ZW
1299 {
1300 arg = argv[++i];
1301 if (!arg)
1302 {
bdee42b1 1303 cpp_error (pfile, DL_ERROR,
ebef4e8c 1304 cl_options[opt_index].msg, argv[i - 1]);
e23c0ba3
ZW
1305 return argc;
1306 }
1307 }
1308 }
ae79697b 1309
e23c0ba3
ZW
1310 switch (opt_code)
1311 {
4a58aab6 1312 case N_OPTS: /* Shut GCC up. */
e23c0ba3 1313 break;
f4cdc368 1314
e23c0ba3 1315 case OPT_D:
f8f769ea 1316 new_pending_directive (pend, arg, cpp_define);
e23c0ba3 1317 break;
e23c0ba3 1318 case OPT_iprefix:
ae79697b
ZW
1319 CPP_OPTION (pfile, include_prefix) = arg;
1320 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
e23c0ba3 1321 break;
96302433 1322
e23c0ba3 1323 case OPT_A:
e1e97c4f 1324 if (arg[0] == '-')
0b22d65c 1325 {
e1e97c4f
NB
1326 /* -A with an argument beginning with '-' acts as
1327 #unassert on whatever immediately follows the '-'.
1328 If "-" is the whole argument, we eliminate all
1329 predefined macros and assertions, including those
1330 that were specified earlier on the command line.
1331 That way we can get rid of any that were passed
1332 automatically in from GCC. */
1333
1334 if (arg[1] == '\0')
0b22d65c 1335 {
29401c30 1336 free_chain (pend->directive_head);
e33f6253
NB
1337 pend->directive_head = NULL;
1338 pend->directive_tail = NULL;
0b22d65c 1339 }
e1e97c4f 1340 else
e33f6253 1341 new_pending_directive (pend, arg + 1, cpp_unassert);
6de1e2a9 1342 }
e1e97c4f 1343 else
e33f6253 1344 new_pending_directive (pend, arg, cpp_assert);
e23c0ba3
ZW
1345 break;
1346 case OPT_U:
e33f6253 1347 new_pending_directive (pend, arg, cpp_undef);
e23c0ba3
ZW
1348 break;
1349 case OPT_I: /* Add directory to path for includes. */
1350 if (!strcmp (arg, "-"))
df383483 1351 {
e23c0ba3
ZW
1352 /* -I- means:
1353 Use the preceding -I directories for #include "..."
1354 but not #include <...>.
1355 Don't search the directory of the present file
1356 for #include "...". (Note that -I. -I- is not the same as
1357 the default setup; -I. uses the compiler's working dir.) */
ae79697b 1358 if (! CPP_OPTION (pfile, ignore_srcdir))
e23c0ba3 1359 {
ae79697b
ZW
1360 pend->quote_head = pend->brack_head;
1361 pend->quote_tail = pend->brack_tail;
1362 pend->brack_head = 0;
1363 pend->brack_tail = 0;
1364 CPP_OPTION (pfile, ignore_srcdir) = 1;
e23c0ba3
ZW
1365 }
1366 else
1367 {
bdee42b1 1368 cpp_error (pfile, DL_ERROR, "-I- specified twice");
e23c0ba3
ZW
1369 return argc;
1370 }
df383483
KH
1371 }
1372 else
e33f6253 1373 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
e23c0ba3
ZW
1374 break;
1375 case OPT_isystem:
1376 /* Add directory to beginning of system include path, as a system
4a58aab6 1377 include directory. */
e33f6253 1378 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
e23c0ba3
ZW
1379 break;
1380 case OPT_include:
e23c0ba3
ZW
1381 case OPT_imacros:
1382 {
1383 struct pending_option *o = (struct pending_option *)
1384 xmalloc (sizeof (struct pending_option));
1385 o->arg = arg;
1386 o->next = NULL;
ae79697b 1387
d7bc7a98
NB
1388 if (opt_code == OPT_include)
1389 APPEND (pend, include, o);
1390 else
1391 APPEND (pend, imacros, o);
e23c0ba3
ZW
1392 }
1393 break;
1394 case OPT_iwithprefix:
1395 /* Add directory to end of path for includes,
1396 with the default prefix at the front of its name. */
1397 /* fall through */
1398 case OPT_iwithprefixbefore:
1399 /* Add directory to main path for includes,
1400 with the default prefix at the front of its name. */
1401 {
1402 char *fname;
1403 int len;
ae79697b 1404
e23c0ba3 1405 len = strlen (arg);
ae79697b
ZW
1406
1407 if (CPP_OPTION (pfile, include_prefix) != 0)
e23c0ba3 1408 {
ae79697b
ZW
1409 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1410 fname = xmalloc (ipl + len + 1);
1411 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1412 memcpy (fname + ipl, arg, len + 1);
e23c0ba3 1413 }
60893f43 1414 else if (cpp_GCC_INCLUDE_DIR_len)
e23c0ba3 1415 {
60893f43
ZW
1416 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1417 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1418 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
e23c0ba3 1419 }
60893f43
ZW
1420 else
1421 fname = xstrdup (arg);
ae79697b 1422
e33f6253 1423 append_include_chain (pfile, fname,
e23c0ba3
ZW
1424 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1425 }
1426 break;
1427 case OPT_idirafter:
1428 /* Add directory to end of path for includes. */
e33f6253 1429 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
e23c0ba3 1430 break;
df383483 1431 }
e23c0ba3 1432 }
6de1e2a9 1433 return i + 1;
e23c0ba3 1434}
0b22d65c 1435
6de1e2a9
ZW
1436/* Handle command-line options in (argc, argv).
1437 Can be called multiple times, to handle multiple sets of options.
1438 Returns if an unrecognized option is seen.
1439 Returns number of strings consumed. */
6de1e2a9
ZW
1440int
1441cpp_handle_options (pfile, argc, argv)
1442 cpp_reader *pfile;
1443 int argc;
1444 char **argv;
1445{
1446 int i;
1447 int strings_processed;
e23c0ba3 1448
6de1e2a9
ZW
1449 for (i = 0; i < argc; i += strings_processed)
1450 {
09e77dee 1451 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
6de1e2a9
ZW
1452 if (strings_processed == 0)
1453 break;
1454 }
96302433 1455
6de1e2a9
ZW
1456 return i;
1457}
1458
f4ff5a69
NB
1459static void
1460post_options (pfile)
96302433
NB
1461 cpp_reader *pfile;
1462{
1463 /* -Wtraditional is not useful in C++ mode. */
1464 if (CPP_OPTION (pfile, cplusplus))
1465 CPP_OPTION (pfile, warn_traditional) = 0;
1466
6d4587f7 1467 /* Permanently disable macro expansion if we are rescanning
43612ffb 1468 preprocessed text. Read preprocesed source in ISO mode. */
6d4587f7 1469 if (CPP_OPTION (pfile, preprocessed))
43612ffb
NB
1470 {
1471 pfile->state.prevent_expansion = 1;
1472 CPP_OPTION (pfile, traditional) = 0;
1473 }
1474
1475 /* Traditional CPP does not accurately track column information. */
1476 if (CPP_OPTION (pfile, traditional))
1477 CPP_OPTION (pfile, show_column) = 0;
7ca3d2b1 1478}
This page took 0.899119 seconds and 5 git commands to generate.