]>
Commit | Line | Data |
---|---|---|
a949941c | 1 | /* CPP Library. (Directive handling.) |
5e7b4e25 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
d9221e01 | 3 | 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
4c8cc616 | 4 | Contributed by Per Bothner, 1994-95. |
d8bfa78c | 5 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
956d6950 | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
7f2935c7 | 21 | |
956d6950 | 22 | #include "config.h" |
b04cd507 | 23 | #include "system.h" |
956d6950 | 24 | #include "cpplib.h" |
4f4e53dd | 25 | #include "internal.h" |
c6e83800 | 26 | #include "mkdeps.h" |
c71f835b | 27 | #include "obstack.h" |
7f2935c7 | 28 | |
93c80368 NB |
29 | /* Chained list of answers to an assertion. */ |
30 | struct answer | |
31 | { | |
32 | struct answer *next; | |
33 | unsigned int count; | |
34 | cpp_token first[1]; | |
35 | }; | |
36 | ||
88ae23e7 ZW |
37 | /* Stack of conditionals currently in progress |
38 | (including both successful and failing conditionals). */ | |
88ae23e7 ZW |
39 | struct if_stack |
40 | { | |
41 | struct if_stack *next; | |
50410426 | 42 | unsigned int line; /* Line where condition started. */ |
93c80368 | 43 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
cef0d199 NB |
44 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
45 | bool was_skipping; /* If were skipping on entry. */ | |
6cf87ca4 | 46 | int type; /* Most recent conditional for diagnostics. */ |
88ae23e7 | 47 | }; |
88ae23e7 | 48 | |
a5da89c6 | 49 | /* Contains a registered pragma or pragma namespace. */ |
6cf87ca4 | 50 | typedef void (*pragma_cb) (cpp_reader *); |
a5da89c6 NB |
51 | struct pragma_entry |
52 | { | |
53 | struct pragma_entry *next; | |
4b115ff0 | 54 | const cpp_hashnode *pragma; /* Name and length. */ |
a5da89c6 NB |
55 | int is_nspace; |
56 | union { | |
57 | pragma_cb handler; | |
58 | struct pragma_entry *space; | |
59 | } u; | |
60 | }; | |
61 | ||
93c80368 NB |
62 | /* Values for the origin field of struct directive. KANDR directives |
63 | come from traditional (K&R) C. STDC89 directives come from the | |
64 | 1989 C standard. EXTENSION directives are extensions. */ | |
65 | #define KANDR 0 | |
66 | #define STDC89 1 | |
67 | #define EXTENSION 2 | |
68 | ||
69 | /* Values for the flags field of struct directive. COND indicates a | |
70 | conditional; IF_COND an opening conditional. INCL means to treat | |
71 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
72 | means this directive should be handled even if -fpreprocessed is in | |
1a76916c NB |
73 | effect (these are the directives with callback hooks). |
74 | ||
d97371e0 | 75 | EXPAND is set on directives that are always macro-expanded. */ |
93c80368 NB |
76 | #define COND (1 << 0) |
77 | #define IF_COND (1 << 1) | |
78 | #define INCL (1 << 2) | |
79 | #define IN_I (1 << 3) | |
1a76916c | 80 | #define EXPAND (1 << 4) |
93c80368 NB |
81 | |
82 | /* Defines one #-directive, including how to handle it. */ | |
6cf87ca4 | 83 | typedef void (*directive_handler) (cpp_reader *); |
93c80368 NB |
84 | typedef struct directive directive; |
85 | struct directive | |
86 | { | |
87 | directive_handler handler; /* Function to handle directive. */ | |
562a5c27 | 88 | const uchar *name; /* Name of directive. */ |
93c80368 NB |
89 | unsigned short length; /* Length of name. */ |
90 | unsigned char origin; /* Origin of directive. */ | |
91 | unsigned char flags; /* Flags describing this directive. */ | |
92 | }; | |
93 | ||
1316f1f7 ZW |
94 | /* Forward declarations. */ |
95 | ||
6cf87ca4 ZW |
96 | static void skip_rest_of_line (cpp_reader *); |
97 | static void check_eol (cpp_reader *); | |
98 | static void start_directive (cpp_reader *); | |
99 | static void prepare_directive_trad (cpp_reader *); | |
100 | static void end_directive (cpp_reader *, int); | |
101 | static void directive_diagnostics (cpp_reader *, const directive *, int); | |
102 | static void run_directive (cpp_reader *, int, const char *, size_t); | |
103 | static char *glue_header_name (cpp_reader *); | |
104 | static const char *parse_include (cpp_reader *, int *); | |
105 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); | |
106 | static unsigned int read_flag (cpp_reader *, unsigned int); | |
6cf87ca4 ZW |
107 | static int strtoul_for_line (const uchar *, unsigned int, unsigned long *); |
108 | static void do_diagnostic (cpp_reader *, int, int); | |
109 | static cpp_hashnode *lex_macro_node (cpp_reader *); | |
d1bd0ded | 110 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
6cf87ca4 ZW |
111 | static void do_include_common (cpp_reader *, enum include_type); |
112 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, | |
113 | const cpp_hashnode *); | |
114 | static struct pragma_entry *insert_pragma_entry (cpp_reader *, | |
115 | struct pragma_entry **, | |
116 | const cpp_hashnode *, | |
117 | pragma_cb); | |
118 | static int count_registered_pragmas (struct pragma_entry *); | |
119 | static char ** save_registered_pragmas (struct pragma_entry *, char **); | |
120 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, | |
121 | char **); | |
122 | static void do_pragma_once (cpp_reader *); | |
123 | static void do_pragma_poison (cpp_reader *); | |
124 | static void do_pragma_system_header (cpp_reader *); | |
125 | static void do_pragma_dependency (cpp_reader *); | |
126 | static void do_linemarker (cpp_reader *); | |
127 | static const cpp_token *get_token_no_padding (cpp_reader *); | |
128 | static const cpp_token *get__Pragma_string (cpp_reader *); | |
129 | static void destringize_and_run (cpp_reader *, const cpp_string *); | |
130 | static int parse_answer (cpp_reader *, struct answer **, int); | |
131 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); | |
132 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); | |
133 | static void handle_assertion (cpp_reader *, const char *, int); | |
d481b69b | 134 | |
168d3732 ZW |
135 | /* This is the table of directive handlers. It is ordered by |
136 | frequency of occurrence; the numbers at the end are directive | |
137 | counts from all the source code I have lying around (egcs and libc | |
138 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
93c80368 NB |
139 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
140 | is now O(1). All extensions other than #warning and #include_next | |
141 | are deprecated. The name is where the extension appears to have | |
142 | come from. */ | |
168d3732 | 143 | |
93c80368 NB |
144 | #define DIRECTIVE_TABLE \ |
145 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
d97371e0 | 146 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
93c80368 NB |
147 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
148 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
1a76916c | 149 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
93c80368 NB |
150 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
151 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
152 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
1a76916c NB |
153 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
154 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
93c80368 NB |
155 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
156 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
157 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
d97371e0 | 158 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
93c80368 | 159 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
d97371e0 | 160 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
93c80368 NB |
161 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
162 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
74d06cf2 | 163 | D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */ |
07aa0b04 | 164 | |
168d3732 ZW |
165 | /* Use the table to generate a series of prototypes, an enum for the |
166 | directive names, and an array of directive handlers. */ | |
167 | ||
6cf87ca4 | 168 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
168d3732 ZW |
169 | DIRECTIVE_TABLE |
170 | #undef D | |
171 | ||
041c3194 | 172 | #define D(n, tag, o, f) tag, |
168d3732 ZW |
173 | enum |
174 | { | |
175 | DIRECTIVE_TABLE | |
176 | N_DIRECTIVES | |
7f2935c7 | 177 | }; |
168d3732 ZW |
178 | #undef D |
179 | ||
041c3194 | 180 | #define D(name, t, origin, flags) \ |
9a238586 KG |
181 | { do_##name, (const uchar *) #name, \ |
182 | sizeof #name - 1, origin, flags }, | |
93c80368 | 183 | static const directive dtable[] = |
168d3732 ZW |
184 | { |
185 | DIRECTIVE_TABLE | |
186 | }; | |
187 | #undef D | |
188 | #undef DIRECTIVE_TABLE | |
7f2935c7 | 189 | |
dcc229e5 ZW |
190 | /* Wrapper struct directive for linemarkers. |
191 | The origin is more or less true - the original K+R cpp | |
192 | did use this notation in its preprocessed output. */ | |
193 | static const directive linemarker_dir = | |
194 | { | |
195 | do_linemarker, U"#", 1, KANDR, IN_I | |
196 | }; | |
197 | ||
1a76916c | 198 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
67821e3a | 199 | |
93c80368 NB |
200 | /* Skip any remaining tokens in a directive. */ |
201 | static void | |
6cf87ca4 | 202 | skip_rest_of_line (cpp_reader *pfile) |
c5a04734 | 203 | { |
93c80368 | 204 | /* Discard all stacked contexts. */ |
8128cccf | 205 | while (pfile->context->prev) |
93c80368 NB |
206 | _cpp_pop_context (pfile); |
207 | ||
b528a07e | 208 | /* Sweep up all tokens remaining on the line. */ |
345894b4 NB |
209 | if (! SEEN_EOL ()) |
210 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
211 | ; | |
93c80368 | 212 | } |
c5a04734 | 213 | |
93c80368 NB |
214 | /* Ensure there are no stray tokens at the end of a directive. */ |
215 | static void | |
6cf87ca4 | 216 | check_eol (cpp_reader *pfile) |
93c80368 | 217 | { |
345894b4 | 218 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
0527bc4e | 219 | cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", |
ebef4e8c | 220 | pfile->directive->name); |
93c80368 NB |
221 | } |
222 | ||
fe6c2db9 NB |
223 | /* Called when entering a directive, _Pragma or command-line directive. */ |
224 | static void | |
6cf87ca4 | 225 | start_directive (cpp_reader *pfile) |
93c80368 | 226 | { |
7f2f1a66 NB |
227 | /* Setup in-directive state. */ |
228 | pfile->state.in_directive = 1; | |
229 | pfile->state.save_comments = 0; | |
230 | ||
93c80368 | 231 | /* Some handlers need the position of the # for diagnostics. */ |
500bee0a | 232 | pfile->directive_line = pfile->line_table->highest_line; |
fe6c2db9 NB |
233 | } |
234 | ||
235 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
236 | static void | |
6cf87ca4 | 237 | end_directive (cpp_reader *pfile, int skip_line) |
fe6c2db9 | 238 | { |
1a76916c NB |
239 | if (CPP_OPTION (pfile, traditional)) |
240 | { | |
d97371e0 NB |
241 | /* Revert change of prepare_directive_trad. */ |
242 | pfile->state.prevent_expansion--; | |
243 | ||
b66377c1 | 244 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c NB |
245 | _cpp_remove_overlay (pfile); |
246 | } | |
fe6c2db9 | 247 | /* We don't skip for an assembler #. */ |
b66377c1 | 248 | else if (skip_line) |
67821e3a NB |
249 | { |
250 | skip_rest_of_line (pfile); | |
bdcbe496 NB |
251 | if (!pfile->keep_tokens) |
252 | { | |
253 | pfile->cur_run = &pfile->base_run; | |
254 | pfile->cur_token = pfile->base_run.base; | |
255 | } | |
67821e3a | 256 | } |
fe6c2db9 NB |
257 | |
258 | /* Restore state. */ | |
fe6c2db9 NB |
259 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
260 | pfile->state.in_directive = 0; | |
d97371e0 | 261 | pfile->state.in_expression = 0; |
fe6c2db9 NB |
262 | pfile->state.angled_headers = 0; |
263 | pfile->directive = 0; | |
264 | } | |
265 | ||
1a76916c NB |
266 | /* Prepare to handle the directive in pfile->directive. */ |
267 | static void | |
6cf87ca4 | 268 | prepare_directive_trad (cpp_reader *pfile) |
1a76916c | 269 | { |
951a0766 | 270 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c | 271 | { |
b66377c1 NB |
272 | bool no_expand = (pfile->directive |
273 | && ! (pfile->directive->flags & EXPAND)); | |
974c43f1 | 274 | bool was_skipping = pfile->state.skipping; |
1a76916c | 275 | |
d97371e0 NB |
276 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
277 | || pfile->directive == &dtable[T_ELIF]); | |
45f2492c NB |
278 | if (pfile->state.in_expression) |
279 | pfile->state.skipping = false; | |
280 | ||
1a76916c NB |
281 | if (no_expand) |
282 | pfile->state.prevent_expansion++; | |
43839642 | 283 | _cpp_scan_out_logical_line (pfile, NULL); |
1a76916c NB |
284 | if (no_expand) |
285 | pfile->state.prevent_expansion--; | |
45f2492c | 286 | |
974c43f1 | 287 | pfile->state.skipping = was_skipping; |
1a76916c NB |
288 | _cpp_overlay_buffer (pfile, pfile->out.base, |
289 | pfile->out.cur - pfile->out.base); | |
290 | } | |
d97371e0 NB |
291 | |
292 | /* Stop ISO C from expanding anything. */ | |
293 | pfile->state.prevent_expansion++; | |
1a76916c NB |
294 | } |
295 | ||
da7d8304 | 296 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
18a9d8ff | 297 | the '#' was indented. */ |
18a9d8ff | 298 | static void |
6cf87ca4 | 299 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
18a9d8ff | 300 | { |
dcc229e5 ZW |
301 | /* Issue -pedantic warnings for extensions. */ |
302 | if (CPP_PEDANTIC (pfile) | |
303 | && ! pfile->state.skipping | |
304 | && dir->origin == EXTENSION) | |
0527bc4e | 305 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); |
dcc229e5 ZW |
306 | |
307 | /* Traditionally, a directive is ignored unless its # is in | |
308 | column 1. Therefore in code intended to work with K+R | |
309 | compilers, directives added by C89 must have their # | |
310 | indented, and directives present in traditional C must not. | |
311 | This is true even of directives in skipped conditional | |
312 | blocks. #elif cannot be used at all. */ | |
313 | if (CPP_WTRADITIONAL (pfile)) | |
18a9d8ff | 314 | { |
dcc229e5 | 315 | if (dir == &dtable[T_ELIF]) |
0527bc4e | 316 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 317 | "suggest not using #elif in traditional C"); |
dcc229e5 | 318 | else if (indented && dir->origin == KANDR) |
0527bc4e | 319 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c NB |
320 | "traditional C ignores #%s with the # indented", |
321 | dir->name); | |
dcc229e5 | 322 | else if (!indented && dir->origin != KANDR) |
0527bc4e | 323 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c NB |
324 | "suggest hiding #%s from traditional C with an indented #", |
325 | dir->name); | |
18a9d8ff NB |
326 | } |
327 | } | |
328 | ||
da7d8304 | 329 | /* Check if we have a known directive. INDENTED is nonzero if the |
18a9d8ff NB |
330 | '#' of the directive was indented. This function is in this file |
331 | to save unnecessarily exporting dtable etc. to cpplex.c. Returns | |
da7d8304 | 332 | nonzero if the line of tokens has been handled, zero if we should |
18a9d8ff | 333 | continue processing the line. */ |
fe6c2db9 | 334 | int |
6cf87ca4 | 335 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
fe6c2db9 | 336 | { |
fe6c2db9 | 337 | const directive *dir = 0; |
345894b4 | 338 | const cpp_token *dname; |
e808ec9c | 339 | bool was_parsing_args = pfile->state.parsing_args; |
c6e83800 | 340 | bool was_discarding_output = pfile->state.discarding_output; |
fe6c2db9 NB |
341 | int skip = 1; |
342 | ||
c6e83800 ZW |
343 | if (was_discarding_output) |
344 | pfile->state.prevent_expansion = 0; | |
345 | ||
e808ec9c NB |
346 | if (was_parsing_args) |
347 | { | |
348 | if (CPP_OPTION (pfile, pedantic)) | |
0527bc4e | 349 | cpp_error (pfile, CPP_DL_PEDWARN, |
e808ec9c NB |
350 | "embedding a directive within macro arguments is not portable"); |
351 | pfile->state.parsing_args = 0; | |
352 | pfile->state.prevent_expansion = 0; | |
353 | } | |
fe6c2db9 | 354 | start_directive (pfile); |
345894b4 | 355 | dname = _cpp_lex_token (pfile); |
0d9f234d | 356 | |
345894b4 | 357 | if (dname->type == CPP_NAME) |
0d9f234d | 358 | { |
4977bab6 ZW |
359 | if (dname->val.node->is_directive) |
360 | dir = &dtable[dname->val.node->directive_index]; | |
0d9f234d | 361 | } |
05713b80 | 362 | /* We do not recognize the # followed by a number extension in |
18a9d8ff | 363 | assembler code. */ |
345894b4 | 364 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
0d9f234d | 365 | { |
dcc229e5 ZW |
366 | dir = &linemarker_dir; |
367 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
368 | && ! pfile->state.skipping) | |
0527bc4e | 369 | cpp_error (pfile, CPP_DL_PEDWARN, |
ebef4e8c | 370 | "style of line directive is a GCC extension"); |
93c80368 | 371 | } |
0d9f234d | 372 | |
93c80368 NB |
373 | if (dir) |
374 | { | |
18a9d8ff NB |
375 | /* If we have a directive that is not an opening conditional, |
376 | invalidate any control macro. */ | |
377 | if (! (dir->flags & IF_COND)) | |
378 | pfile->mi_valid = false; | |
379 | ||
380 | /* Kluge alert. In order to be sure that code like this | |
381 | ||
382 | #define HASH # | |
383 | HASH define foo bar | |
384 | ||
385 | does not cause '#define foo bar' to get executed when | |
386 | compiled with -save-temps, we recognize directives in | |
387 | -fpreprocessed mode only if the # is in column 1. cppmacro.c | |
a1f300c0 | 388 | puts a space in front of any '#' at the start of a macro. */ |
18a9d8ff NB |
389 | if (CPP_OPTION (pfile, preprocessed) |
390 | && (indented || !(dir->flags & IN_I))) | |
6d4587f7 | 391 | { |
18a9d8ff NB |
392 | skip = 0; |
393 | dir = 0; | |
6d4587f7 ZW |
394 | } |
395 | else | |
93c80368 | 396 | { |
18a9d8ff NB |
397 | /* In failed conditional groups, all non-conditional |
398 | directives are ignored. Before doing that, whether | |
399 | skipping or not, we should lex angle-bracketed headers | |
400 | correctly, and maybe output some diagnostics. */ | |
401 | pfile->state.angled_headers = dir->flags & INCL; | |
a8d0ddaf | 402 | pfile->state.directive_wants_padding = dir->flags & INCL; |
18a9d8ff NB |
403 | if (! CPP_OPTION (pfile, preprocessed)) |
404 | directive_diagnostics (pfile, dir, indented); | |
405 | if (pfile->state.skipping && !(dir->flags & COND)) | |
406 | dir = 0; | |
93c80368 NB |
407 | } |
408 | } | |
345894b4 | 409 | else if (dname->type == CPP_EOF) |
18a9d8ff NB |
410 | ; /* CPP_EOF is the "null directive". */ |
411 | else | |
93c80368 NB |
412 | { |
413 | /* An unknown directive. Don't complain about it in assembly | |
414 | source: we don't know where the comments are, and # may | |
415 | introduce assembler pseudo-ops. Don't complain about invalid | |
416 | directives in skipped conditional groups (6.10 p4). */ | |
bdb05a7b | 417 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
18a9d8ff NB |
418 | skip = 0; |
419 | else if (!pfile->state.skipping) | |
0527bc4e | 420 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
345894b4 | 421 | cpp_token_as_text (pfile, dname)); |
0d9f234d NB |
422 | } |
423 | ||
d1a58688 NB |
424 | pfile->directive = dir; |
425 | if (CPP_OPTION (pfile, traditional)) | |
426 | prepare_directive_trad (pfile); | |
427 | ||
18a9d8ff | 428 | if (dir) |
6cf87ca4 | 429 | pfile->directive->handler (pfile); |
18a9d8ff NB |
430 | else if (skip == 0) |
431 | _cpp_backup_tokens (pfile, 1); | |
432 | ||
433 | end_directive (pfile, skip); | |
e808ec9c NB |
434 | if (was_parsing_args) |
435 | { | |
436 | /* Restore state when within macro args. */ | |
437 | pfile->state.parsing_args = 2; | |
438 | pfile->state.prevent_expansion = 1; | |
e808ec9c | 439 | } |
c6e83800 ZW |
440 | if (was_discarding_output) |
441 | pfile->state.prevent_expansion = 1; | |
fe6c2db9 | 442 | return skip; |
041c3194 | 443 | } |
7f2935c7 | 444 | |
93c80368 | 445 | /* Directive handler wrapper used by the command line option |
26aea073 | 446 | processor. BUF is \n terminated. */ |
93c80368 | 447 | static void |
6cf87ca4 | 448 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
3fdc651f | 449 | { |
562a5c27 | 450 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
40de9f76 | 451 | /* from_stage3 */ true); |
8bfb1467 NB |
452 | /* Disgusting hack. */ |
453 | if (dir_no == T_PRAGMA) | |
8f9b4009 | 454 | pfile->buffer->file = pfile->buffer->prev->file; |
0bda4760 | 455 | start_directive (pfile); |
26aea073 NB |
456 | |
457 | /* This is a short-term fix to prevent a leading '#' being | |
458 | interpreted as a directive. */ | |
459 | _cpp_clean_line (pfile); | |
460 | ||
f71aebba | 461 | pfile->directive = &dtable[dir_no]; |
1a76916c NB |
462 | if (CPP_OPTION (pfile, traditional)) |
463 | prepare_directive_trad (pfile); | |
6cf87ca4 | 464 | pfile->directive->handler (pfile); |
0bda4760 | 465 | end_directive (pfile, 1); |
8bfb1467 | 466 | if (dir_no == T_PRAGMA) |
8f9b4009 | 467 | pfile->buffer->file = NULL; |
ef6e958a | 468 | _cpp_pop_buffer (pfile); |
93c80368 NB |
469 | } |
470 | ||
471 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
472 | #ifndef directives. */ | |
041c3194 | 473 | static cpp_hashnode * |
6cf87ca4 | 474 | lex_macro_node (cpp_reader *pfile) |
041c3194 | 475 | { |
1a76916c | 476 | const cpp_token *token = _cpp_lex_token (pfile); |
ea4a453b | 477 | |
92936ecf | 478 | /* The token immediately after #define must be an identifier. That |
b8363a24 ZW |
479 | identifier may not be "defined", per C99 6.10.8p4. |
480 | In C++, it may not be any of the "named operators" either, | |
481 | per C++98 [lex.digraph], [lex.key]. | |
482 | Finally, the identifier may not have been poisoned. (In that case | |
1d63a28a | 483 | the lexer has issued the error message for us.) */ |
cbc69f84 | 484 | |
1a76916c NB |
485 | if (token->type == CPP_NAME) |
486 | { | |
487 | cpp_hashnode *node = token->val.node; | |
92936ecf | 488 | |
1a76916c | 489 | if (node == pfile->spec_nodes.n_defined) |
0527bc4e | 490 | cpp_error (pfile, CPP_DL_ERROR, |
1a76916c NB |
491 | "\"defined\" cannot be used as a macro name"); |
492 | else if (! (node->flags & NODE_POISONED)) | |
493 | return node; | |
ba89d661 | 494 | } |
1a76916c | 495 | else if (token->flags & NAMED_OP) |
0527bc4e | 496 | cpp_error (pfile, CPP_DL_ERROR, |
cbc69f84 | 497 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
1a76916c NB |
498 | NODE_NAME (token->val.node)); |
499 | else if (token->type == CPP_EOF) | |
0527bc4e | 500 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
1a76916c NB |
501 | pfile->directive->name); |
502 | else | |
0527bc4e | 503 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
07aa0b04 | 504 | |
cbc69f84 | 505 | return NULL; |
7f2935c7 | 506 | } |
7f2935c7 | 507 | |
93c80368 | 508 | /* Process a #define directive. Most work is done in cppmacro.c. */ |
711b8824 | 509 | static void |
6cf87ca4 | 510 | do_define (cpp_reader *pfile) |
7f2935c7 | 511 | { |
93c80368 | 512 | cpp_hashnode *node = lex_macro_node (pfile); |
ff2b53ef | 513 | |
93c80368 NB |
514 | if (node) |
515 | { | |
1d63a28a NB |
516 | /* If we have been requested to expand comments into macros, |
517 | then re-enable saving of comments. */ | |
518 | pfile->state.save_comments = | |
519 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
520 | ||
93c80368 NB |
521 | if (_cpp_create_definition (pfile, node)) |
522 | if (pfile->cb.define) | |
6cf87ca4 | 523 | pfile->cb.define (pfile, pfile->directive_line, node); |
93c80368 | 524 | } |
041c3194 ZW |
525 | } |
526 | ||
5d8ebbd8 | 527 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
711b8824 | 528 | static void |
6cf87ca4 | 529 | do_undef (cpp_reader *pfile) |
041c3194 | 530 | { |
df383483 | 531 | cpp_hashnode *node = lex_macro_node (pfile); |
9e62c811 | 532 | |
45f2492c | 533 | if (node) |
9e62c811 | 534 | { |
58fea6af | 535 | if (pfile->cb.undef) |
6cf87ca4 | 536 | pfile->cb.undef (pfile, pfile->directive_line, node); |
9e62c811 | 537 | |
45f2492c NB |
538 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
539 | identifier is not currently defined as a macro name. */ | |
540 | if (node->type == NT_MACRO) | |
541 | { | |
542 | if (node->flags & NODE_WARN) | |
543 | cpp_error (pfile, CPP_DL_WARNING, | |
544 | "undefining \"%s\"", NODE_NAME (node)); | |
9e62c811 | 545 | |
45f2492c NB |
546 | if (CPP_OPTION (pfile, warn_unused_macros)) |
547 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
a69cbaac | 548 | |
45f2492c NB |
549 | _cpp_free_definition (node); |
550 | } | |
9e62c811 | 551 | } |
45f2492c | 552 | |
93c80368 | 553 | check_eol (pfile); |
7f2935c7 PB |
554 | } |
555 | ||
d1bd0ded GK |
556 | /* Undefine a single macro/assertion/whatever. */ |
557 | ||
558 | static int | |
c6e83800 | 559 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
d1bd0ded GK |
560 | void *data_p ATTRIBUTE_UNUSED) |
561 | { | |
c6e83800 ZW |
562 | /* Body of _cpp_free_definition inlined here for speed. |
563 | Macros and assertions no longer have anything to free. */ | |
564 | h->type = NT_VOID; | |
565 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED); | |
d1bd0ded GK |
566 | return 1; |
567 | } | |
568 | ||
569 | /* Undefine all macros and assertions. */ | |
570 | ||
571 | void | |
572 | cpp_undef_all (cpp_reader *pfile) | |
573 | { | |
574 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
575 | } | |
576 | ||
577 | ||
93c80368 NB |
578 | /* Helper routine used by parse_include. Reinterpret the current line |
579 | as an h-char-sequence (< ... >); we are looking at the first token | |
74eb4b3e NB |
580 | after the <. Returns a malloced filename. */ |
581 | static char * | |
6cf87ca4 | 582 | glue_header_name (cpp_reader *pfile) |
93c80368 | 583 | { |
4ed5bcfb | 584 | const cpp_token *token; |
74eb4b3e | 585 | char *buffer; |
2450e0b8 | 586 | size_t len, total_len = 0, capacity = 1024; |
93c80368 NB |
587 | |
588 | /* To avoid lexed tokens overwriting our glued name, we can only | |
589 | allocate from the string pool once we've lexed everything. */ | |
74eb4b3e | 590 | buffer = xmalloc (capacity); |
93c80368 NB |
591 | for (;;) |
592 | { | |
a8d0ddaf | 593 | token = get_token_no_padding (pfile); |
93c80368 | 594 | |
74eb4b3e | 595 | if (token->type == CPP_GREATER) |
93c80368 | 596 | break; |
74eb4b3e NB |
597 | if (token->type == CPP_EOF) |
598 | { | |
0527bc4e | 599 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
74eb4b3e NB |
600 | break; |
601 | } | |
93c80368 | 602 | |
59325650 | 603 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
2450e0b8 | 604 | if (total_len + len > capacity) |
93c80368 | 605 | { |
2450e0b8 | 606 | capacity = (capacity + len) * 2; |
74eb4b3e | 607 | buffer = xrealloc (buffer, capacity); |
93c80368 NB |
608 | } |
609 | ||
4ed5bcfb | 610 | if (token->flags & PREV_WHITE) |
2450e0b8 | 611 | buffer[total_len++] = ' '; |
93c80368 | 612 | |
74eb4b3e NB |
613 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len]) |
614 | - (uchar *) buffer); | |
93c80368 | 615 | } |
041c3194 | 616 | |
74eb4b3e NB |
617 | buffer[total_len] = '\0'; |
618 | return buffer; | |
93c80368 | 619 | } |
7f2935c7 | 620 | |
74eb4b3e NB |
621 | /* Returns the file name of #include, #include_next, #import and |
622 | #pragma dependency. The string is malloced and the caller should | |
623 | free it. Returns NULL on error. */ | |
624 | static const char * | |
6cf87ca4 | 625 | parse_include (cpp_reader *pfile, int *pangle_brackets) |
7f2935c7 | 626 | { |
74eb4b3e | 627 | char *fname; |
4ed5bcfb | 628 | const cpp_token *header; |
7f2935c7 | 629 | |
93c80368 | 630 | /* Allow macro expansion. */ |
a8d0ddaf | 631 | header = get_token_no_padding (pfile); |
74eb4b3e | 632 | if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME) |
7f2935c7 | 633 | { |
6338b358 NB |
634 | fname = xmalloc (header->val.str.len - 1); |
635 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); | |
636 | fname[header->val.str.len - 2] = '\0'; | |
74eb4b3e | 637 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
7f2935c7 | 638 | } |
74eb4b3e | 639 | else if (header->type == CPP_LESS) |
7f2935c7 | 640 | { |
74eb4b3e NB |
641 | fname = glue_header_name (pfile); |
642 | *pangle_brackets = 1; | |
643 | } | |
644 | else | |
645 | { | |
646 | const unsigned char *dir; | |
647 | ||
648 | if (pfile->directive == &dtable[T_PRAGMA]) | |
649 | dir = U"pragma dependency"; | |
650 | else | |
651 | dir = pfile->directive->name; | |
0527bc4e | 652 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
74eb4b3e NB |
653 | dir); |
654 | ||
4ed5bcfb | 655 | return NULL; |
7f2935c7 | 656 | } |
7f2935c7 | 657 | |
3963c2e0 | 658 | check_eol (pfile); |
74eb4b3e | 659 | return fname; |
168d3732 | 660 | } |
3caee4a8 | 661 | |
ba133c96 | 662 | /* Handle #include, #include_next and #import. */ |
711b8824 | 663 | static void |
6cf87ca4 | 664 | do_include_common (cpp_reader *pfile, enum include_type type) |
168d3732 | 665 | { |
74eb4b3e NB |
666 | const char *fname; |
667 | int angle_brackets; | |
668 | ||
669 | fname = parse_include (pfile, &angle_brackets); | |
670 | if (!fname) | |
3963c2e0 | 671 | return; |
7f2935c7 | 672 | |
3963c2e0 | 673 | /* Prevent #include recursion. */ |
50f59cd7 | 674 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
0527bc4e | 675 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
74eb4b3e | 676 | else |
09b82253 | 677 | { |
74eb4b3e NB |
678 | /* Get out of macro context, if we are. */ |
679 | skip_rest_of_line (pfile); | |
09b82253 | 680 | |
74eb4b3e | 681 | if (pfile->cb.include) |
6cf87ca4 ZW |
682 | pfile->cb.include (pfile, pfile->directive_line, |
683 | pfile->directive->name, fname, angle_brackets); | |
3963c2e0 | 684 | |
8f9b4009 | 685 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
74eb4b3e | 686 | } |
3963c2e0 | 687 | |
fad205ff | 688 | free ((void *) fname); |
168d3732 | 689 | } |
e8037d57 | 690 | |
711b8824 | 691 | static void |
6cf87ca4 | 692 | do_include (cpp_reader *pfile) |
168d3732 | 693 | { |
ba133c96 NB |
694 | do_include_common (pfile, IT_INCLUDE); |
695 | } | |
168d3732 | 696 | |
ba133c96 | 697 | static void |
6cf87ca4 | 698 | do_import (cpp_reader *pfile) |
ba133c96 | 699 | { |
ba133c96 | 700 | do_include_common (pfile, IT_IMPORT); |
168d3732 | 701 | } |
7f2935c7 | 702 | |
711b8824 | 703 | static void |
6cf87ca4 | 704 | do_include_next (cpp_reader *pfile) |
168d3732 | 705 | { |
3963c2e0 ZW |
706 | enum include_type type = IT_INCLUDE_NEXT; |
707 | ||
708 | /* If this is the primary source file, warn and use the normal | |
709 | search logic. */ | |
710 | if (! pfile->buffer->prev) | |
711 | { | |
0527bc4e | 712 | cpp_error (pfile, CPP_DL_WARNING, |
3963c2e0 ZW |
713 | "#include_next in primary source file"); |
714 | type = IT_INCLUDE; | |
715 | } | |
716 | do_include_common (pfile, type); | |
7f2935c7 | 717 | } |
7f2935c7 | 718 | |
dcc229e5 ZW |
719 | /* Subroutine of do_linemarker. Read possible flags after file name. |
720 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
721 | flag if it is valid, 0 at the end of the directive. Otherwise | |
722 | complain. */ | |
642ce434 | 723 | static unsigned int |
6cf87ca4 | 724 | read_flag (cpp_reader *pfile, unsigned int last) |
d3a34a0a | 725 | { |
345894b4 | 726 | const cpp_token *token = _cpp_lex_token (pfile); |
d3a34a0a | 727 | |
345894b4 | 728 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
d3a34a0a | 729 | { |
345894b4 | 730 | unsigned int flag = token->val.str.text[0] - '0'; |
28e0f040 NB |
731 | |
732 | if (flag > last && flag <= 4 | |
733 | && (flag != 4 || last == 3) | |
734 | && (flag != 2 || last == 0)) | |
735 | return flag; | |
d3a34a0a | 736 | } |
93c80368 | 737 | |
345894b4 | 738 | if (token->type != CPP_EOF) |
0527bc4e | 739 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
345894b4 | 740 | cpp_token_as_text (pfile, token)); |
93c80368 | 741 | return 0; |
d3a34a0a JM |
742 | } |
743 | ||
dcc229e5 ZW |
744 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
745 | of length LEN, to binary; store it in NUMP, and return 0 if the | |
746 | number was well-formed, 1 if not. Temporary, hopefully. */ | |
041c3194 | 747 | static int |
6cf87ca4 | 748 | strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump) |
041c3194 ZW |
749 | { |
750 | unsigned long reg = 0; | |
562a5c27 | 751 | uchar c; |
041c3194 ZW |
752 | while (len--) |
753 | { | |
754 | c = *str++; | |
755 | if (!ISDIGIT (c)) | |
756 | return 1; | |
757 | reg *= 10; | |
758 | reg += c - '0'; | |
759 | } | |
760 | *nump = reg; | |
761 | return 0; | |
762 | } | |
763 | ||
6de1e2a9 | 764 | /* Interpret #line command. |
dcc229e5 ZW |
765 | Note that the filename string (if any) is a true string constant |
766 | (escapes are interpreted), unlike in #line. */ | |
711b8824 | 767 | static void |
6cf87ca4 | 768 | do_line (cpp_reader *pfile) |
7f2935c7 | 769 | { |
500bee0a PB |
770 | const struct line_maps *line_table = pfile->line_table; |
771 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
4ed5bcfb | 772 | const cpp_token *token; |
12f9df4e | 773 | const char *new_file = map->to_file; |
bb74c963 | 774 | unsigned long new_lineno; |
93c80368 | 775 | |
27e2564a | 776 | /* C99 raised the minimum limit on #line numbers. */ |
dcc229e5 | 777 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
18a9d8ff | 778 | |
93c80368 | 779 | /* #line commands expand macros. */ |
4ed5bcfb NB |
780 | token = cpp_get_token (pfile); |
781 | if (token->type != CPP_NUMBER | |
782 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
783 | &new_lineno)) | |
7f2935c7 | 784 | { |
0527bc4e | 785 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 786 | "\"%s\" after #line is not a positive integer", |
4ed5bcfb | 787 | cpp_token_as_text (pfile, token)); |
9ec7291f | 788 | return; |
df383483 | 789 | } |
7f2935c7 | 790 | |
dcc229e5 | 791 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
0527bc4e | 792 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
7f2935c7 | 793 | |
4ed5bcfb NB |
794 | token = cpp_get_token (pfile); |
795 | if (token->type == CPP_STRING) | |
5538ada6 | 796 | { |
e6cc3a24 | 797 | cpp_string s = { 0, 0 }; |
423e95e2 EC |
798 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
799 | &s, false)) | |
e6cc3a24 | 800 | new_file = (const char *)s.text; |
dcc229e5 ZW |
801 | check_eol (pfile); |
802 | } | |
803 | else if (token->type != CPP_EOF) | |
804 | { | |
0527bc4e | 805 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
dcc229e5 ZW |
806 | cpp_token_as_text (pfile, token)); |
807 | return; | |
808 | } | |
809 | ||
810 | skip_rest_of_line (pfile); | |
811 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, | |
12f9df4e | 812 | map->sysp); |
dcc229e5 ZW |
813 | } |
814 | ||
815 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
816 | different syntax and semantics from #line: Flags are allowed, | |
817 | and we never complain about the line number being too big. */ | |
818 | static void | |
6cf87ca4 | 819 | do_linemarker (cpp_reader *pfile) |
dcc229e5 | 820 | { |
500bee0a PB |
821 | const struct line_maps *line_table = pfile->line_table; |
822 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
dcc229e5 | 823 | const cpp_token *token; |
12f9df4e | 824 | const char *new_file = map->to_file; |
dcc229e5 | 825 | unsigned long new_lineno; |
12f9df4e | 826 | unsigned int new_sysp = map->sysp; |
dcc229e5 ZW |
827 | enum lc_reason reason = LC_RENAME; |
828 | int flag; | |
829 | ||
830 | /* Back up so we can get the number again. Putting this in | |
831 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
832 | some circumstances, which can segfault. */ | |
833 | _cpp_backup_tokens (pfile, 1); | |
834 | ||
835 | /* #line commands expand macros. */ | |
836 | token = cpp_get_token (pfile); | |
837 | if (token->type != CPP_NUMBER | |
838 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
839 | &new_lineno)) | |
840 | { | |
0527bc4e JDA |
841 | cpp_error (pfile, CPP_DL_ERROR, |
842 | "\"%s\" after # is not a positive integer", | |
dcc229e5 ZW |
843 | cpp_token_as_text (pfile, token)); |
844 | return; | |
df383483 | 845 | } |
941e09b6 | 846 | |
dcc229e5 ZW |
847 | token = cpp_get_token (pfile); |
848 | if (token->type == CPP_STRING) | |
849 | { | |
e6cc3a24 | 850 | cpp_string s = { 0, 0 }; |
423e95e2 EC |
851 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
852 | 1, &s, false)) | |
e6cc3a24 | 853 | new_file = (const char *)s.text; |
cf551fba | 854 | |
dcc229e5 ZW |
855 | new_sysp = 0; |
856 | flag = read_flag (pfile, 0); | |
857 | if (flag == 1) | |
858 | { | |
859 | reason = LC_ENTER; | |
860 | /* Fake an include for cpp_included (). */ | |
861 | _cpp_fake_include (pfile, new_file); | |
862 | flag = read_flag (pfile, flag); | |
863 | } | |
864 | else if (flag == 2) | |
865 | { | |
866 | reason = LC_LEAVE; | |
867 | flag = read_flag (pfile, flag); | |
868 | } | |
869 | if (flag == 3) | |
93c80368 | 870 | { |
dcc229e5 ZW |
871 | new_sysp = 1; |
872 | flag = read_flag (pfile, flag); | |
873 | if (flag == 4) | |
874 | new_sysp = 2; | |
12f9df4e | 875 | pfile->buffer->sysp = new_sysp; |
93c80368 | 876 | } |
dcc229e5 | 877 | |
fde84349 | 878 | check_eol (pfile); |
041c3194 | 879 | } |
4ed5bcfb | 880 | else if (token->type != CPP_EOF) |
27e2564a | 881 | { |
0527bc4e | 882 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
4ed5bcfb | 883 | cpp_token_as_text (pfile, token)); |
27e2564a NB |
884 | return; |
885 | } | |
7f2935c7 | 886 | |
bdcbe496 | 887 | skip_rest_of_line (pfile); |
bb74c963 | 888 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
27e2564a NB |
889 | } |
890 | ||
67821e3a | 891 | /* Arrange the file_change callback. pfile->line has changed to |
47d89cf3 | 892 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
a1f300c0 | 893 | header, 2 for a system header that needs to be extern "C" protected, |
47d89cf3 | 894 | and zero otherwise. */ |
eb1f4d9d | 895 | void |
6cf87ca4 ZW |
896 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
897 | const char *to_file, unsigned int file_line, | |
898 | unsigned int sysp) | |
27e2564a | 899 | { |
12f9df4e PB |
900 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
901 | to_file, file_line); | |
500bee0a PB |
902 | if (map != NULL) |
903 | linemap_line_start (pfile->line_table, map->to_line, 127); | |
d82fc108 | 904 | |
eb1f4d9d | 905 | if (pfile->cb.file_change) |
12f9df4e | 906 | pfile->cb.file_change (pfile, map); |
7f2935c7 | 907 | } |
941e09b6 | 908 | |
5d8ebbd8 NB |
909 | /* Report a warning or error detected by the program we are |
910 | processing. Use the directive's tokens in the error message. */ | |
711b8824 | 911 | static void |
6cf87ca4 | 912 | do_diagnostic (cpp_reader *pfile, int code, int print_dir) |
7f2935c7 | 913 | { |
12f9df4e | 914 | if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0)) |
58fea6af | 915 | { |
29b10746 NB |
916 | if (print_dir) |
917 | fprintf (stderr, "#%s ", pfile->directive->name); | |
93c80368 NB |
918 | pfile->state.prevent_expansion++; |
919 | cpp_output_line (pfile, stderr); | |
920 | pfile->state.prevent_expansion--; | |
58fea6af | 921 | } |
7f2935c7 PB |
922 | } |
923 | ||
838f313b | 924 | static void |
6cf87ca4 | 925 | do_error (cpp_reader *pfile) |
838f313b | 926 | { |
0527bc4e | 927 | do_diagnostic (pfile, CPP_DL_ERROR, 1); |
838f313b | 928 | } |
7f2935c7 | 929 | |
711b8824 | 930 | static void |
6cf87ca4 | 931 | do_warning (cpp_reader *pfile) |
7f2935c7 | 932 | { |
2f878973 | 933 | /* We want #warning diagnostics to be emitted in system headers too. */ |
0527bc4e | 934 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); |
7f2935c7 PB |
935 | } |
936 | ||
a2a76ce7 | 937 | /* Report program identification. */ |
711b8824 | 938 | static void |
6cf87ca4 | 939 | do_ident (cpp_reader *pfile) |
7f2935c7 | 940 | { |
4ed5bcfb | 941 | const cpp_token *str = cpp_get_token (pfile); |
58fea6af | 942 | |
4ed5bcfb | 943 | if (str->type != CPP_STRING) |
0527bc4e | 944 | cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive"); |
93c80368 | 945 | else if (pfile->cb.ident) |
6cf87ca4 | 946 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
a2a76ce7 | 947 | |
93c80368 | 948 | check_eol (pfile); |
7f2935c7 PB |
949 | } |
950 | ||
a5da89c6 NB |
951 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
952 | matching entry, or NULL if none is found. The returned entry could | |
953 | be the start of a namespace chain, or a pragma. */ | |
954 | static struct pragma_entry * | |
6cf87ca4 | 955 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
82443371 | 956 | { |
4b115ff0 NB |
957 | while (chain && chain->pragma != pragma) |
958 | chain = chain->next; | |
a5da89c6 NB |
959 | |
960 | return chain; | |
961 | } | |
962 | ||
963 | /* Create and insert a pragma entry for NAME at the beginning of a | |
964 | singly-linked CHAIN. If handler is NULL, it is a namespace, | |
965 | otherwise it is a pragma and its handler. */ | |
966 | static struct pragma_entry * | |
6cf87ca4 ZW |
967 | insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain, |
968 | const cpp_hashnode *pragma, pragma_cb handler) | |
82443371 | 969 | { |
a5da89c6 | 970 | struct pragma_entry *new; |
58fea6af | 971 | |
bef985f3 | 972 | new = (struct pragma_entry *) |
8c3b2693 | 973 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
4b115ff0 | 974 | new->pragma = pragma; |
a5da89c6 NB |
975 | if (handler) |
976 | { | |
977 | new->is_nspace = 0; | |
978 | new->u.handler = handler; | |
979 | } | |
980 | else | |
981 | { | |
982 | new->is_nspace = 1; | |
983 | new->u.space = NULL; | |
984 | } | |
58fea6af | 985 | |
a5da89c6 NB |
986 | new->next = *chain; |
987 | *chain = new; | |
988 | return new; | |
58fea6af | 989 | } |
82443371 | 990 | |
a5da89c6 NB |
991 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
992 | goes in the global namespace. HANDLER is the handler it will call, | |
993 | which must be non-NULL. */ | |
58fea6af | 994 | void |
6cf87ca4 ZW |
995 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, |
996 | pragma_cb handler) | |
82443371 | 997 | { |
a5da89c6 NB |
998 | struct pragma_entry **chain = &pfile->pragmas; |
999 | struct pragma_entry *entry; | |
4b115ff0 | 1000 | const cpp_hashnode *node; |
a5da89c6 NB |
1001 | |
1002 | if (!handler) | |
1003 | abort (); | |
58fea6af | 1004 | |
a5da89c6 | 1005 | if (space) |
58fea6af | 1006 | { |
4b115ff0 NB |
1007 | node = cpp_lookup (pfile, U space, strlen (space)); |
1008 | entry = lookup_pragma_entry (*chain, node); | |
a5da89c6 | 1009 | if (!entry) |
4b115ff0 | 1010 | entry = insert_pragma_entry (pfile, chain, node, NULL); |
a5da89c6 NB |
1011 | else if (!entry->is_nspace) |
1012 | goto clash; | |
1013 | chain = &entry->u.space; | |
58fea6af ZW |
1014 | } |
1015 | ||
a5da89c6 | 1016 | /* Check for duplicates. */ |
4b115ff0 NB |
1017 | node = cpp_lookup (pfile, U name, strlen (name)); |
1018 | entry = lookup_pragma_entry (*chain, node); | |
a5da89c6 NB |
1019 | if (entry) |
1020 | { | |
1021 | if (entry->is_nspace) | |
1022 | clash: | |
0527bc4e | 1023 | cpp_error (pfile, CPP_DL_ICE, |
a5da89c6 | 1024 | "registering \"%s\" as both a pragma and a pragma namespace", |
4b115ff0 | 1025 | NODE_NAME (node)); |
a5da89c6 | 1026 | else if (space) |
0527bc4e | 1027 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", |
ebef4e8c | 1028 | space, name); |
a5da89c6 | 1029 | else |
0527bc4e | 1030 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
a5da89c6 NB |
1031 | } |
1032 | else | |
4b115ff0 | 1033 | insert_pragma_entry (pfile, chain, node, handler); |
58fea6af | 1034 | } |
a5da89c6 NB |
1035 | |
1036 | /* Register the pragmas the preprocessor itself handles. */ | |
58fea6af | 1037 | void |
6cf87ca4 | 1038 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
58fea6af | 1039 | { |
a5da89c6 | 1040 | /* Pragmas in the global namespace. */ |
58fea6af ZW |
1041 | cpp_register_pragma (pfile, 0, "once", do_pragma_once); |
1042 | ||
a5da89c6 | 1043 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
58fea6af ZW |
1044 | cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison); |
1045 | cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header); | |
1046 | cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency); | |
82443371 | 1047 | } |
7f2935c7 | 1048 | |
17211ab5 GK |
1049 | /* Return the number of registered pragmas in PE. */ |
1050 | ||
1051 | static int | |
6cf87ca4 | 1052 | count_registered_pragmas (struct pragma_entry *pe) |
17211ab5 GK |
1053 | { |
1054 | int ct = 0; | |
1055 | for (; pe != NULL; pe = pe->next) | |
1056 | { | |
1057 | if (pe->is_nspace) | |
1058 | ct += count_registered_pragmas (pe->u.space); | |
1059 | ct++; | |
1060 | } | |
1061 | return ct; | |
1062 | } | |
1063 | ||
1064 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1065 | and return a pointer to the next free space in SD. */ | |
1066 | ||
1067 | static char ** | |
6cf87ca4 | 1068 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
17211ab5 GK |
1069 | { |
1070 | for (; pe != NULL; pe = pe->next) | |
1071 | { | |
1072 | if (pe->is_nspace) | |
1073 | sd = save_registered_pragmas (pe->u.space, sd); | |
1074 | *sd++ = xmemdup (HT_STR (&pe->pragma->ident), | |
1075 | HT_LEN (&pe->pragma->ident), | |
1076 | HT_LEN (&pe->pragma->ident) + 1); | |
1077 | } | |
1078 | return sd; | |
1079 | } | |
1080 | ||
1081 | /* Return a newly-allocated array which saves the names of the | |
1082 | registered pragmas. */ | |
1083 | ||
1084 | char ** | |
6cf87ca4 | 1085 | _cpp_save_pragma_names (cpp_reader *pfile) |
17211ab5 GK |
1086 | { |
1087 | int ct = count_registered_pragmas (pfile->pragmas); | |
1088 | char **result = xnewvec (char *, ct); | |
1089 | (void) save_registered_pragmas (pfile->pragmas, result); | |
1090 | return result; | |
1091 | } | |
1092 | ||
1093 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1094 | and return a pointer to the next unused name in SD. */ | |
1095 | ||
1096 | static char ** | |
6cf87ca4 ZW |
1097 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1098 | char **sd) | |
17211ab5 GK |
1099 | { |
1100 | for (; pe != NULL; pe = pe->next) | |
1101 | { | |
1102 | if (pe->is_nspace) | |
1103 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
1104 | pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd)); | |
1105 | free (*sd); | |
1106 | sd++; | |
1107 | } | |
1108 | return sd; | |
1109 | } | |
1110 | ||
1111 | /* Restore the names of the registered pragmas from SAVED. */ | |
1112 | ||
1113 | void | |
6cf87ca4 | 1114 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
17211ab5 GK |
1115 | { |
1116 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1117 | free (saved); | |
1118 | } | |
1119 | ||
a5da89c6 NB |
1120 | /* Pragmata handling. We handle some, and pass the rest on to the |
1121 | front end. C99 defines three pragmas and says that no macro | |
1122 | expansion is to be performed on them; whether or not macro | |
1123 | expansion happens for other pragmas is implementation defined. | |
1124 | This implementation never macro-expands the text after #pragma. */ | |
711b8824 | 1125 | static void |
6cf87ca4 | 1126 | do_pragma (cpp_reader *pfile) |
7f2935c7 | 1127 | { |
a5da89c6 | 1128 | const struct pragma_entry *p = NULL; |
e2e1fa50 | 1129 | const cpp_token *token, *pragma_token = pfile->cur_token; |
a5da89c6 | 1130 | unsigned int count = 1; |
add7091b | 1131 | |
93c80368 | 1132 | pfile->state.prevent_expansion++; |
58fea6af | 1133 | |
4ed5bcfb NB |
1134 | token = cpp_get_token (pfile); |
1135 | if (token->type == CPP_NAME) | |
0172e2bc | 1136 | { |
4b115ff0 | 1137 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
a5da89c6 | 1138 | if (p && p->is_nspace) |
58fea6af | 1139 | { |
a5da89c6 NB |
1140 | count = 2; |
1141 | token = cpp_get_token (pfile); | |
1142 | if (token->type == CPP_NAME) | |
4b115ff0 | 1143 | p = lookup_pragma_entry (p->u.space, token->val.node); |
a5da89c6 NB |
1144 | else |
1145 | p = NULL; | |
58fea6af | 1146 | } |
58fea6af | 1147 | } |
041c3194 | 1148 | |
a5da89c6 | 1149 | if (p) |
e2e1fa50 AO |
1150 | { |
1151 | /* Since the handler below doesn't get the line number, that it | |
1152 | might need for diagnostics, make sure it has the right | |
1153 | numbers in place. */ | |
1154 | if (pfile->cb.line_change) | |
1155 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
1156 | (*p->u.handler) (pfile); | |
e2e1fa50 | 1157 | } |
d82fc108 | 1158 | else if (pfile->cb.def_pragma) |
bdcbe496 NB |
1159 | { |
1160 | _cpp_backup_tokens (pfile, count); | |
6cf87ca4 | 1161 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
bdcbe496 | 1162 | } |
a5da89c6 | 1163 | |
97293897 | 1164 | pfile->state.prevent_expansion--; |
82443371 NS |
1165 | } |
1166 | ||
5d8ebbd8 | 1167 | /* Handle #pragma once. */ |
58fea6af | 1168 | static void |
6cf87ca4 | 1169 | do_pragma_once (cpp_reader *pfile) |
a2a76ce7 | 1170 | { |
642ce434 | 1171 | if (pfile->buffer->prev == NULL) |
0527bc4e | 1172 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
93c80368 NB |
1173 | |
1174 | check_eol (pfile); | |
49634b3a | 1175 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
a2a76ce7 | 1176 | } |
fc009f96 | 1177 | |
c3bf3e6e NB |
1178 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1179 | that the lexer produces a hard error for each subsequent usage. */ | |
58fea6af | 1180 | static void |
6cf87ca4 | 1181 | do_pragma_poison (cpp_reader *pfile) |
a2a76ce7 | 1182 | { |
345894b4 | 1183 | const cpp_token *tok; |
f8f769ea | 1184 | cpp_hashnode *hp; |
a2a76ce7 | 1185 | |
93c80368 | 1186 | pfile->state.poisoned_ok = 1; |
a2a76ce7 ZW |
1187 | for (;;) |
1188 | { | |
345894b4 NB |
1189 | tok = _cpp_lex_token (pfile); |
1190 | if (tok->type == CPP_EOF) | |
a2a76ce7 | 1191 | break; |
345894b4 | 1192 | if (tok->type != CPP_NAME) |
fc009f96 | 1193 | { |
0527bc4e JDA |
1194 | cpp_error (pfile, CPP_DL_ERROR, |
1195 | "invalid #pragma GCC poison directive"); | |
93c80368 | 1196 | break; |
fc009f96 GK |
1197 | } |
1198 | ||
345894b4 | 1199 | hp = tok->val.node; |
93c80368 NB |
1200 | if (hp->flags & NODE_POISONED) |
1201 | continue; | |
1202 | ||
1203 | if (hp->type == NT_MACRO) | |
0527bc4e | 1204 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
ebef4e8c | 1205 | NODE_NAME (hp)); |
93c80368 NB |
1206 | _cpp_free_definition (hp); |
1207 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
fc009f96 | 1208 | } |
93c80368 | 1209 | pfile->state.poisoned_ok = 0; |
7f2935c7 | 1210 | } |
2c0b35cb ZW |
1211 | |
1212 | /* Mark the current header as a system header. This will suppress | |
1213 | some categories of warnings (notably those from -pedantic). It is | |
1214 | intended for use in system libraries that cannot be implemented in | |
1215 | conforming C, but cannot be certain that their headers appear in a | |
1216 | system include directory. To prevent abuse, it is rejected in the | |
1217 | primary source file. */ | |
58fea6af | 1218 | static void |
6cf87ca4 | 1219 | do_pragma_system_header (cpp_reader *pfile) |
2c0b35cb | 1220 | { |
614c7d37 NB |
1221 | cpp_buffer *buffer = pfile->buffer; |
1222 | ||
1223 | if (buffer->prev == 0) | |
0527bc4e | 1224 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 1225 | "#pragma system_header ignored outside include file"); |
2c0b35cb | 1226 | else |
d82fc108 NB |
1227 | { |
1228 | check_eol (pfile); | |
bdcbe496 | 1229 | skip_rest_of_line (pfile); |
d82fc108 NB |
1230 | cpp_make_system_header (pfile, 1, 0); |
1231 | } | |
2c0b35cb | 1232 | } |
f3f751ad NS |
1233 | |
1234 | /* Check the modified date of the current include file against a specified | |
1235 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1236 | determine if a fixed header should be refixed. */ | |
58fea6af | 1237 | static void |
6cf87ca4 | 1238 | do_pragma_dependency (cpp_reader *pfile) |
f3f751ad | 1239 | { |
74eb4b3e NB |
1240 | const char *fname; |
1241 | int angle_brackets, ordering; | |
df383483 | 1242 | |
74eb4b3e NB |
1243 | fname = parse_include (pfile, &angle_brackets); |
1244 | if (!fname) | |
58fea6af | 1245 | return; |
041c3194 | 1246 | |
74eb4b3e | 1247 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
f3f751ad | 1248 | if (ordering < 0) |
0527bc4e | 1249 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
f3f751ad NS |
1250 | else if (ordering > 0) |
1251 | { | |
0527bc4e JDA |
1252 | cpp_error (pfile, CPP_DL_WARNING, |
1253 | "current file is older than %s", fname); | |
4ed5bcfb | 1254 | if (cpp_get_token (pfile)->type != CPP_EOF) |
bdcbe496 NB |
1255 | { |
1256 | _cpp_backup_tokens (pfile, 1); | |
0527bc4e | 1257 | do_diagnostic (pfile, CPP_DL_WARNING, 0); |
bdcbe496 | 1258 | } |
f3f751ad | 1259 | } |
74eb4b3e | 1260 | |
fad205ff | 1261 | free ((void *) fname); |
f3f751ad NS |
1262 | } |
1263 | ||
4ed5bcfb NB |
1264 | /* Get a token but skip padding. */ |
1265 | static const cpp_token * | |
6cf87ca4 | 1266 | get_token_no_padding (cpp_reader *pfile) |
a5c3cccd | 1267 | { |
4ed5bcfb NB |
1268 | for (;;) |
1269 | { | |
1270 | const cpp_token *result = cpp_get_token (pfile); | |
1271 | if (result->type != CPP_PADDING) | |
1272 | return result; | |
1273 | } | |
1274 | } | |
a5c3cccd | 1275 | |
4ed5bcfb NB |
1276 | /* Check syntax is "(string-literal)". Returns the string on success, |
1277 | or NULL on failure. */ | |
1278 | static const cpp_token * | |
6cf87ca4 | 1279 | get__Pragma_string (cpp_reader *pfile) |
4ed5bcfb NB |
1280 | { |
1281 | const cpp_token *string; | |
a5c3cccd | 1282 | |
4ed5bcfb NB |
1283 | if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN) |
1284 | return NULL; | |
1285 | ||
1286 | string = get_token_no_padding (pfile); | |
a5c3cccd | 1287 | if (string->type != CPP_STRING && string->type != CPP_WSTRING) |
4ed5bcfb NB |
1288 | return NULL; |
1289 | ||
1290 | if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) | |
1291 | return NULL; | |
a5c3cccd | 1292 | |
4ed5bcfb | 1293 | return string; |
a5c3cccd NB |
1294 | } |
1295 | ||
87062813 NB |
1296 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1297 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1298 | static void | |
6cf87ca4 | 1299 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
a5c3cccd NB |
1300 | { |
1301 | const unsigned char *src, *limit; | |
87062813 | 1302 | char *dest, *result; |
a5c3cccd | 1303 | |
6338b358 NB |
1304 | dest = result = alloca (in->len - 1); |
1305 | src = in->text + 1 + (in->text[0] == 'L'); | |
1306 | limit = in->text + in->len - 1; | |
1307 | while (src < limit) | |
a5c3cccd NB |
1308 | { |
1309 | /* We know there is a character following the backslash. */ | |
1310 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1311 | src++; | |
1312 | *dest++ = *src++; | |
1313 | } | |
26aea073 | 1314 | *dest = '\n'; |
a5c3cccd | 1315 | |
8128cccf NB |
1316 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1317 | tokens when in the middle of a macro expansion. Use a new | |
1318 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1319 | doesn't go beyond the end of the text. Also, remember the | |
1320 | current lexing position so we can return to it later. | |
1321 | ||
1322 | Something like line-at-a-time lexing should remove the need for | |
1323 | this. */ | |
1324 | { | |
1325 | cpp_context *saved_context = pfile->context; | |
1326 | cpp_token *saved_cur_token = pfile->cur_token; | |
1327 | tokenrun *saved_cur_run = pfile->cur_run; | |
1328 | ||
1329 | pfile->context = xnew (cpp_context); | |
1330 | pfile->context->macro = 0; | |
1331 | pfile->context->prev = 0; | |
1332 | run_directive (pfile, T_PRAGMA, result, dest - result); | |
1333 | free (pfile->context); | |
1334 | pfile->context = saved_context; | |
1335 | pfile->cur_token = saved_cur_token; | |
1336 | pfile->cur_run = saved_cur_run; | |
8128cccf | 1337 | } |
79ba5e3b NB |
1338 | |
1339 | /* See above comment. For the moment, we'd like | |
1340 | ||
1341 | token1 _Pragma ("foo") token2 | |
1342 | ||
1343 | to be output as | |
1344 | ||
1345 | token1 | |
1346 | # 7 "file.c" | |
1347 | #pragma foo | |
1348 | # 7 "file.c" | |
1349 | token2 | |
1350 | ||
1351 | Getting the line markers is a little tricky. */ | |
1352 | if (pfile->cb.line_change) | |
6cf87ca4 | 1353 | pfile->cb.line_change (pfile, pfile->cur_token, false); |
a5c3cccd NB |
1354 | } |
1355 | ||
87062813 | 1356 | /* Handle the _Pragma operator. */ |
a5c3cccd | 1357 | void |
6cf87ca4 | 1358 | _cpp_do__Pragma (cpp_reader *pfile) |
a5c3cccd | 1359 | { |
4ed5bcfb | 1360 | const cpp_token *string = get__Pragma_string (pfile); |
a5c3cccd | 1361 | |
79ba5e3b NB |
1362 | if (string) |
1363 | destringize_and_run (pfile, &string->val.str); | |
1364 | else | |
0527bc4e | 1365 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 1366 | "_Pragma takes a parenthesized string literal"); |
a5c3cccd NB |
1367 | } |
1368 | ||
6cf87ca4 | 1369 | /* Ignore #sccs on all systems. */ |
711b8824 | 1370 | static void |
6cf87ca4 | 1371 | do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED) |
7f2935c7 | 1372 | { |
7f2935c7 | 1373 | } |
1d0e51ba | 1374 | |
5d8ebbd8 | 1375 | /* Handle #ifdef. */ |
711b8824 | 1376 | static void |
6cf87ca4 | 1377 | do_ifdef (cpp_reader *pfile) |
168d3732 | 1378 | { |
93c80368 | 1379 | int skip = 1; |
041c3194 | 1380 | |
cef0d199 | 1381 | if (! pfile->state.skipping) |
93c80368 NB |
1382 | { |
1383 | const cpp_hashnode *node = lex_macro_node (pfile); | |
041c3194 | 1384 | |
93c80368 | 1385 | if (node) |
a69cbaac NB |
1386 | { |
1387 | skip = node->type != NT_MACRO; | |
1388 | _cpp_mark_macro_used (node); | |
1389 | check_eol (pfile); | |
1390 | } | |
93c80368 | 1391 | } |
168d3732 | 1392 | |
93c80368 NB |
1393 | push_conditional (pfile, skip, T_IFDEF, 0); |
1394 | } | |
168d3732 | 1395 | |
5d8ebbd8 | 1396 | /* Handle #ifndef. */ |
711b8824 | 1397 | static void |
6cf87ca4 | 1398 | do_ifndef (cpp_reader *pfile) |
168d3732 | 1399 | { |
93c80368 | 1400 | int skip = 1; |
bfb9dc7f | 1401 | const cpp_hashnode *node = 0; |
168d3732 | 1402 | |
cef0d199 | 1403 | if (! pfile->state.skipping) |
5af7e2c2 | 1404 | { |
93c80368 | 1405 | node = lex_macro_node (pfile); |
b43db0b3 GK |
1406 | |
1407 | if (node) | |
a69cbaac NB |
1408 | { |
1409 | skip = node->type == NT_MACRO; | |
1410 | _cpp_mark_macro_used (node); | |
1411 | check_eol (pfile); | |
1412 | } | |
5af7e2c2 | 1413 | } |
041c3194 | 1414 | |
93c80368 | 1415 | push_conditional (pfile, skip, T_IFNDEF, node); |
7f2935c7 PB |
1416 | } |
1417 | ||
6d18adbc NB |
1418 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1419 | pfile->mi_ind_cmacro so we can handle multiple-include | |
6356f892 | 1420 | optimizations. If macro expansion occurs in the expression, we |
6d18adbc NB |
1421 | cannot treat it as a controlling conditional, since the expansion |
1422 | could change in the future. That is handled by cpp_get_token. */ | |
711b8824 | 1423 | static void |
6cf87ca4 | 1424 | do_if (cpp_reader *pfile) |
7f2935c7 | 1425 | { |
93c80368 | 1426 | int skip = 1; |
7f2935c7 | 1427 | |
cef0d199 | 1428 | if (! pfile->state.skipping) |
87ed109f | 1429 | skip = _cpp_parse_expr (pfile) == false; |
93c80368 | 1430 | |
6d18adbc | 1431 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
7f2935c7 PB |
1432 | } |
1433 | ||
b528a07e | 1434 | /* Flip skipping state if appropriate and continue without changing |
ea4a453b ZW |
1435 | if_stack; this is so that the error message for missing #endif's |
1436 | etc. will point to the original #if. */ | |
711b8824 | 1437 | static void |
6cf87ca4 | 1438 | do_else (cpp_reader *pfile) |
ed705a82 | 1439 | { |
b528a07e NB |
1440 | cpp_buffer *buffer = pfile->buffer; |
1441 | struct if_stack *ifs = buffer->if_stack; | |
ff2b53ef | 1442 | |
ea4a453b | 1443 | if (ifs == NULL) |
0527bc4e | 1444 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
93c80368 | 1445 | else |
ff2b53ef | 1446 | { |
93c80368 NB |
1447 | if (ifs->type == T_ELSE) |
1448 | { | |
0527bc4e JDA |
1449 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
1450 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
93c80368 NB |
1451 | "the conditional began here"); |
1452 | } | |
b528a07e NB |
1453 | ifs->type = T_ELSE; |
1454 | ||
cef0d199 NB |
1455 | /* Skip any future (erroneous) #elses or #elifs. */ |
1456 | pfile->state.skipping = ifs->skip_elses; | |
1457 | ifs->skip_elses = true; | |
7f2935c7 | 1458 | |
93c80368 NB |
1459 | /* Invalidate any controlling macro. */ |
1460 | ifs->mi_cmacro = 0; | |
93c80368 | 1461 | |
cef0d199 | 1462 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1463 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1464 | check_eol (pfile); |
1465 | } | |
7f2935c7 PB |
1466 | } |
1467 | ||
5d8ebbd8 | 1468 | /* Handle a #elif directive by not changing if_stack either. See the |
93c80368 | 1469 | comment above do_else. */ |
711b8824 | 1470 | static void |
6cf87ca4 | 1471 | do_elif (cpp_reader *pfile) |
7f2935c7 | 1472 | { |
b528a07e NB |
1473 | cpp_buffer *buffer = pfile->buffer; |
1474 | struct if_stack *ifs = buffer->if_stack; | |
7f2935c7 | 1475 | |
ea4a453b | 1476 | if (ifs == NULL) |
0527bc4e | 1477 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
b528a07e | 1478 | else |
40ea76de | 1479 | { |
b528a07e NB |
1480 | if (ifs->type == T_ELSE) |
1481 | { | |
0527bc4e JDA |
1482 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
1483 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
b528a07e NB |
1484 | "the conditional began here"); |
1485 | } | |
1486 | ifs->type = T_ELIF; | |
93c80368 | 1487 | |
cef0d199 NB |
1488 | /* Only evaluate this if we aren't skipping elses. During |
1489 | evaluation, set skipping to false to get lexer warnings. */ | |
1490 | if (ifs->skip_elses) | |
1491 | pfile->state.skipping = 1; | |
1492 | else | |
b528a07e | 1493 | { |
cef0d199 NB |
1494 | pfile->state.skipping = 0; |
1495 | pfile->state.skipping = ! _cpp_parse_expr (pfile); | |
1496 | ifs->skip_elses = ! pfile->state.skipping; | |
b528a07e | 1497 | } |
cef0d199 NB |
1498 | |
1499 | /* Invalidate any controlling macro. */ | |
1500 | ifs->mi_cmacro = 0; | |
40ea76de | 1501 | } |
7f2935c7 PB |
1502 | } |
1503 | ||
cef0d199 | 1504 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
711b8824 | 1505 | static void |
6cf87ca4 | 1506 | do_endif (cpp_reader *pfile) |
7f2935c7 | 1507 | { |
b528a07e NB |
1508 | cpp_buffer *buffer = pfile->buffer; |
1509 | struct if_stack *ifs = buffer->if_stack; | |
ea4a453b | 1510 | |
ea4a453b | 1511 | if (ifs == NULL) |
0527bc4e | 1512 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
7f2935c7 PB |
1513 | else |
1514 | { | |
cef0d199 | 1515 | /* Only check EOL if was not originally skipping. */ |
909de5da | 1516 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
cef0d199 NB |
1517 | check_eol (pfile); |
1518 | ||
93c80368 NB |
1519 | /* If potential control macro, we go back outside again. */ |
1520 | if (ifs->next == 0 && ifs->mi_cmacro) | |
1521 | { | |
6d18adbc | 1522 | pfile->mi_valid = true; |
93c80368 NB |
1523 | pfile->mi_cmacro = ifs->mi_cmacro; |
1524 | } | |
1525 | ||
b528a07e | 1526 | buffer->if_stack = ifs->next; |
cef0d199 | 1527 | pfile->state.skipping = ifs->was_skipping; |
2a967f3d | 1528 | obstack_free (&pfile->buffer_ob, ifs); |
7f2935c7 | 1529 | } |
93c80368 | 1530 | } |
041c3194 | 1531 | |
5d8ebbd8 NB |
1532 | /* Push an if_stack entry for a preprocessor conditional, and set |
1533 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
1534 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
1535 | we need to check here that we are at the top of the file. */ | |
ea4a453b | 1536 | static void |
6cf87ca4 ZW |
1537 | push_conditional (cpp_reader *pfile, int skip, int type, |
1538 | const cpp_hashnode *cmacro) | |
ea4a453b ZW |
1539 | { |
1540 | struct if_stack *ifs; | |
b528a07e | 1541 | cpp_buffer *buffer = pfile->buffer; |
ea4a453b | 1542 | |
2a967f3d | 1543 | ifs = xobnew (&pfile->buffer_ob, struct if_stack); |
50410426 | 1544 | ifs->line = pfile->directive_line; |
b528a07e | 1545 | ifs->next = buffer->if_stack; |
cef0d199 NB |
1546 | ifs->skip_elses = pfile->state.skipping || !skip; |
1547 | ifs->was_skipping = pfile->state.skipping; | |
ea4a453b | 1548 | ifs->type = type; |
6d18adbc NB |
1549 | /* This condition is effectively a test for top-of-file. */ |
1550 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
93c80368 NB |
1551 | ifs->mi_cmacro = cmacro; |
1552 | else | |
1553 | ifs->mi_cmacro = 0; | |
ea4a453b | 1554 | |
cef0d199 | 1555 | pfile->state.skipping = skip; |
b528a07e | 1556 | buffer->if_stack = ifs; |
782331f4 | 1557 | } |
7061aa5a | 1558 | |
5d8ebbd8 NB |
1559 | /* Read the tokens of the answer into the macro pool, in a directive |
1560 | of type TYPE. Only commit the memory if we intend it as permanent | |
1561 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
1562 | ANSWERP to point to the answer. */ | |
93c80368 | 1563 | static int |
6cf87ca4 | 1564 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type) |
7f2935c7 | 1565 | { |
4ed5bcfb | 1566 | const cpp_token *paren; |
93c80368 | 1567 | struct answer *answer; |
8c3b2693 | 1568 | unsigned int acount; |
93c80368 NB |
1569 | |
1570 | /* In a conditional, it is legal to not have an open paren. We | |
1571 | should save the following token in this case. */ | |
4ed5bcfb | 1572 | paren = cpp_get_token (pfile); |
93c80368 NB |
1573 | |
1574 | /* If not a paren, see if we're OK. */ | |
4ed5bcfb | 1575 | if (paren->type != CPP_OPEN_PAREN) |
041c3194 | 1576 | { |
93c80368 NB |
1577 | /* In a conditional no answer is a test for any answer. It |
1578 | could be followed by any token. */ | |
1579 | if (type == T_IF) | |
bdcbe496 NB |
1580 | { |
1581 | _cpp_backup_tokens (pfile, 1); | |
1582 | return 0; | |
1583 | } | |
93c80368 NB |
1584 | |
1585 | /* #unassert with no answer is valid - it removes all answers. */ | |
4ed5bcfb | 1586 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
93c80368 | 1587 | return 0; |
15dad1d9 | 1588 | |
0527bc4e | 1589 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate"); |
93c80368 | 1590 | return 1; |
041c3194 | 1591 | } |
7061aa5a | 1592 | |
8c3b2693 | 1593 | for (acount = 0;; acount++) |
041c3194 | 1594 | { |
8c3b2693 NB |
1595 | size_t room_needed; |
1596 | const cpp_token *token = cpp_get_token (pfile); | |
1597 | cpp_token *dest; | |
93c80368 | 1598 | |
041c3194 ZW |
1599 | if (token->type == CPP_CLOSE_PAREN) |
1600 | break; | |
a7abcbbf | 1601 | |
93c80368 | 1602 | if (token->type == CPP_EOF) |
041c3194 | 1603 | { |
0527bc4e | 1604 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
93c80368 | 1605 | return 1; |
041c3194 | 1606 | } |
8c3b2693 NB |
1607 | |
1608 | /* struct answer includes the space for one token. */ | |
1609 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
1610 | ||
1611 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
1612 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
1613 | ||
1614 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
1615 | *dest = *token; | |
1616 | ||
1617 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
1618 | if (acount == 0) | |
1619 | dest->flags &= ~PREV_WHITE; | |
041c3194 | 1620 | } |
15dad1d9 | 1621 | |
8c3b2693 | 1622 | if (acount == 0) |
7061aa5a | 1623 | { |
0527bc4e | 1624 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
93c80368 | 1625 | return 1; |
7f2935c7 | 1626 | } |
041c3194 | 1627 | |
8c3b2693 NB |
1628 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
1629 | answer->count = acount; | |
1630 | answer->next = NULL; | |
93c80368 | 1631 | *answerp = answer; |
041c3194 | 1632 | |
93c80368 NB |
1633 | return 0; |
1634 | } | |
041c3194 | 1635 | |
5d8ebbd8 NB |
1636 | /* Parses an assertion directive of type TYPE, returning a pointer to |
1637 | the hash node of the predicate, or 0 on error. If an answer was | |
1638 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
93c80368 | 1639 | static cpp_hashnode * |
6cf87ca4 | 1640 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
93c80368 NB |
1641 | { |
1642 | cpp_hashnode *result = 0; | |
4ed5bcfb | 1643 | const cpp_token *predicate; |
93c80368 NB |
1644 | |
1645 | /* We don't expand predicates or answers. */ | |
1646 | pfile->state.prevent_expansion++; | |
1647 | ||
93c80368 | 1648 | *answerp = 0; |
4ed5bcfb NB |
1649 | predicate = cpp_get_token (pfile); |
1650 | if (predicate->type == CPP_EOF) | |
0527bc4e | 1651 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
4ed5bcfb | 1652 | else if (predicate->type != CPP_NAME) |
0527bc4e | 1653 | cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier"); |
93c80368 NB |
1654 | else if (parse_answer (pfile, answerp, type) == 0) |
1655 | { | |
4ed5bcfb | 1656 | unsigned int len = NODE_LEN (predicate->val.node); |
93c80368 | 1657 | unsigned char *sym = alloca (len + 1); |
041c3194 | 1658 | |
93c80368 NB |
1659 | /* Prefix '#' to get it out of macro namespace. */ |
1660 | sym[0] = '#'; | |
4ed5bcfb | 1661 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
93c80368 NB |
1662 | result = cpp_lookup (pfile, sym, len + 1); |
1663 | } | |
7061aa5a | 1664 | |
93c80368 NB |
1665 | pfile->state.prevent_expansion--; |
1666 | return result; | |
7f2935c7 | 1667 | } |
7061aa5a | 1668 | |
5d8ebbd8 | 1669 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
041c3194 | 1670 | or a pointer to NULL if the answer is not in the chain. */ |
93c80368 | 1671 | static struct answer ** |
6cf87ca4 | 1672 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
7f2935c7 | 1673 | { |
93c80368 | 1674 | unsigned int i; |
041c3194 | 1675 | struct answer **result; |
7f2935c7 | 1676 | |
041c3194 | 1677 | for (result = &node->value.answers; *result; result = &(*result)->next) |
93c80368 NB |
1678 | { |
1679 | struct answer *answer = *result; | |
1680 | ||
1681 | if (answer->count == candidate->count) | |
1682 | { | |
1683 | for (i = 0; i < answer->count; i++) | |
1684 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
1685 | break; | |
1686 | ||
1687 | if (i == answer->count) | |
1688 | break; | |
1689 | } | |
1690 | } | |
ff2b53ef | 1691 | |
041c3194 ZW |
1692 | return result; |
1693 | } | |
15dad1d9 | 1694 | |
93c80368 | 1695 | /* Test an assertion within a preprocessor conditional. Returns |
da7d8304 | 1696 | nonzero on failure, zero on success. On success, the result of |
2402645b | 1697 | the test is written into VALUE, otherwise the value 0. */ |
93c80368 | 1698 | int |
6cf87ca4 | 1699 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
93c80368 NB |
1700 | { |
1701 | struct answer *answer; | |
1702 | cpp_hashnode *node; | |
1703 | ||
1704 | node = parse_assertion (pfile, &answer, T_IF); | |
2402645b HPN |
1705 | |
1706 | /* For recovery, an erroneous assertion expression is handled as a | |
1707 | failing assertion. */ | |
1708 | *value = 0; | |
1709 | ||
93c80368 NB |
1710 | if (node) |
1711 | *value = (node->type == NT_ASSERTION && | |
1712 | (answer == 0 || *find_answer (node, answer) != 0)); | |
91318908 NB |
1713 | else if (pfile->cur_token[-1].type == CPP_EOF) |
1714 | _cpp_backup_tokens (pfile, 1); | |
93c80368 NB |
1715 | |
1716 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1717 | return node == 0; | |
1718 | } | |
1719 | ||
5d8ebbd8 | 1720 | /* Handle #assert. */ |
711b8824 | 1721 | static void |
6cf87ca4 | 1722 | do_assert (cpp_reader *pfile) |
041c3194 ZW |
1723 | { |
1724 | struct answer *new_answer; | |
1725 | cpp_hashnode *node; | |
df383483 | 1726 | |
93c80368 | 1727 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
041c3194 | 1728 | if (node) |
ff2b53ef | 1729 | { |
93c80368 NB |
1730 | /* Place the new answer in the answer list. First check there |
1731 | is not a duplicate. */ | |
041c3194 | 1732 | new_answer->next = 0; |
93c80368 | 1733 | if (node->type == NT_ASSERTION) |
041c3194 | 1734 | { |
93c80368 NB |
1735 | if (*find_answer (node, new_answer)) |
1736 | { | |
0527bc4e | 1737 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
ebef4e8c | 1738 | NODE_NAME (node) + 1); |
93c80368 NB |
1739 | return; |
1740 | } | |
041c3194 ZW |
1741 | new_answer->next = node->value.answers; |
1742 | } | |
8c3b2693 | 1743 | |
93c80368 | 1744 | node->type = NT_ASSERTION; |
041c3194 | 1745 | node->value.answers = new_answer; |
8c3b2693 NB |
1746 | BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer) |
1747 | + (new_answer->count - 1) | |
1748 | * sizeof (cpp_token)); | |
1749 | check_eol (pfile); | |
ff2b53ef | 1750 | } |
041c3194 | 1751 | } |
15dad1d9 | 1752 | |
5d8ebbd8 | 1753 | /* Handle #unassert. */ |
711b8824 | 1754 | static void |
6cf87ca4 | 1755 | do_unassert (cpp_reader *pfile) |
041c3194 ZW |
1756 | { |
1757 | cpp_hashnode *node; | |
93c80368 | 1758 | struct answer *answer; |
df383483 | 1759 | |
93c80368 NB |
1760 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
1761 | /* It isn't an error to #unassert something that isn't asserted. */ | |
1762 | if (node && node->type == NT_ASSERTION) | |
7061aa5a | 1763 | { |
93c80368 | 1764 | if (answer) |
15dad1d9 | 1765 | { |
93c80368 | 1766 | struct answer **p = find_answer (node, answer), *temp; |
041c3194 | 1767 | |
93c80368 NB |
1768 | /* Remove the answer from the list. */ |
1769 | temp = *p; | |
1770 | if (temp) | |
1771 | *p = temp->next; | |
a7abcbbf | 1772 | |
93c80368 NB |
1773 | /* Did we free the last answer? */ |
1774 | if (node->value.answers == 0) | |
1775 | node->type = NT_VOID; | |
8c3b2693 NB |
1776 | |
1777 | check_eol (pfile); | |
93c80368 NB |
1778 | } |
1779 | else | |
1780 | _cpp_free_definition (node); | |
041c3194 | 1781 | } |
93c80368 NB |
1782 | |
1783 | /* We don't commit the memory for the answer - it's temporary only. */ | |
7f2935c7 | 1784 | } |
7f2935c7 | 1785 | |
45b966db ZW |
1786 | /* These are for -D, -U, -A. */ |
1787 | ||
1788 | /* Process the string STR as if it appeared as the body of a #define. | |
1789 | If STR is just an identifier, define it with value 1. | |
1790 | If STR has anything after the identifier, then it should | |
ec5c56db | 1791 | be identifier=definition. */ |
0b22d65c | 1792 | void |
6cf87ca4 | 1793 | cpp_define (cpp_reader *pfile, const char *str) |
0b22d65c | 1794 | { |
45b966db ZW |
1795 | char *buf, *p; |
1796 | size_t count; | |
1797 | ||
df383483 | 1798 | /* Copy the entire option so we can modify it. |
45b966db | 1799 | Change the first "=" in the string to a space. If there is none, |
86368122 NB |
1800 | tack " 1" on the end. */ |
1801 | ||
86368122 | 1802 | count = strlen (str); |
703ad42b | 1803 | buf = alloca (count + 3); |
86368122 NB |
1804 | memcpy (buf, str, count); |
1805 | ||
1806 | p = strchr (str, '='); | |
45b966db | 1807 | if (p) |
86368122 | 1808 | buf[p - str] = ' '; |
45b966db ZW |
1809 | else |
1810 | { | |
86368122 NB |
1811 | buf[count++] = ' '; |
1812 | buf[count++] = '1'; | |
0b22d65c | 1813 | } |
26aea073 | 1814 | buf[count] = '\n'; |
cf4ed945 | 1815 | |
29401c30 | 1816 | run_directive (pfile, T_DEFINE, buf, count); |
2c8f0515 ZW |
1817 | } |
1818 | ||
ad2a084d | 1819 | /* Slight variant of the above for use by initialize_builtins. */ |
2c8f0515 | 1820 | void |
6cf87ca4 | 1821 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
2c8f0515 | 1822 | { |
26aea073 NB |
1823 | size_t len = strlen (str); |
1824 | char *buf = alloca (len + 1); | |
1825 | memcpy (buf, str, len); | |
1826 | buf[len] = '\n'; | |
1827 | run_directive (pfile, T_DEFINE, buf, len); | |
45b966db | 1828 | } |
0f41302f | 1829 | |
45b966db ZW |
1830 | /* Process MACRO as if it appeared as the body of an #undef. */ |
1831 | void | |
6cf87ca4 | 1832 | cpp_undef (cpp_reader *pfile, const char *macro) |
7f2935c7 | 1833 | { |
26aea073 NB |
1834 | size_t len = strlen (macro); |
1835 | char *buf = alloca (len + 1); | |
1836 | memcpy (buf, macro, len); | |
1837 | buf[len] = '\n'; | |
1838 | run_directive (pfile, T_UNDEF, buf, len); | |
7f2935c7 PB |
1839 | } |
1840 | ||
ec5c56db | 1841 | /* Process the string STR as if it appeared as the body of a #assert. */ |
45b966db | 1842 | void |
6cf87ca4 | 1843 | cpp_assert (cpp_reader *pfile, const char *str) |
45b966db | 1844 | { |
86368122 | 1845 | handle_assertion (pfile, str, T_ASSERT); |
45b966db | 1846 | } |
7f2935c7 | 1847 | |
ec5c56db | 1848 | /* Process STR as if it appeared as the body of an #unassert. */ |
45b966db | 1849 | void |
6cf87ca4 | 1850 | cpp_unassert (cpp_reader *pfile, const char *str) |
7f2935c7 | 1851 | { |
86368122 | 1852 | handle_assertion (pfile, str, T_UNASSERT); |
df383483 | 1853 | } |
3fdc651f | 1854 | |
86368122 NB |
1855 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
1856 | static void | |
6cf87ca4 | 1857 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
86368122 NB |
1858 | { |
1859 | size_t count = strlen (str); | |
1860 | const char *p = strchr (str, '='); | |
1861 | ||
26aea073 NB |
1862 | /* Copy the entire option so we can modify it. Change the first |
1863 | "=" in the string to a '(', and tack a ')' on the end. */ | |
703ad42b | 1864 | char *buf = alloca (count + 2); |
26aea073 NB |
1865 | |
1866 | memcpy (buf, str, count); | |
86368122 NB |
1867 | if (p) |
1868 | { | |
86368122 NB |
1869 | buf[p - str] = '('; |
1870 | buf[count++] = ')'; | |
86368122 | 1871 | } |
26aea073 NB |
1872 | buf[count] = '\n'; |
1873 | str = buf; | |
86368122 | 1874 | |
29401c30 | 1875 | run_directive (pfile, type, str, count); |
86368122 NB |
1876 | } |
1877 | ||
7e96d768 NB |
1878 | /* The number of errors for a given reader. */ |
1879 | unsigned int | |
6cf87ca4 | 1880 | cpp_errors (cpp_reader *pfile) |
7e96d768 NB |
1881 | { |
1882 | return pfile->errors; | |
1883 | } | |
1884 | ||
1885 | /* The options structure. */ | |
1886 | cpp_options * | |
6cf87ca4 | 1887 | cpp_get_options (cpp_reader *pfile) |
7e96d768 NB |
1888 | { |
1889 | return &pfile->opts; | |
1890 | } | |
1891 | ||
1892 | /* The callbacks structure. */ | |
1893 | cpp_callbacks * | |
6cf87ca4 | 1894 | cpp_get_callbacks (cpp_reader *pfile) |
7e96d768 NB |
1895 | { |
1896 | return &pfile->cb; | |
1897 | } | |
1898 | ||
1899 | /* Copy the given callbacks structure to our own. */ | |
1900 | void | |
6cf87ca4 | 1901 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
7e96d768 NB |
1902 | { |
1903 | pfile->cb = *cb; | |
1904 | } | |
1905 | ||
c6e83800 ZW |
1906 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
1907 | struct deps * | |
1908 | cpp_get_deps (cpp_reader *pfile) | |
1909 | { | |
1910 | if (!pfile->deps) | |
1911 | pfile->deps = deps_init (); | |
1912 | return pfile->deps; | |
1913 | } | |
1914 | ||
eb1f4d9d NB |
1915 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
1916 | doesn't fail. It does not generate a file change call back; that | |
1917 | is the responsibility of the caller. */ | |
c71f835b | 1918 | cpp_buffer * |
6cf87ca4 | 1919 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
40de9f76 | 1920 | int from_stage3) |
c71f835b | 1921 | { |
2a967f3d | 1922 | cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer); |
93c80368 | 1923 | |
fde84349 NB |
1924 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
1925 | memset (new, 0, sizeof (cpp_buffer)); | |
1926 | ||
26aea073 | 1927 | new->next_line = new->buf = buffer; |
fde84349 | 1928 | new->rlimit = buffer + len; |
26aea073 | 1929 | new->from_stage3 = from_stage3; |
3cf3593f | 1930 | new->prev = pfile->buffer; |
26aea073 | 1931 | new->need_line = true; |
0bda4760 | 1932 | |
3cf3593f | 1933 | pfile->buffer = new; |
cf551fba | 1934 | |
c71f835b ZW |
1935 | return new; |
1936 | } | |
1937 | ||
af0d16cd NB |
1938 | /* Pops a single buffer, with a file change call-back if appropriate. |
1939 | Then pushes the next -include file, if any remain. */ | |
ef6e958a | 1940 | void |
6cf87ca4 | 1941 | _cpp_pop_buffer (cpp_reader *pfile) |
c71f835b | 1942 | { |
fde84349 | 1943 | cpp_buffer *buffer = pfile->buffer; |
8f9b4009 | 1944 | struct _cpp_file *inc = buffer->file; |
ad2a084d | 1945 | struct if_stack *ifs; |
c71f835b | 1946 | |
fde84349 NB |
1947 | /* Walk back up the conditional stack till we reach its level at |
1948 | entry to this file, issuing error messages. */ | |
1949 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
0527bc4e | 1950 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
fde84349 | 1951 | "unterminated #%s", dtable[ifs->type].name); |
eb1f4d9d | 1952 | |
97293897 | 1953 | /* In case of a missing #endif. */ |
67821e3a | 1954 | pfile->state.skipping = 0; |
29401c30 | 1955 | |
af0d16cd | 1956 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
29401c30 NB |
1957 | pfile->buffer = buffer->prev; |
1958 | ||
26aea073 NB |
1959 | free (buffer->notes); |
1960 | ||
af0d16cd NB |
1961 | /* Free the buffer object now; we may want to push a new buffer |
1962 | in _cpp_push_next_include_file. */ | |
1963 | obstack_free (&pfile->buffer_ob, buffer); | |
29401c30 | 1964 | |
af0d16cd NB |
1965 | if (inc) |
1966 | { | |
1967 | _cpp_pop_file_buffer (pfile, inc); | |
1968 | ||
40de9f76 | 1969 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
af0d16cd | 1970 | } |
c71f835b ZW |
1971 | } |
1972 | ||
05713b80 | 1973 | /* Enter all recognized directives in the hash table. */ |
c71f835b | 1974 | void |
6cf87ca4 | 1975 | _cpp_init_directives (cpp_reader *pfile) |
c71f835b | 1976 | { |
766ee681 | 1977 | unsigned int i; |
93c80368 | 1978 | cpp_hashnode *node; |
bfb9dc7f | 1979 | |
37b8524c | 1980 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
93c80368 | 1981 | { |
766ee681 | 1982 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
4977bab6 ZW |
1983 | node->is_directive = 1; |
1984 | node->directive_index = i; | |
93c80368 | 1985 | } |
c71f835b | 1986 | } |