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