1/* CPP Library.
2 Copyright (C) 1986-2026 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
6
7This program is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
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"
28
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
36
37static void init_library (void);
38static void mark_named_operators (cpp_reader *, int);
39static bool read_original_filename (cpp_reader *);
40static void read_original_directory (cpp_reader *);
41static void post_options (cpp_reader *);
42
43/* If we have designated initializers (GCC >2.7) these tables can be
44 initialized, constant data. Similarly for C++14 and later.
45 Otherwise, they have to be filled in at runtime. */
46#if HAVE_DESIGNATED_INITIALIZERS
47
48#define init_trigraph_map() /* Nothing. */
49#define TRIGRAPH_MAP \
50__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
51
52#define END };
53#define s(p, v) [p] = v,
54
55#elif __cpp_constexpr >= 201304L
56
57#define init_trigraph_map() /* Nothing. */
58#define TRIGRAPH_MAP \
59constexpr _cpp_trigraph_map_s::_cpp_trigraph_map_s () : map {} {
60#define END } \
61constexpr _cpp_trigraph_map_s _cpp_trigraph_map_d;
62#define s(p, v) map[p] = v;
63
64#else
65
66#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
67 static void init_trigraph_map (void) { \
68 unsigned char *x = _cpp_trigraph_map;
69
70#define END }
71#define s(p, v) x[p] = v;
72
73#endif
74
75TRIGRAPH_MAP
76 s('=', '#') s(')', ']') s('!', '|')
77 s('(', '[') s('\'', '^') s('>', '}')
78 s('/', '\\') s('<', '{') s('-', '~')
79END
80
81#undef s
82#undef END
83#undef TRIGRAPH_MAP
84
85/* A set of booleans indicating what CPP features each source language
86 requires. */
87struct lang_flags
88{
89 unsigned int c99 : 1;
90 unsigned int cplusplus : 1;
91 unsigned int extended_numbers : 1;
92 unsigned int extended_identifiers : 1;
93 unsigned int c11_identifiers : 1;
94 unsigned int xid_identifiers : 1;
95 unsigned int std : 1;
96 unsigned int digraphs : 1;
97 unsigned int uliterals : 1;
98 unsigned int rliterals : 1;
99 unsigned int user_literals : 1;
100 unsigned int binary_constants : 1;
101 unsigned int digit_separators : 1;
102 unsigned int trigraphs : 1;
103 unsigned int utf8_char_literals : 1;
104 unsigned int va_opt : 1;
105 unsigned int scope : 1;
106 unsigned int dfp_constants : 1;
107 unsigned int size_t_literals : 1;
108 unsigned int elifdef : 1;
109 unsigned int warning_directive : 1;
110 unsigned int delimited_escape_seqs : 1;
111 unsigned int named_uc_escape_seqs : 1;
112 unsigned int octal_constants : 1;
113 unsigned int true_false : 1;
114 unsigned int embed : 1;
115 unsigned int imaginary_constants : 1;
116 unsigned int low_ucns : 1;
117};
118
119static const struct lang_flags lang_defaults[] = {
120 /* u e w n
121 b d 8 l a a t l
122 x u i i c v s s i r d m o r e o
123 x i d u r d n g t h a c z f n e e c u m i w
124 c c n x c d s i l l l c s r l o o d l d d l d t f b m u
125 9 + u i 1 i t g i i i s e i i p p f i e i i u a a e a c
126 9 + m d 1 d d r t t t t p g t t e p t f r m c l l d g n */
127 /* GNUC89 */ { .c99: 0,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 0,.c11_identifiers: 0,.xid_identifiers: 0,.std: 0,.digraphs: 1,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
128 /* GNUC99 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 0,.xid_identifiers: 0,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
129 /* GNUC11 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 0,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
130 /* GNUC17 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 0,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
131 /* GNUC23 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 0,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 1,.size_t_literals: 0,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 1,.imaginary_constants: 0,.low_ucns: 1 },
132 /* GNUC2Y */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 0,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 1,.size_t_literals: 0,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 0,.octal_constants: 1,.true_false: 1,.embed: 1,.imaginary_constants: 1,.low_ucns: 1 },
133 /* STDC89 */ { .c99: 0,.cplusplus: 0,.extended_numbers: 0,.extended_identifiers: 0,.c11_identifiers: 0,.xid_identifiers: 0,.std: 1,.digraphs: 0,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
134 /* STDC94 */ { .c99: 0,.cplusplus: 0,.extended_numbers: 0,.extended_identifiers: 0,.c11_identifiers: 0,.xid_identifiers: 0,.std: 1,.digraphs: 1,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
135 /* STDC99 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 0,.xid_identifiers: 0,.std: 1,.digraphs: 1,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
136 /* STDC11 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 0,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
137 /* STDC17 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 0,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 },
138 /* STDC23 */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 0,.user_literals: 0,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 1,.size_t_literals: 0,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 1,.imaginary_constants: 0,.low_ucns: 1 },
139 /* STDC2Y */ { .c99: 1,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 0,.user_literals: 0,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 1,.size_t_literals: 0,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 0,.octal_constants: 1,.true_false: 1,.embed: 1,.imaginary_constants: 1,.low_ucns: 1 },
140 /* GNUCXX */ { .c99: 0,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 0,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
141 /* CXX98 */ { .c99: 0,.cplusplus: 1,.extended_numbers: 0,.extended_identifiers: 1,.c11_identifiers: 0,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
142 /* GNUCXX11 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
143 /* CXX11 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 0,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 0,.digit_separators: 0,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
144 /* GNUCXX14 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
145 /* CXX14 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 0,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 1,.utf8_char_literals: 0,.va_opt: 0,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
146 /* GNUCXX17 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
147 /* CXX17 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 0,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
148 /* GNUCXX20 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
149 /* CXX20 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
150 /* GNUCXX23 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 1,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 1,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
151 /* CXX23 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 1,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 1,.octal_constants: 0,.true_false: 1,.embed: 0,.imaginary_constants: 0,.low_ucns: 1 },
152 /* GNUCXX26 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 0,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 1,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 1,.octal_constants: 0,.true_false: 1,.embed: 1,.imaginary_constants: 0,.low_ucns: 1 },
153 /* CXX26 */ { .c99: 1,.cplusplus: 1,.extended_numbers: 1,.extended_identifiers: 1,.c11_identifiers: 1,.xid_identifiers: 1,.std: 1,.digraphs: 1,.uliterals: 1,.rliterals: 1,.user_literals: 1,.binary_constants: 1,.digit_separators: 1,.trigraphs: 0,.utf8_char_literals: 1,.va_opt: 1,.scope: 1,.dfp_constants: 0,.size_t_literals: 1,.elifdef: 1,.warning_directive: 1,.delimited_escape_seqs: 1,.named_uc_escape_seqs: 1,.octal_constants: 0,.true_false: 1,.embed: 1,.imaginary_constants: 0,.low_ucns: 1 },
154 /* ASM */ { .c99: 0,.cplusplus: 0,.extended_numbers: 1,.extended_identifiers: 0,.c11_identifiers: 0,.xid_identifiers: 0,.std: 0,.digraphs: 0,.uliterals: 0,.rliterals: 0,.user_literals: 0,.binary_constants: 0,.digit_separators: 0,.trigraphs: 0,.utf8_char_literals: 0,.va_opt: 0,.scope: 0,.dfp_constants: 0,.size_t_literals: 0,.elifdef: 0,.warning_directive: 0,.delimited_escape_seqs: 0,.named_uc_escape_seqs: 0,.octal_constants: 0,.true_false: 0,.embed: 0,.imaginary_constants: 0,.low_ucns: 0 }
155};
156
157/* Sets internal flags correctly for a given language. */
158void
159cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
160{
161 const struct lang_flags *l = &lang_defaults[(int) lang];
162
163 CPP_OPTION (pfile, lang) = lang;
164
165 CPP_OPTION (pfile, c99) = l->c99;
166 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
167 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
168 CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers;
169 CPP_OPTION (pfile, c11_identifiers) = l->c11_identifiers;
170 CPP_OPTION (pfile, xid_identifiers) = l->xid_identifiers;
171 CPP_OPTION (pfile, std) = l->std;
172 CPP_OPTION (pfile, digraphs) = l->digraphs;
173 CPP_OPTION (pfile, uliterals) = l->uliterals;
174 CPP_OPTION (pfile, rliterals) = l->rliterals;
175 CPP_OPTION (pfile, user_literals) = l->user_literals;
176 CPP_OPTION (pfile, binary_constants) = l->binary_constants;
177 CPP_OPTION (pfile, digit_separators) = l->digit_separators;
178 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
179 CPP_OPTION (pfile, utf8_char_literals) = l->utf8_char_literals;
180 CPP_OPTION (pfile, va_opt) = l->va_opt;
181 CPP_OPTION (pfile, scope) = l->scope;
182 CPP_OPTION (pfile, dfp_constants) = l->dfp_constants;
183 CPP_OPTION (pfile, size_t_literals) = l->size_t_literals;
184 CPP_OPTION (pfile, elifdef) = l->elifdef;
185 CPP_OPTION (pfile, warning_directive) = l->warning_directive;
186 CPP_OPTION (pfile, delimited_escape_seqs) = l->delimited_escape_seqs;
187 CPP_OPTION (pfile, named_uc_escape_seqs) = l->named_uc_escape_seqs;
188 CPP_OPTION (pfile, octal_constants) = l->octal_constants;
189 CPP_OPTION (pfile, true_false) = l->true_false;
190 CPP_OPTION (pfile, embed) = l->embed;
191 CPP_OPTION (pfile, imaginary_constants) = l->imaginary_constants;
192 CPP_OPTION (pfile, low_ucns) = l->low_ucns;
193}
194
195/* Initialize library global state. */
196static void
197init_library (void)
198{
199 static int initialized = 0;
200
201 if (! initialized)
202 {
203 initialized = 1;
204
205 _cpp_init_lexer ();
206
207 /* Set up the trigraph map. This doesn't need to do anything if
208 we were compiled with a compiler that supports C99 designated
209 initializers. */
210 init_trigraph_map ();
211
212#ifdef ENABLE_NLS
213 (void) bindtextdomain (PACKAGE, LOCALEDIR);
214#endif
215 }
216}
217
218/* Initialize a cpp_reader structure. */
219cpp_reader *
220cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
221 class line_maps *line_table, cpp_hash_table *extra_table)
222{
223 cpp_reader *pfile;
224
225 /* Initialize this instance of the library if it hasn't been already. */
226 init_library ();
227
228 pfile = XCNEW (cpp_reader);
229 memset (s: &pfile->base_context, c: 0, n: sizeof (pfile->base_context));
230
231 cpp_set_lang (pfile, lang);
232 CPP_OPTION (pfile, warn_multichar) = 1;
233 CPP_OPTION (pfile, discard_comments) = 1;
234 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
235 CPP_OPTION (pfile, max_include_depth) = 200;
236 CPP_OPTION (pfile, operator_names) = 1;
237 CPP_OPTION (pfile, warn_trigraphs) = 2;
238 CPP_OPTION (pfile, warn_endif_labels) = 1;
239 CPP_OPTION (pfile, cpp_warn_c90_c99_compat) = -1;
240 CPP_OPTION (pfile, cpp_warn_c11_c23_compat) = -1;
241 CPP_OPTION (pfile, cpp_warn_c23_c2y_compat) = -1;
242 CPP_OPTION (pfile, cpp_warn_cxx11_compat) = 0;
243 CPP_OPTION (pfile, cpp_warn_cxx20_compat) = 0;
244 CPP_OPTION (pfile, cpp_warn_deprecated) = 1;
245 CPP_OPTION (pfile, cpp_warn_long_long) = 0;
246 CPP_OPTION (pfile, dollars_in_ident) = 1;
247 CPP_OPTION (pfile, warn_dollars) = 1;
248 CPP_OPTION (pfile, warn_variadic_macros) = 1;
249 CPP_OPTION (pfile, suppress_builtin_macro_warnings) = 0;
250 CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
251 CPP_OPTION (pfile, cpp_warn_implicit_fallthrough) = 0;
252 CPP_OPTION (pfile, warn_header_guard) = 0;
253 /* By default, track locations of tokens resulting from macro
254 expansion. The '2' means, track the locations with the highest
255 accuracy. Read the comments for struct
256 cpp_options::track_macro_expansion to learn about the other
257 values. */
258 CPP_OPTION (pfile, track_macro_expansion) = 2;
259 CPP_OPTION (pfile, warn_normalize) = normalized_C;
260 CPP_OPTION (pfile, warn_literal_suffix) = 1;
261 CPP_OPTION (pfile, canonical_system_headers)
262 = ENABLE_CANONICAL_SYSTEM_HEADERS;
263 CPP_OPTION (pfile, ext_numeric_literals) = 1;
264 CPP_OPTION (pfile, warn_date_time) = 0;
265 CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
266 CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
267 CPP_OPTION (pfile, cpp_warn_unicode) = 1;
268 CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
269 CPP_OPTION (pfile, cpp_tabstop) = 8;
270
271 /* Default CPP arithmetic to something sensible for the host for the
272 benefit of dumb users like fix-header. */
273 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
274 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
275 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
276 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
277 CPP_OPTION (pfile, unsigned_char) = 0;
278 CPP_OPTION (pfile, unsigned_wchar) = 1;
279 CPP_OPTION (pfile, unsigned_utf8char) = 1;
280 CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */
281
282 /* Default to no charset conversion. */
283 CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
284 CPP_OPTION (pfile, wide_charset) = 0;
285
286 /* Default the input character set to UTF-8. */
287 CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
288
289 /* A fake empty "directory" used as the starting point for files
290 looked up without a search path. Name cannot be '/' because we
291 don't want to prepend anything at all to filenames using it. All
292 other entries are correct zero-initialized. */
293 pfile->no_search_path.name = (char *) "";
294
295 /* Initialize the line map. */
296 pfile->line_table = line_table;
297
298 /* Initialize lexer state. */
299 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
300
301 /* Set up static tokens. */
302 pfile->avoid_paste.type = CPP_PADDING;
303 pfile->avoid_paste.val.source = NULL;
304 pfile->avoid_paste.src_loc = 0;
305 pfile->endarg.type = CPP_EOF;
306 pfile->endarg.flags = 0;
307 pfile->endarg.src_loc = 0;
308
309 /* Create a token buffer for the lexer. */
310 _cpp_init_tokenrun (&pfile->base_run, 250);
311 pfile->cur_run = &pfile->base_run;
312 pfile->cur_token = pfile->base_run.base;
313
314 /* Initialize the base context. */
315 pfile->context = &pfile->base_context;
316 pfile->base_context.c.macro = 0;
317 pfile->base_context.prev = pfile->base_context.next = 0;
318
319 /* Aligned and unaligned storage. */
320 pfile->a_buff = _cpp_get_buff (pfile, 0);
321 pfile->u_buff = _cpp_get_buff (pfile, 0);
322
323 /* Initialize table for push_macro/pop_macro. */
324 pfile->pushed_macros = 0;
325
326 /* Do not force token locations by default. */
327 pfile->forced_token_location = 0;
328
329 /* Note the timestamp is unset. */
330 pfile->time_stamp = time_t (-1);
331 pfile->time_stamp_kind = 0;
332
333 /* The expression parser stack. */
334 _cpp_expand_op_stack (pfile);
335
336 /* Initialize the buffer obstack. */
337 obstack_specify_allocation (&pfile->buffer_ob, 0, 0, xmalloc, free);
338
339 _cpp_init_files (pfile);
340
341 _cpp_init_hashtable (pfile, table, extra_table);
342
343 return pfile;
344}
345
346/* Set the line_table entry in PFILE. This is called after reading a
347 PCH file, as the old line_table will be incorrect. */
348void
349cpp_set_line_map (cpp_reader *pfile, class line_maps *line_table)
350{
351 pfile->line_table = line_table;
352}
353
354/* Free resources used by PFILE. Accessing PFILE after this function
355 returns leads to undefined behavior. Returns the error count. */
356void
357cpp_destroy (cpp_reader *pfile)
358{
359 cpp_context *context, *contextn;
360 struct def_pragma_macro *pmacro;
361 tokenrun *run, *runn;
362 int i;
363
364 free (ptr: pfile->op_stack);
365
366 while (CPP_BUFFER (pfile) != NULL)
367 _cpp_pop_buffer (pfile);
368
369 free (ptr: pfile->out.base);
370
371 if (pfile->macro_buffer)
372 {
373 free (ptr: pfile->macro_buffer);
374 pfile->macro_buffer = NULL;
375 pfile->macro_buffer_len = 0;
376 }
377
378 if (pfile->deps)
379 deps_free (pfile->deps);
380 obstack_free (&pfile->buffer_ob, 0);
381
382 _cpp_destroy_hashtable (pfile);
383 _cpp_cleanup_files (pfile);
384 _cpp_destroy_iconv (pfile);
385
386 _cpp_free_buff (pfile->a_buff);
387 _cpp_free_buff (pfile->u_buff);
388 _cpp_free_buff (pfile->free_buffs);
389
390 for (run = &pfile->base_run; run; run = runn)
391 {
392 runn = run->next;
393 free (ptr: run->base);
394 if (run != &pfile->base_run)
395 free (ptr: run);
396 }
397
398 for (context = pfile->base_context.next; context; context = contextn)
399 {
400 contextn = context->next;
401 free (ptr: context);
402 }
403
404 if (pfile->comments.entries)
405 {
406 for (i = 0; i < pfile->comments.count; i++)
407 free (ptr: pfile->comments.entries[i].comment);
408
409 free (ptr: pfile->comments.entries);
410 }
411 if (pfile->pushed_macros)
412 {
413 do
414 {
415 pmacro = pfile->pushed_macros;
416 pfile->pushed_macros = pmacro->next;
417 free (ptr: pmacro->name);
418 free (ptr: pmacro);
419 }
420 while (pfile->pushed_macros);
421 }
422
423 free (ptr: pfile);
424}
425
426/* This structure defines one built-in identifier. A node will be
427 entered in the hash table under the name NAME, with value VALUE.
428
429 There are two tables of these. builtin_array holds all the
430 "builtin" macros: these are handled by builtin_macro() in
431 macro.cc. Builtin is somewhat of a misnomer -- the property of
432 interest is that these macros require special code to compute their
433 expansions. The value is a "cpp_builtin_type" enumerator.
434
435 operator_array holds the C++ named operators. These are keywords
436 which act as aliases for punctuators. In C++, they cannot be
437 altered through #define, and #if recognizes them as operators. In
438 C, these are not entered into the hash table at all (but see
439 <iso646.h>). The value is a token-type enumerator. */
440struct builtin_macro
441{
442 const uchar *const name;
443 const unsigned short len;
444 const unsigned short value;
445 const bool always_warn_if_redefined;
446};
447
448#define B(n, t, f) { DSC(n), t, f }
449static const struct builtin_macro builtin_array[] =
450{
451 B("__TIMESTAMP__", BT_TIMESTAMP, false),
452 B("__TIME__", BT_TIME, false),
453 B("__DATE__", BT_DATE, false),
454 B("__FILE__", BT_FILE, false),
455 B("__FILE_NAME__", BT_FILE_NAME, false),
456 B("__BASE_FILE__", BT_BASE_FILE, false),
457 B("__LINE__", BT_SPECLINE, true),
458 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
459 B("__COUNTER__", BT_COUNTER, true),
460 /* Make sure to update the list of built-in
461 function-like macros in traditional.cc:
462 fun_like_macro() when adding more following */
463 B("__has_attribute", BT_HAS_ATTRIBUTE, true),
464 B("__has_c_attribute", BT_HAS_STD_ATTRIBUTE, true),
465 B("__has_cpp_attribute", BT_HAS_ATTRIBUTE, true),
466 B("__has_builtin", BT_HAS_BUILTIN, true),
467 B("__has_include", BT_HAS_INCLUDE, true),
468 B("__has_include_next",BT_HAS_INCLUDE_NEXT, true),
469 B("__has_embed", BT_HAS_EMBED, true),
470 B("__has_feature", BT_HAS_FEATURE, true),
471 B("__has_extension", BT_HAS_EXTENSION, true),
472 /* Keep builtins not used for -traditional-cpp at the end, and
473 update init_builtins() if any more are added. */
474 B("_Pragma", BT_PRAGMA, true),
475 B("__STDC__", BT_STDC, true),
476};
477#undef B
478
479struct builtin_operator
480{
481 const uchar *const name;
482 const unsigned short len;
483 const unsigned short value;
484};
485
486#define B(n, t) { DSC(n), t }
487static const struct builtin_operator operator_array[] =
488{
489 B("and", CPP_AND_AND),
490 B("and_eq", CPP_AND_EQ),
491 B("bitand", CPP_AND),
492 B("bitor", CPP_OR),
493 B("compl", CPP_COMPL),
494 B("not", CPP_NOT),
495 B("not_eq", CPP_NOT_EQ),
496 B("or", CPP_OR_OR),
497 B("or_eq", CPP_OR_EQ),
498 B("xor", CPP_XOR),
499 B("xor_eq", CPP_XOR_EQ)
500};
501#undef B
502
503/* Mark the C++ named operators in the hash table. */
504static void
505mark_named_operators (cpp_reader *pfile, int flags)
506{
507 const struct builtin_operator *b;
508
509 for (b = operator_array;
510 b < (operator_array + ARRAY_SIZE (operator_array));
511 b++)
512 {
513 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
514 hp->flags |= flags;
515 hp->is_directive = 0;
516 hp->directive_index = b->value;
517 }
518}
519
520/* Helper function of cpp_type2name. Return the string associated with
521 named operator TYPE. */
522const char *
523cpp_named_operator2name (enum cpp_ttype type)
524{
525 const struct builtin_operator *b;
526
527 for (b = operator_array;
528 b < (operator_array + ARRAY_SIZE (operator_array));
529 b++)
530 {
531 if (type == b->value)
532 return (const char *) b->name;
533 }
534
535 return NULL;
536}
537
538void
539cpp_init_special_builtins (cpp_reader *pfile)
540{
541 const struct builtin_macro *b;
542 size_t n = ARRAY_SIZE (builtin_array);
543
544 if (CPP_OPTION (pfile, traditional))
545 n -= 2;
546 else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
547 || CPP_OPTION (pfile, std))
548 n--;
549
550 for (b = builtin_array; b < builtin_array + n; b++)
551 {
552 if ((b->value == BT_HAS_ATTRIBUTE
553 || b->value == BT_HAS_STD_ATTRIBUTE
554 || b->value == BT_HAS_BUILTIN)
555 && (CPP_OPTION (pfile, lang) == CLK_ASM
556 || pfile->cb.has_attribute == NULL))
557 continue;
558 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
559 hp->type = NT_BUILTIN_MACRO;
560 if (b->always_warn_if_redefined)
561 hp->flags |= NODE_WARN;
562 hp->value.builtin = (enum cpp_builtin_type) b->value;
563 }
564}
565
566/* Restore macro C to builtin macro definition. */
567
568void
569_cpp_restore_special_builtin (cpp_reader *pfile, struct def_pragma_macro *c)
570{
571 size_t len = strlen (s: c->name);
572
573 for (const struct builtin_macro *b = builtin_array;
574 b < builtin_array + ARRAY_SIZE (builtin_array); b++)
575 if (b->len == len && memcmp (s1: c->name, s2: b->name, n: len + 1) == 0)
576 {
577 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
578 hp->type = NT_BUILTIN_MACRO;
579 if (b->always_warn_if_redefined)
580 hp->flags |= NODE_WARN;
581 hp->value.builtin = (enum cpp_builtin_type) b->value;
582 }
583}
584
585/* Read the builtins table above and enter them, and language-specific
586 macros, into the hash table. HOSTED is true if this is a hosted
587 environment. */
588void
589cpp_init_builtins (cpp_reader *pfile, int hosted)
590{
591 cpp_init_special_builtins (pfile);
592
593 if (!CPP_OPTION (pfile, traditional)
594 && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
595 || CPP_OPTION (pfile, std)))
596 _cpp_define_builtin (pfile, "__STDC__ 1");
597 else if (CPP_OPTION (pfile, cplusplus))
598 cpp_warn (pfile, name: "__STDC__");
599
600 if (CPP_OPTION (pfile, cplusplus))
601 {
602 /* C++26 is not yet a standard. For now, use an invalid
603 year/month, 202400L, which is larger than 202302L. */
604 if (CPP_OPTION (pfile, lang) == CLK_CXX26
605 || CPP_OPTION (pfile, lang) == CLK_GNUCXX26)
606 _cpp_define_builtin (pfile, "__cplusplus 202400L");
607 else if (CPP_OPTION (pfile, lang) == CLK_CXX23
608 || CPP_OPTION (pfile, lang) == CLK_GNUCXX23)
609 _cpp_define_builtin (pfile, "__cplusplus 202302L");
610 else if (CPP_OPTION (pfile, lang) == CLK_CXX20
611 || CPP_OPTION (pfile, lang) == CLK_GNUCXX20)
612 _cpp_define_builtin (pfile, "__cplusplus 202002L");
613 else if (CPP_OPTION (pfile, lang) == CLK_CXX17
614 || CPP_OPTION (pfile, lang) == CLK_GNUCXX17)
615 _cpp_define_builtin (pfile, "__cplusplus 201703L");
616 else if (CPP_OPTION (pfile, lang) == CLK_CXX14
617 || CPP_OPTION (pfile, lang) == CLK_GNUCXX14)
618 _cpp_define_builtin (pfile, "__cplusplus 201402L");
619 else if (CPP_OPTION (pfile, lang) == CLK_CXX11
620 || CPP_OPTION (pfile, lang) == CLK_GNUCXX11)
621 _cpp_define_builtin (pfile, "__cplusplus 201103L");
622 else
623 _cpp_define_builtin (pfile, "__cplusplus 199711L");
624 cpp_warn (pfile, name: "__cplusplus");
625 if (CPP_OPTION (pfile, lang) >= CLK_GNUCXX11)
626 {
627 cpp_warn (pfile, name: "__STDC_VERSION__");
628 cpp_warn (pfile, name: "__STDC_MB_MIGHT_NEQ_WC__");
629 if (CPP_OPTION (pfile, lang) < CLK_GNUCXX23)
630 cpp_warn (pfile, name: "__STDCPP_STRICT_POINTER_SAFETY__");
631 }
632 }
633 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
634 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
635 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
636 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
637 else if (CPP_OPTION (pfile, lang) == CLK_STDC23
638 || CPP_OPTION (pfile, lang) == CLK_GNUC23)
639 _cpp_define_builtin (pfile, "__STDC_VERSION__ 202311L");
640 else if (CPP_OPTION (pfile, lang) == CLK_STDC2Y
641 || CPP_OPTION (pfile, lang) == CLK_GNUC2Y)
642 _cpp_define_builtin (pfile, "__STDC_VERSION__ 202500L");
643 else if (CPP_OPTION (pfile, lang) == CLK_STDC17
644 || CPP_OPTION (pfile, lang) == CLK_GNUC17)
645 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201710L");
646 else if (CPP_OPTION (pfile, lang) == CLK_STDC11
647 || CPP_OPTION (pfile, lang) == CLK_GNUC11)
648 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201112L");
649 else if (CPP_OPTION (pfile, c99))
650 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
651
652 if (CPP_OPTION (pfile, uliterals)
653 && !(CPP_OPTION (pfile, cplusplus)
654 && (CPP_OPTION (pfile, lang) == CLK_GNUCXX
655 || CPP_OPTION (pfile, lang) == CLK_CXX98)))
656 {
657 _cpp_define_builtin (pfile, "__STDC_UTF_16__ 1");
658 _cpp_define_builtin (pfile, "__STDC_UTF_32__ 1");
659 }
660
661 if (hosted)
662 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
663 else
664 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
665
666 _cpp_define_builtin (pfile, "__STDC_EMBED_NOT_FOUND__ 0");
667 _cpp_define_builtin (pfile, "__STDC_EMBED_FOUND__ 1");
668 _cpp_define_builtin (pfile, "__STDC_EMBED_EMPTY__ 2");
669
670 if (CPP_OPTION (pfile, objc))
671 _cpp_define_builtin (pfile, "__OBJC__ 1");
672}
673
674/* Sanity-checks are dependent on command-line options, so it is
675 called as a subroutine of cpp_read_main_file. */
676#if CHECKING_P
677static void sanity_checks (cpp_reader *);
678static void sanity_checks (cpp_reader *pfile)
679{
680 cppchar_t test = 0;
681 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
682
683 /* Sanity checks for assumptions about CPP arithmetic and target
684 type precisions made by cpplib. */
685 test--;
686 if (test < 1)
687 cpp_error (pfile, CPP_DL_ICE, msgid: "%<cppchar_t%> must be an unsigned type");
688
689 if (CPP_OPTION (pfile, precision) > max_precision)
690 cpp_error (pfile, CPP_DL_ICE,
691 msgid: "preprocessor arithmetic has maximum precision of %lu bits;"
692 " target requires %lu bits",
693 (unsigned long) max_precision,
694 (unsigned long) CPP_OPTION (pfile, precision));
695
696 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
697 cpp_error (pfile, CPP_DL_ICE,
698 msgid: "CPP arithmetic must be at least as precise as a target "
699 "%<int%>");
700
701 if (CPP_OPTION (pfile, char_precision) < 8)
702 cpp_error (pfile, CPP_DL_ICE, msgid: "target %<char%> is less than 8 bits wide");
703
704 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
705 cpp_error (pfile, CPP_DL_ICE,
706 msgid: "target %<wchar_t%> is narrower than target %<char%>");
707
708 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
709 cpp_error (pfile, CPP_DL_ICE,
710 msgid: "target %<int%> is narrower than target %<char%>");
711
712 /* This is assumed in eval_token() and could be fixed if necessary. */
713 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
714 cpp_error (pfile, CPP_DL_ICE,
715 msgid: "CPP half-integer narrower than CPP character");
716
717 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
718 cpp_error (pfile, CPP_DL_ICE,
719 msgid: "CPP on this host cannot handle wide character constants over"
720 " %lu bits, but the target requires %lu bits",
721 (unsigned long) BITS_PER_CPPCHAR_T,
722 (unsigned long) CPP_OPTION (pfile, wchar_precision));
723}
724#else
725# define sanity_checks(PFILE)
726#endif
727
728/* This is called after options have been parsed, and partially
729 processed. */
730void
731cpp_post_options (cpp_reader *pfile)
732{
733 int flags;
734
735 sanity_checks (pfile);
736
737 post_options (pfile);
738
739 /* Mark named operators before handling command line macros. */
740 flags = 0;
741 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
742 flags |= NODE_OPERATOR;
743 if (CPP_OPTION (pfile, warn_cxx_operator_names))
744 flags |= NODE_DIAGNOSTIC | NODE_WARN_OPERATOR;
745 if (flags != 0)
746 mark_named_operators (pfile, flags);
747}
748
749/* Setup for processing input from the file named FNAME, or stdin if
750 it is the empty string. Return the original filename on success
751 (e.g. foo.i->foo.c), or NULL on failure. INJECTING is true if
752 there may be injected headers before line 1 of the main file. */
753const char *
754cpp_read_main_file (cpp_reader *pfile, const char *fname, bool injecting)
755{
756 if (mkdeps *deps = cpp_get_deps (pfile))
757 /* Set the default target (if there is none already). */
758 deps_add_default_target (deps, fname);
759
760 auto main_search = CPP_OPTION (pfile, main_search);
761 bool angle = main_search == CMS_system;
762 cpp_dir *start_dir = (main_search < CMS_user ? &pfile->no_search_path
763 : search_path_head (pfile, fname, angle, IT_CMDLINE));
764 pfile->main_file
765 = _cpp_find_file (pfile, fname, start_dir, angle, _cpp_FFK_NORMAL, 0);
766
767 if (_cpp_find_failed (pfile->main_file))
768 return NULL;
769
770 _cpp_stack_file (pfile, pfile->main_file,
771 injecting || CPP_OPTION (pfile, preprocessed)
772 ? IT_PRE_MAIN : IT_MAIN, 0);
773
774 /* For foo.i, read the original filename foo.c now, for the benefit
775 of the front ends. */
776 if (CPP_OPTION (pfile, preprocessed))
777 if (!read_original_filename (pfile))
778 {
779 /* We're on line 1 after all. */
780 auto *last = linemap_check_ordinary
781 (map: LINEMAPS_LAST_MAP (set: pfile->line_table, map_kind: false));
782 last->to_line = 1;
783 /* Inform of as-if a file change. */
784 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, LINEMAP_FILE (ord_map: last),
785 LINEMAP_LINE (ord_map: last), LINEMAP_SYSP (ord_map: last));
786 }
787
788 auto *map = LINEMAPS_LAST_ORDINARY_MAP (set: pfile->line_table);
789 pfile->main_loc = MAP_START_LOCATION (map);
790
791 return ORDINARY_MAP_FILE_NAME (ord_map: map);
792}
793
794location_t
795cpp_main_loc (const cpp_reader *pfile)
796{
797 return pfile->main_loc;
798}
799
800/* For preprocessed files, if the very first characters are
801 '#<SPACE>[01]<SPACE>', then handle a line directive so we know the
802 original file name. This will generate file_change callbacks,
803 which the front ends must handle appropriately given their state of
804 initialization. We peek directly into the character buffer, so
805 that we're not confused by otherwise-skipped white space &
806 comments. We can be very picky, because this should have been
807 machine-generated text (by us, no less). This way we do not
808 interfere with the module directive state machine. */
809
810static bool
811read_original_filename (cpp_reader *pfile)
812{
813 auto *buf = pfile->buffer->next_line;
814
815 if (pfile->buffer->rlimit - buf > 4
816 && buf[0] == '#'
817 && buf[1] == ' '
818 // Also permit '1', as that's what used to be here
819 && (buf[2] == '0' || buf[2] == '1')
820 && buf[3] == ' ')
821 {
822 const cpp_token *token = _cpp_lex_direct (pfile);
823 gcc_checking_assert (token->type == CPP_HASH);
824 if (_cpp_handle_directive (pfile, token->flags & PREV_WHITE))
825 {
826 read_original_directory (pfile);
827
828 auto *penult = &linemap_check_ordinary
829 (map: LINEMAPS_LAST_MAP (set: pfile->line_table, map_kind: false))[-1];
830 if (penult[1].reason == LC_RENAME_VERBATIM)
831 {
832 /* Expunge any evidence of the original linemap. */
833 pfile->line_table->highest_location
834 = pfile->line_table->highest_line
835 = penult[0].start_location;
836
837 penult[1].start_location = penult[0].start_location;
838 penult[1].reason = penult[0].reason;
839 penult[0] = penult[1];
840 pfile->line_table->info_ordinary.used--;
841 pfile->line_table->info_ordinary.m_cache = 0;
842 }
843
844 return true;
845 }
846 }
847
848 return false;
849}
850
851/* For preprocessed files, if the tokens following the first filename
852 line is of the form # <line> "/path/name//", handle the
853 directive so we know the original current directory.
854
855 As with the first line peeking, we can do this without lexing by
856 being picky. */
857static void
858read_original_directory (cpp_reader *pfile)
859{
860 auto *buf = pfile->buffer->next_line;
861
862 if (pfile->buffer->rlimit - buf > 4
863 && buf[0] == '#'
864 && buf[1] == ' '
865 // Also permit '1', as that's what used to be here
866 && (buf[2] == '0' || buf[2] == '1')
867 && buf[3] == ' ')
868 {
869 const cpp_token *hash = _cpp_lex_direct (pfile);
870 gcc_checking_assert (hash->type == CPP_HASH);
871 pfile->state.in_directive = 1;
872 const cpp_token *number = _cpp_lex_direct (pfile);
873 gcc_checking_assert (number->type == CPP_NUMBER);
874 const cpp_token *string = _cpp_lex_direct (pfile);
875 pfile->state.in_directive = 0;
876
877 const unsigned char *text = nullptr;
878 size_t len = 0;
879 if (string->type == CPP_STRING)
880 {
881 /* The string value includes the quotes. */
882 text = string->val.str.text;
883 len = string->val.str.len;
884 }
885 if (len < 5
886 || !IS_DIR_SEPARATOR (text[len - 2])
887 || !IS_DIR_SEPARATOR (text[len - 3]))
888 {
889 /* That didn't work out, back out. */
890 _cpp_backup_tokens (pfile, 3);
891 return;
892 }
893
894 if (pfile->cb.dir_change)
895 {
896 cpp_string s = { .len: 0, .text: 0 };
897 const char *dir_slashslash;
898 unsigned int dir_slashslash_len;
899
900 /* If we fail to decode escape sequences in the string literal, fall
901 back onto the literal itself, manually removing the opening and
902 closing quotes ("). */
903 if (cpp_interpret_string_notranslate (pfile, &string->val.str, 1, &s,
904 CPP_STRING))
905 {
906 /* At this point, the trailing NUL byte in S is included in its
907 length, so take it out. */
908 dir_slashslash = (const char *) s.text;
909 dir_slashslash_len = s.len - 1;
910 }
911 else
912 {
913 dir_slashslash = (const char *) string->val.str.text + 1;
914 dir_slashslash_len = string->val.str.len - 2;
915 }
916
917 /* Strip the trailing double slash. */
918 const unsigned dir_len = dir_slashslash_len - 2;
919 char *dir = (char *) alloca (dir_len + 1);
920 memcpy (dest: dir, src: dir_slashslash, n: dir_len);
921 dir[dir_len] = '\0';
922
923 pfile->cb.dir_change (pfile, dir);
924 }
925
926 /* We should be at EOL. */
927 }
928}
929
930/* This is called at the end of preprocessing. It pops the last
931 buffer and writes dependency output.
932
933 Maybe it should also reset state, such that you could call
934 cpp_start_read with a new filename to restart processing. */
935void
936cpp_finish (struct cpp_reader *pfile, FILE *deps_stream, FILE *fdeps_stream)
937{
938 /* Warn about unused macros before popping the final buffer. */
939 if (CPP_OPTION (pfile, warn_unused_macros))
940 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
941
942 /* lex.cc leaves the final buffer on the stack. This it so that
943 it returns an unending stream of CPP_EOFs to the client. If we
944 popped the buffer, we'd dereference a NULL buffer pointer and
945 segfault. It's nice to allow the client to do worry-free excess
946 cpp_get_token calls. */
947 while (pfile->buffer)
948 _cpp_pop_buffer (pfile);
949
950 cpp_fdeps_format fdeps_format = CPP_OPTION (pfile, deps.fdeps_format);
951 if (fdeps_format == FDEPS_FMT_P1689R5 && fdeps_stream)
952 deps_write_p1689r5 (pfile->deps, fdeps_stream);
953
954 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
955 && deps_stream)
956 {
957 deps_write (pfile, deps_stream, 72);
958 }
959
960 /* Report on headers that could use multiple include guards. */
961 if (CPP_OPTION (pfile, print_include_names))
962 _cpp_report_missing_guards (pfile);
963}
964
965static void
966post_options (cpp_reader *pfile)
967{
968 /* -Wtraditional is not useful in C++ mode. */
969 if (CPP_OPTION (pfile, cplusplus))
970 CPP_OPTION (pfile, cpp_warn_traditional) = 0;
971
972 /* Permanently disable macro expansion if we are rescanning
973 preprocessed text. Read preprocesed source in ISO mode. */
974 if (CPP_OPTION (pfile, preprocessed))
975 {
976 if (!CPP_OPTION (pfile, directives_only))
977 pfile->state.prevent_expansion = 1;
978 CPP_OPTION (pfile, traditional) = 0;
979 }
980
981 if (CPP_OPTION (pfile, warn_trigraphs) == 2)
982 CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
983
984 if (CPP_OPTION (pfile, traditional))
985 {
986 CPP_OPTION (pfile, trigraphs) = 0;
987 CPP_OPTION (pfile, warn_trigraphs) = 0;
988 }
989
990 if (CPP_OPTION (pfile, module_directives))
991 {
992 /* These unspellable tokens have a leading space. */
993 const char *const inits[spec_nodes::M_HWM]
994 = {"export ", "module ", "import ", "__import"};
995
996 for (int ix = 0; ix != spec_nodes::M_HWM; ix++)
997 {
998 cpp_hashnode *node = cpp_lookup (pfile, UC (inits[ix]),
999 strlen (s: inits[ix]));
1000
1001 /* Token we pass to the compiler. */
1002 pfile->spec_nodes.n_modules[ix][1] = node;
1003
1004 if (ix != spec_nodes::M__IMPORT)
1005 /* Token we recognize when lexing, drop the trailing ' '. */
1006 node = cpp_lookup (pfile, NODE_NAME (node), NODE_LEN (node) - 1);
1007
1008 node->flags |= NODE_MODULE;
1009 pfile->spec_nodes.n_modules[ix][0] = node;
1010 }
1011 }
1012}
1013

source code of libcpp/init.cc