Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / libcpp / init.cc
blobc508f06112afefb240f40a86ed39113315ff1ef0
1 /* CPP Library.
2 Copyright (C) 1986-2023 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "internal.h"
25 #include "mkdeps.h"
26 #include "localedir.h"
27 #include "filenames.h"
29 #ifndef ENABLE_CANONICAL_SYSTEM_HEADERS
30 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
31 #define ENABLE_CANONICAL_SYSTEM_HEADERS 1
32 #else
33 #define ENABLE_CANONICAL_SYSTEM_HEADERS 0
34 #endif
35 #endif
37 static void init_library (void);
38 static void mark_named_operators (cpp_reader *, int);
39 static bool read_original_filename (cpp_reader *);
40 static void read_original_directory (cpp_reader *);
41 static void post_options (cpp_reader *);
43 /* If we have designated initializers (GCC >2.7) these tables can be
44 initialized, constant data. Otherwise, they have to be filled in at
45 runtime. */
46 #if HAVE_DESIGNATED_INITIALIZERS
48 #define init_trigraph_map() /* Nothing. */
49 #define TRIGRAPH_MAP \
50 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
52 #define END };
53 #define s(p, v) [p] = v,
55 #else
57 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
58 static void init_trigraph_map (void) { \
59 unsigned char *x = _cpp_trigraph_map;
61 #define END }
62 #define s(p, v) x[p] = v;
64 #endif
66 TRIGRAPH_MAP
67 s('=', '#') s(')', ']') s('!', '|')
68 s('(', '[') s('\'', '^') s('>', '}')
69 s('/', '\\') s('<', '{') s('-', '~')
70 END
72 #undef s
73 #undef END
74 #undef TRIGRAPH_MAP
76 /* A set of booleans indicating what CPP features each source language
77 requires. */
78 struct lang_flags
80 char c99;
81 char cplusplus;
82 char extended_numbers;
83 char extended_identifiers;
84 char c11_identifiers;
85 char xid_identifiers;
86 char std;
87 char digraphs;
88 char uliterals;
89 char rliterals;
90 char user_literals;
91 char binary_constants;
92 char digit_separators;
93 char trigraphs;
94 char utf8_char_literals;
95 char va_opt;
96 char scope;
97 char dfp_constants;
98 char size_t_literals;
99 char elifdef;
100 char warning_directive;
101 char delimited_escape_seqs;
102 char true_false;
105 static const struct lang_flags lang_defaults[] =
106 { /* c99 c++ xnum xid c11 xidid std digr ulit rlit udlit bincst digsep trig u8chlit vaopt scope dfp szlit elifdef warndir delim trufal */
107 /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
108 /* GNUC99 */ { 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
109 /* GNUC11 */ { 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
110 /* GNUC17 */ { 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
111 /* GNUC2X */ { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1 },
112 /* STDC89 */ { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
113 /* STDC94 */ { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
114 /* STDC99 */ { 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
115 /* STDC11 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
116 /* STDC17 */ { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
117 /* STDC2X */ { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1 },
118 /* GNUCXX */ { 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
119 /* CXX98 */ { 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
120 /* GNUCXX11 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
121 /* CXX11 */ { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
122 /* GNUCXX14 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
123 /* CXX14 */ { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
124 /* GNUCXX17 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 },
125 /* CXX17 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
126 /* GNUCXX20 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 },
127 /* CXX20 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 },
128 /* GNUCXX23 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1 },
129 /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1 },
130 /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
133 /* Sets internal flags correctly for a given language. */
134 void
135 cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
137 const struct lang_flags *l = &lang_defaults[(int) lang];
139 CPP_OPTION (pfile, lang) = lang;
141 CPP_OPTION (pfile, c99) = l->c99;
142 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
143 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
144 CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers;
145 CPP_OPTION (pfile, c11_identifiers) = l->c11_identifiers;
146 CPP_OPTION (pfile, xid_identifiers) = l->xid_identifiers;
147 CPP_OPTION (pfile, std) = l->std;
148 CPP_OPTION (pfile, digraphs) = l->digraphs;
149 CPP_OPTION (pfile, uliterals) = l->uliterals;
150 CPP_OPTION (pfile, rliterals) = l->rliterals;
151 CPP_OPTION (pfile, user_literals) = l->user_literals;
152 CPP_OPTION (pfile, binary_constants) = l->binary_constants;
153 CPP_OPTION (pfile, digit_separators) = l->digit_separators;
154 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
155 CPP_OPTION (pfile, utf8_char_literals) = l->utf8_char_literals;
156 CPP_OPTION (pfile, va_opt) = l->va_opt;
157 CPP_OPTION (pfile, scope) = l->scope;
158 CPP_OPTION (pfile, dfp_constants) = l->dfp_constants;
159 CPP_OPTION (pfile, size_t_literals) = l->size_t_literals;
160 CPP_OPTION (pfile, elifdef) = l->elifdef;
161 CPP_OPTION (pfile, warning_directive) = l->warning_directive;
162 CPP_OPTION (pfile, delimited_escape_seqs) = l->delimited_escape_seqs;
163 CPP_OPTION (pfile, true_false) = l->true_false;
166 /* Initialize library global state. */
167 static void
168 init_library (void)
170 static int initialized = 0;
172 if (! initialized)
174 initialized = 1;
176 _cpp_init_lexer ();
178 /* Set up the trigraph map. This doesn't need to do anything if
179 we were compiled with a compiler that supports C99 designated
180 initializers. */
181 init_trigraph_map ();
183 #ifdef ENABLE_NLS
184 (void) bindtextdomain (PACKAGE, LOCALEDIR);
185 #endif
189 /* Initialize a cpp_reader structure. */
190 cpp_reader *
191 cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
192 class line_maps *line_table)
194 cpp_reader *pfile;
196 /* Initialize this instance of the library if it hasn't been already. */
197 init_library ();
199 pfile = XCNEW (cpp_reader);
200 memset (&pfile->base_context, 0, sizeof (pfile->base_context));
202 cpp_set_lang (pfile, lang);
203 CPP_OPTION (pfile, warn_multichar) = 1;
204 CPP_OPTION (pfile, discard_comments) = 1;
205 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
206 CPP_OPTION (pfile, max_include_depth) = 200;
207 CPP_OPTION (pfile, operator_names) = 1;
208 CPP_OPTION (pfile, warn_trigraphs) = 2;
209 CPP_OPTION (pfile, warn_endif_labels) = 1;
210 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) = -1;
211 CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) = -1;
212 CPP_OPTION (pfile, cpp_warn_cxx11_compat) = 0;
213 CPP_OPTION (pfile, cpp_warn_cxx20_compat) = 0;
214 CPP_OPTION (pfile, cpp_warn_deprecated) = 1;
215 CPP_OPTION (pfile, cpp_warn_long_long) = 0;
216 CPP_OPTION (pfile, dollars_in_ident) = 1;
217 CPP_OPTION (pfile, warn_dollars) = 1;
218 CPP_OPTION (pfile, warn_variadic_macros) = 1;
219 CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
220 CPP_OPTION (pfile, cpp_warn_implicit_fallthrough) = 0;
221 /* By default, track locations of tokens resulting from macro
222 expansion. The '2' means, track the locations with the highest
223 accuracy. Read the comments for struct
224 cpp_options::track_macro_expansion to learn about the other
225 values. */
226 CPP_OPTION (pfile, track_macro_expansion) = 2;
227 CPP_OPTION (pfile, warn_normalize) = normalized_C;
228 CPP_OPTION (pfile, warn_literal_suffix) = 1;
229 CPP_OPTION (pfile, canonical_system_headers)
230 = ENABLE_CANONICAL_SYSTEM_HEADERS;
231 CPP_OPTION (pfile, ext_numeric_literals) = 1;
232 CPP_OPTION (pfile, warn_date_time) = 0;
233 CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
234 CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
235 CPP_OPTION (pfile, cpp_warn_unicode) = 1;
236 CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
238 /* Default CPP arithmetic to something sensible for the host for the
239 benefit of dumb users like fix-header. */
240 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
241 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
242 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
243 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
244 CPP_OPTION (pfile, unsigned_char) = 0;
245 CPP_OPTION (pfile, unsigned_wchar) = 1;
246 CPP_OPTION (pfile, unsigned_utf8char) = 1;
247 CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */
249 /* Default to no charset conversion. */
250 CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
251 CPP_OPTION (pfile, wide_charset) = 0;
253 /* Default the input character set to UTF-8. */
254 CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
256 /* A fake empty "directory" used as the starting point for files
257 looked up without a search path. Name cannot be '/' because we
258 don't want to prepend anything at all to filenames using it. All
259 other entries are correct zero-initialized. */
260 pfile->no_search_path.name = (char *) "";
262 /* Initialize the line map. */
263 pfile->line_table = line_table;
265 /* Initialize lexer state. */
266 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
268 /* Set up static tokens. */
269 pfile->avoid_paste.type = CPP_PADDING;
270 pfile->avoid_paste.val.source = NULL;
271 pfile->avoid_paste.src_loc = 0;
272 pfile->endarg.type = CPP_EOF;
273 pfile->endarg.flags = 0;
274 pfile->endarg.src_loc = 0;
276 /* Create a token buffer for the lexer. */
277 _cpp_init_tokenrun (&pfile->base_run, 250);
278 pfile->cur_run = &pfile->base_run;
279 pfile->cur_token = pfile->base_run.base;
281 /* Initialize the base context. */
282 pfile->context = &pfile->base_context;
283 pfile->base_context.c.macro = 0;
284 pfile->base_context.prev = pfile->base_context.next = 0;
286 /* Aligned and unaligned storage. */
287 pfile->a_buff = _cpp_get_buff (pfile, 0);
288 pfile->u_buff = _cpp_get_buff (pfile, 0);
290 /* Initialize table for push_macro/pop_macro. */
291 pfile->pushed_macros = 0;
293 /* Do not force token locations by default. */
294 pfile->forced_token_location = 0;
296 /* Note the timestamp is unset. */
297 pfile->time_stamp = time_t (-1);
298 pfile->time_stamp_kind = 0;
300 /* The expression parser stack. */
301 _cpp_expand_op_stack (pfile);
303 /* Initialize the buffer obstack. */
304 obstack_specify_allocation (&pfile->buffer_ob, 0, 0, xmalloc, free);
306 _cpp_init_files (pfile);
308 _cpp_init_hashtable (pfile, table);
310 return pfile;
313 /* Set the line_table entry in PFILE. This is called after reading a
314 PCH file, as the old line_table will be incorrect. */
315 void
316 cpp_set_line_map (cpp_reader *pfile, class line_maps *line_table)
318 pfile->line_table = line_table;
321 /* Free resources used by PFILE. Accessing PFILE after this function
322 returns leads to undefined behavior. Returns the error count. */
323 void
324 cpp_destroy (cpp_reader *pfile)
326 cpp_context *context, *contextn;
327 struct def_pragma_macro *pmacro;
328 tokenrun *run, *runn;
329 int i;
331 free (pfile->op_stack);
333 while (CPP_BUFFER (pfile) != NULL)
334 _cpp_pop_buffer (pfile);
336 free (pfile->out.base);
338 if (pfile->macro_buffer)
340 free (pfile->macro_buffer);
341 pfile->macro_buffer = NULL;
342 pfile->macro_buffer_len = 0;
345 if (pfile->deps)
346 deps_free (pfile->deps);
347 obstack_free (&pfile->buffer_ob, 0);
349 _cpp_destroy_hashtable (pfile);
350 _cpp_cleanup_files (pfile);
351 _cpp_destroy_iconv (pfile);
353 _cpp_free_buff (pfile->a_buff);
354 _cpp_free_buff (pfile->u_buff);
355 _cpp_free_buff (pfile->free_buffs);
357 for (run = &pfile->base_run; run; run = runn)
359 runn = run->next;
360 free (run->base);
361 if (run != &pfile->base_run)
362 free (run);
365 for (context = pfile->base_context.next; context; context = contextn)
367 contextn = context->next;
368 free (context);
371 if (pfile->comments.entries)
373 for (i = 0; i < pfile->comments.count; i++)
374 free (pfile->comments.entries[i].comment);
376 free (pfile->comments.entries);
378 if (pfile->pushed_macros)
382 pmacro = pfile->pushed_macros;
383 pfile->pushed_macros = pmacro->next;
384 free (pmacro->name);
385 free (pmacro);
387 while (pfile->pushed_macros);
390 free (pfile);
393 /* This structure defines one built-in identifier. A node will be
394 entered in the hash table under the name NAME, with value VALUE.
396 There are two tables of these. builtin_array holds all the
397 "builtin" macros: these are handled by builtin_macro() in
398 macro.cc. Builtin is somewhat of a misnomer -- the property of
399 interest is that these macros require special code to compute their
400 expansions. The value is a "cpp_builtin_type" enumerator.
402 operator_array holds the C++ named operators. These are keywords
403 which act as aliases for punctuators. In C++, they cannot be
404 altered through #define, and #if recognizes them as operators. In
405 C, these are not entered into the hash table at all (but see
406 <iso646.h>). The value is a token-type enumerator. */
407 struct builtin_macro
409 const uchar *const name;
410 const unsigned short len;
411 const unsigned short value;
412 const bool always_warn_if_redefined;
415 #define B(n, t, f) { DSC(n), t, f }
416 static const struct builtin_macro builtin_array[] =
418 B("__TIMESTAMP__", BT_TIMESTAMP, false),
419 B("__TIME__", BT_TIME, false),
420 B("__DATE__", BT_DATE, false),
421 B("__FILE__", BT_FILE, false),
422 B("__FILE_NAME__", BT_FILE_NAME, false),
423 B("__BASE_FILE__", BT_BASE_FILE, false),
424 B("__LINE__", BT_SPECLINE, true),
425 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
426 B("__COUNTER__", BT_COUNTER, true),
427 /* Make sure to update the list of built-in
428 function-like macros in traditional.cc:
429 fun_like_macro() when adding more following */
430 B("__has_attribute", BT_HAS_ATTRIBUTE, true),
431 B("__has_c_attribute", BT_HAS_STD_ATTRIBUTE, true),
432 B("__has_cpp_attribute", BT_HAS_ATTRIBUTE, true),
433 B("__has_builtin", BT_HAS_BUILTIN, true),
434 B("__has_include", BT_HAS_INCLUDE, true),
435 B("__has_include_next",BT_HAS_INCLUDE_NEXT, true),
436 /* Keep builtins not used for -traditional-cpp at the end, and
437 update init_builtins() if any more are added. */
438 B("_Pragma", BT_PRAGMA, true),
439 B("__STDC__", BT_STDC, true),
441 #undef B
443 struct builtin_operator
445 const uchar *const name;
446 const unsigned short len;
447 const unsigned short value;
450 #define B(n, t) { DSC(n), t }
451 static const struct builtin_operator operator_array[] =
453 B("and", CPP_AND_AND),
454 B("and_eq", CPP_AND_EQ),
455 B("bitand", CPP_AND),
456 B("bitor", CPP_OR),
457 B("compl", CPP_COMPL),
458 B("not", CPP_NOT),
459 B("not_eq", CPP_NOT_EQ),
460 B("or", CPP_OR_OR),
461 B("or_eq", CPP_OR_EQ),
462 B("xor", CPP_XOR),
463 B("xor_eq", CPP_XOR_EQ)
465 #undef B
467 /* Mark the C++ named operators in the hash table. */
468 static void
469 mark_named_operators (cpp_reader *pfile, int flags)
471 const struct builtin_operator *b;
473 for (b = operator_array;
474 b < (operator_array + ARRAY_SIZE (operator_array));
475 b++)
477 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
478 hp->flags |= flags;
479 hp->is_directive = 0;
480 hp->directive_index = b->value;
484 /* Helper function of cpp_type2name. Return the string associated with
485 named operator TYPE. */
486 const char *
487 cpp_named_operator2name (enum cpp_ttype type)
489 const struct builtin_operator *b;
491 for (b = operator_array;
492 b < (operator_array + ARRAY_SIZE (operator_array));
493 b++)
495 if (type == b->value)
496 return (const char *) b->name;
499 return NULL;
502 void
503 cpp_init_special_builtins (cpp_reader *pfile)
505 const struct builtin_macro *b;
506 size_t n = ARRAY_SIZE (builtin_array);
508 if (CPP_OPTION (pfile, traditional))
509 n -= 2;
510 else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
511 || CPP_OPTION (pfile, std))
512 n--;
514 for (b = builtin_array; b < builtin_array + n; b++)
516 if ((b->value == BT_HAS_ATTRIBUTE
517 || b->value == BT_HAS_STD_ATTRIBUTE
518 || b->value == BT_HAS_BUILTIN)
519 && (CPP_OPTION (pfile, lang) == CLK_ASM
520 || pfile->cb.has_attribute == NULL))
521 continue;
522 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
523 hp->type = NT_BUILTIN_MACRO;
524 if (b->always_warn_if_redefined)
525 hp->flags |= NODE_WARN;
526 hp->value.builtin = (enum cpp_builtin_type) b->value;
530 /* Restore macro C to builtin macro definition. */
532 void
533 _cpp_restore_special_builtin (cpp_reader *pfile, struct def_pragma_macro *c)
535 size_t len = strlen (c->name);
537 for (const struct builtin_macro *b = builtin_array;
538 b < builtin_array + ARRAY_SIZE (builtin_array); b++)
539 if (b->len == len && memcmp (c->name, b->name, len + 1) == 0)
541 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
542 hp->type = NT_BUILTIN_MACRO;
543 if (b->always_warn_if_redefined)
544 hp->flags |= NODE_WARN;
545 hp->value.builtin = (enum cpp_builtin_type) b->value;
549 /* Read the builtins table above and enter them, and language-specific
550 macros, into the hash table. HOSTED is true if this is a hosted
551 environment. */
552 void
553 cpp_init_builtins (cpp_reader *pfile, int hosted)
555 cpp_init_special_builtins (pfile);
557 if (!CPP_OPTION (pfile, traditional)
558 && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
559 || CPP_OPTION (pfile, std)))
560 _cpp_define_builtin (pfile, "__STDC__ 1");
562 if (CPP_OPTION (pfile, cplusplus))
564 /* C++23 is not yet a standard. For now, use an invalid
565 * year/month, 202100L, which is larger than 202002L. */
566 if (CPP_OPTION (pfile, lang) == CLK_CXX23
567 || CPP_OPTION (pfile, lang) == CLK_GNUCXX23)
568 _cpp_define_builtin (pfile, "__cplusplus 202100L");
569 else if (CPP_OPTION (pfile, lang) == CLK_CXX20
570 || CPP_OPTION (pfile, lang) == CLK_GNUCXX20)
571 _cpp_define_builtin (pfile, "__cplusplus 202002L");
572 else if (CPP_OPTION (pfile, lang) == CLK_CXX17
573 || CPP_OPTION (pfile, lang) == CLK_GNUCXX17)
574 _cpp_define_builtin (pfile, "__cplusplus 201703L");
575 else if (CPP_OPTION (pfile, lang) == CLK_CXX14
576 || CPP_OPTION (pfile, lang) == CLK_GNUCXX14)
577 _cpp_define_builtin (pfile, "__cplusplus 201402L");
578 else if (CPP_OPTION (pfile, lang) == CLK_CXX11
579 || CPP_OPTION (pfile, lang) == CLK_GNUCXX11)
580 _cpp_define_builtin (pfile, "__cplusplus 201103L");
581 else
582 _cpp_define_builtin (pfile, "__cplusplus 199711L");
584 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
585 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
586 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
587 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
588 else if (CPP_OPTION (pfile, lang) == CLK_STDC2X
589 || CPP_OPTION (pfile, lang) == CLK_GNUC2X)
590 _cpp_define_builtin (pfile, "__STDC_VERSION__ 202000L");
591 else if (CPP_OPTION (pfile, lang) == CLK_STDC17
592 || CPP_OPTION (pfile, lang) == CLK_GNUC17)
593 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201710L");
594 else if (CPP_OPTION (pfile, lang) == CLK_STDC11
595 || CPP_OPTION (pfile, lang) == CLK_GNUC11)
596 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201112L");
597 else if (CPP_OPTION (pfile, c99))
598 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
600 if (CPP_OPTION (pfile, uliterals)
601 && !(CPP_OPTION (pfile, cplusplus)
602 && (CPP_OPTION (pfile, lang) == CLK_GNUCXX
603 || CPP_OPTION (pfile, lang) == CLK_CXX98)))
605 _cpp_define_builtin (pfile, "__STDC_UTF_16__ 1");
606 _cpp_define_builtin (pfile, "__STDC_UTF_32__ 1");
609 if (hosted)
610 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
611 else
612 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
614 if (CPP_OPTION (pfile, objc))
615 _cpp_define_builtin (pfile, "__OBJC__ 1");
618 /* Sanity-checks are dependent on command-line options, so it is
619 called as a subroutine of cpp_read_main_file. */
620 #if CHECKING_P
621 static void sanity_checks (cpp_reader *);
622 static void sanity_checks (cpp_reader *pfile)
624 cppchar_t test = 0;
625 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
627 /* Sanity checks for assumptions about CPP arithmetic and target
628 type precisions made by cpplib. */
629 test--;
630 if (test < 1)
631 cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
633 if (CPP_OPTION (pfile, precision) > max_precision)
634 cpp_error (pfile, CPP_DL_ICE,
635 "preprocessor arithmetic has maximum precision of %lu bits;"
636 " target requires %lu bits",
637 (unsigned long) max_precision,
638 (unsigned long) CPP_OPTION (pfile, precision));
640 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
641 cpp_error (pfile, CPP_DL_ICE,
642 "CPP arithmetic must be at least as precise as a target int");
644 if (CPP_OPTION (pfile, char_precision) < 8)
645 cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
647 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
648 cpp_error (pfile, CPP_DL_ICE,
649 "target wchar_t is narrower than target char");
651 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
652 cpp_error (pfile, CPP_DL_ICE,
653 "target int is narrower than target char");
655 /* This is assumed in eval_token() and could be fixed if necessary. */
656 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
657 cpp_error (pfile, CPP_DL_ICE,
658 "CPP half-integer narrower than CPP character");
660 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
661 cpp_error (pfile, CPP_DL_ICE,
662 "CPP on this host cannot handle wide character constants over"
663 " %lu bits, but the target requires %lu bits",
664 (unsigned long) BITS_PER_CPPCHAR_T,
665 (unsigned long) CPP_OPTION (pfile, wchar_precision));
667 #else
668 # define sanity_checks(PFILE)
669 #endif
671 /* This is called after options have been parsed, and partially
672 processed. */
673 void
674 cpp_post_options (cpp_reader *pfile)
676 int flags;
678 sanity_checks (pfile);
680 post_options (pfile);
682 /* Mark named operators before handling command line macros. */
683 flags = 0;
684 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
685 flags |= NODE_OPERATOR;
686 if (CPP_OPTION (pfile, warn_cxx_operator_names))
687 flags |= NODE_DIAGNOSTIC | NODE_WARN_OPERATOR;
688 if (flags != 0)
689 mark_named_operators (pfile, flags);
692 /* Setup for processing input from the file named FNAME, or stdin if
693 it is the empty string. Return the original filename on success
694 (e.g. foo.i->foo.c), or NULL on failure. INJECTING is true if
695 there may be injected headers before line 1 of the main file. */
696 const char *
697 cpp_read_main_file (cpp_reader *pfile, const char *fname, bool injecting)
699 if (mkdeps *deps = cpp_get_deps (pfile))
700 /* Set the default target (if there is none already). */
701 deps_add_default_target (deps, fname);
703 pfile->main_file
704 = _cpp_find_file (pfile, fname,
705 CPP_OPTION (pfile, preprocessed) ? &pfile->no_search_path
706 : CPP_OPTION (pfile, main_search) == CMS_user
707 ? pfile->quote_include
708 : CPP_OPTION (pfile, main_search) == CMS_system
709 ? pfile->bracket_include : &pfile->no_search_path,
710 /*angle=*/0, _cpp_FFK_NORMAL, 0);
712 if (_cpp_find_failed (pfile->main_file))
713 return NULL;
715 _cpp_stack_file (pfile, pfile->main_file,
716 injecting || CPP_OPTION (pfile, preprocessed)
717 ? IT_PRE_MAIN : IT_MAIN, 0);
719 /* For foo.i, read the original filename foo.c now, for the benefit
720 of the front ends. */
721 if (CPP_OPTION (pfile, preprocessed))
722 if (!read_original_filename (pfile))
724 /* We're on line 1 after all. */
725 auto *last = linemap_check_ordinary
726 (LINEMAPS_LAST_MAP (pfile->line_table, false));
727 last->to_line = 1;
728 /* Inform of as-if a file change. */
729 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, LINEMAP_FILE (last),
730 LINEMAP_LINE (last), LINEMAP_SYSP (last));
733 auto *map = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
734 pfile->main_loc = MAP_START_LOCATION (map);
736 return ORDINARY_MAP_FILE_NAME (map);
739 location_t
740 cpp_main_loc (const cpp_reader *pfile)
742 return pfile->main_loc;
745 /* For preprocessed files, if the very first characters are
746 '#<SPACE>[01]<SPACE>', then handle a line directive so we know the
747 original file name. This will generate file_change callbacks,
748 which the front ends must handle appropriately given their state of
749 initialization. We peek directly into the character buffer, so
750 that we're not confused by otherwise-skipped white space &
751 comments. We can be very picky, because this should have been
752 machine-generated text (by us, no less). This way we do not
753 interfere with the module directive state machine. */
755 static bool
756 read_original_filename (cpp_reader *pfile)
758 auto *buf = pfile->buffer->next_line;
760 if (pfile->buffer->rlimit - buf > 4
761 && buf[0] == '#'
762 && buf[1] == ' '
763 // Also permit '1', as that's what used to be here
764 && (buf[2] == '0' || buf[2] == '1')
765 && buf[3] == ' ')
767 const cpp_token *token = _cpp_lex_direct (pfile);
768 gcc_checking_assert (token->type == CPP_HASH);
769 if (_cpp_handle_directive (pfile, token->flags & PREV_WHITE))
771 read_original_directory (pfile);
773 auto *penult = &linemap_check_ordinary
774 (LINEMAPS_LAST_MAP (pfile->line_table, false))[-1];
775 if (penult[1].reason == LC_RENAME_VERBATIM)
777 /* Expunge any evidence of the original linemap. */
778 pfile->line_table->highest_location
779 = pfile->line_table->highest_line
780 = penult[0].start_location;
782 penult[1].start_location = penult[0].start_location;
783 penult[1].reason = penult[0].reason;
784 penult[0] = penult[1];
785 pfile->line_table->info_ordinary.used--;
786 pfile->line_table->info_ordinary.cache = 0;
789 return true;
793 return false;
796 /* For preprocessed files, if the tokens following the first filename
797 line is of the form # <line> "/path/name//", handle the
798 directive so we know the original current directory.
800 As with the first line peeking, we can do this without lexing by
801 being picky. */
802 static void
803 read_original_directory (cpp_reader *pfile)
805 auto *buf = pfile->buffer->next_line;
807 if (pfile->buffer->rlimit - buf > 4
808 && buf[0] == '#'
809 && buf[1] == ' '
810 // Also permit '1', as that's what used to be here
811 && (buf[2] == '0' || buf[2] == '1')
812 && buf[3] == ' ')
814 const cpp_token *hash = _cpp_lex_direct (pfile);
815 gcc_checking_assert (hash->type == CPP_HASH);
816 pfile->state.in_directive = 1;
817 const cpp_token *number = _cpp_lex_direct (pfile);
818 gcc_checking_assert (number->type == CPP_NUMBER);
819 const cpp_token *string = _cpp_lex_direct (pfile);
820 pfile->state.in_directive = 0;
822 const unsigned char *text = nullptr;
823 size_t len = 0;
824 if (string->type == CPP_STRING)
826 /* The string value includes the quotes. */
827 text = string->val.str.text;
828 len = string->val.str.len;
830 if (len < 5
831 || !IS_DIR_SEPARATOR (text[len - 2])
832 || !IS_DIR_SEPARATOR (text[len - 3]))
834 /* That didn't work out, back out. */
835 _cpp_backup_tokens (pfile, 3);
836 return;
839 if (pfile->cb.dir_change)
841 /* Smash the string directly, it's dead at this point */
842 char *smashy = (char *)text;
843 smashy[len - 3] = 0;
845 pfile->cb.dir_change (pfile, smashy + 1);
848 /* We should be at EOL. */
852 /* This is called at the end of preprocessing. It pops the last
853 buffer and writes dependency output.
855 Maybe it should also reset state, such that you could call
856 cpp_start_read with a new filename to restart processing. */
857 void
858 cpp_finish (cpp_reader *pfile, FILE *deps_stream)
860 /* Warn about unused macros before popping the final buffer. */
861 if (CPP_OPTION (pfile, warn_unused_macros))
862 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
864 /* lex.cc leaves the final buffer on the stack. This it so that
865 it returns an unending stream of CPP_EOFs to the client. If we
866 popped the buffer, we'd dereference a NULL buffer pointer and
867 segfault. It's nice to allow the client to do worry-free excess
868 cpp_get_token calls. */
869 while (pfile->buffer)
870 _cpp_pop_buffer (pfile);
872 if (deps_stream)
873 deps_write (pfile, deps_stream, 72);
875 /* Report on headers that could use multiple include guards. */
876 if (CPP_OPTION (pfile, print_include_names))
877 _cpp_report_missing_guards (pfile);
880 static void
881 post_options (cpp_reader *pfile)
883 /* -Wtraditional is not useful in C++ mode. */
884 if (CPP_OPTION (pfile, cplusplus))
885 CPP_OPTION (pfile, cpp_warn_traditional) = 0;
887 /* Permanently disable macro expansion if we are rescanning
888 preprocessed text. Read preprocesed source in ISO mode. */
889 if (CPP_OPTION (pfile, preprocessed))
891 if (!CPP_OPTION (pfile, directives_only))
892 pfile->state.prevent_expansion = 1;
893 CPP_OPTION (pfile, traditional) = 0;
896 if (CPP_OPTION (pfile, warn_trigraphs) == 2)
897 CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
899 if (CPP_OPTION (pfile, traditional))
901 CPP_OPTION (pfile, trigraphs) = 0;
902 CPP_OPTION (pfile, warn_trigraphs) = 0;
905 if (CPP_OPTION (pfile, module_directives))
907 /* These unspellable tokens have a leading space. */
908 const char *const inits[spec_nodes::M_HWM]
909 = {"export ", "module ", "import ", "__import"};
911 for (int ix = 0; ix != spec_nodes::M_HWM; ix++)
913 cpp_hashnode *node = cpp_lookup (pfile, UC (inits[ix]),
914 strlen (inits[ix]));
916 /* Token we pass to the compiler. */
917 pfile->spec_nodes.n_modules[ix][1] = node;
919 if (ix != spec_nodes::M__IMPORT)
920 /* Token we recognize when lexing, drop the trailing ' '. */
921 node = cpp_lookup (pfile, NODE_NAME (node), NODE_LEN (node) - 1);
923 node->flags |= NODE_MODULE;
924 pfile->spec_nodes.n_modules[ix][0] = node;