1/* CPP Library.
2 Copyright (C) 1986-2025 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, warn_builtin_macro_redefined) = 1;
250 CPP_OPTION (pfile, cpp_warn_implicit_fallthrough) = 0;
251 CPP_OPTION (pfile, warn_header_guard) = 0;
252 /* By default, track locations of tokens resulting from macro
253 expansion. The '2' means, track the locations with the highest
254 accuracy. Read the comments for struct
255 cpp_options::track_macro_expansion to learn about the other
256 values. */
257 CPP_OPTION (pfile, track_macro_expansion) = 2;
258 CPP_OPTION (pfile, warn_normalize) = normalized_C;
259 CPP_OPTION (pfile, warn_literal_suffix) = 1;
260 CPP_OPTION (pfile, canonical_system_headers)
261 = ENABLE_CANONICAL_SYSTEM_HEADERS;
262 CPP_OPTION (pfile, ext_numeric_literals) = 1;
263 CPP_OPTION (pfile, warn_date_time) = 0;
264 CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
265 CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
266 CPP_OPTION (pfile, cpp_warn_unicode) = 1;
267 CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
268 CPP_OPTION (pfile, cpp_tabstop) = 8;
269
270 /* Default CPP arithmetic to something sensible for the host for the
271 benefit of dumb users like fix-header. */
272 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
273 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
274 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
275 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
276 CPP_OPTION (pfile, unsigned_char) = 0;
277 CPP_OPTION (pfile, unsigned_wchar) = 1;
278 CPP_OPTION (pfile, unsigned_utf8char) = 1;
279 CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */
280
281 /* Default to no charset conversion. */
282 CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
283 CPP_OPTION (pfile, wide_charset) = 0;
284
285 /* Default the input character set to UTF-8. */
286 CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
287
288 /* A fake empty "directory" used as the starting point for files
289 looked up without a search path. Name cannot be '/' because we
290 don't want to prepend anything at all to filenames using it. All
291 other entries are correct zero-initialized. */
292 pfile->no_search_path.name = (char *) "";
293
294 /* Initialize the line map. */
295 pfile->line_table = line_table;
296
297 /* Initialize lexer state. */
298 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
299
300 /* Set up static tokens. */
301 pfile->avoid_paste.type = CPP_PADDING;
302 pfile->avoid_paste.val.source = NULL;
303 pfile->avoid_paste.src_loc = 0;
304 pfile->endarg.type = CPP_EOF;
305 pfile->endarg.flags = 0;
306 pfile->endarg.src_loc = 0;
307
308 /* Create a token buffer for the lexer. */
309 _cpp_init_tokenrun (&pfile->base_run, 250);
310 pfile->cur_run = &pfile->base_run;
311 pfile->cur_token = pfile->base_run.base;
312
313 /* Initialize the base context. */
314 pfile->context = &pfile->base_context;
315 pfile->base_context.c.macro = 0;
316 pfile->base_context.prev = pfile->base_context.next = 0;
317
318 /* Aligned and unaligned storage. */
319 pfile->a_buff = _cpp_get_buff (pfile, 0);
320 pfile->u_buff = _cpp_get_buff (pfile, 0);
321
322 /* Initialize table for push_macro/pop_macro. */
323 pfile->pushed_macros = 0;
324
325 /* Do not force token locations by default. */
326 pfile->forced_token_location = 0;
327
328 /* Note the timestamp is unset. */
329 pfile->time_stamp = time_t (-1);
330 pfile->time_stamp_kind = 0;
331
332 /* The expression parser stack. */
333 _cpp_expand_op_stack (pfile);
334
335 /* Initialize the buffer obstack. */
336 obstack_specify_allocation (&pfile->buffer_ob, 0, 0, xmalloc, free);
337
338 _cpp_init_files (pfile);
339
340 _cpp_init_hashtable (pfile, table, extra_table);
341
342 return pfile;
343}
344
345/* Set the line_table entry in PFILE. This is called after reading a
346 PCH file, as the old line_table will be incorrect. */
347void
348cpp_set_line_map (cpp_reader *pfile, class line_maps *line_table)
349{
350 pfile->line_table = line_table;
351}
352
353/* Free resources used by PFILE. Accessing PFILE after this function
354 returns leads to undefined behavior. Returns the error count. */
355void
356cpp_destroy (cpp_reader *pfile)
357{
358 cpp_context *context, *contextn;
359 struct def_pragma_macro *pmacro;
360 tokenrun *run, *runn;
361 int i;
362
363 free (ptr: pfile->op_stack);
364
365 while (CPP_BUFFER (pfile) != NULL)
366 _cpp_pop_buffer (pfile);
367
368 free (ptr: pfile->out.base);
369
370 if (pfile->macro_buffer)
371 {
372 free (ptr: pfile->macro_buffer);
373 pfile->macro_buffer = NULL;
374 pfile->macro_buffer_len = 0;
375 }
376
377 if (pfile->deps)
378 deps_free (pfile->deps);
379 obstack_free (&pfile->buffer_ob, 0);
380
381 _cpp_destroy_hashtable (pfile);
382 _cpp_cleanup_files (pfile);
383 _cpp_destroy_iconv (pfile);
384
385 _cpp_free_buff (pfile->a_buff);
386 _cpp_free_buff (pfile->u_buff);
387 _cpp_free_buff (pfile->free_buffs);
388
389 for (run = &pfile->base_run; run; run = runn)
390 {
391 runn = run->next;
392 free (ptr: run->base);
393 if (run != &pfile->base_run)
394 free (ptr: run);
395 }
396
397 for (context = pfile->base_context.next; context; context = contextn)
398 {
399 contextn = context->next;
400 free (ptr: context);
401 }
402
403 if (pfile->comments.entries)
404 {
405 for (i = 0; i < pfile->comments.count; i++)
406 free (ptr: pfile->comments.entries[i].comment);
407
408 free (ptr: pfile->comments.entries);
409 }
410 if (pfile->pushed_macros)
411 {
412 do
413 {
414 pmacro = pfile->pushed_macros;
415 pfile->pushed_macros = pmacro->next;
416 free (ptr: pmacro->name);
417 free (ptr: pmacro);
418 }
419 while (pfile->pushed_macros);
420 }
421
422 free (ptr: pfile);
423}
424
425/* This structure defines one built-in identifier. A node will be
426 entered in the hash table under the name NAME, with value VALUE.
427
428 There are two tables of these. builtin_array holds all the
429 "builtin" macros: these are handled by builtin_macro() in
430 macro.cc. Builtin is somewhat of a misnomer -- the property of
431 interest is that these macros require special code to compute their
432 expansions. The value is a "cpp_builtin_type" enumerator.
433
434 operator_array holds the C++ named operators. These are keywords
435 which act as aliases for punctuators. In C++, they cannot be
436 altered through #define, and #if recognizes them as operators. In
437 C, these are not entered into the hash table at all (but see
438 <iso646.h>). The value is a token-type enumerator. */
439struct builtin_macro
440{
441 const uchar *const name;
442 const unsigned short len;
443 const unsigned short value;
444 const bool always_warn_if_redefined;
445};
446
447#define B(n, t, f) { DSC(n), t, f }
448static const struct builtin_macro builtin_array[] =
449{
450 B("__TIMESTAMP__", BT_TIMESTAMP, false),
451 B("__TIME__", BT_TIME, false),
452 B("__DATE__", BT_DATE, false),
453 B("__FILE__", BT_FILE, false),
454 B("__FILE_NAME__", BT_FILE_NAME, false),
455 B("__BASE_FILE__", BT_BASE_FILE, false),
456 B("__LINE__", BT_SPECLINE, true),
457 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
458 B("__COUNTER__", BT_COUNTER, true),
459 /* Make sure to update the list of built-in
460 function-like macros in traditional.cc:
461 fun_like_macro() when adding more following */
462 B("__has_attribute", BT_HAS_ATTRIBUTE, true),
463 B("__has_c_attribute", BT_HAS_STD_ATTRIBUTE, true),
464 B("__has_cpp_attribute", BT_HAS_ATTRIBUTE, true),
465 B("__has_builtin", BT_HAS_BUILTIN, true),
466 B("__has_include", BT_HAS_INCLUDE, true),
467 B("__has_include_next",BT_HAS_INCLUDE_NEXT, true),
468 B("__has_embed", BT_HAS_EMBED, true),
469 B("__has_feature", BT_HAS_FEATURE, true),
470 B("__has_extension", BT_HAS_EXTENSION, true),
471 /* Keep builtins not used for -traditional-cpp at the end, and
472 update init_builtins() if any more are added. */
473 B("_Pragma", BT_PRAGMA, true),
474 B("__STDC__", BT_STDC, true),
475};
476#undef B
477
478struct builtin_operator
479{
480 const uchar *const name;
481 const unsigned short len;
482 const unsigned short value;
483};
484
485#define B(n, t) { DSC(n), t }
486static const struct builtin_operator operator_array[] =
487{
488 B("and", CPP_AND_AND),
489 B("and_eq", CPP_AND_EQ),
490 B("bitand", CPP_AND),
491 B("bitor", CPP_OR),
492 B("compl", CPP_COMPL),
493 B("not", CPP_NOT),
494 B("not_eq", CPP_NOT_EQ),
495 B("or", CPP_OR_OR),
496 B("or_eq", CPP_OR_EQ),
497 B("xor", CPP_XOR),
498 B("xor_eq", CPP_XOR_EQ)
499};
500#undef B
501
502/* Mark the C++ named operators in the hash table. */
503static void
504mark_named_operators (cpp_reader *pfile, int flags)
505{
506 const struct builtin_operator *b;
507
508 for (b = operator_array;
509 b < (operator_array + ARRAY_SIZE (operator_array));
510 b++)
511 {
512 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
513 hp->flags |= flags;
514 hp->is_directive = 0;
515 hp->directive_index = b->value;
516 }
517}
518
519/* Helper function of cpp_type2name. Return the string associated with
520 named operator TYPE. */
521const char *
522cpp_named_operator2name (enum cpp_ttype type)
523{
524 const struct builtin_operator *b;
525
526 for (b = operator_array;
527 b < (operator_array + ARRAY_SIZE (operator_array));
528 b++)
529 {
530 if (type == b->value)
531 return (const char *) b->name;
532 }
533
534 return NULL;
535}
536
537void
538cpp_init_special_builtins (cpp_reader *pfile)
539{
540 const struct builtin_macro *b;
541 size_t n = ARRAY_SIZE (builtin_array);
542
543 if (CPP_OPTION (pfile, traditional))
544 n -= 2;
545 else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
546 || CPP_OPTION (pfile, std))
547 n--;
548
549 for (b = builtin_array; b < builtin_array + n; b++)
550 {
551 if ((b->value == BT_HAS_ATTRIBUTE
552 || b->value == BT_HAS_STD_ATTRIBUTE
553 || b->value == BT_HAS_BUILTIN)
554 && (CPP_OPTION (pfile, lang) == CLK_ASM
555 || pfile->cb.has_attribute == NULL))
556 continue;
557 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
558 hp->type = NT_BUILTIN_MACRO;
559 if (b->always_warn_if_redefined)
560 hp->flags |= NODE_WARN;
561 hp->value.builtin = (enum cpp_builtin_type) b->value;
562 }
563}
564
565/* Restore macro C to builtin macro definition. */
566
567void
568_cpp_restore_special_builtin (cpp_reader *pfile, struct def_pragma_macro *c)
569{
570 size_t len = strlen (s: c->name);
571
572 for (const struct builtin_macro *b = builtin_array;
573 b < builtin_array + ARRAY_SIZE (builtin_array); b++)
574 if (b->len == len && memcmp (s1: c->name, s2: b->name, n: len + 1) == 0)
575 {
576 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
577 hp->type = NT_BUILTIN_MACRO;
578 if (b->always_warn_if_redefined)
579 hp->flags |= NODE_WARN;
580 hp->value.builtin = (enum cpp_builtin_type) b->value;
581 }
582}
583
584/* Read the builtins table above and enter them, and language-specific
585 macros, into the hash table. HOSTED is true if this is a hosted
586 environment. */
587void
588cpp_init_builtins (cpp_reader *pfile, int hosted)
589{
590 cpp_init_special_builtins (pfile);
591
592 if (!CPP_OPTION (pfile, traditional)
593 && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
594 || CPP_OPTION (pfile, std)))
595 _cpp_define_builtin (pfile, "__STDC__ 1");
596
597 if (CPP_OPTION (pfile, cplusplus))
598 {
599 /* C++26 is not yet a standard. For now, use an invalid
600 year/month, 202400L, which is larger than 202302L. */
601 if (CPP_OPTION (pfile, lang) == CLK_CXX26
602 || CPP_OPTION (pfile, lang) == CLK_GNUCXX26)
603 _cpp_define_builtin (pfile, "__cplusplus 202400L");
604 else if (CPP_OPTION (pfile, lang) == CLK_CXX23
605 || CPP_OPTION (pfile, lang) == CLK_GNUCXX23)
606 _cpp_define_builtin (pfile, "__cplusplus 202302L");
607 else if (CPP_OPTION (pfile, lang) == CLK_CXX20
608 || CPP_OPTION (pfile, lang) == CLK_GNUCXX20)
609 _cpp_define_builtin (pfile, "__cplusplus 202002L");
610 else if (CPP_OPTION (pfile, lang) == CLK_CXX17
611 || CPP_OPTION (pfile, lang) == CLK_GNUCXX17)
612 _cpp_define_builtin (pfile, "__cplusplus 201703L");
613 else if (CPP_OPTION (pfile, lang) == CLK_CXX14
614 || CPP_OPTION (pfile, lang) == CLK_GNUCXX14)
615 _cpp_define_builtin (pfile, "__cplusplus 201402L");
616 else if (CPP_OPTION (pfile, lang) == CLK_CXX11
617 || CPP_OPTION (pfile, lang) == CLK_GNUCXX11)
618 _cpp_define_builtin (pfile, "__cplusplus 201103L");
619 else
620 _cpp_define_builtin (pfile, "__cplusplus 199711L");
621 }
622 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
623 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
624 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
625 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
626 else if (CPP_OPTION (pfile, lang) == CLK_STDC23
627 || CPP_OPTION (pfile, lang) == CLK_GNUC23)
628 _cpp_define_builtin (pfile, "__STDC_VERSION__ 202311L");
629 else if (CPP_OPTION (pfile, lang) == CLK_STDC2Y
630 || CPP_OPTION (pfile, lang) == CLK_GNUC2Y)
631 _cpp_define_builtin (pfile, "__STDC_VERSION__ 202500L");
632 else if (CPP_OPTION (pfile, lang) == CLK_STDC17
633 || CPP_OPTION (pfile, lang) == CLK_GNUC17)
634 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201710L");
635 else if (CPP_OPTION (pfile, lang) == CLK_STDC11
636 || CPP_OPTION (pfile, lang) == CLK_GNUC11)
637 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201112L");
638 else if (CPP_OPTION (pfile, c99))
639 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
640
641 if (CPP_OPTION (pfile, uliterals)
642 && !(CPP_OPTION (pfile, cplusplus)
643 && (CPP_OPTION (pfile, lang) == CLK_GNUCXX
644 || CPP_OPTION (pfile, lang) == CLK_CXX98)))
645 {
646 _cpp_define_builtin (pfile, "__STDC_UTF_16__ 1");
647 _cpp_define_builtin (pfile, "__STDC_UTF_32__ 1");
648 }
649
650 if (hosted)
651 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
652 else
653 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
654
655 _cpp_define_builtin (pfile, "__STDC_EMBED_NOT_FOUND__ 0");
656 _cpp_define_builtin (pfile, "__STDC_EMBED_FOUND__ 1");
657 _cpp_define_builtin (pfile, "__STDC_EMBED_EMPTY__ 2");
658
659 if (CPP_OPTION (pfile, objc))
660 _cpp_define_builtin (pfile, "__OBJC__ 1");
661}
662
663/* Sanity-checks are dependent on command-line options, so it is
664 called as a subroutine of cpp_read_main_file. */
665#if CHECKING_P
666static void sanity_checks (cpp_reader *);
667static void sanity_checks (cpp_reader *pfile)
668{
669 cppchar_t test = 0;
670 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
671
672 /* Sanity checks for assumptions about CPP arithmetic and target
673 type precisions made by cpplib. */
674 test--;
675 if (test < 1)
676 cpp_error (pfile, CPP_DL_ICE, msgid: "%<cppchar_t%> must be an unsigned type");
677
678 if (CPP_OPTION (pfile, precision) > max_precision)
679 cpp_error (pfile, CPP_DL_ICE,
680 msgid: "preprocessor arithmetic has maximum precision of %lu bits;"
681 " target requires %lu bits",
682 (unsigned long) max_precision,
683 (unsigned long) CPP_OPTION (pfile, precision));
684
685 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
686 cpp_error (pfile, CPP_DL_ICE,
687 msgid: "CPP arithmetic must be at least as precise as a target "
688 "%<int%>");
689
690 if (CPP_OPTION (pfile, char_precision) < 8)
691 cpp_error (pfile, CPP_DL_ICE, msgid: "target %<char%> is less than 8 bits wide");
692
693 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
694 cpp_error (pfile, CPP_DL_ICE,
695 msgid: "target %<wchar_t%> is narrower than target %<char%>");
696
697 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
698 cpp_error (pfile, CPP_DL_ICE,
699 msgid: "target %<int%> is narrower than target %<char%>");
700
701 /* This is assumed in eval_token() and could be fixed if necessary. */
702 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
703 cpp_error (pfile, CPP_DL_ICE,
704 msgid: "CPP half-integer narrower than CPP character");
705
706 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
707 cpp_error (pfile, CPP_DL_ICE,
708 msgid: "CPP on this host cannot handle wide character constants over"
709 " %lu bits, but the target requires %lu bits",
710 (unsigned long) BITS_PER_CPPCHAR_T,
711 (unsigned long) CPP_OPTION (pfile, wchar_precision));
712}
713#else
714# define sanity_checks(PFILE)
715#endif
716
717/* This is called after options have been parsed, and partially
718 processed. */
719void
720cpp_post_options (cpp_reader *pfile)
721{
722 int flags;
723
724 sanity_checks (pfile);
725
726 post_options (pfile);
727
728 /* Mark named operators before handling command line macros. */
729 flags = 0;
730 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
731 flags |= NODE_OPERATOR;
732 if (CPP_OPTION (pfile, warn_cxx_operator_names))
733 flags |= NODE_DIAGNOSTIC | NODE_WARN_OPERATOR;
734 if (flags != 0)
735 mark_named_operators (pfile, flags);
736}
737
738/* Setup for processing input from the file named FNAME, or stdin if
739 it is the empty string. Return the original filename on success
740 (e.g. foo.i->foo.c), or NULL on failure. INJECTING is true if
741 there may be injected headers before line 1 of the main file. */
742const char *
743cpp_read_main_file (cpp_reader *pfile, const char *fname, bool injecting)
744{
745 if (mkdeps *deps = cpp_get_deps (pfile))
746 /* Set the default target (if there is none already). */
747 deps_add_default_target (deps, fname);
748
749 auto main_search = CPP_OPTION (pfile, main_search);
750 bool angle = main_search == CMS_system;
751 cpp_dir *start_dir = (main_search < CMS_user ? &pfile->no_search_path
752 : search_path_head (pfile, fname, angle, IT_CMDLINE));
753 pfile->main_file
754 = _cpp_find_file (pfile, fname, start_dir, angle, _cpp_FFK_NORMAL, 0);
755
756 if (_cpp_find_failed (pfile->main_file))
757 return NULL;
758
759 _cpp_stack_file (pfile, pfile->main_file,
760 injecting || CPP_OPTION (pfile, preprocessed)
761 ? IT_PRE_MAIN : IT_MAIN, 0);
762
763 /* For foo.i, read the original filename foo.c now, for the benefit
764 of the front ends. */
765 if (CPP_OPTION (pfile, preprocessed))
766 if (!read_original_filename (pfile))
767 {
768 /* We're on line 1 after all. */
769 auto *last = linemap_check_ordinary
770 (map: LINEMAPS_LAST_MAP (set: pfile->line_table, map_kind: false));
771 last->to_line = 1;
772 /* Inform of as-if a file change. */
773 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, LINEMAP_FILE (ord_map: last),
774 LINEMAP_LINE (ord_map: last), LINEMAP_SYSP (ord_map: last));
775 }
776
777 auto *map = LINEMAPS_LAST_ORDINARY_MAP (set: pfile->line_table);
778 pfile->main_loc = MAP_START_LOCATION (map);
779
780 return ORDINARY_MAP_FILE_NAME (ord_map: map);
781}
782
783location_t
784cpp_main_loc (const cpp_reader *pfile)
785{
786 return pfile->main_loc;
787}
788
789/* For preprocessed files, if the very first characters are
790 '#<SPACE>[01]<SPACE>', then handle a line directive so we know the
791 original file name. This will generate file_change callbacks,
792 which the front ends must handle appropriately given their state of
793 initialization. We peek directly into the character buffer, so
794 that we're not confused by otherwise-skipped white space &
795 comments. We can be very picky, because this should have been
796 machine-generated text (by us, no less). This way we do not
797 interfere with the module directive state machine. */
798
799static bool
800read_original_filename (cpp_reader *pfile)
801{
802 auto *buf = pfile->buffer->next_line;
803
804 if (pfile->buffer->rlimit - buf > 4
805 && buf[0] == '#'
806 && buf[1] == ' '
807 // Also permit '1', as that's what used to be here
808 && (buf[2] == '0' || buf[2] == '1')
809 && buf[3] == ' ')
810 {
811 const cpp_token *token = _cpp_lex_direct (pfile);
812 gcc_checking_assert (token->type == CPP_HASH);
813 if (_cpp_handle_directive (pfile, token->flags & PREV_WHITE))
814 {
815 read_original_directory (pfile);
816
817 auto *penult = &linemap_check_ordinary
818 (map: LINEMAPS_LAST_MAP (set: pfile->line_table, map_kind: false))[-1];
819 if (penult[1].reason == LC_RENAME_VERBATIM)
820 {
821 /* Expunge any evidence of the original linemap. */
822 pfile->line_table->highest_location
823 = pfile->line_table->highest_line
824 = penult[0].start_location;
825
826 penult[1].start_location = penult[0].start_location;
827 penult[1].reason = penult[0].reason;
828 penult[0] = penult[1];
829 pfile->line_table->info_ordinary.used--;
830 pfile->line_table->info_ordinary.m_cache = 0;
831 }
832
833 return true;
834 }
835 }
836
837 return false;
838}
839
840/* For preprocessed files, if the tokens following the first filename
841 line is of the form # <line> "/path/name//", handle the
842 directive so we know the original current directory.
843
844 As with the first line peeking, we can do this without lexing by
845 being picky. */
846static void
847read_original_directory (cpp_reader *pfile)
848{
849 auto *buf = pfile->buffer->next_line;
850
851 if (pfile->buffer->rlimit - buf > 4
852 && buf[0] == '#'
853 && buf[1] == ' '
854 // Also permit '1', as that's what used to be here
855 && (buf[2] == '0' || buf[2] == '1')
856 && buf[3] == ' ')
857 {
858 const cpp_token *hash = _cpp_lex_direct (pfile);
859 gcc_checking_assert (hash->type == CPP_HASH);
860 pfile->state.in_directive = 1;
861 const cpp_token *number = _cpp_lex_direct (pfile);
862 gcc_checking_assert (number->type == CPP_NUMBER);
863 const cpp_token *string = _cpp_lex_direct (pfile);
864 pfile->state.in_directive = 0;
865
866 const unsigned char *text = nullptr;
867 size_t len = 0;
868 if (string->type == CPP_STRING)
869 {
870 /* The string value includes the quotes. */
871 text = string->val.str.text;
872 len = string->val.str.len;
873 }
874 if (len < 5
875 || !IS_DIR_SEPARATOR (text[len - 2])
876 || !IS_DIR_SEPARATOR (text[len - 3]))
877 {
878 /* That didn't work out, back out. */
879 _cpp_backup_tokens (pfile, 3);
880 return;
881 }
882
883 if (pfile->cb.dir_change)
884 {
885 /* Smash the string directly, it's dead at this point */
886 char *smashy = (char *)text;
887 smashy[len - 3] = 0;
888
889 pfile->cb.dir_change (pfile, smashy + 1);
890 }
891
892 /* We should be at EOL. */
893 }
894}
895
896/* This is called at the end of preprocessing. It pops the last
897 buffer and writes dependency output.
898
899 Maybe it should also reset state, such that you could call
900 cpp_start_read with a new filename to restart processing. */
901void
902cpp_finish (struct cpp_reader *pfile, FILE *deps_stream, FILE *fdeps_stream)
903{
904 /* Warn about unused macros before popping the final buffer. */
905 if (CPP_OPTION (pfile, warn_unused_macros))
906 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
907
908 /* lex.cc leaves the final buffer on the stack. This it so that
909 it returns an unending stream of CPP_EOFs to the client. If we
910 popped the buffer, we'd dereference a NULL buffer pointer and
911 segfault. It's nice to allow the client to do worry-free excess
912 cpp_get_token calls. */
913 while (pfile->buffer)
914 _cpp_pop_buffer (pfile);
915
916 cpp_fdeps_format fdeps_format = CPP_OPTION (pfile, deps.fdeps_format);
917 if (fdeps_format == FDEPS_FMT_P1689R5 && fdeps_stream)
918 deps_write_p1689r5 (pfile->deps, fdeps_stream);
919
920 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
921 && deps_stream)
922 {
923 deps_write (pfile, deps_stream, 72);
924 }
925
926 /* Report on headers that could use multiple include guards. */
927 if (CPP_OPTION (pfile, print_include_names))
928 _cpp_report_missing_guards (pfile);
929}
930
931static void
932post_options (cpp_reader *pfile)
933{
934 /* -Wtraditional is not useful in C++ mode. */
935 if (CPP_OPTION (pfile, cplusplus))
936 CPP_OPTION (pfile, cpp_warn_traditional) = 0;
937
938 /* Permanently disable macro expansion if we are rescanning
939 preprocessed text. Read preprocesed source in ISO mode. */
940 if (CPP_OPTION (pfile, preprocessed))
941 {
942 if (!CPP_OPTION (pfile, directives_only))
943 pfile->state.prevent_expansion = 1;
944 CPP_OPTION (pfile, traditional) = 0;
945 }
946
947 if (CPP_OPTION (pfile, warn_trigraphs) == 2)
948 CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
949
950 if (CPP_OPTION (pfile, traditional))
951 {
952 CPP_OPTION (pfile, trigraphs) = 0;
953 CPP_OPTION (pfile, warn_trigraphs) = 0;
954 }
955
956 if (CPP_OPTION (pfile, module_directives))
957 {
958 /* These unspellable tokens have a leading space. */
959 const char *const inits[spec_nodes::M_HWM]
960 = {"export ", "module ", "import ", "__import"};
961
962 for (int ix = 0; ix != spec_nodes::M_HWM; ix++)
963 {
964 cpp_hashnode *node = cpp_lookup (pfile, UC (inits[ix]),
965 strlen (s: inits[ix]));
966
967 /* Token we pass to the compiler. */
968 pfile->spec_nodes.n_modules[ix][1] = node;
969
970 if (ix != spec_nodes::M__IMPORT)
971 /* Token we recognize when lexing, drop the trailing ' '. */
972 node = cpp_lookup (pfile, NODE_NAME (node), NODE_LEN (node) - 1);
973
974 node->flags |= NODE_MODULE;
975 pfile->spec_nodes.n_modules[ix][0] = node;
976 }
977 }
978}
979

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of libcpp/init.cc