]>
Commit | Line | Data |
---|---|---|
5538ada6 | 1 | /* CPP Library. |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
5793b276 | 3 | 1999, 2000, 2001, 2002, 2003 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" | |
4977bab6 ZW |
24 | #include "coretypes.h" |
25 | #include "tm.h" | |
6de1e2a9 ZW |
26 | #include "cpplib.h" |
27 | #include "cpphash.h" | |
49e6c08e | 28 | #include "mkdeps.h" |
88ae23e7 | 29 | |
674c3b40 | 30 | static void init_library PARAMS ((void)); |
17645b15 | 31 | static void mark_named_operators PARAMS ((cpp_reader *)); |
f5e99456 | 32 | static void read_original_filename PARAMS ((cpp_reader *)); |
f4ff5a69 | 33 | static void post_options PARAMS ((cpp_reader *)); |
6de1e2a9 | 34 | |
61d0346d NB |
35 | /* If we have designated initializers (GCC >2.7) these tables can be |
36 | initialized, constant data. Otherwise, they have to be filled in at | |
12cf91fe | 37 | runtime. */ |
61d0346d | 38 | #if HAVE_DESIGNATED_INITIALIZERS |
a9ae4483 | 39 | |
4a58aab6 | 40 | #define init_trigraph_map() /* Nothing. */ |
61d0346d | 41 | #define TRIGRAPH_MAP \ |
562a5c27 | 42 | __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { |
61d0346d | 43 | |
a9ae4483 | 44 | #define END }; |
455d2586 | 45 | #define s(p, v) [p] = v, |
61d0346d | 46 | |
a9ae4483 | 47 | #else |
61d0346d | 48 | |
562a5c27 | 49 | #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \ |
61d0346d NB |
50 | static void init_trigraph_map PARAMS ((void)) { \ |
51 | unsigned char *x = _cpp_trigraph_map; | |
52 | ||
ae79697b | 53 | #define END } |
455d2586 | 54 | #define s(p, v) x[p] = v; |
61d0346d | 55 | |
a9ae4483 | 56 | #endif |
6de1e2a9 | 57 | |
61d0346d NB |
58 | TRIGRAPH_MAP |
59 | s('=', '#') s(')', ']') s('!', '|') | |
60 | s('(', '[') s('\'', '^') s('>', '}') | |
61 | s('/', '\\') s('<', '{') s('-', '~') | |
62 | END | |
63 | ||
a9ae4483 | 64 | #undef s |
455d2586 | 65 | #undef END |
61d0346d | 66 | #undef TRIGRAPH_MAP |
6de1e2a9 | 67 | |
5d8ebbd8 NB |
68 | /* A set of booleans indicating what CPP features each source language |
69 | requires. */ | |
a01eb545 ZW |
70 | struct lang_flags |
71 | { | |
72 | char c99; | |
a01eb545 ZW |
73 | char cplusplus; |
74 | char extended_numbers; | |
58551c23 | 75 | char std; |
a01eb545 ZW |
76 | char dollars_in_ident; |
77 | char cplusplus_comments; | |
78 | char digraphs; | |
79 | }; | |
80 | ||
81 | /* ??? Enable $ in identifiers in assembly? */ | |
82 | static const struct lang_flags lang_defaults[] = | |
58551c23 | 83 | { /* c99 c++ xnum std dollar c++comm digr */ |
bdcae02b NB |
84 | /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 }, |
85 | /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 }, | |
86 | /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 }, | |
87 | /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 }, | |
88 | /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 }, | |
89 | /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 }, | |
90 | /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 }, | |
91 | /* ASM */ { 0, 0, 1, 0, 0, 1, 0 } | |
a01eb545 ZW |
92 | }; |
93 | ||
5d8ebbd8 | 94 | /* Sets internal flags correctly for a given language. */ |
f749a36b NB |
95 | void |
96 | cpp_set_lang (pfile, lang) | |
dd07b884 NB |
97 | cpp_reader *pfile; |
98 | enum c_lang lang; | |
99 | { | |
a01eb545 | 100 | const struct lang_flags *l = &lang_defaults[(int) lang]; |
df383483 | 101 | |
bdb05a7b | 102 | CPP_OPTION (pfile, lang) = lang; |
dd07b884 | 103 | |
a01eb545 | 104 | CPP_OPTION (pfile, c99) = l->c99; |
a01eb545 ZW |
105 | CPP_OPTION (pfile, cplusplus) = l->cplusplus; |
106 | CPP_OPTION (pfile, extended_numbers) = l->extended_numbers; | |
58551c23 NB |
107 | CPP_OPTION (pfile, std) = l->std; |
108 | CPP_OPTION (pfile, trigraphs) = l->std; | |
a01eb545 ZW |
109 | CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident; |
110 | CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments; | |
111 | CPP_OPTION (pfile, digraphs) = l->digraphs; | |
dd07b884 NB |
112 | } |
113 | ||
c1bad961 | 114 | /* Initialize library global state. */ |
cf44ea52 | 115 | static void |
674c3b40 | 116 | init_library () |
cf44ea52 | 117 | { |
7ca3d2b1 NB |
118 | static int initialized = 0; |
119 | ||
120 | if (! initialized) | |
121 | { | |
122 | initialized = 1; | |
123 | ||
7ca3d2b1 NB |
124 | /* Set up the trigraph map. This doesn't need to do anything if |
125 | we were compiled with a compiler that supports C99 designated | |
126 | initializers. */ | |
127 | init_trigraph_map (); | |
9d10c9a9 NB |
128 | |
129 | _cpp_init_mbchar (); | |
7ca3d2b1 | 130 | } |
cf44ea52 NB |
131 | } |
132 | ||
ec5c56db | 133 | /* Initialize a cpp_reader structure. */ |
cf44ea52 | 134 | cpp_reader * |
f5e99456 | 135 | cpp_create_reader (lang) |
dd07b884 | 136 | enum c_lang lang; |
6de1e2a9 | 137 | { |
7ca3d2b1 | 138 | cpp_reader *pfile; |
93c80368 | 139 | |
4912a07c | 140 | /* Initialize this instance of the library if it hasn't been already. */ |
674c3b40 | 141 | init_library (); |
7ca3d2b1 NB |
142 | |
143 | pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader)); | |
93c80368 | 144 | |
f749a36b | 145 | cpp_set_lang (pfile, lang); |
ae79697b | 146 | CPP_OPTION (pfile, warn_import) = 1; |
a5a49440 | 147 | CPP_OPTION (pfile, warn_multichar) = 1; |
ae79697b | 148 | CPP_OPTION (pfile, discard_comments) = 1; |
477cdac7 | 149 | CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1; |
ae79697b | 150 | CPP_OPTION (pfile, show_column) = 1; |
6ab3e7dd | 151 | CPP_OPTION (pfile, tabstop) = 8; |
be768055 | 152 | CPP_OPTION (pfile, operator_names) = 1; |
909de5da | 153 | CPP_OPTION (pfile, warn_endif_labels) = 1; |
9fbd3e41 | 154 | CPP_OPTION (pfile, warn_deprecated) = 1; |
f4ff5a69 | 155 | CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99); |
ae79697b | 156 | |
2443d4e1 NB |
157 | /* Default CPP arithmetic to something sensible for the host for the |
158 | benefit of dumb users like fix-header. */ | |
c9220e3a | 159 | CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long); |
2443d4e1 NB |
160 | CPP_OPTION (pfile, char_precision) = CHAR_BIT; |
161 | CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int); | |
162 | CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int); | |
2a1dc0d8 | 163 | CPP_OPTION (pfile, unsigned_char) = 0; |
44a147ad | 164 | CPP_OPTION (pfile, unsigned_wchar) = 1; |
4268e8bb | 165 | |
4912a07c | 166 | /* Initialize the line map. Start at logical line 1, so we can use |
50410426 | 167 | a line number of zero for special states. */ |
d82fc108 | 168 | init_line_maps (&pfile->line_maps); |
1a76916c | 169 | pfile->line = 1; |
d82fc108 | 170 | |
4a58aab6 | 171 | /* Initialize lexer state. */ |
93c80368 NB |
172 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
173 | ||
4ed5bcfb | 174 | /* Set up static tokens. */ |
4ed5bcfb NB |
175 | pfile->avoid_paste.type = CPP_PADDING; |
176 | pfile->avoid_paste.val.source = NULL; | |
177 | pfile->eof.type = CPP_EOF; | |
178 | pfile->eof.flags = 0; | |
93c80368 | 179 | |
5fddcffc NB |
180 | /* Create a token buffer for the lexer. */ |
181 | _cpp_init_tokenrun (&pfile->base_run, 250); | |
182 | pfile->cur_run = &pfile->base_run; | |
183 | pfile->cur_token = pfile->base_run.base; | |
5fddcffc | 184 | |
4912a07c | 185 | /* Initialize the base context. */ |
93c80368 NB |
186 | pfile->context = &pfile->base_context; |
187 | pfile->base_context.macro = 0; | |
188 | pfile->base_context.prev = pfile->base_context.next = 0; | |
189 | ||
8c3b2693 NB |
190 | /* Aligned and unaligned storage. */ |
191 | pfile->a_buff = _cpp_get_buff (pfile, 0); | |
ece54d54 | 192 | pfile->u_buff = _cpp_get_buff (pfile, 0); |
93c80368 | 193 | |
87ed109f NB |
194 | /* The expression parser stack. */ |
195 | _cpp_expand_op_stack (pfile); | |
196 | ||
4912a07c | 197 | /* Initialize the buffer obstack. */ |
2a967f3d NB |
198 | gcc_obstack_init (&pfile->buffer_ob); |
199 | ||
c71f835b | 200 | _cpp_init_includes (pfile); |
cf44ea52 NB |
201 | |
202 | return pfile; | |
f2d5f0cc ZW |
203 | } |
204 | ||
400023a3 | 205 | /* Free resources used by PFILE. Accessing PFILE after this function |
8d9afc4e | 206 | returns leads to undefined behavior. Returns the error count. */ |
76c3e73e | 207 | void |
400023a3 | 208 | cpp_destroy (pfile) |
6de1e2a9 ZW |
209 | cpp_reader *pfile; |
210 | { | |
93c80368 | 211 | cpp_context *context, *contextn; |
50410426 | 212 | tokenrun *run, *runn; |
709e9e50 | 213 | |
87ed109f | 214 | free (pfile->op_stack); |
af0d16cd | 215 | |
38b24ee2 | 216 | while (CPP_BUFFER (pfile) != NULL) |
ef6e958a | 217 | _cpp_pop_buffer (pfile); |
6de1e2a9 | 218 | |
1a76916c NB |
219 | if (pfile->out.base) |
220 | free (pfile->out.base); | |
004cb263 | 221 | |
93c80368 | 222 | if (pfile->macro_buffer) |
4b49c365 AO |
223 | { |
224 | free ((PTR) pfile->macro_buffer); | |
225 | pfile->macro_buffer = NULL; | |
226 | pfile->macro_buffer_len = 0; | |
227 | } | |
93c80368 | 228 | |
f4ff5a69 NB |
229 | if (pfile->deps) |
230 | deps_free (pfile->deps); | |
2a967f3d | 231 | obstack_free (&pfile->buffer_ob, 0); |
49e6c08e | 232 | |
2a967f3d | 233 | _cpp_destroy_hashtable (pfile); |
bfb9dc7f | 234 | _cpp_cleanup_includes (pfile); |
709e9e50 | 235 | |
8c3b2693 | 236 | _cpp_free_buff (pfile->a_buff); |
ece54d54 | 237 | _cpp_free_buff (pfile->u_buff); |
b8af0ca5 | 238 | _cpp_free_buff (pfile->free_buffs); |
93c80368 | 239 | |
50410426 NB |
240 | for (run = &pfile->base_run; run; run = runn) |
241 | { | |
242 | runn = run->next; | |
243 | free (run->base); | |
244 | if (run != &pfile->base_run) | |
245 | free (run); | |
246 | } | |
247 | ||
93c80368 NB |
248 | for (context = pfile->base_context.next; context; context = contextn) |
249 | { | |
250 | contextn = context->next; | |
251 | free (context); | |
252 | } | |
400023a3 | 253 | |
d82fc108 | 254 | free_line_maps (&pfile->line_maps); |
400023a3 | 255 | free (pfile); |
6de1e2a9 ZW |
256 | } |
257 | ||
93c80368 | 258 | /* This structure defines one built-in identifier. A node will be |
f24a153a ZW |
259 | entered in the hash table under the name NAME, with value VALUE. |
260 | ||
261 | There are two tables of these. builtin_array holds all the | |
262 | "builtin" macros: these are handled by builtin_macro() in | |
263 | cppmacro.c. Builtin is somewhat of a misnomer -- the property of | |
264 | interest is that these macros require special code to compute their | |
265 | expansions. The value is a "builtin_type" enumerator. | |
266 | ||
267 | operator_array holds the C++ named operators. These are keywords | |
268 | which act as aliases for punctuators. In C++, they cannot be | |
269 | altered through #define, and #if recognizes them as operators. In | |
270 | C, these are not entered into the hash table at all (but see | |
271 | <iso646.h>). The value is a token-type enumerator. */ | |
a9ae4483 ZW |
272 | struct builtin |
273 | { | |
562a5c27 | 274 | const uchar *name; |
93c80368 | 275 | unsigned short len; |
f24a153a | 276 | unsigned short value; |
a9ae4483 | 277 | }; |
93c80368 | 278 | |
f24a153a | 279 | #define B(n, t) { DSC(n), t } |
a9ae4483 | 280 | static const struct builtin builtin_array[] = |
6de1e2a9 | 281 | { |
93c80368 NB |
282 | B("__TIME__", BT_TIME), |
283 | B("__DATE__", BT_DATE), | |
284 | B("__FILE__", BT_FILE), | |
285 | B("__BASE_FILE__", BT_BASE_FILE), | |
286 | B("__LINE__", BT_SPECLINE), | |
287 | B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL), | |
278c4662 NB |
288 | /* Keep builtins not used for -traditional-cpp at the end, and |
289 | update init_builtins() if any more are added. */ | |
644eddaa | 290 | B("_Pragma", BT_PRAGMA), |
618cdda7 | 291 | B("__STDC__", BT_STDC), |
f24a153a | 292 | }; |
92936ecf | 293 | |
f24a153a ZW |
294 | static const struct builtin operator_array[] = |
295 | { | |
296 | B("and", CPP_AND_AND), | |
297 | B("and_eq", CPP_AND_EQ), | |
298 | B("bitand", CPP_AND), | |
299 | B("bitor", CPP_OR), | |
300 | B("compl", CPP_COMPL), | |
301 | B("not", CPP_NOT), | |
302 | B("not_eq", CPP_NOT_EQ), | |
303 | B("or", CPP_OR_OR), | |
304 | B("or_eq", CPP_OR_EQ), | |
305 | B("xor", CPP_XOR), | |
306 | B("xor_eq", CPP_XOR_EQ) | |
a9ae4483 | 307 | }; |
12cf91fe | 308 | #undef B |
a9ae4483 | 309 | |
17645b15 NB |
310 | /* Mark the C++ named operators in the hash table. */ |
311 | static void | |
312 | mark_named_operators (pfile) | |
313 | cpp_reader *pfile; | |
314 | { | |
315 | const struct builtin *b; | |
316 | ||
317 | for (b = operator_array; | |
318 | b < (operator_array + ARRAY_SIZE (operator_array)); | |
319 | b++) | |
320 | { | |
321 | cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); | |
322 | hp->flags |= NODE_OPERATOR; | |
4977bab6 ZW |
323 | hp->is_directive = 0; |
324 | hp->directive_index = b->value; | |
17645b15 NB |
325 | } |
326 | } | |
327 | ||
c1bad961 NB |
328 | /* Read the builtins table above and enter them, and language-specific |
329 | macros, into the hash table. */ | |
330 | void | |
331 | cpp_init_builtins (pfile) | |
a9ae4483 ZW |
332 | cpp_reader *pfile; |
333 | { | |
a9ae4483 | 334 | const struct builtin *b; |
278c4662 | 335 | size_t n = ARRAY_SIZE (builtin_array); |
771c4df3 | 336 | |
278c4662 NB |
337 | if (CPP_OPTION (pfile, traditional)) |
338 | n -= 2; | |
339 | ||
340 | for(b = builtin_array; b < builtin_array + n; b++) | |
6de1e2a9 | 341 | { |
f24a153a ZW |
342 | cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); |
343 | hp->type = NT_MACRO; | |
344 | hp->flags |= NODE_BUILTIN | NODE_WARN; | |
345 | hp->value.builtin = b->value; | |
6de1e2a9 | 346 | } |
c740cee2 NB |
347 | |
348 | if (CPP_OPTION (pfile, cplusplus)) | |
3d90d290 | 349 | _cpp_define_builtin (pfile, "__cplusplus 1"); |
2a1dc0d8 ZW |
350 | else if (CPP_OPTION (pfile, lang) == CLK_ASM) |
351 | _cpp_define_builtin (pfile, "__ASSEMBLER__ 1"); | |
0f7866e7 | 352 | else if (CPP_OPTION (pfile, lang) == CLK_STDC94) |
c740cee2 NB |
353 | _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L"); |
354 | else if (CPP_OPTION (pfile, c99)) | |
355 | _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L"); | |
356 | ||
0f7866e7 ZL |
357 | if (CPP_OPTION (pfile, objc)) |
358 | _cpp_define_builtin (pfile, "__OBJC__ 1"); | |
4a58aab6 NB |
359 | } |
360 | ||
2443d4e1 NB |
361 | /* Sanity-checks are dependent on command-line options, so it is |
362 | called as a subroutine of cpp_read_main_file (). */ | |
363 | #if ENABLE_CHECKING | |
364 | static void sanity_checks PARAMS ((cpp_reader *)); | |
365 | static void sanity_checks (pfile) | |
366 | cpp_reader *pfile; | |
367 | { | |
368 | cppchar_t test = 0; | |
c9220e3a | 369 | size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part); |
2443d4e1 NB |
370 | |
371 | /* Sanity checks for assumptions about CPP arithmetic and target | |
372 | type precisions made by cpplib. */ | |
373 | test--; | |
374 | if (test < 1) | |
bdee42b1 | 375 | cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type"); |
2443d4e1 | 376 | |
c9220e3a | 377 | if (CPP_OPTION (pfile, precision) > max_precision) |
bdee42b1 | 378 | cpp_error (pfile, DL_ICE, |
68bd6dd6 | 379 | "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits", |
c9220e3a NB |
380 | (unsigned long) max_precision, |
381 | (unsigned long) CPP_OPTION (pfile, precision)); | |
2443d4e1 NB |
382 | |
383 | if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision)) | |
bdee42b1 | 384 | cpp_error (pfile, DL_ICE, |
2443d4e1 NB |
385 | "CPP arithmetic must be at least as precise as a target int"); |
386 | ||
387 | if (CPP_OPTION (pfile, char_precision) < 8) | |
bdee42b1 | 388 | cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide"); |
2443d4e1 NB |
389 | |
390 | if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision)) | |
bdee42b1 | 391 | cpp_error (pfile, DL_ICE, |
2443d4e1 NB |
392 | "target wchar_t is narrower than target char"); |
393 | ||
394 | if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision)) | |
bdee42b1 | 395 | cpp_error (pfile, DL_ICE, |
2443d4e1 NB |
396 | "target int is narrower than target char"); |
397 | ||
c9220e3a NB |
398 | /* This is assumed in eval_token() and could be fixed if necessary. */ |
399 | if (sizeof (cppchar_t) > sizeof (cpp_num_part)) | |
400 | cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character"); | |
401 | ||
2443d4e1 | 402 | if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T) |
bdee42b1 | 403 | cpp_error (pfile, DL_ICE, |
68bd6dd6 | 404 | "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits", |
c9220e3a NB |
405 | (unsigned long) BITS_PER_CPPCHAR_T, |
406 | (unsigned long) CPP_OPTION (pfile, wchar_precision)); | |
2443d4e1 NB |
407 | } |
408 | #else | |
409 | # define sanity_checks(PFILE) | |
410 | #endif | |
411 | ||
76c3e73e NB |
412 | /* Add a dependency target. Can be called any number of times before |
413 | cpp_read_main_file(). If no targets have been added before | |
414 | cpp_read_main_file(), then the default target is used. */ | |
415 | void | |
416 | cpp_add_dependency_target (pfile, target, quote) | |
417 | cpp_reader *pfile; | |
418 | const char *target; | |
419 | int quote; | |
420 | { | |
421 | if (!pfile->deps) | |
422 | pfile->deps = deps_init (); | |
423 | ||
424 | deps_add_target (pfile->deps, target, quote); | |
425 | } | |
426 | ||
f5e99456 NB |
427 | /* This is called after options have been parsed, and partially |
428 | processed. Setup for processing input from the file named FNAME, | |
429 | or stdin if it is the empty string. Return the original filename | |
430 | on success (e.g. foo.i->foo.c), or NULL on failure. */ | |
431 | const char * | |
432 | cpp_read_main_file (pfile, fname, table) | |
6de1e2a9 | 433 | cpp_reader *pfile; |
7ceb3598 | 434 | const char *fname; |
f5e99456 | 435 | hash_table *table; |
6de1e2a9 | 436 | { |
2443d4e1 NB |
437 | sanity_checks (pfile); |
438 | ||
f4ff5a69 NB |
439 | post_options (pfile); |
440 | ||
f5e99456 NB |
441 | /* The front ends don't set up the hash table until they have |
442 | finished processing the command line options, so initializing the | |
443 | hashtable is deferred until now. */ | |
444 | _cpp_init_hashtable (pfile, table); | |
445 | ||
c19b12cb NB |
446 | /* Mark named operators before handling command line macros. */ |
447 | if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names)) | |
448 | mark_named_operators (pfile); | |
449 | ||
f4ff5a69 NB |
450 | if (CPP_OPTION (pfile, deps.style) != DEPS_NONE) |
451 | { | |
452 | if (!pfile->deps) | |
453 | pfile->deps = deps_init (); | |
454 | ||
455 | /* Set the default target (if there is none already). */ | |
456 | deps_add_default_target (pfile->deps, fname); | |
457 | } | |
96302433 | 458 | |
f5e99456 | 459 | /* Open the main input file. */ |
614c7d37 | 460 | if (!_cpp_read_file (pfile, fname)) |
f5e99456 | 461 | return NULL; |
c45da1ca | 462 | |
f4ff5a69 NB |
463 | /* Set this here so the client can change the option if it wishes, |
464 | and after stacking the main file so we don't trace the main | |
465 | file. */ | |
5993019d NB |
466 | pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names); |
467 | ||
f5e99456 NB |
468 | /* For foo.i, read the original filename foo.c now, for the benefit |
469 | of the front ends. */ | |
470 | if (CPP_OPTION (pfile, preprocessed)) | |
471 | read_original_filename (pfile); | |
472 | ||
473 | return pfile->map->to_file; | |
474 | } | |
475 | ||
476 | /* For preprocessed files, if the first tokens are of the form # NUM. | |
477 | handle the directive so we know the original file name. This will | |
478 | generate file_change callbacks, which the front ends must handle | |
479 | appropriately given their state of initialization. */ | |
480 | static void | |
481 | read_original_filename (pfile) | |
482 | cpp_reader *pfile; | |
483 | { | |
484 | const cpp_token *token, *token1; | |
485 | ||
486 | /* Lex ahead; if the first tokens are of the form # NUM, then | |
487 | process the directive, otherwise back up. */ | |
488 | token = _cpp_lex_direct (pfile); | |
489 | if (token->type == CPP_HASH) | |
490 | { | |
491 | token1 = _cpp_lex_direct (pfile); | |
492 | _cpp_backup_tokens (pfile, 1); | |
493 | ||
494 | /* If it's a #line directive, handle it. */ | |
495 | if (token1->type == CPP_NUMBER) | |
496 | { | |
497 | _cpp_handle_directive (pfile, token->flags & PREV_WHITE); | |
498 | return; | |
499 | } | |
500 | } | |
501 | ||
502 | /* Backup as if nothing happened. */ | |
503 | _cpp_backup_tokens (pfile, 1); | |
504 | } | |
505 | ||
76c3e73e NB |
506 | /* This is called at the end of preprocessing. It pops the last |
507 | buffer and writes dependency output, and returns the number of | |
508 | errors. | |
509 | ||
510 | Maybe it should also reset state, such that you could call | |
511 | cpp_start_read with a new filename to restart processing. */ | |
512 | int | |
513 | cpp_finish (pfile, deps_stream) | |
6de1e2a9 | 514 | cpp_reader *pfile; |
76c3e73e | 515 | FILE *deps_stream; |
6de1e2a9 | 516 | { |
a69cbaac NB |
517 | /* Warn about unused macros before popping the final buffer. */ |
518 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
519 | cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL); | |
520 | ||
7364fdd8 NB |
521 | /* cpplex.c leaves the final buffer on the stack. This it so that |
522 | it returns an unending stream of CPP_EOFs to the client. If we | |
a1f300c0 | 523 | popped the buffer, we'd dereference a NULL buffer pointer and |
7364fdd8 NB |
524 | segfault. It's nice to allow the client to do worry-free excess |
525 | cpp_get_token calls. */ | |
526 | while (pfile->buffer) | |
527 | _cpp_pop_buffer (pfile); | |
c1212d2f | 528 | |
76c3e73e | 529 | /* Don't write the deps file if there are errors. */ |
f4ff5a69 NB |
530 | if (CPP_OPTION (pfile, deps.style) != DEPS_NONE |
531 | && deps_stream && pfile->errors == 0) | |
76c3e73e NB |
532 | { |
533 | deps_write (pfile->deps, deps_stream, 72); | |
534 | ||
f4ff5a69 | 535 | if (CPP_OPTION (pfile, deps.phony_targets)) |
76c3e73e NB |
536 | deps_phony_targets (pfile->deps, deps_stream); |
537 | } | |
3caee4a8 | 538 | |
d4506961 ZW |
539 | /* Report on headers that could use multiple include guards. */ |
540 | if (CPP_OPTION (pfile, print_include_names)) | |
c71f835b | 541 | _cpp_report_missing_guards (pfile); |
76c3e73e NB |
542 | |
543 | return pfile->errors; | |
6de1e2a9 ZW |
544 | } |
545 | ||
f4ff5a69 NB |
546 | static void |
547 | post_options (pfile) | |
96302433 NB |
548 | cpp_reader *pfile; |
549 | { | |
550 | /* -Wtraditional is not useful in C++ mode. */ | |
551 | if (CPP_OPTION (pfile, cplusplus)) | |
552 | CPP_OPTION (pfile, warn_traditional) = 0; | |
553 | ||
6d4587f7 | 554 | /* Permanently disable macro expansion if we are rescanning |
43612ffb | 555 | preprocessed text. Read preprocesed source in ISO mode. */ |
6d4587f7 | 556 | if (CPP_OPTION (pfile, preprocessed)) |
43612ffb NB |
557 | { |
558 | pfile->state.prevent_expansion = 1; | |
559 | CPP_OPTION (pfile, traditional) = 0; | |
560 | } | |
561 | ||
562 | /* Traditional CPP does not accurately track column information. */ | |
563 | if (CPP_OPTION (pfile, traditional)) | |
564 | CPP_OPTION (pfile, show_column) = 0; | |
7ca3d2b1 | 565 | } |