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