]>
Commit | Line | Data |
---|---|---|
5538ada6 | 1 | /* CPP Library. |
5e7b4e25 JL |
2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
3 | 1999, 2000 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 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, 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" | |
26 | #include "output.h" | |
27 | #include "prefix.h" | |
28 | #include "intl.h" | |
9f8f4efe | 29 | #include "version.h" |
49e6c08e | 30 | #include "mkdeps.h" |
60893f43 | 31 | #include "cppdefault.h" |
6de1e2a9 ZW |
32 | |
33 | /* Predefined symbols, built-in macros, and the default include path. */ | |
34 | ||
35 | #ifndef GET_ENV_PATH_LIST | |
36 | #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0) | |
37 | #endif | |
38 | ||
88ae23e7 ZW |
39 | /* Windows does not natively support inodes, and neither does MSDOS. |
40 | Cygwin's emulation can generate non-unique inodes, so don't use it. | |
41 | VMS has non-numeric inodes. */ | |
42 | #ifdef VMS | |
43 | #define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a))) | |
44 | #elif (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__ | |
45 | #define INO_T_EQ(a, b) 0 | |
46 | #else | |
47 | #define INO_T_EQ(a, b) ((a) == (b)) | |
48 | #endif | |
49 | ||
6de1e2a9 ZW |
50 | /* Internal structures and prototypes. */ |
51 | ||
0b22d65c ZW |
52 | /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros |
53 | switch. There are four lists: one for -D and -U, one for -A, one | |
54 | for -include, one for -imacros. `undef' is set for -U, clear for | |
55 | -D, ignored for the others. | |
56 | (Future: add an equivalent of -U for -A) */ | |
40eac643 | 57 | |
8be1ddca | 58 | typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *)); |
0b22d65c | 59 | struct pending_option |
6de1e2a9 | 60 | { |
0b22d65c | 61 | struct pending_option *next; |
7ceb3598 | 62 | const char *arg; |
40eac643 | 63 | cl_directive_handler handler; |
6de1e2a9 | 64 | }; |
0b22d65c | 65 | |
88ae23e7 ZW |
66 | /* The `pending' structure accumulates all the options that are not |
67 | actually processed until we hit cpp_start_read. It consists of | |
68 | several lists, one for each type of option. We keep both head and | |
69 | tail pointers for quick insertion. */ | |
70 | struct cpp_pending | |
71 | { | |
40eac643 | 72 | struct pending_option *directive_head, *directive_tail; |
88ae23e7 ZW |
73 | |
74 | struct file_name_list *quote_head, *quote_tail; | |
75 | struct file_name_list *brack_head, *brack_tail; | |
76 | struct file_name_list *systm_head, *systm_tail; | |
77 | struct file_name_list *after_head, *after_tail; | |
78 | ||
79 | struct pending_option *imacros_head, *imacros_tail; | |
80 | struct pending_option *include_head, *include_tail; | |
81 | }; | |
82 | ||
0b22d65c ZW |
83 | #ifdef __STDC__ |
84 | #define APPEND(pend, list, elt) \ | |
85 | do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \ | |
86 | else (pend)->list##_tail->next = (elt); \ | |
87 | (pend)->list##_tail = (elt); \ | |
88 | } while (0) | |
89 | #else | |
90 | #define APPEND(pend, list, elt) \ | |
91 | do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \ | |
92 | else (pend)->list/**/_tail->next = (elt); \ | |
93 | (pend)->list/**/_tail = (elt); \ | |
94 | } while (0) | |
95 | #endif | |
6de1e2a9 | 96 | |
6de1e2a9 | 97 | static void print_help PARAMS ((void)); |
0b22d65c | 98 | static void path_include PARAMS ((cpp_reader *, |
0b22d65c | 99 | char *, int)); |
6de1e2a9 | 100 | static void initialize_builtins PARAMS ((cpp_reader *)); |
0b22d65c | 101 | static void append_include_chain PARAMS ((cpp_reader *, |
c45da1ca | 102 | char *, int, int)); |
ae79697b | 103 | static void merge_include_chains PARAMS ((cpp_reader *)); |
88ae23e7 | 104 | |
bcc5cac9 | 105 | static void initialize_dependency_output PARAMS ((cpp_reader *)); |
c45da1ca | 106 | static void initialize_standard_includes PARAMS ((cpp_reader *)); |
2c0accc9 | 107 | static void new_pending_directive PARAMS ((struct cpp_pending *, |
40eac643 NB |
108 | const char *, |
109 | cl_directive_handler)); | |
e23c0ba3 ZW |
110 | #ifdef HOST_EBCDIC |
111 | static int opt_comp PARAMS ((const void *, const void *)); | |
112 | #endif | |
113 | static int parse_option PARAMS ((const char *)); | |
6de1e2a9 | 114 | |
c45da1ca | 115 | /* Fourth argument to append_include_chain: chain to use */ |
0b22d65c | 116 | enum { QUOTE = 0, BRACKET, SYSTEM, AFTER }; |
6de1e2a9 | 117 | |
12cf91fe ZW |
118 | /* If we have designated initializers (GCC >2.7) this table can be |
119 | initialized, constant data. Otherwise, it has to be filled in at | |
120 | runtime. */ | |
a9ae4483 | 121 | |
12cf91fe | 122 | #if (GCC_VERSION >= 2007) |
455d2586 | 123 | #define init_IStable() /* nothing */ |
12cf91fe | 124 | #define ISTABLE __extension__ const unsigned char _cpp_IStable[256] = { |
a9ae4483 | 125 | #define END }; |
455d2586 | 126 | #define s(p, v) [p] = v, |
a9ae4483 | 127 | #else |
455d2586 ZW |
128 | #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \ |
129 | static void init_IStable PARAMS ((void)) { \ | |
c6491210 | 130 | unsigned char *x = _cpp_IStable; |
ae79697b | 131 | #define END } |
455d2586 | 132 | #define s(p, v) x[p] = v; |
a9ae4483 | 133 | #endif |
6de1e2a9 | 134 | |
a9ae4483 ZW |
135 | #define A(x) s(x, ISidnum|ISidstart) |
136 | #define N(x) s(x, ISidnum|ISnumstart) | |
137 | #define H(x) s(x, IShspace|ISspace) | |
91fcd158 | 138 | #define V(x) s(x, ISvspace|ISspace) |
a9ae4483 | 139 | #define S(x) s(x, ISspace) |
6de1e2a9 | 140 | |
455d2586 | 141 | ISTABLE |
a9ae4483 | 142 | A('_') |
6de1e2a9 | 143 | |
a9ae4483 ZW |
144 | A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i') |
145 | A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r') | |
146 | A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z') | |
6de1e2a9 | 147 | |
a9ae4483 ZW |
148 | A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I') |
149 | A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R') | |
150 | A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z') | |
6de1e2a9 | 151 | |
a9ae4483 | 152 | N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0') |
5538ada6 | 153 | |
91fcd158 | 154 | H(' ') H('\t') |
6de1e2a9 | 155 | |
91fcd158 NB |
156 | V('\n') V('\r') |
157 | ||
158 | S('\0') S('\v') S('\f') | |
a9ae4483 ZW |
159 | END |
160 | ||
161 | #undef A | |
162 | #undef N | |
163 | #undef H | |
91fcd158 | 164 | #undef V |
a9ae4483 | 165 | #undef S |
a9ae4483 | 166 | #undef s |
455d2586 ZW |
167 | #undef ISTABLE |
168 | #undef END | |
6de1e2a9 ZW |
169 | |
170 | /* Given a colon-separated list of file names PATH, | |
171 | add all the names to the search path for include files. */ | |
172 | ||
173 | static void | |
e33f6253 | 174 | path_include (pfile, list, path) |
6de1e2a9 | 175 | cpp_reader *pfile; |
0b22d65c ZW |
176 | char *list; |
177 | int path; | |
6de1e2a9 | 178 | { |
0b22d65c | 179 | char *p, *q, *name; |
6de1e2a9 | 180 | |
0b22d65c | 181 | p = list; |
6de1e2a9 | 182 | |
0b22d65c ZW |
183 | do |
184 | { | |
6de1e2a9 | 185 | /* Find the end of this name. */ |
0b22d65c | 186 | q = p; |
6de1e2a9 | 187 | while (*q != 0 && *q != PATH_SEPARATOR) q++; |
0b22d65c ZW |
188 | if (q == p) |
189 | { | |
190 | /* An empty name in the path stands for the current directory. */ | |
191 | name = (char *) xmalloc (2); | |
192 | name[0] = '.'; | |
193 | name[1] = 0; | |
194 | } | |
195 | else | |
196 | { | |
197 | /* Otherwise use the directory that is named. */ | |
198 | name = (char *) xmalloc (q - p + 1); | |
199 | memcpy (name, p, q - p); | |
200 | name[q - p] = 0; | |
201 | } | |
6de1e2a9 | 202 | |
e33f6253 | 203 | append_include_chain (pfile, name, path, 0); |
6de1e2a9 ZW |
204 | |
205 | /* Advance past this name. */ | |
0b22d65c | 206 | if (*q == 0) |
6de1e2a9 | 207 | break; |
0b22d65c ZW |
208 | p = q + 1; |
209 | } | |
210 | while (1); | |
211 | } | |
212 | ||
0b22d65c ZW |
213 | /* Append DIR to include path PATH. DIR must be permanently allocated |
214 | and writable. */ | |
215 | static void | |
e33f6253 | 216 | append_include_chain (pfile, dir, path, cxx_aware) |
0b22d65c | 217 | cpp_reader *pfile; |
0b22d65c ZW |
218 | char *dir; |
219 | int path; | |
c45da1ca | 220 | int cxx_aware; |
0b22d65c | 221 | { |
e33f6253 | 222 | struct cpp_pending *pend = CPP_OPTION (pfile, pending); |
0b22d65c ZW |
223 | struct file_name_list *new; |
224 | struct stat st; | |
225 | unsigned int len; | |
226 | ||
b0699dad | 227 | _cpp_simplify_pathname (dir); |
0b22d65c ZW |
228 | if (stat (dir, &st)) |
229 | { | |
230 | /* Dirs that don't exist are silently ignored. */ | |
231 | if (errno != ENOENT) | |
c1212d2f | 232 | cpp_notice_from_errno (pfile, dir); |
ae79697b | 233 | else if (CPP_OPTION (pfile, verbose)) |
041c3194 | 234 | fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir); |
0b22d65c ZW |
235 | return; |
236 | } | |
237 | ||
238 | if (!S_ISDIR (st.st_mode)) | |
239 | { | |
c1212d2f | 240 | cpp_notice (pfile, "%s: Not a directory", dir); |
0b22d65c ZW |
241 | return; |
242 | } | |
243 | ||
244 | len = strlen (dir); | |
245 | if (len > pfile->max_include_len) | |
246 | pfile->max_include_len = len; | |
ae79697b | 247 | |
7ceb3598 | 248 | new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); |
0b22d65c ZW |
249 | new->name = dir; |
250 | new->nlen = len; | |
251 | new->ino = st.st_ino; | |
252 | new->dev = st.st_dev; | |
c45da1ca ZW |
253 | if (path == SYSTEM) |
254 | new->sysp = cxx_aware ? 1 : 2; | |
255 | else | |
256 | new->sysp = 0; | |
0b22d65c | 257 | new->name_map = NULL; |
503cb436 DB |
258 | new->next = NULL; |
259 | new->alloc = NULL; | |
0b22d65c ZW |
260 | |
261 | switch (path) | |
262 | { | |
263 | case QUOTE: APPEND (pend, quote, new); break; | |
264 | case BRACKET: APPEND (pend, brack, new); break; | |
265 | case SYSTEM: APPEND (pend, systm, new); break; | |
266 | case AFTER: APPEND (pend, after, new); break; | |
6de1e2a9 ZW |
267 | } |
268 | } | |
269 | ||
88ae23e7 ZW |
270 | /* Merge the four include chains together in the order quote, bracket, |
271 | system, after. Remove duplicate dirs (as determined by | |
272 | INO_T_EQ()). The system_include and after_include chains are never | |
273 | referred to again after this function; all access is through the | |
274 | bracket_include path. | |
275 | ||
276 | For the future: Check if the directory is empty (but | |
277 | how?) and possibly preload the include hash. */ | |
278 | ||
279 | static void | |
ae79697b ZW |
280 | merge_include_chains (pfile) |
281 | cpp_reader *pfile; | |
88ae23e7 ZW |
282 | { |
283 | struct file_name_list *prev, *cur, *other; | |
284 | struct file_name_list *quote, *brack, *systm, *after; | |
285 | struct file_name_list *qtail, *btail, *stail, *atail; | |
286 | ||
ae79697b | 287 | struct cpp_pending *pend = CPP_OPTION (pfile, pending); |
88ae23e7 | 288 | |
ae79697b ZW |
289 | qtail = pend->quote_tail; |
290 | btail = pend->brack_tail; | |
291 | stail = pend->systm_tail; | |
292 | atail = pend->after_tail; | |
293 | ||
294 | quote = pend->quote_head; | |
295 | brack = pend->brack_head; | |
296 | systm = pend->systm_head; | |
297 | after = pend->after_head; | |
88ae23e7 ZW |
298 | |
299 | /* Paste together bracket, system, and after include chains. */ | |
300 | if (stail) | |
301 | stail->next = after; | |
302 | else | |
303 | systm = after; | |
304 | if (btail) | |
305 | btail->next = systm; | |
306 | else | |
307 | brack = systm; | |
308 | ||
309 | /* This is a bit tricky. | |
310 | First we drop dupes from the quote-include list. | |
311 | Then we drop dupes from the bracket-include list. | |
312 | Finally, if qtail and brack are the same directory, | |
313 | we cut out qtail. | |
314 | ||
315 | We can't just merge the lists and then uniquify them because | |
316 | then we may lose directories from the <> search path that should | |
317 | be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however | |
318 | safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written | |
319 | -Ibar -I- -Ifoo -Iquux. | |
320 | ||
321 | Note that this algorithm is quadratic in the number of -I switches, | |
322 | which is acceptable since there aren't usually that many of them. */ | |
323 | ||
324 | for (cur = quote, prev = NULL; cur; cur = cur->next) | |
325 | { | |
326 | for (other = quote; other != cur; other = other->next) | |
327 | if (INO_T_EQ (cur->ino, other->ino) | |
328 | && cur->dev == other->dev) | |
329 | { | |
ae79697b | 330 | if (CPP_OPTION (pfile, verbose)) |
041c3194 | 331 | fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), |
88ae23e7 ZW |
332 | cur->name); |
333 | ||
334 | prev->next = cur->next; | |
335 | free (cur->name); | |
336 | free (cur); | |
337 | cur = prev; | |
338 | break; | |
339 | } | |
340 | prev = cur; | |
341 | } | |
342 | qtail = prev; | |
343 | ||
344 | for (cur = brack; cur; cur = cur->next) | |
345 | { | |
346 | for (other = brack; other != cur; other = other->next) | |
347 | if (INO_T_EQ (cur->ino, other->ino) | |
348 | && cur->dev == other->dev) | |
349 | { | |
ae79697b | 350 | if (CPP_OPTION (pfile, verbose)) |
041c3194 | 351 | fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), |
88ae23e7 ZW |
352 | cur->name); |
353 | ||
354 | prev->next = cur->next; | |
355 | free (cur->name); | |
356 | free (cur); | |
357 | cur = prev; | |
358 | break; | |
359 | } | |
360 | prev = cur; | |
361 | } | |
362 | ||
363 | if (quote) | |
364 | { | |
365 | if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev) | |
366 | { | |
367 | if (quote == qtail) | |
368 | { | |
ae79697b | 369 | if (CPP_OPTION (pfile, verbose)) |
041c3194 | 370 | fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), |
88ae23e7 ZW |
371 | quote->name); |
372 | ||
373 | free (quote->name); | |
374 | free (quote); | |
375 | quote = brack; | |
376 | } | |
377 | else | |
378 | { | |
379 | cur = quote; | |
380 | while (cur->next != qtail) | |
381 | cur = cur->next; | |
382 | cur->next = brack; | |
ae79697b | 383 | if (CPP_OPTION (pfile, verbose)) |
041c3194 | 384 | fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), |
88ae23e7 ZW |
385 | qtail->name); |
386 | ||
387 | free (qtail->name); | |
388 | free (qtail); | |
389 | } | |
390 | } | |
391 | else | |
392 | qtail->next = brack; | |
393 | } | |
394 | else | |
395 | quote = brack; | |
396 | ||
ae79697b ZW |
397 | CPP_OPTION (pfile, quote_include) = quote; |
398 | CPP_OPTION (pfile, bracket_include) = brack; | |
88ae23e7 ZW |
399 | } |
400 | ||
c154ba66 NB |
401 | void |
402 | cpp_init (void) | |
403 | { | |
404 | #ifdef HOST_EBCDIC | |
405 | /* For non-ASCII hosts, the array needs to be sorted at runtime. */ | |
406 | qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp); | |
407 | #endif | |
408 | ||
409 | /* Set up the IStable. This doesn't do anything if we were compiled | |
410 | with a compiler that supports C99 designated initializers. */ | |
411 | init_IStable (); | |
412 | } | |
0b22d65c | 413 | |
6de1e2a9 ZW |
414 | /* Initialize a cpp_reader structure. */ |
415 | void | |
416 | cpp_reader_init (pfile) | |
417 | cpp_reader *pfile; | |
418 | { | |
7ceb3598 | 419 | memset ((char *) pfile, 0, sizeof (cpp_reader)); |
6de1e2a9 | 420 | |
ae79697b ZW |
421 | CPP_OPTION (pfile, dollars_in_ident) = 1; |
422 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
423 | CPP_OPTION (pfile, warn_import) = 1; | |
041c3194 | 424 | CPP_OPTION (pfile, warn_paste) = 1; |
9b55f29a | 425 | CPP_OPTION (pfile, digraphs) = 1; |
ae79697b ZW |
426 | CPP_OPTION (pfile, discard_comments) = 1; |
427 | CPP_OPTION (pfile, show_column) = 1; | |
6ab3e7dd | 428 | CPP_OPTION (pfile, tabstop) = 8; |
ae79697b ZW |
429 | |
430 | CPP_OPTION (pfile, pending) = | |
431 | (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending)); | |
432 | ||
c71f835b | 433 | _cpp_init_macros (pfile); |
bfb9dc7f | 434 | _cpp_init_stacks (pfile); |
c71f835b | 435 | _cpp_init_includes (pfile); |
58fea6af | 436 | _cpp_init_internal_pragmas (pfile); |
6de1e2a9 ZW |
437 | } |
438 | ||
f2d5f0cc ZW |
439 | /* Initialize a cpp_printer structure. As a side effect, open the |
440 | output file. */ | |
441 | cpp_printer * | |
442 | cpp_printer_init (pfile, print) | |
443 | cpp_reader *pfile; | |
444 | cpp_printer *print; | |
445 | { | |
446 | memset (print, '\0', sizeof (cpp_printer)); | |
447 | if (CPP_OPTION (pfile, out_fname) == NULL) | |
448 | CPP_OPTION (pfile, out_fname) = ""; | |
449 | ||
450 | if (CPP_OPTION (pfile, out_fname)[0] == '\0') | |
451 | print->outf = stdout; | |
452 | else | |
453 | { | |
454 | print->outf = fopen (CPP_OPTION (pfile, out_fname), "w"); | |
455 | if (! print->outf) | |
456 | { | |
457 | cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname)); | |
458 | return NULL; | |
459 | } | |
460 | } | |
461 | return print; | |
462 | } | |
463 | ||
6de1e2a9 ZW |
464 | /* Free resources used by PFILE. |
465 | This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */ | |
466 | void | |
467 | cpp_cleanup (pfile) | |
468 | cpp_reader *pfile; | |
469 | { | |
38b24ee2 | 470 | while (CPP_BUFFER (pfile) != NULL) |
6de1e2a9 ZW |
471 | cpp_pop_buffer (pfile); |
472 | ||
49e6c08e ZW |
473 | if (pfile->deps) |
474 | deps_free (pfile->deps); | |
475 | ||
bfb9dc7f ZW |
476 | if (pfile->spec_nodes) |
477 | free (pfile->spec_nodes); | |
478 | ||
479 | _cpp_free_temp_tokens (pfile); | |
480 | _cpp_cleanup_includes (pfile); | |
c71f835b ZW |
481 | _cpp_cleanup_stacks (pfile); |
482 | _cpp_cleanup_macros (pfile); | |
6de1e2a9 ZW |
483 | } |
484 | ||
485 | ||
a9ae4483 ZW |
486 | /* This structure defines one built-in macro. A node of type TYPE will |
487 | be entered in the macro hash table under the name NAME, with value | |
92936ecf ZW |
488 | VALUE (if any). If TYPE is T_OPERATOR, the CODE field is used instead. |
489 | ||
490 | Two values are not compile time constants, so we tag | |
041c3194 | 491 | them in the FLAGS field instead: |
8c389f84 ZW |
492 | VERS value is the global version_string, quoted |
493 | ULP value is the global user_label_prefix | |
92936ecf ZW |
494 | |
495 | Also, macros with CPLUS set in the flags field are entered only for C++. | |
a9ae4483 ZW |
496 | */ |
497 | ||
498 | struct builtin | |
499 | { | |
12cf91fe | 500 | const U_CHAR *name; |
041c3194 | 501 | const char *value; |
92936ecf ZW |
502 | unsigned char code; |
503 | unsigned char type; | |
a9ae4483 | 504 | unsigned short flags; |
12cf91fe | 505 | unsigned int len; |
a9ae4483 | 506 | }; |
92936ecf ZW |
507 | #define VERS 0x01 |
508 | #define ULP 0x02 | |
509 | #define CPLUS 0x04 | |
510 | ||
511 | #define B(n, t) { U n, 0, 0, t, 0, sizeof n - 1 } | |
512 | #define C(n, v) { U n, v, 0, T_MACRO, 0, sizeof n - 1 } | |
513 | #define X(n, f) { U n, 0, 0, T_MACRO, f, sizeof n - 1 } | |
514 | #define O(n, c, f) { U n, 0, c, T_OPERATOR, f, sizeof n - 1 } | |
a9ae4483 | 515 | static const struct builtin builtin_array[] = |
6de1e2a9 | 516 | { |
12cf91fe ZW |
517 | B("__TIME__", T_TIME), |
518 | B("__DATE__", T_DATE), | |
519 | B("__FILE__", T_FILE), | |
520 | B("__BASE_FILE__", T_BASE_FILE), | |
521 | B("__LINE__", T_SPECLINE), | |
522 | B("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL), | |
041c3194 | 523 | B("__STDC__", T_STDC), |
12cf91fe | 524 | |
041c3194 ZW |
525 | X("__VERSION__", VERS), |
526 | X("__USER_LABEL_PREFIX__", ULP), | |
12cf91fe ZW |
527 | C("__REGISTER_PREFIX__", REGISTER_PREFIX), |
528 | C("__HAVE_BUILTIN_SETJMP__", "1"), | |
6de1e2a9 | 529 | #ifndef NO_BUILTIN_SIZE_TYPE |
12cf91fe | 530 | C("__SIZE_TYPE__", SIZE_TYPE), |
6de1e2a9 ZW |
531 | #endif |
532 | #ifndef NO_BUILTIN_PTRDIFF_TYPE | |
12cf91fe | 533 | C("__PTRDIFF_TYPE__", PTRDIFF_TYPE), |
6de1e2a9 | 534 | #endif |
0209c340 | 535 | #ifndef NO_BUILTIN_WCHAR_TYPE |
12cf91fe | 536 | C("__WCHAR_TYPE__", WCHAR_TYPE), |
0209c340 | 537 | #endif |
d6777972 JM |
538 | #ifndef NO_BUILTIN_WINT_TYPE |
539 | C("__WINT_TYPE__", WINT_TYPE), | |
540 | #endif | |
92936ecf ZW |
541 | |
542 | /* Named operators known to the preprocessor. These cannot be #defined | |
543 | and always have their stated meaning. They are treated like normal | |
544 | string tokens except for the type code and the meaning. Most of them | |
545 | are only for C++ (but see iso646.h). */ | |
546 | O("defined", CPP_DEFINED, 0), | |
547 | O("and", CPP_AND_AND, CPLUS), | |
548 | O("and_eq", CPP_AND_EQ, CPLUS), | |
549 | O("bitand", CPP_AND, CPLUS), | |
550 | O("bitor", CPP_OR, CPLUS), | |
551 | O("compl", CPP_COMPL, CPLUS), | |
552 | O("not", CPP_NOT, CPLUS), | |
553 | O("not_eq", CPP_NOT_EQ, CPLUS), | |
554 | O("or", CPP_OR_OR, CPLUS), | |
555 | O("or_eq", CPP_OR_EQ, CPLUS), | |
556 | O("xor", CPP_XOR, CPLUS), | |
557 | O("xor_eq", CPP_XOR_EQ, CPLUS), | |
a9ae4483 | 558 | }; |
12cf91fe ZW |
559 | #undef B |
560 | #undef C | |
561 | #undef X | |
8c389f84 ZW |
562 | #define builtin_array_end \ |
563 | builtin_array + sizeof(builtin_array)/sizeof(struct builtin) | |
a9ae4483 ZW |
564 | |
565 | /* Subroutine of cpp_start_read; reads the builtins table above and | |
566 | enters the macros into the hash table. */ | |
a9ae4483 ZW |
567 | static void |
568 | initialize_builtins (pfile) | |
569 | cpp_reader *pfile; | |
570 | { | |
a9ae4483 | 571 | const struct builtin *b; |
8c389f84 | 572 | for(b = builtin_array; b < builtin_array_end; b++) |
6de1e2a9 | 573 | { |
92936ecf ZW |
574 | if (b->flags & CPLUS && ! CPP_OPTION (pfile, cplusplus)) |
575 | continue; | |
576 | ||
041c3194 | 577 | if (b->type == T_MACRO) |
8c389f84 | 578 | { |
041c3194 ZW |
579 | const char *val; |
580 | char *str; | |
581 | ||
582 | if (b->flags & VERS) | |
583 | { | |
584 | /* Allocate enough space for 'name="value"\0'. */ | |
585 | str = xmalloc (b->len + strlen (version_string) + 4); | |
586 | sprintf (str, "%s=\"%s\"", b->name, version_string); | |
587 | } | |
588 | else | |
589 | { | |
590 | if (b->flags & ULP) | |
591 | val = user_label_prefix; | |
592 | else | |
593 | val = b->value; | |
594 | ||
595 | /* Allocate enough space for "name=value\0". */ | |
596 | str = xmalloc (b->len + strlen (val) + 2); | |
597 | sprintf(str, "%s=%s", b->name, val); | |
598 | } | |
c154ba66 | 599 | |
041c3194 | 600 | cpp_define (pfile, str); |
c154ba66 | 601 | free (str); |
8c389f84 | 602 | } |
6feb7728 | 603 | else |
041c3194 | 604 | { |
f9a0e96c | 605 | cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); |
041c3194 | 606 | hp->type = b->type; |
92936ecf ZW |
607 | if (b->type == T_OPERATOR) |
608 | hp->value.code = b->code; | |
041c3194 | 609 | } |
6de1e2a9 ZW |
610 | } |
611 | } | |
6feb7728 | 612 | #undef VERS |
a9ae4483 | 613 | #undef ULP |
041c3194 | 614 | #undef builtin_array_end |
6de1e2a9 | 615 | |
0b22d65c ZW |
616 | /* Another subroutine of cpp_start_read. This one sets up to do |
617 | dependency-file output. */ | |
618 | static void | |
619 | initialize_dependency_output (pfile) | |
620 | cpp_reader *pfile; | |
621 | { | |
0b22d65c | 622 | char *spec, *s, *output_file; |
ae79697b | 623 | |
0b22d65c ZW |
624 | /* Either of two environment variables can specify output of deps. |
625 | Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET", | |
626 | where OUTPUT_FILE is the file to write deps info to | |
627 | and DEPS_TARGET is the target to mention in the deps. */ | |
628 | ||
ae79697b | 629 | if (CPP_OPTION (pfile, print_deps) == 0) |
0b22d65c ZW |
630 | { |
631 | spec = getenv ("DEPENDENCIES_OUTPUT"); | |
632 | if (spec) | |
ae79697b | 633 | CPP_OPTION (pfile, print_deps) = 1; |
0b22d65c ZW |
634 | else |
635 | { | |
636 | spec = getenv ("SUNPRO_DEPENDENCIES"); | |
637 | if (spec) | |
ae79697b | 638 | CPP_OPTION (pfile, print_deps) = 2; |
0b22d65c ZW |
639 | else |
640 | return; | |
641 | } | |
642 | ||
643 | /* Find the space before the DEPS_TARGET, if there is one. */ | |
644 | s = strchr (spec, ' '); | |
645 | if (s) | |
646 | { | |
ae79697b | 647 | CPP_OPTION (pfile, deps_target) = s + 1; |
0b22d65c ZW |
648 | output_file = (char *) xmalloc (s - spec + 1); |
649 | memcpy (output_file, spec, s - spec); | |
650 | output_file[s - spec] = 0; | |
651 | } | |
652 | else | |
653 | { | |
ae79697b | 654 | CPP_OPTION (pfile, deps_target) = 0; |
0b22d65c ZW |
655 | output_file = spec; |
656 | } | |
657 | ||
ae79697b ZW |
658 | CPP_OPTION (pfile, deps_file) = output_file; |
659 | CPP_OPTION (pfile, print_deps_append) = 1; | |
0b22d65c ZW |
660 | } |
661 | ||
49e6c08e | 662 | pfile->deps = deps_init (); |
0b22d65c | 663 | |
49e6c08e | 664 | /* Print the expected object file name as the target of this Make-rule. */ |
ae79697b ZW |
665 | if (CPP_OPTION (pfile, deps_target)) |
666 | deps_add_target (pfile->deps, CPP_OPTION (pfile, deps_target)); | |
667 | else if (*CPP_OPTION (pfile, in_fname) == 0) | |
49e6c08e | 668 | deps_add_target (pfile->deps, "-"); |
0b22d65c | 669 | else |
ae79697b | 670 | deps_calc_target (pfile->deps, CPP_OPTION (pfile, in_fname)); |
0b22d65c | 671 | |
ae79697b ZW |
672 | if (CPP_OPTION (pfile, in_fname)) |
673 | deps_add_dep (pfile->deps, CPP_OPTION (pfile, in_fname)); | |
0b22d65c ZW |
674 | } |
675 | ||
c45da1ca ZW |
676 | /* And another subroutine. This one sets up the standard include path. */ |
677 | static void | |
678 | initialize_standard_includes (pfile) | |
679 | cpp_reader *pfile; | |
680 | { | |
c45da1ca | 681 | char *path; |
455d2586 | 682 | const struct default_include *p; |
ae79697b | 683 | const char *specd_prefix = CPP_OPTION (pfile, include_prefix); |
c45da1ca ZW |
684 | |
685 | /* Several environment variables may add to the include search path. | |
686 | CPATH specifies an additional list of directories to be searched | |
687 | as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, | |
688 | etc. specify an additional list of directories to be searched as | |
689 | if specified with -isystem, for the language indicated. */ | |
690 | ||
691 | GET_ENV_PATH_LIST (path, "CPATH"); | |
692 | if (path != 0 && *path != 0) | |
e33f6253 | 693 | path_include (pfile, path, BRACKET); |
c45da1ca | 694 | |
ae79697b | 695 | switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus)) |
c45da1ca ZW |
696 | { |
697 | case 0: | |
698 | GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH"); | |
699 | break; | |
700 | case 1: | |
701 | GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH"); | |
702 | break; | |
703 | case 2: | |
704 | GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH"); | |
705 | break; | |
706 | case 3: | |
707 | GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH"); | |
708 | break; | |
709 | } | |
710 | if (path != 0 && *path != 0) | |
e33f6253 | 711 | path_include (pfile, path, SYSTEM); |
c45da1ca ZW |
712 | |
713 | /* Search "translated" versions of GNU directories. | |
714 | These have /usr/local/lib/gcc... replaced by specd_prefix. */ | |
60893f43 | 715 | if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len) |
c45da1ca | 716 | { |
c45da1ca ZW |
717 | /* Remove the `include' from /usr/local/lib/gcc.../include. |
718 | GCC_INCLUDE_DIR will always end in /include. */ | |
60893f43 ZW |
719 | int default_len = cpp_GCC_INCLUDE_DIR_len; |
720 | char *default_prefix = (char *) alloca (default_len + 1); | |
c45da1ca ZW |
721 | int specd_len = strlen (specd_prefix); |
722 | ||
60893f43 | 723 | memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len); |
c45da1ca ZW |
724 | default_prefix[default_len] = '\0'; |
725 | ||
60893f43 | 726 | for (p = cpp_include_defaults; p->fname; p++) |
c45da1ca ZW |
727 | { |
728 | /* Some standard dirs are only for C++. */ | |
729 | if (!p->cplusplus | |
ae79697b ZW |
730 | || (CPP_OPTION (pfile, cplusplus) |
731 | && !CPP_OPTION (pfile, no_standard_cplusplus_includes))) | |
c45da1ca ZW |
732 | { |
733 | /* Does this dir start with the prefix? */ | |
734 | if (!strncmp (p->fname, default_prefix, default_len)) | |
735 | { | |
736 | /* Yes; change prefix and add to search list. */ | |
737 | int flen = strlen (p->fname); | |
738 | int this_len = specd_len + flen - default_len; | |
739 | char *str = (char *) xmalloc (this_len + 1); | |
740 | memcpy (str, specd_prefix, specd_len); | |
741 | memcpy (str + specd_len, | |
742 | p->fname + default_len, | |
743 | flen - default_len + 1); | |
744 | ||
e33f6253 | 745 | append_include_chain (pfile, str, SYSTEM, p->cxx_aware); |
c45da1ca ZW |
746 | } |
747 | } | |
748 | } | |
749 | } | |
750 | ||
751 | /* Search ordinary names for GNU include directories. */ | |
60893f43 | 752 | for (p = cpp_include_defaults; p->fname; p++) |
c45da1ca ZW |
753 | { |
754 | /* Some standard dirs are only for C++. */ | |
755 | if (!p->cplusplus | |
ae79697b ZW |
756 | || (CPP_OPTION (pfile, cplusplus) |
757 | && !CPP_OPTION (pfile, no_standard_cplusplus_includes))) | |
c45da1ca ZW |
758 | { |
759 | /* XXX Potential memory leak! */ | |
760 | char *str = xstrdup (update_path (p->fname, p->component)); | |
e33f6253 | 761 | append_include_chain (pfile, str, SYSTEM, p->cxx_aware); |
c45da1ca ZW |
762 | } |
763 | } | |
764 | } | |
765 | ||
6de1e2a9 ZW |
766 | /* This is called after options have been processed. |
767 | * Check options for consistency, and setup for processing input | |
768 | * from the file named FNAME. (Use standard input if FNAME==NULL.) | |
769 | * Return 1 on success, 0 on failure. | |
770 | */ | |
771 | ||
772 | int | |
f2d5f0cc | 773 | cpp_start_read (pfile, print, fname) |
6de1e2a9 | 774 | cpp_reader *pfile; |
f2d5f0cc | 775 | cpp_printer *print; |
7ceb3598 | 776 | const char *fname; |
6de1e2a9 | 777 | { |
0b22d65c | 778 | struct pending_option *p, *q; |
6de1e2a9 | 779 | |
0b22d65c ZW |
780 | /* -MG doesn't select the form of output and must be specified with one of |
781 | -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't | |
782 | inhibit compilation. */ | |
ae79697b ZW |
783 | if (CPP_OPTION (pfile, print_deps_missing_files) |
784 | && (CPP_OPTION (pfile, print_deps) == 0 | |
785 | || !CPP_OPTION (pfile, no_output))) | |
0b22d65c ZW |
786 | { |
787 | cpp_fatal (pfile, "-MG must be specified with one of -M or -MM"); | |
788 | return 0; | |
789 | } | |
6de1e2a9 | 790 | |
bfab56e7 ZW |
791 | /* -Wtraditional is not useful in C++ mode. */ |
792 | if (CPP_OPTION (pfile, cplusplus)) | |
793 | CPP_OPTION (pfile, warn_traditional) = 0; | |
794 | ||
5ef865d5 | 795 | /* Do not warn about invalid token pasting if -lang-asm. */ |
f9a0e96c | 796 | if (CPP_OPTION (pfile, lang_asm)) |
041c3194 ZW |
797 | CPP_OPTION (pfile, warn_paste) = 0; |
798 | ||
0b22d65c ZW |
799 | /* Set this if it hasn't been set already. */ |
800 | if (user_label_prefix == NULL) | |
801 | user_label_prefix = USER_LABEL_PREFIX; | |
c45da1ca | 802 | |
041c3194 ZW |
803 | /* Figure out if we need to save function macro parameter spellings. |
804 | We don't use CPP_PEDANTIC() here because that depends on whether | |
805 | or not the current file is a system header, and there is no | |
806 | current file yet. */ | |
807 | pfile->save_parameter_spellings = | |
808 | CPP_OPTION (pfile, pedantic) | |
809 | || CPP_OPTION (pfile, debug_output) | |
810 | || CPP_OPTION (pfile, dump_macros) == dump_definitions | |
811 | || CPP_OPTION (pfile, dump_macros) == dump_only; | |
812 | ||
45b966db ZW |
813 | /* Set up the tables used by read_and_prescan. */ |
814 | _cpp_init_input_buffer (pfile); | |
ae79697b | 815 | |
c45da1ca | 816 | /* Set up the include search path now. */ |
ae79697b | 817 | if (! CPP_OPTION (pfile, no_standard_includes)) |
c45da1ca ZW |
818 | initialize_standard_includes (pfile); |
819 | ||
ae79697b | 820 | merge_include_chains (pfile); |
c45da1ca ZW |
821 | |
822 | /* With -v, print the list of dirs to search. */ | |
ae79697b | 823 | if (CPP_OPTION (pfile, verbose)) |
c45da1ca ZW |
824 | { |
825 | struct file_name_list *l; | |
826 | fprintf (stderr, _("#include \"...\" search starts here:\n")); | |
ae79697b | 827 | for (l = CPP_OPTION (pfile, quote_include); l; l = l->next) |
c45da1ca | 828 | { |
ae79697b | 829 | if (l == CPP_OPTION (pfile, bracket_include)) |
c45da1ca ZW |
830 | fprintf (stderr, _("#include <...> search starts here:\n")); |
831 | fprintf (stderr, " %s\n", l->name); | |
832 | } | |
833 | fprintf (stderr, _("End of search list.\n")); | |
834 | } | |
835 | ||
4b3fe5b6 ZW |
836 | /* Open the main input file. This must be done early, so we have a |
837 | buffer to stand on. */ | |
ae79697b ZW |
838 | if (CPP_OPTION (pfile, in_fname) == NULL |
839 | || *CPP_OPTION (pfile, in_fname) == 0) | |
6de1e2a9 | 840 | { |
ae79697b ZW |
841 | CPP_OPTION (pfile, in_fname) = fname; |
842 | if (CPP_OPTION (pfile, in_fname) == NULL) | |
843 | CPP_OPTION (pfile, in_fname) = ""; | |
6de1e2a9 | 844 | } |
ae79697b ZW |
845 | if (CPP_OPTION (pfile, out_fname) == NULL) |
846 | CPP_OPTION (pfile, out_fname) = ""; | |
6de1e2a9 | 847 | |
c45da1ca ZW |
848 | if (!cpp_read_file (pfile, fname)) |
849 | return 0; | |
850 | ||
4b3fe5b6 | 851 | initialize_dependency_output (pfile); |
ae79697b | 852 | |
c45da1ca | 853 | /* Install __LINE__, etc. */ |
6de1e2a9 ZW |
854 | initialize_builtins (pfile); |
855 | ||
6de1e2a9 | 856 | /* Do -U's, -D's and -A's in the order they were seen. */ |
ae79697b | 857 | p = CPP_OPTION (pfile, pending)->directive_head; |
0b22d65c | 858 | while (p) |
6de1e2a9 | 859 | { |
b1b74f93 | 860 | (*p->handler) (pfile, p->arg); |
0b22d65c ZW |
861 | q = p->next; |
862 | free (p); | |
863 | p = q; | |
6de1e2a9 | 864 | } |
ae79697b | 865 | pfile->done_initializing = 1; |
041c3194 | 866 | |
58fea6af ZW |
867 | /* We start at line 1 of the main input file. */ |
868 | if (print) | |
869 | { | |
870 | print->last_fname = CPP_BUFFER (pfile)->nominal_fname; | |
871 | print->lineno = 1; | |
872 | } | |
6de1e2a9 ZW |
873 | |
874 | /* The -imacros files can be scanned now, but the -include files | |
875 | have to be pushed onto the include stack and processed later, | |
0b22d65c | 876 | in the main loop calling cpp_get_token. */ |
ae79697b | 877 | |
ae79697b | 878 | p = CPP_OPTION (pfile, pending)->imacros_head; |
0b22d65c | 879 | while (p) |
6de1e2a9 | 880 | { |
c45da1ca | 881 | if (cpp_read_file (pfile, p->arg)) |
f2d5f0cc | 882 | cpp_scan_buffer_nooutput (pfile); |
0b22d65c ZW |
883 | q = p->next; |
884 | free (p); | |
885 | p = q; | |
6de1e2a9 | 886 | } |
0b22d65c | 887 | |
ae79697b | 888 | p = CPP_OPTION (pfile, pending)->include_head; |
0b22d65c | 889 | while (p) |
6de1e2a9 | 890 | { |
58fea6af | 891 | cpp_read_file (pfile, p->arg); |
0b22d65c ZW |
892 | q = p->next; |
893 | free (p); | |
894 | p = q; | |
6de1e2a9 | 895 | } |
6de1e2a9 | 896 | |
ae79697b ZW |
897 | free (CPP_OPTION (pfile, pending)); |
898 | CPP_OPTION (pfile, pending) = NULL; | |
6de1e2a9 ZW |
899 | |
900 | return 1; | |
901 | } | |
902 | ||
903 | /* This is called at the end of preprocessing. It pops the | |
904 | last buffer and writes dependency output. It should also | |
905 | clear macro definitions, such that you could call cpp_start_read | |
906 | with a new filename to restart processing. */ | |
907 | void | |
f2d5f0cc | 908 | cpp_finish (pfile, print) |
6de1e2a9 | 909 | cpp_reader *pfile; |
f2d5f0cc | 910 | cpp_printer *print; |
6de1e2a9 | 911 | { |
f2d5f0cc ZW |
912 | if (CPP_BUFFER (pfile)) |
913 | { | |
914 | cpp_ice (pfile, "buffers still stacked in cpp_finish"); | |
915 | while (CPP_BUFFER (pfile)) | |
916 | cpp_pop_buffer (pfile); | |
917 | } | |
c1212d2f | 918 | |
49e6c08e | 919 | /* Don't write the deps file if preprocessing has failed. */ |
ae79697b | 920 | if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0) |
6de1e2a9 ZW |
921 | { |
922 | /* Stream on which to print the dependency information. */ | |
c1212d2f | 923 | FILE *deps_stream = 0; |
ae79697b ZW |
924 | const char *deps_mode |
925 | = CPP_OPTION (pfile, print_deps_append) ? "a" : "w"; | |
926 | if (CPP_OPTION (pfile, deps_file) == 0) | |
49e6c08e | 927 | deps_stream = stdout; |
ae79697b ZW |
928 | else |
929 | { | |
930 | deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode); | |
931 | if (deps_stream == 0) | |
932 | cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file)); | |
933 | } | |
49e6c08e ZW |
934 | if (deps_stream) |
935 | { | |
936 | deps_write (pfile->deps, deps_stream, 72); | |
ae79697b | 937 | if (CPP_OPTION (pfile, deps_file)) |
6de1e2a9 | 938 | { |
49e6c08e ZW |
939 | if (ferror (deps_stream) || fclose (deps_stream) != 0) |
940 | cpp_fatal (pfile, "I/O error on output"); | |
6de1e2a9 ZW |
941 | } |
942 | } | |
943 | } | |
3caee4a8 | 944 | |
f2d5f0cc ZW |
945 | /* Flush any pending output. */ |
946 | if (print) | |
947 | { | |
58fea6af ZW |
948 | if (pfile->need_newline) |
949 | putc ('\n', print->outf); | |
f2d5f0cc ZW |
950 | if (ferror (print->outf) || fclose (print->outf)) |
951 | cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname)); | |
952 | } | |
d4506961 ZW |
953 | |
954 | /* Report on headers that could use multiple include guards. */ | |
955 | if (CPP_OPTION (pfile, print_include_names)) | |
c71f835b | 956 | _cpp_report_missing_guards (pfile); |
6de1e2a9 ZW |
957 | } |
958 | ||
223dca6a | 959 | static void |
ae79697b ZW |
960 | new_pending_directive (pend, text, handler) |
961 | struct cpp_pending *pend; | |
223dca6a | 962 | const char *text; |
40eac643 | 963 | cl_directive_handler handler; |
223dca6a RH |
964 | { |
965 | struct pending_option *o = (struct pending_option *) | |
966 | xmalloc (sizeof (struct pending_option)); | |
967 | ||
7ceb3598 | 968 | o->arg = text; |
223dca6a | 969 | o->next = NULL; |
40eac643 | 970 | o->handler = handler; |
ae79697b | 971 | APPEND (pend, directive, o); |
223dca6a RH |
972 | } |
973 | ||
ae79697b ZW |
974 | /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string"); |
975 | I.e. a const string initializer with parens around it. That is | |
976 | what N_("string") resolves to, so we make no_* be macros instead. */ | |
977 | #define no_arg N_("Argument missing after %s") | |
978 | #define no_ass N_("Assertion missing after %s") | |
979 | #define no_dir N_("Directory name missing after %s") | |
980 | #define no_fil N_("File name missing after %s") | |
981 | #define no_mac N_("Macro name missing after %s") | |
982 | #define no_pth N_("Path name missing after %s") | |
6ab3e7dd | 983 | #define no_num N_("Number missing after %s") |
ae79697b ZW |
984 | |
985 | /* This is the list of all command line options, with the leading | |
986 | "-" removed. It must be sorted in ASCII collating order. */ | |
987 | #define COMMAND_LINE_OPTIONS \ | |
988 | DEF_OPT("", 0, OPT_stdin_stdout) \ | |
989 | DEF_OPT("$", 0, OPT_dollar) \ | |
990 | DEF_OPT("+", 0, OPT_plus) \ | |
991 | DEF_OPT("-help", 0, OPT__help) \ | |
992 | DEF_OPT("-version", 0, OPT__version) \ | |
993 | DEF_OPT("A", no_ass, OPT_A) \ | |
994 | DEF_OPT("C", 0, OPT_C) \ | |
995 | DEF_OPT("D", no_mac, OPT_D) \ | |
996 | DEF_OPT("H", 0, OPT_H) \ | |
997 | DEF_OPT("I", no_dir, OPT_I) \ | |
998 | DEF_OPT("M", 0, OPT_M) \ | |
999 | DEF_OPT("MD", no_fil, OPT_MD) \ | |
1000 | DEF_OPT("MG", 0, OPT_MG) \ | |
1001 | DEF_OPT("MM", 0, OPT_MM) \ | |
1002 | DEF_OPT("MMD", no_fil, OPT_MMD) \ | |
1003 | DEF_OPT("P", 0, OPT_P) \ | |
1004 | DEF_OPT("U", no_mac, OPT_U) \ | |
1005 | DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \ | |
1006 | DEF_OPT("d", no_arg, OPT_d) \ | |
1007 | DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \ | |
1008 | DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \ | |
1009 | DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \ | |
1010 | DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \ | |
1011 | DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \ | |
1012 | DEF_OPT("fshow-column", 0, OPT_fshow_column) \ | |
6ab3e7dd | 1013 | DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \ |
ae79697b ZW |
1014 | DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \ |
1015 | DEF_OPT("h", 0, OPT_h) \ | |
1016 | DEF_OPT("idirafter", no_dir, OPT_idirafter) \ | |
1017 | DEF_OPT("imacros", no_fil, OPT_imacros) \ | |
1018 | DEF_OPT("include", no_fil, OPT_include) \ | |
1019 | DEF_OPT("iprefix", no_pth, OPT_iprefix) \ | |
1020 | DEF_OPT("isystem", no_dir, OPT_isystem) \ | |
1021 | DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \ | |
1022 | DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \ | |
1023 | DEF_OPT("lang-asm", 0, OPT_lang_asm) \ | |
1024 | DEF_OPT("lang-c", 0, OPT_lang_c) \ | |
1025 | DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \ | |
1026 | DEF_OPT("lang-c89", 0, OPT_lang_c89) \ | |
ae79697b ZW |
1027 | DEF_OPT("lang-objc", 0, OPT_lang_objc) \ |
1028 | DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \ | |
1029 | DEF_OPT("nostdinc", 0, OPT_nostdinc) \ | |
1030 | DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \ | |
1031 | DEF_OPT("o", no_fil, OPT_o) \ | |
1032 | DEF_OPT("pedantic", 0, OPT_pedantic) \ | |
1033 | DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \ | |
1034 | DEF_OPT("remap", 0, OPT_remap) \ | |
1035 | DEF_OPT("std=c89", 0, OPT_std_c89) \ | |
1036 | DEF_OPT("std=c99", 0, OPT_std_c99) \ | |
1037 | DEF_OPT("std=c9x", 0, OPT_std_c9x) \ | |
1038 | DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \ | |
1039 | DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \ | |
1040 | DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \ | |
1041 | DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \ | |
1042 | DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \ | |
1043 | DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \ | |
1044 | DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \ | |
ae79697b ZW |
1045 | DEF_OPT("trigraphs", 0, OPT_trigraphs) \ |
1046 | DEF_OPT("v", 0, OPT_v) \ | |
1047 | DEF_OPT("w", 0, OPT_w) | |
1048 | ||
1049 | #define DEF_OPT(text, msg, code) code, | |
e23c0ba3 ZW |
1050 | enum opt_code |
1051 | { | |
ae79697b | 1052 | COMMAND_LINE_OPTIONS |
e23c0ba3 ZW |
1053 | N_OPTS |
1054 | }; | |
ae79697b | 1055 | #undef DEF_OPT |
e23c0ba3 ZW |
1056 | |
1057 | struct cl_option | |
1058 | { | |
1059 | const char *opt_text; | |
1060 | const char *msg; | |
1061 | size_t opt_len; | |
1062 | enum opt_code opt_code; | |
1063 | }; | |
1064 | ||
ae79697b | 1065 | #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code }, |
e23c0ba3 ZW |
1066 | #ifdef HOST_EBCDIC |
1067 | static struct cl_option cl_options[] = | |
1068 | #else | |
1069 | static const struct cl_option cl_options[] = | |
1070 | #endif | |
1071 | { | |
ae79697b | 1072 | COMMAND_LINE_OPTIONS |
e23c0ba3 ZW |
1073 | }; |
1074 | #undef DEF_OPT | |
ae79697b | 1075 | #undef COMMAND_LINE_OPTIONS |
e23c0ba3 ZW |
1076 | |
1077 | /* Perform a binary search to find which, if any, option the given | |
1078 | command-line matches. Returns its index in the option array, | |
1079 | negative on failure. Complications arise since some options can be | |
1080 | suffixed with an argument, and multiple complete matches can occur, | |
1081 | e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to | |
1082 | accept options beginning with -g and -W that we do not recognise, | |
1083 | but not to swallow any subsequent command line argument; these are | |
1084 | handled as special cases in cpp_handle_option */ | |
1085 | static int | |
1086 | parse_option (input) | |
1087 | const char *input; | |
1088 | { | |
1089 | unsigned int md, mn, mx; | |
1090 | size_t opt_len; | |
1091 | int comp; | |
1092 | ||
1093 | mn = 0; | |
1094 | mx = N_OPTS; | |
1095 | ||
1096 | while (mx > mn) | |
1097 | { | |
1098 | md = (mn + mx) / 2; | |
ae79697b | 1099 | |
e23c0ba3 ZW |
1100 | opt_len = cl_options[md].opt_len; |
1101 | comp = strncmp (input, cl_options[md].opt_text, opt_len); | |
ae79697b | 1102 | |
e23c0ba3 ZW |
1103 | if (comp > 0) |
1104 | mn = md + 1; | |
1105 | else if (comp < 0) | |
1106 | mx = md; | |
1107 | else | |
1108 | { | |
1109 | if (input[opt_len] == '\0') | |
1110 | return md; | |
1111 | /* We were passed more text. If the option takes an argument, | |
1112 | we may match a later option or we may have been passed the | |
1113 | argument. The longest possible option match succeeds. | |
1114 | If the option takes no arguments we have not matched and | |
1115 | continue the search (e.g. input="stdc++" match was "stdc") */ | |
1116 | mn = md + 1; | |
1117 | if (cl_options[md].msg) | |
1118 | { | |
1119 | /* Scan forwards. If we get an exact match, return it. | |
1120 | Otherwise, return the longest option-accepting match. | |
1121 | This loops no more than twice with current options */ | |
1122 | mx = md; | |
1123 | for (; mn < N_OPTS; mn++) | |
1124 | { | |
1125 | opt_len = cl_options[mn].opt_len; | |
1126 | if (strncmp (input, cl_options[mn].opt_text, opt_len)) | |
1127 | break; | |
1128 | if (input[opt_len] == '\0') | |
1129 | return mn; | |
1130 | if (cl_options[mn].msg) | |
1131 | mx = mn; | |
1132 | } | |
1133 | return mx; | |
1134 | } | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | return -1; | |
1139 | } | |
1140 | ||
6de1e2a9 ZW |
1141 | /* Handle one command-line option in (argc, argv). |
1142 | Can be called multiple times, to handle multiple sets of options. | |
1143 | Returns number of strings consumed. */ | |
c8d8ed65 | 1144 | |
2c0accc9 ZW |
1145 | int |
1146 | cpp_handle_option (pfile, argc, argv) | |
6de1e2a9 ZW |
1147 | cpp_reader *pfile; |
1148 | int argc; | |
1149 | char **argv; | |
1150 | { | |
6de1e2a9 | 1151 | int i = 0; |
f8f769ea | 1152 | struct cpp_pending *pend = CPP_OPTION (pfile, pending); |
6de1e2a9 | 1153 | |
0b22d65c ZW |
1154 | if (argv[i][0] != '-') |
1155 | { | |
ae79697b ZW |
1156 | if (CPP_OPTION (pfile, out_fname) != NULL) |
1157 | cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info", | |
1158 | progname); | |
1159 | else if (CPP_OPTION (pfile, in_fname) != NULL) | |
1160 | CPP_OPTION (pfile, out_fname) = argv[i]; | |
0b22d65c | 1161 | else |
ae79697b | 1162 | CPP_OPTION (pfile, in_fname) = argv[i]; |
0b22d65c ZW |
1163 | } |
1164 | else | |
e23c0ba3 ZW |
1165 | { |
1166 | enum opt_code opt_code; | |
1167 | int opt_index; | |
7ceb3598 | 1168 | const char *arg = 0; |
e23c0ba3 ZW |
1169 | |
1170 | /* Skip over '-' */ | |
1171 | opt_index = parse_option (&argv[i][1]); | |
1172 | if (opt_index < 0) | |
1173 | return i; | |
1174 | ||
1175 | opt_code = cl_options[opt_index].opt_code; | |
1176 | if (cl_options[opt_index].msg) | |
1177 | { | |
1178 | arg = &argv[i][cl_options[opt_index].opt_len + 1]; | |
1179 | ||
1180 | /* Yuk. Special case for -g and -W as they must not swallow | |
1181 | up any following argument. If this becomes common, add | |
1182 | another field to the cl_options table */ | |
1183 | if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W)) | |
1184 | { | |
1185 | arg = argv[++i]; | |
1186 | if (!arg) | |
1187 | { | |
d88b89e5 | 1188 | cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]); |
e23c0ba3 ZW |
1189 | return argc; |
1190 | } | |
1191 | } | |
1192 | } | |
ae79697b | 1193 | |
e23c0ba3 ZW |
1194 | switch (opt_code) |
1195 | { | |
1196 | case N_OPTS: /* shut GCC up */ | |
1197 | break; | |
1198 | case OPT_fleading_underscore: | |
0b22d65c | 1199 | user_label_prefix = "_"; |
e23c0ba3 ZW |
1200 | break; |
1201 | case OPT_fno_leading_underscore: | |
0b22d65c | 1202 | user_label_prefix = ""; |
e23c0ba3 ZW |
1203 | break; |
1204 | case OPT_fpreprocessed: | |
ae79697b | 1205 | CPP_OPTION (pfile, preprocessed) = 1; |
e23c0ba3 ZW |
1206 | break; |
1207 | case OPT_fno_preprocessed: | |
ae79697b ZW |
1208 | CPP_OPTION (pfile, preprocessed) = 0; |
1209 | break; | |
1210 | case OPT_fshow_column: | |
1211 | CPP_OPTION (pfile, show_column) = 1; | |
1212 | break; | |
1213 | case OPT_fno_show_column: | |
1214 | CPP_OPTION (pfile, show_column) = 0; | |
e23c0ba3 | 1215 | break; |
6ab3e7dd NB |
1216 | case OPT_ftabstop: |
1217 | /* Silently ignore empty string, non-longs and silly values. */ | |
1218 | if (arg[0] != '\0') | |
1219 | { | |
1220 | char *endptr; | |
1221 | long tabstop = strtol (arg, &endptr, 10); | |
1222 | if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100) | |
1223 | CPP_OPTION (pfile, tabstop) = tabstop; | |
1224 | } | |
1225 | break; | |
e23c0ba3 | 1226 | case OPT_w: |
ae79697b | 1227 | CPP_OPTION (pfile, inhibit_warnings) = 1; |
e23c0ba3 ZW |
1228 | break; |
1229 | case OPT_g: /* Silently ignore anything but -g3 */ | |
1230 | if (!strcmp(&argv[i][2], "3")) | |
ae79697b | 1231 | CPP_OPTION (pfile, debug_output) = 1; |
e23c0ba3 ZW |
1232 | break; |
1233 | case OPT_h: | |
1234 | case OPT__help: | |
1235 | print_help (); | |
1236 | exit (0); /* XXX */ | |
1237 | break; | |
1238 | case OPT__version: | |
1239 | fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string); | |
1240 | exit (0); /* XXX */ | |
1241 | break; | |
1242 | case OPT_C: | |
ae79697b | 1243 | CPP_OPTION (pfile, discard_comments) = 0; |
e23c0ba3 ZW |
1244 | break; |
1245 | case OPT_P: | |
ae79697b | 1246 | CPP_OPTION (pfile, no_line_commands) = 1; |
e23c0ba3 ZW |
1247 | break; |
1248 | case OPT_dollar: /* Don't include $ in identifiers. */ | |
ae79697b | 1249 | CPP_OPTION (pfile, dollars_in_ident) = 0; |
e23c0ba3 ZW |
1250 | break; |
1251 | case OPT_H: | |
ae79697b | 1252 | CPP_OPTION (pfile, print_include_names) = 1; |
e23c0ba3 ZW |
1253 | break; |
1254 | case OPT_D: | |
f8f769ea | 1255 | new_pending_directive (pend, arg, cpp_define); |
e23c0ba3 ZW |
1256 | break; |
1257 | case OPT_pedantic_errors: | |
ae79697b | 1258 | CPP_OPTION (pfile, pedantic_errors) = 1; |
e23c0ba3 ZW |
1259 | /* fall through */ |
1260 | case OPT_pedantic: | |
ae79697b | 1261 | CPP_OPTION (pfile, pedantic) = 1; |
e23c0ba3 | 1262 | break; |
e23c0ba3 | 1263 | case OPT_trigraphs: |
ae79697b | 1264 | CPP_OPTION (pfile, trigraphs) = 1; |
e23c0ba3 ZW |
1265 | break; |
1266 | case OPT_plus: | |
ae79697b ZW |
1267 | CPP_OPTION (pfile, cplusplus) = 1; |
1268 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
e23c0ba3 ZW |
1269 | break; |
1270 | case OPT_remap: | |
ae79697b | 1271 | CPP_OPTION (pfile, remap) = 1; |
e23c0ba3 ZW |
1272 | break; |
1273 | case OPT_iprefix: | |
ae79697b ZW |
1274 | CPP_OPTION (pfile, include_prefix) = arg; |
1275 | CPP_OPTION (pfile, include_prefix_len) = strlen (arg); | |
e23c0ba3 ZW |
1276 | break; |
1277 | case OPT_lang_c: | |
ae79697b ZW |
1278 | CPP_OPTION (pfile, cplusplus) = 0; |
1279 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
1280 | CPP_OPTION (pfile, c89) = 0; | |
1281 | CPP_OPTION (pfile, c99) = 1; | |
9b55f29a | 1282 | CPP_OPTION (pfile, digraphs) = 1; |
ae79697b | 1283 | CPP_OPTION (pfile, objc) = 0; |
e23c0ba3 | 1284 | break; |
e23c0ba3 | 1285 | case OPT_lang_cplusplus: |
ae79697b ZW |
1286 | CPP_OPTION (pfile, cplusplus) = 1; |
1287 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
1288 | CPP_OPTION (pfile, c89) = 0; | |
1289 | CPP_OPTION (pfile, c99) = 0; | |
1290 | CPP_OPTION (pfile, objc) = 0; | |
9b55f29a | 1291 | CPP_OPTION (pfile, digraphs) = 1; |
f8f769ea | 1292 | new_pending_directive (pend, "__cplusplus", cpp_define); |
e23c0ba3 | 1293 | break; |
e23c0ba3 | 1294 | case OPT_lang_objcplusplus: |
f8f769ea ZW |
1295 | CPP_OPTION (pfile, cplusplus) = 1; |
1296 | new_pending_directive (pend, "__cplusplus", cpp_define); | |
1297 | /* fall through */ | |
1298 | case OPT_lang_objc: | |
ae79697b ZW |
1299 | CPP_OPTION (pfile, cplusplus_comments) = 1; |
1300 | CPP_OPTION (pfile, c89) = 0; | |
1301 | CPP_OPTION (pfile, c99) = 0; | |
1302 | CPP_OPTION (pfile, objc) = 1; | |
f8f769ea | 1303 | new_pending_directive (pend, "__OBJC__", cpp_define); |
e23c0ba3 ZW |
1304 | break; |
1305 | case OPT_lang_asm: | |
ae79697b | 1306 | CPP_OPTION (pfile, lang_asm) = 1; |
f8f769ea ZW |
1307 | CPP_OPTION (pfile, dollars_in_ident) = 0; |
1308 | new_pending_directive (pend, "__ASSEMBLER__", cpp_define); | |
e23c0ba3 | 1309 | break; |
e23c0ba3 ZW |
1310 | case OPT_nostdinc: |
1311 | /* -nostdinc causes no default include directories. | |
1312 | You must specify all include-file directories with -I. */ | |
ae79697b | 1313 | CPP_OPTION (pfile, no_standard_includes) = 1; |
e23c0ba3 ZW |
1314 | break; |
1315 | case OPT_nostdincplusplus: | |
1316 | /* -nostdinc++ causes no default C++-specific include directories. */ | |
ae79697b | 1317 | CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1; |
e23c0ba3 ZW |
1318 | break; |
1319 | case OPT_std_gnu89: | |
ae79697b ZW |
1320 | CPP_OPTION (pfile, cplusplus) = 0; |
1321 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
1322 | CPP_OPTION (pfile, c89) = 1; | |
1323 | CPP_OPTION (pfile, c99) = 0; | |
1324 | CPP_OPTION (pfile, objc) = 0; | |
9b55f29a | 1325 | CPP_OPTION (pfile, digraphs) = 1; |
e23c0ba3 ZW |
1326 | break; |
1327 | case OPT_std_gnu9x: | |
1328 | case OPT_std_gnu99: | |
ae79697b ZW |
1329 | CPP_OPTION (pfile, cplusplus) = 0; |
1330 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
1331 | CPP_OPTION (pfile, c89) = 0; | |
1332 | CPP_OPTION (pfile, c99) = 1; | |
9b55f29a | 1333 | CPP_OPTION (pfile, digraphs) = 1; |
ae79697b | 1334 | CPP_OPTION (pfile, objc) = 0; |
e33f6253 | 1335 | new_pending_directive (pend, "__STDC_VERSION__=199901L", cpp_define); |
e23c0ba3 ZW |
1336 | break; |
1337 | case OPT_std_iso9899_199409: | |
e33f6253 | 1338 | new_pending_directive (pend, "__STDC_VERSION__=199409L", cpp_define); |
e23c0ba3 ZW |
1339 | /* Fall through */ |
1340 | case OPT_std_iso9899_1990: | |
1341 | case OPT_std_c89: | |
9b55f29a | 1342 | case OPT_lang_c89: |
ae79697b ZW |
1343 | CPP_OPTION (pfile, cplusplus) = 0; |
1344 | CPP_OPTION (pfile, cplusplus_comments) = 0; | |
1345 | CPP_OPTION (pfile, c89) = 1; | |
1346 | CPP_OPTION (pfile, c99) = 0; | |
1347 | CPP_OPTION (pfile, objc) = 0; | |
9b55f29a | 1348 | CPP_OPTION (pfile, digraphs) = opt_code == OPT_std_iso9899_199409; |
ae79697b | 1349 | CPP_OPTION (pfile, trigraphs) = 1; |
9b55f29a | 1350 | new_pending_directive (pend, "__STRICT_ANSI__", cpp_define); |
e23c0ba3 ZW |
1351 | break; |
1352 | case OPT_std_iso9899_199x: | |
1353 | case OPT_std_iso9899_1999: | |
1354 | case OPT_std_c9x: | |
1355 | case OPT_std_c99: | |
ae79697b ZW |
1356 | CPP_OPTION (pfile, cplusplus) = 0; |
1357 | CPP_OPTION (pfile, cplusplus_comments) = 1; | |
1358 | CPP_OPTION (pfile, c89) = 0; | |
1359 | CPP_OPTION (pfile, c99) = 1; | |
1360 | CPP_OPTION (pfile, objc) = 0; | |
9b55f29a | 1361 | CPP_OPTION (pfile, digraphs) = 1; |
ae79697b | 1362 | CPP_OPTION (pfile, trigraphs) = 1; |
e33f6253 NB |
1363 | new_pending_directive (pend, "__STRICT_ANSI__", cpp_define); |
1364 | new_pending_directive (pend, "__STDC_VERSION__=199901L", cpp_define); | |
e23c0ba3 ZW |
1365 | break; |
1366 | case OPT_o: | |
ae79697b | 1367 | if (CPP_OPTION (pfile, out_fname) != NULL) |
0b22d65c | 1368 | { |
e23c0ba3 ZW |
1369 | cpp_fatal (pfile, "Output filename specified twice"); |
1370 | return argc; | |
1371 | } | |
ae79697b ZW |
1372 | CPP_OPTION (pfile, out_fname) = arg; |
1373 | if (!strcmp (CPP_OPTION (pfile, out_fname), "-")) | |
1374 | CPP_OPTION (pfile, out_fname) = ""; | |
e23c0ba3 ZW |
1375 | break; |
1376 | case OPT_v: | |
1377 | fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string); | |
1378 | #ifdef TARGET_VERSION | |
1379 | TARGET_VERSION; | |
1380 | #endif | |
1381 | fputc ('\n', stderr); | |
ae79697b | 1382 | CPP_OPTION (pfile, verbose) = 1; |
e23c0ba3 ZW |
1383 | break; |
1384 | case OPT_stdin_stdout: | |
1385 | /* JF handle '-' as file name meaning stdin or stdout */ | |
ae79697b ZW |
1386 | if (CPP_OPTION (pfile, in_fname) == NULL) |
1387 | CPP_OPTION (pfile, in_fname) = ""; | |
1388 | else if (CPP_OPTION (pfile, out_fname) == NULL) | |
1389 | CPP_OPTION (pfile, out_fname) = ""; | |
e23c0ba3 ZW |
1390 | break; |
1391 | case OPT_d: | |
1392 | /* Args to -d specify what parts of macros to dump. | |
1393 | Silently ignore unrecognised options; they may | |
1394 | be aimed at the compiler proper. */ | |
1395 | { | |
1396 | char c; | |
ae79697b | 1397 | |
e23c0ba3 ZW |
1398 | while ((c = *arg++) != '\0') |
1399 | switch (c) | |
1400 | { | |
1401 | case 'M': | |
ae79697b ZW |
1402 | CPP_OPTION (pfile, dump_macros) = dump_only; |
1403 | CPP_OPTION (pfile, no_output) = 1; | |
0b22d65c ZW |
1404 | break; |
1405 | case 'N': | |
ae79697b | 1406 | CPP_OPTION (pfile, dump_macros) = dump_names; |
0b22d65c ZW |
1407 | break; |
1408 | case 'D': | |
ae79697b | 1409 | CPP_OPTION (pfile, dump_macros) = dump_definitions; |
0b22d65c ZW |
1410 | break; |
1411 | case 'I': | |
ae79697b | 1412 | CPP_OPTION (pfile, dump_includes) = 1; |
0b22d65c ZW |
1413 | break; |
1414 | } | |
e23c0ba3 ZW |
1415 | } |
1416 | break; | |
1417 | /* The style of the choices here is a bit mixed. | |
1418 | The chosen scheme is a hybrid of keeping all options in one string | |
1419 | and specifying each option in a separate argument: | |
1420 | -M|-MM|-MD file|-MMD file [-MG]. An alternative is: | |
1421 | -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely: | |
1422 | -M[M][G][D file]. This is awkward to handle in specs, and is not | |
1423 | as extensible. */ | |
1424 | /* ??? -MG must be specified in addition to one of -M or -MM. | |
1425 | This can be relaxed in the future without breaking anything. | |
1426 | The converse isn't true. */ | |
ae79697b | 1427 | |
e23c0ba3 ZW |
1428 | /* -MG isn't valid with -MD or -MMD. This is checked for later. */ |
1429 | case OPT_MG: | |
ae79697b | 1430 | CPP_OPTION (pfile, print_deps_missing_files) = 1; |
e23c0ba3 ZW |
1431 | break; |
1432 | case OPT_M: | |
1433 | case OPT_MD: | |
1434 | case OPT_MM: | |
1435 | case OPT_MMD: | |
1436 | if (opt_code == OPT_M || opt_code == OPT_MD) | |
ae79697b | 1437 | CPP_OPTION (pfile, print_deps) = 2; |
e23c0ba3 | 1438 | else |
ae79697b | 1439 | CPP_OPTION (pfile, print_deps) = 1; |
e23c0ba3 ZW |
1440 | |
1441 | /* For -MD and -MMD options, write deps on file named by next arg */ | |
1442 | /* For -M and -MM, write deps on standard output | |
1443 | and suppress the usual output. */ | |
1444 | if (opt_code == OPT_MD || opt_code == OPT_MMD) | |
ae79697b | 1445 | CPP_OPTION (pfile, deps_file) = arg; |
e23c0ba3 | 1446 | else |
ae79697b | 1447 | CPP_OPTION (pfile, no_output) = 1; |
e23c0ba3 ZW |
1448 | break; |
1449 | case OPT_A: | |
e1e97c4f | 1450 | if (arg[0] == '-') |
0b22d65c | 1451 | { |
e1e97c4f NB |
1452 | /* -A with an argument beginning with '-' acts as |
1453 | #unassert on whatever immediately follows the '-'. | |
1454 | If "-" is the whole argument, we eliminate all | |
1455 | predefined macros and assertions, including those | |
1456 | that were specified earlier on the command line. | |
1457 | That way we can get rid of any that were passed | |
1458 | automatically in from GCC. */ | |
1459 | ||
1460 | if (arg[1] == '\0') | |
0b22d65c | 1461 | { |
e1e97c4f NB |
1462 | struct pending_option *o1, *o2; |
1463 | ||
e33f6253 | 1464 | o1 = pend->directive_head; |
e1e97c4f NB |
1465 | while (o1) |
1466 | { | |
1467 | o2 = o1->next; | |
1468 | free (o1); | |
1469 | o1 = o2; | |
1470 | } | |
e33f6253 NB |
1471 | pend->directive_head = NULL; |
1472 | pend->directive_tail = NULL; | |
0b22d65c | 1473 | } |
e1e97c4f | 1474 | else |
e33f6253 | 1475 | new_pending_directive (pend, arg + 1, cpp_unassert); |
6de1e2a9 | 1476 | } |
e1e97c4f | 1477 | else |
e33f6253 | 1478 | new_pending_directive (pend, arg, cpp_assert); |
e23c0ba3 ZW |
1479 | break; |
1480 | case OPT_U: | |
e33f6253 | 1481 | new_pending_directive (pend, arg, cpp_undef); |
e23c0ba3 ZW |
1482 | break; |
1483 | case OPT_I: /* Add directory to path for includes. */ | |
1484 | if (!strcmp (arg, "-")) | |
1485 | { | |
1486 | /* -I- means: | |
1487 | Use the preceding -I directories for #include "..." | |
1488 | but not #include <...>. | |
1489 | Don't search the directory of the present file | |
1490 | for #include "...". (Note that -I. -I- is not the same as | |
1491 | the default setup; -I. uses the compiler's working dir.) */ | |
ae79697b | 1492 | if (! CPP_OPTION (pfile, ignore_srcdir)) |
e23c0ba3 | 1493 | { |
ae79697b ZW |
1494 | pend->quote_head = pend->brack_head; |
1495 | pend->quote_tail = pend->brack_tail; | |
1496 | pend->brack_head = 0; | |
1497 | pend->brack_tail = 0; | |
1498 | CPP_OPTION (pfile, ignore_srcdir) = 1; | |
e23c0ba3 ZW |
1499 | } |
1500 | else | |
1501 | { | |
1502 | cpp_fatal (pfile, "-I- specified twice"); | |
1503 | return argc; | |
1504 | } | |
1505 | } | |
1506 | else | |
e33f6253 | 1507 | append_include_chain (pfile, xstrdup (arg), BRACKET, 0); |
e23c0ba3 ZW |
1508 | break; |
1509 | case OPT_isystem: | |
1510 | /* Add directory to beginning of system include path, as a system | |
1511 | include directory. */ | |
e33f6253 | 1512 | append_include_chain (pfile, xstrdup (arg), SYSTEM, 0); |
e23c0ba3 ZW |
1513 | break; |
1514 | case OPT_include: | |
1515 | { | |
1516 | struct pending_option *o = (struct pending_option *) | |
1517 | xmalloc (sizeof (struct pending_option)); | |
1518 | o->arg = arg; | |
1519 | ||
1520 | /* This list has to be built in reverse order so that | |
1521 | when cpp_start_read pushes all the -include files onto | |
1522 | the buffer stack, they will be scanned in forward order. */ | |
e33f6253 NB |
1523 | o->next = pend->include_head; |
1524 | pend->include_head = o; | |
e23c0ba3 ZW |
1525 | } |
1526 | break; | |
1527 | case OPT_imacros: | |
1528 | { | |
1529 | struct pending_option *o = (struct pending_option *) | |
1530 | xmalloc (sizeof (struct pending_option)); | |
1531 | o->arg = arg; | |
1532 | o->next = NULL; | |
ae79697b | 1533 | |
e33f6253 | 1534 | APPEND (pend, imacros, o); |
e23c0ba3 ZW |
1535 | } |
1536 | break; | |
1537 | case OPT_iwithprefix: | |
1538 | /* Add directory to end of path for includes, | |
1539 | with the default prefix at the front of its name. */ | |
1540 | /* fall through */ | |
1541 | case OPT_iwithprefixbefore: | |
1542 | /* Add directory to main path for includes, | |
1543 | with the default prefix at the front of its name. */ | |
1544 | { | |
1545 | char *fname; | |
1546 | int len; | |
ae79697b | 1547 | |
e23c0ba3 | 1548 | len = strlen (arg); |
ae79697b ZW |
1549 | |
1550 | if (CPP_OPTION (pfile, include_prefix) != 0) | |
e23c0ba3 | 1551 | { |
ae79697b ZW |
1552 | size_t ipl = CPP_OPTION (pfile, include_prefix_len); |
1553 | fname = xmalloc (ipl + len + 1); | |
1554 | memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl); | |
1555 | memcpy (fname + ipl, arg, len + 1); | |
e23c0ba3 | 1556 | } |
60893f43 | 1557 | else if (cpp_GCC_INCLUDE_DIR_len) |
e23c0ba3 | 1558 | { |
60893f43 ZW |
1559 | fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1); |
1560 | memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len); | |
1561 | memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1); | |
e23c0ba3 | 1562 | } |
60893f43 ZW |
1563 | else |
1564 | fname = xstrdup (arg); | |
ae79697b | 1565 | |
e33f6253 | 1566 | append_include_chain (pfile, fname, |
e23c0ba3 ZW |
1567 | opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0); |
1568 | } | |
1569 | break; | |
1570 | case OPT_idirafter: | |
1571 | /* Add directory to end of path for includes. */ | |
e33f6253 | 1572 | append_include_chain (pfile, xstrdup (arg), AFTER, 0); |
e23c0ba3 ZW |
1573 | break; |
1574 | case OPT_W: | |
1575 | /* Silently ignore unrecognised options */ | |
1576 | if (!strcmp (argv[i], "-Wall")) | |
0b22d65c | 1577 | { |
ae79697b ZW |
1578 | CPP_OPTION (pfile, warn_trigraphs) = 1; |
1579 | CPP_OPTION (pfile, warn_comments) = 1; | |
0b22d65c | 1580 | } |
e23c0ba3 | 1581 | else if (!strcmp (argv[i], "-Wtraditional")) |
07aa0b04 | 1582 | CPP_OPTION (pfile, warn_traditional) = 1; |
e23c0ba3 | 1583 | else if (!strcmp (argv[i], "-Wtrigraphs")) |
ae79697b | 1584 | CPP_OPTION (pfile, warn_trigraphs) = 1; |
e23c0ba3 | 1585 | else if (!strcmp (argv[i], "-Wcomment")) |
ae79697b | 1586 | CPP_OPTION (pfile, warn_comments) = 1; |
e23c0ba3 | 1587 | else if (!strcmp (argv[i], "-Wcomments")) |
ae79697b | 1588 | CPP_OPTION (pfile, warn_comments) = 1; |
e23c0ba3 | 1589 | else if (!strcmp (argv[i], "-Wundef")) |
ae79697b | 1590 | CPP_OPTION (pfile, warn_undef) = 1; |
e23c0ba3 | 1591 | else if (!strcmp (argv[i], "-Wimport")) |
ae79697b | 1592 | CPP_OPTION (pfile, warn_import) = 1; |
041c3194 ZW |
1593 | else if (!strcmp (argv[i], "-Wpaste")) |
1594 | CPP_OPTION (pfile, warn_paste) = 1; | |
e23c0ba3 | 1595 | else if (!strcmp (argv[i], "-Werror")) |
ae79697b | 1596 | CPP_OPTION (pfile, warnings_are_errors) = 1; |
e23c0ba3 | 1597 | else if (!strcmp (argv[i], "-Wno-traditional")) |
07aa0b04 | 1598 | CPP_OPTION (pfile, warn_traditional) = 0; |
e23c0ba3 | 1599 | else if (!strcmp (argv[i], "-Wno-trigraphs")) |
ae79697b | 1600 | CPP_OPTION (pfile, warn_trigraphs) = 0; |
e23c0ba3 | 1601 | else if (!strcmp (argv[i], "-Wno-comment")) |
ae79697b | 1602 | CPP_OPTION (pfile, warn_comments) = 0; |
e23c0ba3 | 1603 | else if (!strcmp (argv[i], "-Wno-comments")) |
ae79697b | 1604 | CPP_OPTION (pfile, warn_comments) = 0; |
e23c0ba3 | 1605 | else if (!strcmp (argv[i], "-Wno-undef")) |
ae79697b | 1606 | CPP_OPTION (pfile, warn_undef) = 0; |
e23c0ba3 | 1607 | else if (!strcmp (argv[i], "-Wno-import")) |
ae79697b | 1608 | CPP_OPTION (pfile, warn_import) = 0; |
041c3194 ZW |
1609 | else if (!strcmp (argv[i], "-Wno-paste")) |
1610 | CPP_OPTION (pfile, warn_paste) = 0; | |
e23c0ba3 | 1611 | else if (!strcmp (argv[i], "-Wno-error")) |
ae79697b | 1612 | CPP_OPTION (pfile, warnings_are_errors) = 0; |
e23c0ba3 ZW |
1613 | break; |
1614 | } | |
1615 | } | |
6de1e2a9 | 1616 | return i + 1; |
e23c0ba3 | 1617 | } |
0b22d65c | 1618 | |
e23c0ba3 ZW |
1619 | #ifdef HOST_EBCDIC |
1620 | static int | |
1621 | opt_comp (const void *p1, const void *p2) | |
1622 | { | |
1623 | return strcmp (((struct cl_option *)p1)->opt_text, | |
1624 | ((struct cl_option *)p2)->opt_text); | |
6de1e2a9 | 1625 | } |
e23c0ba3 | 1626 | #endif |
6de1e2a9 ZW |
1627 | |
1628 | /* Handle command-line options in (argc, argv). | |
1629 | Can be called multiple times, to handle multiple sets of options. | |
1630 | Returns if an unrecognized option is seen. | |
1631 | Returns number of strings consumed. */ | |
6de1e2a9 ZW |
1632 | int |
1633 | cpp_handle_options (pfile, argc, argv) | |
1634 | cpp_reader *pfile; | |
1635 | int argc; | |
1636 | char **argv; | |
1637 | { | |
1638 | int i; | |
1639 | int strings_processed; | |
e23c0ba3 | 1640 | |
6de1e2a9 ZW |
1641 | for (i = 0; i < argc; i += strings_processed) |
1642 | { | |
2c0accc9 | 1643 | strings_processed = cpp_handle_option (pfile, argc - i, argv + i); |
6de1e2a9 ZW |
1644 | if (strings_processed == 0) |
1645 | break; | |
1646 | } | |
1647 | return i; | |
1648 | } | |
1649 | ||
1650 | static void | |
1651 | print_help () | |
1652 | { | |
c1212d2f | 1653 | fprintf (stderr, _("Usage: %s [switches] input output\n"), progname); |
aaaf7848 DT |
1654 | /* To keep the lines from getting too long for some compilers, limit |
1655 | to about 500 characters (6 lines) per chunk. */ | |
6de1e2a9 ZW |
1656 | fputs (_("\ |
1657 | Switches:\n\ | |
1658 | -include <file> Include the contents of <file> before other files\n\ | |
1659 | -imacros <file> Accept definition of macros in <file>\n\ | |
1660 | -iprefix <path> Specify <path> as a prefix for next two options\n\ | |
1661 | -iwithprefix <dir> Add <dir> to the end of the system include path\n\ | |
1662 | -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\ | |
1663 | -isystem <dir> Add <dir> to the start of the system include path\n\ | |
aaaf7848 DT |
1664 | "), stdout); |
1665 | fputs (_("\ | |
6de1e2a9 ZW |
1666 | -idirafter <dir> Add <dir> to the end of the system include path\n\ |
1667 | -I <dir> Add <dir> to the end of the main include path\n\ | |
e23c0ba3 | 1668 | -I- Fine-grained include path control; see info docs\n\ |
6de1e2a9 ZW |
1669 | -nostdinc Do not search system include directories\n\ |
1670 | (dirs specified with -isystem will still be used)\n\ | |
1671 | -nostdinc++ Do not search system include directories for C++\n\ | |
1672 | -o <file> Put output into <file>\n\ | |
aaaf7848 DT |
1673 | "), stdout); |
1674 | fputs (_("\ | |
041c3194 | 1675 | -pedantic Issue all warnings demanded by strict ISO C\n\ |
e23c0ba3 | 1676 | -pedantic-errors Issue -pedantic warnings as errors instead\n\ |
041c3194 | 1677 | -trigraphs Support ISO C trigraphs\n\ |
6de1e2a9 ZW |
1678 | -lang-c Assume that the input sources are in C\n\ |
1679 | -lang-c89 Assume that the input sources are in C89\n\ | |
aaaf7848 DT |
1680 | "), stdout); |
1681 | fputs (_("\ | |
f9a0e96c | 1682 | -lang-c++ Assume that the input sources are in C++\n\ |
6de1e2a9 ZW |
1683 | -lang-objc Assume that the input sources are in ObjectiveC\n\ |
1684 | -lang-objc++ Assume that the input sources are in ObjectiveC++\n\ | |
1685 | -lang-asm Assume that the input sources are in assembler\n\ | |
aaaf7848 DT |
1686 | "), stdout); |
1687 | fputs (_("\ | |
6de1e2a9 | 1688 | -std=<std name> Specify the conformance standard; one of:\n\ |
916269ab UD |
1689 | gnu89, gnu99, c89, c99, iso9899:1990,\n\ |
1690 | iso9899:199409, iso9899:1999\n\ | |
6de1e2a9 ZW |
1691 | -+ Allow parsing of C++ style features\n\ |
1692 | -w Inhibit warning messages\n\ | |
1693 | -Wtrigraphs Warn if trigraphs are encountered\n\ | |
1694 | -Wno-trigraphs Do not warn about trigraphs\n\ | |
1695 | -Wcomment{s} Warn if one comment starts inside another\n\ | |
aaaf7848 DT |
1696 | "), stdout); |
1697 | fputs (_("\ | |
6de1e2a9 | 1698 | -Wno-comment{s} Do not warn about comments\n\ |
f9a0e96c ZW |
1699 | -Wtraditional Warn about features not present in traditional C\n\ |
1700 | -Wno-traditional Do not warn about traditional C\n\ | |
6de1e2a9 ZW |
1701 | -Wundef Warn if an undefined macro is used by #if\n\ |
1702 | -Wno-undef Do not warn about testing undefined macros\n\ | |
1703 | -Wimport Warn about the use of the #import directive\n\ | |
aaaf7848 DT |
1704 | "), stdout); |
1705 | fputs (_("\ | |
6de1e2a9 ZW |
1706 | -Wno-import Do not warn about the use of #import\n\ |
1707 | -Werror Treat all warnings as errors\n\ | |
1708 | -Wno-error Do not treat warnings as errors\n\ | |
1709 | -Wall Enable all preprocessor warnings\n\ | |
1710 | -M Generate make dependencies\n\ | |
1711 | -MM As -M, but ignore system header files\n\ | |
aaaf7848 DT |
1712 | "), stdout); |
1713 | fputs (_("\ | |
6de1e2a9 ZW |
1714 | -MD As -M, but put output in a .d file\n\ |
1715 | -MMD As -MD, but ignore system header files\n\ | |
1716 | -MG Treat missing header file as generated files\n\ | |
e23c0ba3 | 1717 | -g3 Include #define and #undef directives in the output\n\ |
6de1e2a9 ZW |
1718 | -D<macro> Define a <macro> with string '1' as its value\n\ |
1719 | -D<macro>=<val> Define a <macro> with <val> as its value\n\ | |
aaaf7848 DT |
1720 | "), stdout); |
1721 | fputs (_("\ | |
6de1e2a9 | 1722 | -A<question> (<answer>) Assert the <answer> to <question>\n\ |
e1e97c4f | 1723 | -A-<question> (<answer>) Disable the <answer> to <question>\n\ |
6de1e2a9 | 1724 | -U<macro> Undefine <macro> \n\ |
6de1e2a9 ZW |
1725 | -v Display the version number\n\ |
1726 | -H Print the name of header files as they are used\n\ | |
1727 | -C Do not discard comments\n\ | |
aaaf7848 DT |
1728 | "), stdout); |
1729 | fputs (_("\ | |
6de1e2a9 ZW |
1730 | -dM Display a list of macro definitions active at end\n\ |
1731 | -dD Preserve macro definitions in output\n\ | |
1732 | -dN As -dD except that only the names are preserved\n\ | |
1733 | -dI Include #include directives in the output\n\ | |
6ab3e7dd | 1734 | -ftabstop=<number> Distance between tab stops for column reporting\n\ |
6de1e2a9 ZW |
1735 | -P Do not generate #line directives\n\ |
1736 | -$ Do not allow '$' in identifiers\n\ | |
aaaf7848 DT |
1737 | "), stdout); |
1738 | fputs (_("\ | |
6de1e2a9 | 1739 | -remap Remap file names when including files.\n\ |
e23c0ba3 | 1740 | --version Display version information\n\ |
6de1e2a9 ZW |
1741 | -h or --help Display this information\n\ |
1742 | "), stdout); | |
1743 | } |