]>
Commit | Line | Data |
---|---|---|
a949941c | 1 | /* CPP Library. (Directive handling.) |
35c3d610 | 2 | Copyright (C) 1986-2014 Free Software Foundation, Inc. |
4c8cc616 | 3 | Contributed by Per Bothner, 1994-95. |
d8bfa78c | 4 | Based on CCCP program by Paul Rubin, June 1986 |
7f2935c7 PB |
5 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
6 | ||
7 | This program is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
748086b7 | 9 | Free Software Foundation; either version 3, or (at your option) any |
7f2935c7 PB |
10 | later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
748086b7 JJ |
18 | along with this program; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
7f2935c7 | 20 | |
956d6950 | 21 | #include "config.h" |
b04cd507 | 22 | #include "system.h" |
956d6950 | 23 | #include "cpplib.h" |
4f4e53dd | 24 | #include "internal.h" |
c6e83800 | 25 | #include "mkdeps.h" |
c71f835b | 26 | #include "obstack.h" |
7f2935c7 | 27 | |
88ae23e7 ZW |
28 | /* Stack of conditionals currently in progress |
29 | (including both successful and failing conditionals). */ | |
88ae23e7 ZW |
30 | struct if_stack |
31 | { | |
32 | struct if_stack *next; | |
d0a9fbe1 | 33 | source_location line; /* Line where condition started. */ |
93c80368 | 34 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
cef0d199 NB |
35 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
36 | bool was_skipping; /* If were skipping on entry. */ | |
6cf87ca4 | 37 | int type; /* Most recent conditional for diagnostics. */ |
88ae23e7 | 38 | }; |
88ae23e7 | 39 | |
a5da89c6 | 40 | /* Contains a registered pragma or pragma namespace. */ |
6cf87ca4 | 41 | typedef void (*pragma_cb) (cpp_reader *); |
a5da89c6 NB |
42 | struct pragma_entry |
43 | { | |
44 | struct pragma_entry *next; | |
4b115ff0 | 45 | const cpp_hashnode *pragma; /* Name and length. */ |
21b11495 ZW |
46 | bool is_nspace; |
47 | bool is_internal; | |
bc4071dd RH |
48 | bool is_deferred; |
49 | bool allow_expansion; | |
a5da89c6 NB |
50 | union { |
51 | pragma_cb handler; | |
52 | struct pragma_entry *space; | |
bc4071dd | 53 | unsigned int ident; |
a5da89c6 NB |
54 | } u; |
55 | }; | |
56 | ||
93c80368 NB |
57 | /* Values for the origin field of struct directive. KANDR directives |
58 | come from traditional (K&R) C. STDC89 directives come from the | |
59 | 1989 C standard. EXTENSION directives are extensions. */ | |
60 | #define KANDR 0 | |
61 | #define STDC89 1 | |
62 | #define EXTENSION 2 | |
63 | ||
64 | /* Values for the flags field of struct directive. COND indicates a | |
65 | conditional; IF_COND an opening conditional. INCL means to treat | |
66 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
67 | means this directive should be handled even if -fpreprocessed is in | |
1a76916c NB |
68 | effect (these are the directives with callback hooks). |
69 | ||
d97371e0 | 70 | EXPAND is set on directives that are always macro-expanded. */ |
93c80368 NB |
71 | #define COND (1 << 0) |
72 | #define IF_COND (1 << 1) | |
73 | #define INCL (1 << 2) | |
74 | #define IN_I (1 << 3) | |
1a76916c | 75 | #define EXPAND (1 << 4) |
899015a0 | 76 | #define DEPRECATED (1 << 5) |
93c80368 NB |
77 | |
78 | /* Defines one #-directive, including how to handle it. */ | |
6cf87ca4 | 79 | typedef void (*directive_handler) (cpp_reader *); |
93c80368 NB |
80 | typedef struct directive directive; |
81 | struct directive | |
82 | { | |
83 | directive_handler handler; /* Function to handle directive. */ | |
562a5c27 | 84 | const uchar *name; /* Name of directive. */ |
93c80368 NB |
85 | unsigned short length; /* Length of name. */ |
86 | unsigned char origin; /* Origin of directive. */ | |
87 | unsigned char flags; /* Flags describing this directive. */ | |
88 | }; | |
89 | ||
1316f1f7 ZW |
90 | /* Forward declarations. */ |
91 | ||
6cf87ca4 | 92 | static void skip_rest_of_line (cpp_reader *); |
a5cb563b | 93 | static void check_eol (cpp_reader *, bool); |
6cf87ca4 ZW |
94 | static void start_directive (cpp_reader *); |
95 | static void prepare_directive_trad (cpp_reader *); | |
96 | static void end_directive (cpp_reader *, int); | |
97 | static void directive_diagnostics (cpp_reader *, const directive *, int); | |
98 | static void run_directive (cpp_reader *, int, const char *, size_t); | |
99 | static char *glue_header_name (cpp_reader *); | |
a28fbdba MLI |
100 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***, |
101 | source_location *); | |
6cf87ca4 ZW |
102 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
103 | static unsigned int read_flag (cpp_reader *, unsigned int); | |
3b8f20a1 | 104 | static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *); |
87cf0651 | 105 | static void do_diagnostic (cpp_reader *, int, int, int); |
ee1c2a10 | 106 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
d1bd0ded | 107 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
6cf87ca4 ZW |
108 | static void do_include_common (cpp_reader *, enum include_type); |
109 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, | |
110 | const cpp_hashnode *); | |
6cf87ca4 ZW |
111 | static int count_registered_pragmas (struct pragma_entry *); |
112 | static char ** save_registered_pragmas (struct pragma_entry *, char **); | |
113 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, | |
114 | char **); | |
115 | static void do_pragma_once (cpp_reader *); | |
116 | static void do_pragma_poison (cpp_reader *); | |
117 | static void do_pragma_system_header (cpp_reader *); | |
118 | static void do_pragma_dependency (cpp_reader *); | |
f591bd8f FW |
119 | static void do_pragma_warning_or_error (cpp_reader *, bool error); |
120 | static void do_pragma_warning (cpp_reader *); | |
121 | static void do_pragma_error (cpp_reader *); | |
6cf87ca4 ZW |
122 | static void do_linemarker (cpp_reader *); |
123 | static const cpp_token *get_token_no_padding (cpp_reader *); | |
124 | static const cpp_token *get__Pragma_string (cpp_reader *); | |
125 | static void destringize_and_run (cpp_reader *, const cpp_string *); | |
a28fbdba | 126 | static int parse_answer (cpp_reader *, struct answer **, int, source_location); |
6cf87ca4 ZW |
127 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); |
128 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); | |
129 | static void handle_assertion (cpp_reader *, const char *, int); | |
17e7cb85 KT |
130 | static void do_pragma_push_macro (cpp_reader *); |
131 | static void do_pragma_pop_macro (cpp_reader *); | |
d6874138 | 132 | static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *); |
d481b69b | 133 | |
168d3732 ZW |
134 | /* This is the table of directive handlers. It is ordered by |
135 | frequency of occurrence; the numbers at the end are directive | |
136 | counts from all the source code I have lying around (egcs and libc | |
137 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
93c80368 | 138 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
899015a0 TT |
139 | is now O(1). All extensions other than #warning, #include_next, |
140 | and #import are deprecated. The name is where the extension | |
141 | appears to have come from. */ | |
168d3732 | 142 | |
93c80368 NB |
143 | #define DIRECTIVE_TABLE \ |
144 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
d97371e0 | 145 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
93c80368 NB |
146 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
147 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
1a76916c | 148 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
93c80368 NB |
149 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
150 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
151 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
1a76916c NB |
152 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
153 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
93c80368 NB |
154 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
155 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
156 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
d97371e0 | 157 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
2214382c | 158 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
d97371e0 | 159 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
899015a0 TT |
160 | D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ |
161 | D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ | |
2214382c | 162 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
1ed17cd5 ZW |
163 | |
164 | /* #sccs is synonymous with #ident. */ | |
165 | #define do_sccs do_ident | |
07aa0b04 | 166 | |
168d3732 ZW |
167 | /* Use the table to generate a series of prototypes, an enum for the |
168 | directive names, and an array of directive handlers. */ | |
169 | ||
6cf87ca4 | 170 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
168d3732 ZW |
171 | DIRECTIVE_TABLE |
172 | #undef D | |
173 | ||
041c3194 | 174 | #define D(n, tag, o, f) tag, |
168d3732 ZW |
175 | enum |
176 | { | |
177 | DIRECTIVE_TABLE | |
178 | N_DIRECTIVES | |
7f2935c7 | 179 | }; |
168d3732 ZW |
180 | #undef D |
181 | ||
041c3194 | 182 | #define D(name, t, origin, flags) \ |
9a238586 KG |
183 | { do_##name, (const uchar *) #name, \ |
184 | sizeof #name - 1, origin, flags }, | |
93c80368 | 185 | static const directive dtable[] = |
168d3732 ZW |
186 | { |
187 | DIRECTIVE_TABLE | |
188 | }; | |
189 | #undef D | |
190 | #undef DIRECTIVE_TABLE | |
7f2935c7 | 191 | |
dcc229e5 ZW |
192 | /* Wrapper struct directive for linemarkers. |
193 | The origin is more or less true - the original K+R cpp | |
194 | did use this notation in its preprocessed output. */ | |
195 | static const directive linemarker_dir = | |
196 | { | |
b6baa67d | 197 | do_linemarker, UC"#", 1, KANDR, IN_I |
dcc229e5 ZW |
198 | }; |
199 | ||
1a76916c | 200 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
67821e3a | 201 | |
93c80368 NB |
202 | /* Skip any remaining tokens in a directive. */ |
203 | static void | |
6cf87ca4 | 204 | skip_rest_of_line (cpp_reader *pfile) |
c5a04734 | 205 | { |
93c80368 | 206 | /* Discard all stacked contexts. */ |
8128cccf | 207 | while (pfile->context->prev) |
93c80368 NB |
208 | _cpp_pop_context (pfile); |
209 | ||
b528a07e | 210 | /* Sweep up all tokens remaining on the line. */ |
345894b4 NB |
211 | if (! SEEN_EOL ()) |
212 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
213 | ; | |
93c80368 | 214 | } |
c5a04734 | 215 | |
81b5d104 MLI |
216 | /* Helper function for check_oel. */ |
217 | ||
93c80368 | 218 | static void |
81b5d104 | 219 | check_eol_1 (cpp_reader *pfile, bool expand, int reason) |
93c80368 | 220 | { |
a5cb563b JM |
221 | if (! SEEN_EOL () && (expand |
222 | ? cpp_get_token (pfile) | |
223 | : _cpp_lex_token (pfile))->type != CPP_EOF) | |
81b5d104 MLI |
224 | cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive", |
225 | pfile->directive->name); | |
226 | } | |
227 | ||
228 | /* Variant of check_eol used for Wendif-labels warnings. */ | |
229 | ||
230 | static void | |
231 | check_eol_endif_labels (cpp_reader *pfile) | |
232 | { | |
233 | check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS); | |
234 | } | |
235 | ||
236 | /* Ensure there are no stray tokens at the end of a directive. If | |
237 | EXPAND is true, tokens macro-expanding to nothing are allowed. */ | |
238 | ||
239 | static void | |
240 | check_eol (cpp_reader *pfile, bool expand) | |
241 | { | |
242 | check_eol_1 (pfile, expand, CPP_W_NONE); | |
93c80368 NB |
243 | } |
244 | ||
cbc43ae0 ILT |
245 | /* Ensure there are no stray tokens other than comments at the end of |
246 | a directive, and gather the comments. */ | |
247 | static const cpp_token ** | |
248 | check_eol_return_comments (cpp_reader *pfile) | |
249 | { | |
250 | size_t c; | |
251 | size_t capacity = 8; | |
252 | const cpp_token **buf; | |
253 | ||
254 | buf = XNEWVEC (const cpp_token *, capacity); | |
255 | c = 0; | |
256 | if (! SEEN_EOL ()) | |
257 | { | |
258 | while (1) | |
259 | { | |
260 | const cpp_token *tok; | |
261 | ||
262 | tok = _cpp_lex_token (pfile); | |
263 | if (tok->type == CPP_EOF) | |
264 | break; | |
265 | if (tok->type != CPP_COMMENT) | |
266 | cpp_error (pfile, CPP_DL_PEDWARN, | |
267 | "extra tokens at end of #%s directive", | |
268 | pfile->directive->name); | |
269 | else | |
270 | { | |
271 | if (c + 1 >= capacity) | |
272 | { | |
273 | capacity *= 2; | |
274 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); | |
275 | } | |
276 | buf[c] = tok; | |
277 | ++c; | |
278 | } | |
279 | } | |
280 | } | |
281 | buf[c] = NULL; | |
282 | return buf; | |
283 | } | |
284 | ||
fe6c2db9 NB |
285 | /* Called when entering a directive, _Pragma or command-line directive. */ |
286 | static void | |
6cf87ca4 | 287 | start_directive (cpp_reader *pfile) |
93c80368 | 288 | { |
7f2f1a66 NB |
289 | /* Setup in-directive state. */ |
290 | pfile->state.in_directive = 1; | |
291 | pfile->state.save_comments = 0; | |
21b11495 | 292 | pfile->directive_result.type = CPP_PADDING; |
7f2f1a66 | 293 | |
93c80368 | 294 | /* Some handlers need the position of the # for diagnostics. */ |
500bee0a | 295 | pfile->directive_line = pfile->line_table->highest_line; |
fe6c2db9 NB |
296 | } |
297 | ||
298 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
299 | static void | |
6cf87ca4 | 300 | end_directive (cpp_reader *pfile, int skip_line) |
fe6c2db9 | 301 | { |
c5a62c6f | 302 | if (CPP_OPTION (pfile, traditional)) |
1a76916c | 303 | { |
d97371e0 | 304 | /* Revert change of prepare_directive_trad. */ |
c5a62c6f EB |
305 | if (!pfile->state.in_deferred_pragma) |
306 | pfile->state.prevent_expansion--; | |
d97371e0 | 307 | |
b66377c1 | 308 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c NB |
309 | _cpp_remove_overlay (pfile); |
310 | } | |
c5a62c6f EB |
311 | else if (pfile->state.in_deferred_pragma) |
312 | ; | |
fe6c2db9 | 313 | /* We don't skip for an assembler #. */ |
b66377c1 | 314 | else if (skip_line) |
67821e3a NB |
315 | { |
316 | skip_rest_of_line (pfile); | |
bdcbe496 NB |
317 | if (!pfile->keep_tokens) |
318 | { | |
319 | pfile->cur_run = &pfile->base_run; | |
320 | pfile->cur_token = pfile->base_run.base; | |
321 | } | |
67821e3a | 322 | } |
fe6c2db9 NB |
323 | |
324 | /* Restore state. */ | |
fe6c2db9 NB |
325 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
326 | pfile->state.in_directive = 0; | |
d97371e0 | 327 | pfile->state.in_expression = 0; |
fe6c2db9 NB |
328 | pfile->state.angled_headers = 0; |
329 | pfile->directive = 0; | |
330 | } | |
331 | ||
1a76916c NB |
332 | /* Prepare to handle the directive in pfile->directive. */ |
333 | static void | |
6cf87ca4 | 334 | prepare_directive_trad (cpp_reader *pfile) |
1a76916c | 335 | { |
951a0766 | 336 | if (pfile->directive != &dtable[T_DEFINE]) |
1a76916c | 337 | { |
b66377c1 NB |
338 | bool no_expand = (pfile->directive |
339 | && ! (pfile->directive->flags & EXPAND)); | |
974c43f1 | 340 | bool was_skipping = pfile->state.skipping; |
1a76916c | 341 | |
d97371e0 NB |
342 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
343 | || pfile->directive == &dtable[T_ELIF]); | |
45f2492c NB |
344 | if (pfile->state.in_expression) |
345 | pfile->state.skipping = false; | |
346 | ||
1a76916c NB |
347 | if (no_expand) |
348 | pfile->state.prevent_expansion++; | |
43839642 | 349 | _cpp_scan_out_logical_line (pfile, NULL); |
1a76916c NB |
350 | if (no_expand) |
351 | pfile->state.prevent_expansion--; | |
45f2492c | 352 | |
974c43f1 | 353 | pfile->state.skipping = was_skipping; |
1a76916c NB |
354 | _cpp_overlay_buffer (pfile, pfile->out.base, |
355 | pfile->out.cur - pfile->out.base); | |
356 | } | |
d97371e0 NB |
357 | |
358 | /* Stop ISO C from expanding anything. */ | |
359 | pfile->state.prevent_expansion++; | |
1a76916c NB |
360 | } |
361 | ||
da7d8304 | 362 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
18a9d8ff | 363 | the '#' was indented. */ |
18a9d8ff | 364 | static void |
6cf87ca4 | 365 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
18a9d8ff | 366 | { |
899015a0 TT |
367 | /* Issue -pedantic or deprecated warnings for extensions. We let |
368 | -pedantic take precedence if both are applicable. */ | |
369 | if (! pfile->state.skipping) | |
370 | { | |
371 | if (dir->origin == EXTENSION | |
372 | && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc)) | |
373 | && CPP_PEDANTIC (pfile)) | |
374 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); | |
375 | else if (((dir->flags & DEPRECATED) != 0 | |
376 | || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc))) | |
e3339d0f | 377 | && CPP_OPTION (pfile, cpp_warn_deprecated)) |
87cf0651 SB |
378 | cpp_warning (pfile, CPP_W_DEPRECATED, |
379 | "#%s is a deprecated GCC extension", dir->name); | |
899015a0 | 380 | } |
dcc229e5 ZW |
381 | |
382 | /* Traditionally, a directive is ignored unless its # is in | |
383 | column 1. Therefore in code intended to work with K+R | |
384 | compilers, directives added by C89 must have their # | |
385 | indented, and directives present in traditional C must not. | |
386 | This is true even of directives in skipped conditional | |
387 | blocks. #elif cannot be used at all. */ | |
388 | if (CPP_WTRADITIONAL (pfile)) | |
18a9d8ff | 389 | { |
dcc229e5 | 390 | if (dir == &dtable[T_ELIF]) |
87cf0651 SB |
391 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
392 | "suggest not using #elif in traditional C"); | |
dcc229e5 | 393 | else if (indented && dir->origin == KANDR) |
87cf0651 SB |
394 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
395 | "traditional C ignores #%s with the # indented", | |
396 | dir->name); | |
dcc229e5 | 397 | else if (!indented && dir->origin != KANDR) |
87cf0651 SB |
398 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
399 | "suggest hiding #%s from traditional C with an indented #", | |
400 | dir->name); | |
18a9d8ff NB |
401 | } |
402 | } | |
403 | ||
da7d8304 | 404 | /* Check if we have a known directive. INDENTED is nonzero if the |
18a9d8ff | 405 | '#' of the directive was indented. This function is in this file |
a2566ae9 | 406 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
da7d8304 | 407 | nonzero if the line of tokens has been handled, zero if we should |
18a9d8ff | 408 | continue processing the line. */ |
fe6c2db9 | 409 | int |
6cf87ca4 | 410 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
fe6c2db9 | 411 | { |
fe6c2db9 | 412 | const directive *dir = 0; |
345894b4 | 413 | const cpp_token *dname; |
e808ec9c | 414 | bool was_parsing_args = pfile->state.parsing_args; |
c6e83800 | 415 | bool was_discarding_output = pfile->state.discarding_output; |
fe6c2db9 NB |
416 | int skip = 1; |
417 | ||
c6e83800 ZW |
418 | if (was_discarding_output) |
419 | pfile->state.prevent_expansion = 0; | |
420 | ||
e808ec9c NB |
421 | if (was_parsing_args) |
422 | { | |
e3339d0f | 423 | if (CPP_OPTION (pfile, cpp_pedantic)) |
0527bc4e | 424 | cpp_error (pfile, CPP_DL_PEDWARN, |
e808ec9c NB |
425 | "embedding a directive within macro arguments is not portable"); |
426 | pfile->state.parsing_args = 0; | |
427 | pfile->state.prevent_expansion = 0; | |
428 | } | |
fe6c2db9 | 429 | start_directive (pfile); |
345894b4 | 430 | dname = _cpp_lex_token (pfile); |
0d9f234d | 431 | |
345894b4 | 432 | if (dname->type == CPP_NAME) |
0d9f234d | 433 | { |
9a0c6187 JM |
434 | if (dname->val.node.node->is_directive) |
435 | dir = &dtable[dname->val.node.node->directive_index]; | |
0d9f234d | 436 | } |
05713b80 | 437 | /* We do not recognize the # followed by a number extension in |
18a9d8ff | 438 | assembler code. */ |
345894b4 | 439 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
0d9f234d | 440 | { |
dcc229e5 ZW |
441 | dir = &linemarker_dir; |
442 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
443 | && ! pfile->state.skipping) | |
0527bc4e | 444 | cpp_error (pfile, CPP_DL_PEDWARN, |
ebef4e8c | 445 | "style of line directive is a GCC extension"); |
93c80368 | 446 | } |
0d9f234d | 447 | |
93c80368 NB |
448 | if (dir) |
449 | { | |
18a9d8ff NB |
450 | /* If we have a directive that is not an opening conditional, |
451 | invalidate any control macro. */ | |
452 | if (! (dir->flags & IF_COND)) | |
453 | pfile->mi_valid = false; | |
454 | ||
455 | /* Kluge alert. In order to be sure that code like this | |
456 | ||
457 | #define HASH # | |
458 | HASH define foo bar | |
459 | ||
460 | does not cause '#define foo bar' to get executed when | |
461 | compiled with -save-temps, we recognize directives in | |
a2566ae9 | 462 | -fpreprocessed mode only if the # is in column 1. macro.c |
ccfc4c91 OW |
463 | puts a space in front of any '#' at the start of a macro. |
464 | ||
465 | We exclude the -fdirectives-only case because macro expansion | |
466 | has not been performed yet, and block comments can cause spaces | |
7d9641cc | 467 | to precede the directive. */ |
18a9d8ff | 468 | if (CPP_OPTION (pfile, preprocessed) |
ccfc4c91 | 469 | && !CPP_OPTION (pfile, directives_only) |
18a9d8ff | 470 | && (indented || !(dir->flags & IN_I))) |
6d4587f7 | 471 | { |
18a9d8ff NB |
472 | skip = 0; |
473 | dir = 0; | |
6d4587f7 ZW |
474 | } |
475 | else | |
93c80368 | 476 | { |
18a9d8ff NB |
477 | /* In failed conditional groups, all non-conditional |
478 | directives are ignored. Before doing that, whether | |
479 | skipping or not, we should lex angle-bracketed headers | |
480 | correctly, and maybe output some diagnostics. */ | |
481 | pfile->state.angled_headers = dir->flags & INCL; | |
a8d0ddaf | 482 | pfile->state.directive_wants_padding = dir->flags & INCL; |
18a9d8ff NB |
483 | if (! CPP_OPTION (pfile, preprocessed)) |
484 | directive_diagnostics (pfile, dir, indented); | |
485 | if (pfile->state.skipping && !(dir->flags & COND)) | |
486 | dir = 0; | |
93c80368 NB |
487 | } |
488 | } | |
345894b4 | 489 | else if (dname->type == CPP_EOF) |
18a9d8ff NB |
490 | ; /* CPP_EOF is the "null directive". */ |
491 | else | |
93c80368 NB |
492 | { |
493 | /* An unknown directive. Don't complain about it in assembly | |
494 | source: we don't know where the comments are, and # may | |
495 | introduce assembler pseudo-ops. Don't complain about invalid | |
496 | directives in skipped conditional groups (6.10 p4). */ | |
bdb05a7b | 497 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
18a9d8ff NB |
498 | skip = 0; |
499 | else if (!pfile->state.skipping) | |
0527bc4e | 500 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
345894b4 | 501 | cpp_token_as_text (pfile, dname)); |
0d9f234d NB |
502 | } |
503 | ||
d1a58688 NB |
504 | pfile->directive = dir; |
505 | if (CPP_OPTION (pfile, traditional)) | |
506 | prepare_directive_trad (pfile); | |
507 | ||
18a9d8ff | 508 | if (dir) |
6cf87ca4 | 509 | pfile->directive->handler (pfile); |
18a9d8ff NB |
510 | else if (skip == 0) |
511 | _cpp_backup_tokens (pfile, 1); | |
512 | ||
513 | end_directive (pfile, skip); | |
765d600a | 514 | if (was_parsing_args && !pfile->state.in_deferred_pragma) |
e808ec9c NB |
515 | { |
516 | /* Restore state when within macro args. */ | |
517 | pfile->state.parsing_args = 2; | |
518 | pfile->state.prevent_expansion = 1; | |
e808ec9c | 519 | } |
c6e83800 ZW |
520 | if (was_discarding_output) |
521 | pfile->state.prevent_expansion = 1; | |
fe6c2db9 | 522 | return skip; |
041c3194 | 523 | } |
7f2935c7 | 524 | |
93c80368 | 525 | /* Directive handler wrapper used by the command line option |
26aea073 | 526 | processor. BUF is \n terminated. */ |
93c80368 | 527 | static void |
6cf87ca4 | 528 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
3fdc651f | 529 | { |
562a5c27 | 530 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
40de9f76 | 531 | /* from_stage3 */ true); |
0bda4760 | 532 | start_directive (pfile); |
26aea073 NB |
533 | |
534 | /* This is a short-term fix to prevent a leading '#' being | |
535 | interpreted as a directive. */ | |
536 | _cpp_clean_line (pfile); | |
537 | ||
f71aebba | 538 | pfile->directive = &dtable[dir_no]; |
1a76916c NB |
539 | if (CPP_OPTION (pfile, traditional)) |
540 | prepare_directive_trad (pfile); | |
6cf87ca4 | 541 | pfile->directive->handler (pfile); |
0bda4760 | 542 | end_directive (pfile, 1); |
ef6e958a | 543 | _cpp_pop_buffer (pfile); |
93c80368 NB |
544 | } |
545 | ||
546 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
ee1c2a10 TT |
547 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
548 | processing a #define or #undefine directive, and false | |
549 | otherwise. */ | |
041c3194 | 550 | static cpp_hashnode * |
ee1c2a10 | 551 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
041c3194 | 552 | { |
1a76916c | 553 | const cpp_token *token = _cpp_lex_token (pfile); |
ea4a453b | 554 | |
92936ecf | 555 | /* The token immediately after #define must be an identifier. That |
b8363a24 ZW |
556 | identifier may not be "defined", per C99 6.10.8p4. |
557 | In C++, it may not be any of the "named operators" either, | |
558 | per C++98 [lex.digraph], [lex.key]. | |
559 | Finally, the identifier may not have been poisoned. (In that case | |
1d63a28a | 560 | the lexer has issued the error message for us.) */ |
cbc69f84 | 561 | |
1a76916c NB |
562 | if (token->type == CPP_NAME) |
563 | { | |
9a0c6187 | 564 | cpp_hashnode *node = token->val.node.node; |
92936ecf | 565 | |
ee1c2a10 | 566 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
0527bc4e | 567 | cpp_error (pfile, CPP_DL_ERROR, |
1a76916c | 568 | "\"defined\" cannot be used as a macro name"); |
a15f7cb8 ESR |
569 | else if (is_def_or_undef |
570 | && (node == pfile->spec_nodes.n__has_include__ | |
571 | || node == pfile->spec_nodes.n__has_include_next__)) | |
572 | cpp_error (pfile, CPP_DL_ERROR, | |
573 | "\"__has_include__\" cannot be used as a macro name"); | |
1a76916c NB |
574 | else if (! (node->flags & NODE_POISONED)) |
575 | return node; | |
ba89d661 | 576 | } |
1a76916c | 577 | else if (token->flags & NAMED_OP) |
0527bc4e | 578 | cpp_error (pfile, CPP_DL_ERROR, |
cbc69f84 | 579 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
9a0c6187 | 580 | NODE_NAME (token->val.node.node)); |
1a76916c | 581 | else if (token->type == CPP_EOF) |
0527bc4e | 582 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
1a76916c NB |
583 | pfile->directive->name); |
584 | else | |
0527bc4e | 585 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
07aa0b04 | 586 | |
cbc69f84 | 587 | return NULL; |
7f2935c7 | 588 | } |
7f2935c7 | 589 | |
a2566ae9 | 590 | /* Process a #define directive. Most work is done in macro.c. */ |
711b8824 | 591 | static void |
6cf87ca4 | 592 | do_define (cpp_reader *pfile) |
7f2935c7 | 593 | { |
ee1c2a10 | 594 | cpp_hashnode *node = lex_macro_node (pfile, true); |
ff2b53ef | 595 | |
93c80368 NB |
596 | if (node) |
597 | { | |
1d63a28a NB |
598 | /* If we have been requested to expand comments into macros, |
599 | then re-enable saving of comments. */ | |
600 | pfile->state.save_comments = | |
601 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
602 | ||
93d45d9e JM |
603 | if (pfile->cb.before_define) |
604 | pfile->cb.before_define (pfile); | |
605 | ||
93c80368 NB |
606 | if (_cpp_create_definition (pfile, node)) |
607 | if (pfile->cb.define) | |
6cf87ca4 | 608 | pfile->cb.define (pfile, pfile->directive_line, node); |
93d45d9e JM |
609 | |
610 | node->flags &= ~NODE_USED; | |
93c80368 | 611 | } |
041c3194 ZW |
612 | } |
613 | ||
5d8ebbd8 | 614 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
711b8824 | 615 | static void |
6cf87ca4 | 616 | do_undef (cpp_reader *pfile) |
041c3194 | 617 | { |
ee1c2a10 | 618 | cpp_hashnode *node = lex_macro_node (pfile, true); |
9e62c811 | 619 | |
45f2492c | 620 | if (node) |
9e62c811 | 621 | { |
93d45d9e JM |
622 | if (pfile->cb.before_define) |
623 | pfile->cb.before_define (pfile); | |
624 | ||
58fea6af | 625 | if (pfile->cb.undef) |
6cf87ca4 | 626 | pfile->cb.undef (pfile, pfile->directive_line, node); |
9e62c811 | 627 | |
45f2492c NB |
628 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
629 | identifier is not currently defined as a macro name. */ | |
630 | if (node->type == NT_MACRO) | |
631 | { | |
632 | if (node->flags & NODE_WARN) | |
633 | cpp_error (pfile, CPP_DL_WARNING, | |
634 | "undefining \"%s\"", NODE_NAME (node)); | |
b753b37b MLI |
635 | else if ((node->flags & NODE_BUILTIN) |
636 | && CPP_OPTION (pfile, warn_builtin_macro_redefined)) | |
637 | cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED, | |
638 | pfile->directive_line, 0, | |
639 | "undefining \"%s\"", NODE_NAME (node)); | |
9e62c811 | 640 | |
45f2492c NB |
641 | if (CPP_OPTION (pfile, warn_unused_macros)) |
642 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
a69cbaac | 643 | |
45f2492c NB |
644 | _cpp_free_definition (node); |
645 | } | |
9e62c811 | 646 | } |
45f2492c | 647 | |
a5cb563b | 648 | check_eol (pfile, false); |
7f2935c7 PB |
649 | } |
650 | ||
d1bd0ded GK |
651 | /* Undefine a single macro/assertion/whatever. */ |
652 | ||
653 | static int | |
c6e83800 | 654 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
d1bd0ded GK |
655 | void *data_p ATTRIBUTE_UNUSED) |
656 | { | |
c6e83800 ZW |
657 | /* Body of _cpp_free_definition inlined here for speed. |
658 | Macros and assertions no longer have anything to free. */ | |
659 | h->type = NT_VOID; | |
93d45d9e | 660 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); |
d1bd0ded GK |
661 | return 1; |
662 | } | |
663 | ||
664 | /* Undefine all macros and assertions. */ | |
665 | ||
666 | void | |
667 | cpp_undef_all (cpp_reader *pfile) | |
668 | { | |
669 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
670 | } | |
671 | ||
672 | ||
93c80368 NB |
673 | /* Helper routine used by parse_include. Reinterpret the current line |
674 | as an h-char-sequence (< ... >); we are looking at the first token | |
74eb4b3e NB |
675 | after the <. Returns a malloced filename. */ |
676 | static char * | |
6cf87ca4 | 677 | glue_header_name (cpp_reader *pfile) |
93c80368 | 678 | { |
4ed5bcfb | 679 | const cpp_token *token; |
74eb4b3e | 680 | char *buffer; |
2450e0b8 | 681 | size_t len, total_len = 0, capacity = 1024; |
93c80368 NB |
682 | |
683 | /* To avoid lexed tokens overwriting our glued name, we can only | |
684 | allocate from the string pool once we've lexed everything. */ | |
c3f829c1 | 685 | buffer = XNEWVEC (char, capacity); |
93c80368 NB |
686 | for (;;) |
687 | { | |
a8d0ddaf | 688 | token = get_token_no_padding (pfile); |
93c80368 | 689 | |
74eb4b3e | 690 | if (token->type == CPP_GREATER) |
93c80368 | 691 | break; |
74eb4b3e NB |
692 | if (token->type == CPP_EOF) |
693 | { | |
0527bc4e | 694 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
74eb4b3e NB |
695 | break; |
696 | } | |
93c80368 | 697 | |
59325650 | 698 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
2450e0b8 | 699 | if (total_len + len > capacity) |
93c80368 | 700 | { |
2450e0b8 | 701 | capacity = (capacity + len) * 2; |
c3f829c1 | 702 | buffer = XRESIZEVEC (char, buffer, capacity); |
93c80368 NB |
703 | } |
704 | ||
4ed5bcfb | 705 | if (token->flags & PREV_WHITE) |
2450e0b8 | 706 | buffer[total_len++] = ' '; |
93c80368 | 707 | |
47e20491 GK |
708 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
709 | true) | |
74eb4b3e | 710 | - (uchar *) buffer); |
93c80368 | 711 | } |
041c3194 | 712 | |
74eb4b3e NB |
713 | buffer[total_len] = '\0'; |
714 | return buffer; | |
93c80368 | 715 | } |
7f2935c7 | 716 | |
74eb4b3e NB |
717 | /* Returns the file name of #include, #include_next, #import and |
718 | #pragma dependency. The string is malloced and the caller should | |
a28fbdba MLI |
719 | free it. Returns NULL on error. LOCATION is the source location |
720 | of the file name. */ | |
721 | ||
74eb4b3e | 722 | static const char * |
cbc43ae0 | 723 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
a28fbdba | 724 | const cpp_token ***buf, source_location *location) |
7f2935c7 | 725 | { |
74eb4b3e | 726 | char *fname; |
4ed5bcfb | 727 | const cpp_token *header; |
7f2935c7 | 728 | |
93c80368 | 729 | /* Allow macro expansion. */ |
a8d0ddaf | 730 | header = get_token_no_padding (pfile); |
a28fbdba | 731 | *location = header->src_loc; |
2c6e3f55 JJ |
732 | if ((header->type == CPP_STRING && header->val.str.text[0] != 'R') |
733 | || header->type == CPP_HEADER_NAME) | |
7f2935c7 | 734 | { |
c3f829c1 | 735 | fname = XNEWVEC (char, header->val.str.len - 1); |
6338b358 NB |
736 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
737 | fname[header->val.str.len - 2] = '\0'; | |
74eb4b3e | 738 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
7f2935c7 | 739 | } |
74eb4b3e | 740 | else if (header->type == CPP_LESS) |
7f2935c7 | 741 | { |
74eb4b3e NB |
742 | fname = glue_header_name (pfile); |
743 | *pangle_brackets = 1; | |
744 | } | |
745 | else | |
746 | { | |
747 | const unsigned char *dir; | |
748 | ||
749 | if (pfile->directive == &dtable[T_PRAGMA]) | |
b6baa67d | 750 | dir = UC"pragma dependency"; |
74eb4b3e NB |
751 | else |
752 | dir = pfile->directive->name; | |
0527bc4e | 753 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
74eb4b3e NB |
754 | dir); |
755 | ||
4ed5bcfb | 756 | return NULL; |
7f2935c7 | 757 | } |
7f2935c7 | 758 | |
cda5e672 TT |
759 | if (pfile->directive == &dtable[T_PRAGMA]) |
760 | { | |
761 | /* This pragma allows extra tokens after the file name. */ | |
762 | } | |
763 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) | |
61cc8223 | 764 | check_eol (pfile, true); |
cbc43ae0 ILT |
765 | else |
766 | { | |
767 | /* If we are not discarding comments, then gather them while | |
768 | doing the eol check. */ | |
769 | *buf = check_eol_return_comments (pfile); | |
770 | } | |
771 | ||
74eb4b3e | 772 | return fname; |
168d3732 | 773 | } |
3caee4a8 | 774 | |
ba133c96 | 775 | /* Handle #include, #include_next and #import. */ |
711b8824 | 776 | static void |
6cf87ca4 | 777 | do_include_common (cpp_reader *pfile, enum include_type type) |
168d3732 | 778 | { |
74eb4b3e NB |
779 | const char *fname; |
780 | int angle_brackets; | |
cbc43ae0 | 781 | const cpp_token **buf = NULL; |
a28fbdba | 782 | source_location location; |
cbc43ae0 ILT |
783 | |
784 | /* Re-enable saving of comments if requested, so that the include | |
785 | callback can dump comments which follow #include. */ | |
786 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
74eb4b3e | 787 | |
a28fbdba | 788 | fname = parse_include (pfile, &angle_brackets, &buf, &location); |
74eb4b3e | 789 | if (!fname) |
cbc43ae0 ILT |
790 | { |
791 | if (buf) | |
792 | XDELETEVEC (buf); | |
793 | return; | |
794 | } | |
7f2935c7 | 795 | |
28303828 NN |
796 | if (!*fname) |
797 | { | |
a28fbdba MLI |
798 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
799 | "empty filename in #%s", | |
800 | pfile->directive->name); | |
cbc43ae0 ILT |
801 | XDELETEVEC (fname); |
802 | if (buf) | |
803 | XDELETEVEC (buf); | |
28303828 NN |
804 | return; |
805 | } | |
806 | ||
3963c2e0 | 807 | /* Prevent #include recursion. */ |
50f59cd7 | 808 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
0527bc4e | 809 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
74eb4b3e | 810 | else |
09b82253 | 811 | { |
74eb4b3e NB |
812 | /* Get out of macro context, if we are. */ |
813 | skip_rest_of_line (pfile); | |
09b82253 | 814 | |
74eb4b3e | 815 | if (pfile->cb.include) |
6cf87ca4 | 816 | pfile->cb.include (pfile, pfile->directive_line, |
cbc43ae0 ILT |
817 | pfile->directive->name, fname, angle_brackets, |
818 | buf); | |
3963c2e0 | 819 | |
8f9b4009 | 820 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
74eb4b3e | 821 | } |
3963c2e0 | 822 | |
cbc43ae0 ILT |
823 | XDELETEVEC (fname); |
824 | if (buf) | |
825 | XDELETEVEC (buf); | |
168d3732 | 826 | } |
e8037d57 | 827 | |
711b8824 | 828 | static void |
6cf87ca4 | 829 | do_include (cpp_reader *pfile) |
168d3732 | 830 | { |
ba133c96 NB |
831 | do_include_common (pfile, IT_INCLUDE); |
832 | } | |
168d3732 | 833 | |
ba133c96 | 834 | static void |
6cf87ca4 | 835 | do_import (cpp_reader *pfile) |
ba133c96 | 836 | { |
ba133c96 | 837 | do_include_common (pfile, IT_IMPORT); |
168d3732 | 838 | } |
7f2935c7 | 839 | |
711b8824 | 840 | static void |
6cf87ca4 | 841 | do_include_next (cpp_reader *pfile) |
168d3732 | 842 | { |
3963c2e0 ZW |
843 | enum include_type type = IT_INCLUDE_NEXT; |
844 | ||
845 | /* If this is the primary source file, warn and use the normal | |
846 | search logic. */ | |
705e2d28 | 847 | if (cpp_in_primary_file (pfile)) |
3963c2e0 | 848 | { |
0527bc4e | 849 | cpp_error (pfile, CPP_DL_WARNING, |
3963c2e0 ZW |
850 | "#include_next in primary source file"); |
851 | type = IT_INCLUDE; | |
852 | } | |
853 | do_include_common (pfile, type); | |
7f2935c7 | 854 | } |
7f2935c7 | 855 | |
dcc229e5 ZW |
856 | /* Subroutine of do_linemarker. Read possible flags after file name. |
857 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
858 | flag if it is valid, 0 at the end of the directive. Otherwise | |
859 | complain. */ | |
642ce434 | 860 | static unsigned int |
6cf87ca4 | 861 | read_flag (cpp_reader *pfile, unsigned int last) |
d3a34a0a | 862 | { |
345894b4 | 863 | const cpp_token *token = _cpp_lex_token (pfile); |
d3a34a0a | 864 | |
345894b4 | 865 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
d3a34a0a | 866 | { |
345894b4 | 867 | unsigned int flag = token->val.str.text[0] - '0'; |
28e0f040 NB |
868 | |
869 | if (flag > last && flag <= 4 | |
870 | && (flag != 4 || last == 3) | |
871 | && (flag != 2 || last == 0)) | |
872 | return flag; | |
d3a34a0a | 873 | } |
93c80368 | 874 | |
345894b4 | 875 | if (token->type != CPP_EOF) |
0527bc4e | 876 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
345894b4 | 877 | cpp_token_as_text (pfile, token)); |
93c80368 | 878 | return 0; |
d3a34a0a JM |
879 | } |
880 | ||
dcc229e5 | 881 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
3b8f20a1 MLI |
882 | of length LEN, to binary; store it in NUMP, and return false if the |
883 | number was well-formed, true if not. WRAPPED is set to true if the | |
884 | number did not fit into 'unsigned long'. */ | |
885 | static bool | |
886 | strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped) | |
041c3194 | 887 | { |
1bb64668 | 888 | linenum_type reg = 0; |
3b8f20a1 MLI |
889 | linenum_type reg_prev = 0; |
890 | ||
562a5c27 | 891 | uchar c; |
3b8f20a1 | 892 | *wrapped = false; |
041c3194 ZW |
893 | while (len--) |
894 | { | |
895 | c = *str++; | |
896 | if (!ISDIGIT (c)) | |
3b8f20a1 | 897 | return true; |
041c3194 ZW |
898 | reg *= 10; |
899 | reg += c - '0'; | |
3b8f20a1 MLI |
900 | if (reg < reg_prev) |
901 | *wrapped = true; | |
902 | reg_prev = reg; | |
041c3194 ZW |
903 | } |
904 | *nump = reg; | |
3b8f20a1 | 905 | return false; |
041c3194 ZW |
906 | } |
907 | ||
6de1e2a9 | 908 | /* Interpret #line command. |
dcc229e5 ZW |
909 | Note that the filename string (if any) is a true string constant |
910 | (escapes are interpreted), unlike in #line. */ | |
711b8824 | 911 | static void |
6cf87ca4 | 912 | do_line (cpp_reader *pfile) |
7f2935c7 | 913 | { |
500bee0a | 914 | const struct line_maps *line_table = pfile->line_table; |
46427374 | 915 | const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
2203a881 DP |
916 | |
917 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down | |
918 | sysp right now. */ | |
919 | ||
46427374 | 920 | unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
4ed5bcfb | 921 | const cpp_token *token; |
46427374 | 922 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
1bb64668 | 923 | linenum_type new_lineno; |
93c80368 | 924 | |
27e2564a | 925 | /* C99 raised the minimum limit on #line numbers. */ |
1bb64668 | 926 | linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
3b8f20a1 | 927 | bool wrapped; |
18a9d8ff | 928 | |
93c80368 | 929 | /* #line commands expand macros. */ |
4ed5bcfb NB |
930 | token = cpp_get_token (pfile); |
931 | if (token->type != CPP_NUMBER | |
1bb64668 | 932 | || strtolinenum (token->val.str.text, token->val.str.len, |
3b8f20a1 | 933 | &new_lineno, &wrapped)) |
7f2935c7 | 934 | { |
33ae4837 TT |
935 | if (token->type == CPP_EOF) |
936 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); | |
937 | else | |
938 | cpp_error (pfile, CPP_DL_ERROR, | |
939 | "\"%s\" after #line is not a positive integer", | |
940 | cpp_token_as_text (pfile, token)); | |
9ec7291f | 941 | return; |
df383483 | 942 | } |
7f2935c7 | 943 | |
3b8f20a1 | 944 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped)) |
0527bc4e | 945 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
3b8f20a1 MLI |
946 | else if (wrapped) |
947 | cpp_error (pfile, CPP_DL_WARNING, "line number out of range"); | |
7f2935c7 | 948 | |
4ed5bcfb NB |
949 | token = cpp_get_token (pfile); |
950 | if (token->type == CPP_STRING) | |
5538ada6 | 951 | { |
e6cc3a24 | 952 | cpp_string s = { 0, 0 }; |
423e95e2 | 953 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
f1bf410c | 954 | &s, CPP_STRING)) |
e6cc3a24 | 955 | new_file = (const char *)s.text; |
a5cb563b | 956 | check_eol (pfile, true); |
dcc229e5 ZW |
957 | } |
958 | else if (token->type != CPP_EOF) | |
959 | { | |
0527bc4e | 960 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
dcc229e5 ZW |
961 | cpp_token_as_text (pfile, token)); |
962 | return; | |
963 | } | |
964 | ||
965 | skip_rest_of_line (pfile); | |
c7f9c0b9 | 966 | _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno, |
2203a881 | 967 | map_sysp); |
dcc229e5 ZW |
968 | } |
969 | ||
970 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
971 | different syntax and semantics from #line: Flags are allowed, | |
972 | and we never complain about the line number being too big. */ | |
973 | static void | |
6cf87ca4 | 974 | do_linemarker (cpp_reader *pfile) |
dcc229e5 | 975 | { |
500bee0a | 976 | const struct line_maps *line_table = pfile->line_table; |
46427374 | 977 | const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
dcc229e5 | 978 | const cpp_token *token; |
46427374 | 979 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
1bb64668 | 980 | linenum_type new_lineno; |
46427374 | 981 | unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
c7f9c0b9 | 982 | enum lc_reason reason = LC_RENAME_VERBATIM; |
dcc229e5 | 983 | int flag; |
3b8f20a1 | 984 | bool wrapped; |
dcc229e5 ZW |
985 | |
986 | /* Back up so we can get the number again. Putting this in | |
987 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
988 | some circumstances, which can segfault. */ | |
989 | _cpp_backup_tokens (pfile, 1); | |
990 | ||
991 | /* #line commands expand macros. */ | |
992 | token = cpp_get_token (pfile); | |
993 | if (token->type != CPP_NUMBER | |
1bb64668 | 994 | || strtolinenum (token->val.str.text, token->val.str.len, |
3b8f20a1 | 995 | &new_lineno, &wrapped)) |
dcc229e5 | 996 | { |
33ae4837 TT |
997 | /* Unlike #line, there does not seem to be a way to get an EOF |
998 | here. So, it should be safe to always spell the token. */ | |
0527bc4e JDA |
999 | cpp_error (pfile, CPP_DL_ERROR, |
1000 | "\"%s\" after # is not a positive integer", | |
dcc229e5 ZW |
1001 | cpp_token_as_text (pfile, token)); |
1002 | return; | |
df383483 | 1003 | } |
941e09b6 | 1004 | |
dcc229e5 ZW |
1005 | token = cpp_get_token (pfile); |
1006 | if (token->type == CPP_STRING) | |
1007 | { | |
e6cc3a24 | 1008 | cpp_string s = { 0, 0 }; |
423e95e2 | 1009 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
f1bf410c | 1010 | 1, &s, CPP_STRING)) |
e6cc3a24 | 1011 | new_file = (const char *)s.text; |
cf551fba | 1012 | |
dcc229e5 ZW |
1013 | new_sysp = 0; |
1014 | flag = read_flag (pfile, 0); | |
1015 | if (flag == 1) | |
1016 | { | |
1017 | reason = LC_ENTER; | |
1018 | /* Fake an include for cpp_included (). */ | |
1019 | _cpp_fake_include (pfile, new_file); | |
1020 | flag = read_flag (pfile, flag); | |
1021 | } | |
1022 | else if (flag == 2) | |
1023 | { | |
1024 | reason = LC_LEAVE; | |
1025 | flag = read_flag (pfile, flag); | |
1026 | } | |
1027 | if (flag == 3) | |
93c80368 | 1028 | { |
dcc229e5 ZW |
1029 | new_sysp = 1; |
1030 | flag = read_flag (pfile, flag); | |
1031 | if (flag == 4) | |
1032 | new_sysp = 2; | |
93c80368 | 1033 | } |
9d30f270 | 1034 | pfile->buffer->sysp = new_sysp; |
dcc229e5 | 1035 | |
a5cb563b | 1036 | check_eol (pfile, false); |
041c3194 | 1037 | } |
4ed5bcfb | 1038 | else if (token->type != CPP_EOF) |
27e2564a | 1039 | { |
0527bc4e | 1040 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
4ed5bcfb | 1041 | cpp_token_as_text (pfile, token)); |
27e2564a NB |
1042 | return; |
1043 | } | |
7f2935c7 | 1044 | |
bdcbe496 | 1045 | skip_rest_of_line (pfile); |
00b0c19b MLI |
1046 | |
1047 | /* Compensate for the increment in linemap_add that occurs in | |
1048 | _cpp_do_file_change. We're currently at the start of the line | |
1049 | *following* the #line directive. A separate source_location for this | |
1050 | location makes no sense (until we do the LC_LEAVE), and | |
1051 | complicates LAST_SOURCE_LINE_LOCATION. */ | |
1052 | pfile->line_table->highest_location--; | |
1053 | ||
bb74c963 | 1054 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
27e2564a NB |
1055 | } |
1056 | ||
67821e3a | 1057 | /* Arrange the file_change callback. pfile->line has changed to |
47d89cf3 | 1058 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
a1f300c0 | 1059 | header, 2 for a system header that needs to be extern "C" protected, |
47d89cf3 | 1060 | and zero otherwise. */ |
eb1f4d9d | 1061 | void |
6cf87ca4 | 1062 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
1bb64668 | 1063 | const char *to_file, linenum_type file_line, |
6cf87ca4 | 1064 | unsigned int sysp) |
27e2564a | 1065 | { |
12f9df4e PB |
1066 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
1067 | to_file, file_line); | |
500bee0a | 1068 | if (map != NULL) |
46427374 TT |
1069 | linemap_line_start (pfile->line_table, |
1070 | ORDINARY_MAP_STARTING_LINE_NUMBER (map), | |
1071 | 127); | |
d82fc108 | 1072 | |
eb1f4d9d | 1073 | if (pfile->cb.file_change) |
12f9df4e | 1074 | pfile->cb.file_change (pfile, map); |
7f2935c7 | 1075 | } |
941e09b6 | 1076 | |
5d8ebbd8 NB |
1077 | /* Report a warning or error detected by the program we are |
1078 | processing. Use the directive's tokens in the error message. */ | |
711b8824 | 1079 | static void |
87cf0651 | 1080 | do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir) |
7f2935c7 | 1081 | { |
5d6342eb TT |
1082 | const unsigned char *dir_name; |
1083 | unsigned char *line; | |
1084 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1085 | ||
1086 | if (print_dir) | |
1087 | dir_name = pfile->directive->name; | |
1088 | else | |
1089 | dir_name = NULL; | |
1090 | pfile->state.prevent_expansion++; | |
1091 | line = cpp_output_line_to_string (pfile, dir_name); | |
1092 | pfile->state.prevent_expansion--; | |
1093 | ||
87cf0651 SB |
1094 | if (code == CPP_DL_WARNING_SYSHDR && reason) |
1095 | cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line); | |
1096 | else if (code == CPP_DL_WARNING && reason) | |
1097 | cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line); | |
1098 | else | |
1099 | cpp_error_with_line (pfile, code, src_loc, 0, "%s", line); | |
5d6342eb | 1100 | free (line); |
7f2935c7 PB |
1101 | } |
1102 | ||
838f313b | 1103 | static void |
6cf87ca4 | 1104 | do_error (cpp_reader *pfile) |
838f313b | 1105 | { |
87cf0651 | 1106 | do_diagnostic (pfile, CPP_DL_ERROR, 0, 1); |
838f313b | 1107 | } |
7f2935c7 | 1108 | |
711b8824 | 1109 | static void |
6cf87ca4 | 1110 | do_warning (cpp_reader *pfile) |
7f2935c7 | 1111 | { |
2f878973 | 1112 | /* We want #warning diagnostics to be emitted in system headers too. */ |
87cf0651 | 1113 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1); |
7f2935c7 PB |
1114 | } |
1115 | ||
a2a76ce7 | 1116 | /* Report program identification. */ |
711b8824 | 1117 | static void |
6cf87ca4 | 1118 | do_ident (cpp_reader *pfile) |
7f2935c7 | 1119 | { |
4ed5bcfb | 1120 | const cpp_token *str = cpp_get_token (pfile); |
58fea6af | 1121 | |
4ed5bcfb | 1122 | if (str->type != CPP_STRING) |
1ed17cd5 ZW |
1123 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
1124 | pfile->directive->name); | |
93c80368 | 1125 | else if (pfile->cb.ident) |
6cf87ca4 | 1126 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
a2a76ce7 | 1127 | |
a5cb563b | 1128 | check_eol (pfile, false); |
7f2935c7 PB |
1129 | } |
1130 | ||
a5da89c6 NB |
1131 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
1132 | matching entry, or NULL if none is found. The returned entry could | |
1133 | be the start of a namespace chain, or a pragma. */ | |
1134 | static struct pragma_entry * | |
6cf87ca4 | 1135 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
82443371 | 1136 | { |
4b115ff0 NB |
1137 | while (chain && chain->pragma != pragma) |
1138 | chain = chain->next; | |
a5da89c6 NB |
1139 | |
1140 | return chain; | |
1141 | } | |
1142 | ||
bc4071dd RH |
1143 | /* Create and insert a blank pragma entry at the beginning of a |
1144 | singly-linked CHAIN. */ | |
a5da89c6 | 1145 | static struct pragma_entry * |
bc4071dd | 1146 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
82443371 | 1147 | { |
c3f829c1 | 1148 | struct pragma_entry *new_entry; |
58fea6af | 1149 | |
c3f829c1 | 1150 | new_entry = (struct pragma_entry *) |
8c3b2693 | 1151 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
58fea6af | 1152 | |
bc4071dd | 1153 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
c3f829c1 | 1154 | new_entry->next = *chain; |
bc4071dd | 1155 | |
c3f829c1 GDR |
1156 | *chain = new_entry; |
1157 | return new_entry; | |
58fea6af | 1158 | } |
82443371 | 1159 | |
a5da89c6 | 1160 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
bc4071dd RH |
1161 | goes in the global namespace. */ |
1162 | static struct pragma_entry * | |
1163 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, | |
1164 | bool allow_name_expansion) | |
82443371 | 1165 | { |
a5da89c6 NB |
1166 | struct pragma_entry **chain = &pfile->pragmas; |
1167 | struct pragma_entry *entry; | |
4b115ff0 | 1168 | const cpp_hashnode *node; |
a5da89c6 | 1169 | |
a5da89c6 | 1170 | if (space) |
58fea6af | 1171 | { |
b6baa67d | 1172 | node = cpp_lookup (pfile, UC space, strlen (space)); |
4b115ff0 | 1173 | entry = lookup_pragma_entry (*chain, node); |
a5da89c6 | 1174 | if (!entry) |
bc4071dd RH |
1175 | { |
1176 | entry = new_pragma_entry (pfile, chain); | |
1177 | entry->pragma = node; | |
1178 | entry->is_nspace = true; | |
1179 | entry->allow_expansion = allow_name_expansion; | |
1180 | } | |
a5da89c6 NB |
1181 | else if (!entry->is_nspace) |
1182 | goto clash; | |
bc4071dd RH |
1183 | else if (entry->allow_expansion != allow_name_expansion) |
1184 | { | |
1185 | cpp_error (pfile, CPP_DL_ICE, | |
1186 | "registering pragmas in namespace \"%s\" with mismatched " | |
1187 | "name expansion", space); | |
1188 | return NULL; | |
1189 | } | |
a5da89c6 | 1190 | chain = &entry->u.space; |
58fea6af | 1191 | } |
bc4071dd RH |
1192 | else if (allow_name_expansion) |
1193 | { | |
1194 | cpp_error (pfile, CPP_DL_ICE, | |
1195 | "registering pragma \"%s\" with name expansion " | |
1196 | "and no namespace", name); | |
1197 | return NULL; | |
1198 | } | |
58fea6af | 1199 | |
a5da89c6 | 1200 | /* Check for duplicates. */ |
b6baa67d | 1201 | node = cpp_lookup (pfile, UC name, strlen (name)); |
4b115ff0 | 1202 | entry = lookup_pragma_entry (*chain, node); |
bc4071dd | 1203 | if (entry == NULL) |
a5da89c6 | 1204 | { |
bc4071dd RH |
1205 | entry = new_pragma_entry (pfile, chain); |
1206 | entry->pragma = node; | |
1207 | return entry; | |
a5da89c6 | 1208 | } |
bc4071dd RH |
1209 | |
1210 | if (entry->is_nspace) | |
1211 | clash: | |
1212 | cpp_error (pfile, CPP_DL_ICE, | |
1213 | "registering \"%s\" as both a pragma and a pragma namespace", | |
1214 | NODE_NAME (node)); | |
1215 | else if (space) | |
1216 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", | |
1217 | space, name); | |
a5da89c6 | 1218 | else |
bc4071dd RH |
1219 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
1220 | ||
1221 | return NULL; | |
1222 | } | |
1223 | ||
1224 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ | |
1225 | static void | |
1226 | register_pragma_internal (cpp_reader *pfile, const char *space, | |
1227 | const char *name, pragma_cb handler) | |
1228 | { | |
1229 | struct pragma_entry *entry; | |
1230 | ||
1231 | entry = register_pragma_1 (pfile, space, name, false); | |
1232 | entry->is_internal = true; | |
1233 | entry->u.handler = handler; | |
21b11495 ZW |
1234 | } |
1235 | ||
1236 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it | |
1237 | goes in the global namespace. HANDLER is the handler it will call, | |
b5b3e36a DJ |
1238 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
1239 | expansion while parsing pragma NAME. This function is exported | |
1240 | from libcpp. */ | |
21b11495 ZW |
1241 | void |
1242 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, | |
b5b3e36a | 1243 | pragma_cb handler, bool allow_expansion) |
21b11495 | 1244 | { |
bc4071dd RH |
1245 | struct pragma_entry *entry; |
1246 | ||
1247 | if (!handler) | |
1248 | { | |
1249 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); | |
1250 | return; | |
1251 | } | |
1252 | ||
1253 | entry = register_pragma_1 (pfile, space, name, false); | |
1254 | if (entry) | |
1255 | { | |
1256 | entry->allow_expansion = allow_expansion; | |
1257 | entry->u.handler = handler; | |
1258 | } | |
58fea6af | 1259 | } |
a5da89c6 | 1260 | |
bc4071dd RH |
1261 | /* Similarly, but create mark the pragma for deferred processing. |
1262 | When found, a CPP_PRAGMA token will be insertted into the stream | |
1263 | with IDENT in the token->u.pragma slot. */ | |
1264 | void | |
1265 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, | |
1266 | const char *name, unsigned int ident, | |
1267 | bool allow_expansion, bool allow_name_expansion) | |
1268 | { | |
1269 | struct pragma_entry *entry; | |
1270 | ||
1271 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); | |
1272 | if (entry) | |
1273 | { | |
1274 | entry->is_deferred = true; | |
1275 | entry->allow_expansion = allow_expansion; | |
1276 | entry->u.ident = ident; | |
1277 | } | |
1278 | } | |
1279 | ||
a5da89c6 | 1280 | /* Register the pragmas the preprocessor itself handles. */ |
58fea6af | 1281 | void |
6cf87ca4 | 1282 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
58fea6af | 1283 | { |
a5da89c6 | 1284 | /* Pragmas in the global namespace. */ |
bc4071dd | 1285 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
17e7cb85 KT |
1286 | register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro); |
1287 | register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro); | |
58fea6af | 1288 | |
a5da89c6 | 1289 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
bc4071dd RH |
1290 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
1291 | register_pragma_internal (pfile, "GCC", "system_header", | |
1292 | do_pragma_system_header); | |
1293 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); | |
f591bd8f FW |
1294 | register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning); |
1295 | register_pragma_internal (pfile, "GCC", "error", do_pragma_error); | |
82443371 | 1296 | } |
7f2935c7 | 1297 | |
17211ab5 GK |
1298 | /* Return the number of registered pragmas in PE. */ |
1299 | ||
1300 | static int | |
6cf87ca4 | 1301 | count_registered_pragmas (struct pragma_entry *pe) |
17211ab5 GK |
1302 | { |
1303 | int ct = 0; | |
1304 | for (; pe != NULL; pe = pe->next) | |
1305 | { | |
1306 | if (pe->is_nspace) | |
1307 | ct += count_registered_pragmas (pe->u.space); | |
1308 | ct++; | |
1309 | } | |
1310 | return ct; | |
1311 | } | |
1312 | ||
1313 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1314 | and return a pointer to the next free space in SD. */ | |
1315 | ||
1316 | static char ** | |
6cf87ca4 | 1317 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
17211ab5 GK |
1318 | { |
1319 | for (; pe != NULL; pe = pe->next) | |
1320 | { | |
1321 | if (pe->is_nspace) | |
1322 | sd = save_registered_pragmas (pe->u.space, sd); | |
c3f829c1 GDR |
1323 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
1324 | HT_LEN (&pe->pragma->ident), | |
1325 | HT_LEN (&pe->pragma->ident) + 1); | |
17211ab5 GK |
1326 | } |
1327 | return sd; | |
1328 | } | |
1329 | ||
1330 | /* Return a newly-allocated array which saves the names of the | |
1331 | registered pragmas. */ | |
1332 | ||
1333 | char ** | |
6cf87ca4 | 1334 | _cpp_save_pragma_names (cpp_reader *pfile) |
17211ab5 GK |
1335 | { |
1336 | int ct = count_registered_pragmas (pfile->pragmas); | |
72bb2c39 | 1337 | char **result = XNEWVEC (char *, ct); |
17211ab5 GK |
1338 | (void) save_registered_pragmas (pfile->pragmas, result); |
1339 | return result; | |
1340 | } | |
1341 | ||
1342 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1343 | and return a pointer to the next unused name in SD. */ | |
1344 | ||
1345 | static char ** | |
6cf87ca4 ZW |
1346 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1347 | char **sd) | |
17211ab5 GK |
1348 | { |
1349 | for (; pe != NULL; pe = pe->next) | |
1350 | { | |
1351 | if (pe->is_nspace) | |
1352 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
b6baa67d | 1353 | pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd)); |
17211ab5 GK |
1354 | free (*sd); |
1355 | sd++; | |
1356 | } | |
1357 | return sd; | |
1358 | } | |
1359 | ||
1360 | /* Restore the names of the registered pragmas from SAVED. */ | |
1361 | ||
1362 | void | |
6cf87ca4 | 1363 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
17211ab5 GK |
1364 | { |
1365 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1366 | free (saved); | |
1367 | } | |
1368 | ||
a5da89c6 NB |
1369 | /* Pragmata handling. We handle some, and pass the rest on to the |
1370 | front end. C99 defines three pragmas and says that no macro | |
1371 | expansion is to be performed on them; whether or not macro | |
1372 | expansion happens for other pragmas is implementation defined. | |
bc4071dd RH |
1373 | This implementation allows for a mix of both, since GCC did not |
1374 | traditionally macro expand its (few) pragmas, whereas OpenMP | |
1375 | specifies that macro expansion should happen. */ | |
711b8824 | 1376 | static void |
6cf87ca4 | 1377 | do_pragma (cpp_reader *pfile) |
7f2935c7 | 1378 | { |
a5da89c6 | 1379 | const struct pragma_entry *p = NULL; |
f3d25c65 DS |
1380 | const cpp_token *token, *pragma_token; |
1381 | source_location pragma_token_virt_loc = 0; | |
1c90c6f9 | 1382 | cpp_token ns_token; |
a5da89c6 | 1383 | unsigned int count = 1; |
add7091b | 1384 | |
93c80368 | 1385 | pfile->state.prevent_expansion++; |
58fea6af | 1386 | |
f3d25c65 DS |
1387 | pragma_token = token = cpp_get_token_with_location (pfile, |
1388 | &pragma_token_virt_loc); | |
1c90c6f9 | 1389 | ns_token = *token; |
4ed5bcfb | 1390 | if (token->type == CPP_NAME) |
0172e2bc | 1391 | { |
9a0c6187 | 1392 | p = lookup_pragma_entry (pfile->pragmas, token->val.node.node); |
a5da89c6 | 1393 | if (p && p->is_nspace) |
58fea6af | 1394 | { |
bc4071dd RH |
1395 | bool allow_name_expansion = p->allow_expansion; |
1396 | if (allow_name_expansion) | |
828a7f76 | 1397 | pfile->state.prevent_expansion--; |
38fbfaf6 | 1398 | |
a5da89c6 NB |
1399 | token = cpp_get_token (pfile); |
1400 | if (token->type == CPP_NAME) | |
9a0c6187 | 1401 | p = lookup_pragma_entry (p->u.space, token->val.node.node); |
a5da89c6 NB |
1402 | else |
1403 | p = NULL; | |
bc4071dd RH |
1404 | if (allow_name_expansion) |
1405 | pfile->state.prevent_expansion++; | |
1406 | count = 2; | |
58fea6af | 1407 | } |
58fea6af | 1408 | } |
041c3194 | 1409 | |
3da3d587 | 1410 | if (p) |
e2e1fa50 | 1411 | { |
bc4071dd RH |
1412 | if (p->is_deferred) |
1413 | { | |
f3d25c65 | 1414 | pfile->directive_result.src_loc = pragma_token_virt_loc; |
bc4071dd RH |
1415 | pfile->directive_result.type = CPP_PRAGMA; |
1416 | pfile->directive_result.flags = pragma_token->flags; | |
1417 | pfile->directive_result.val.pragma = p->u.ident; | |
1418 | pfile->state.in_deferred_pragma = true; | |
1419 | pfile->state.pragma_allow_expansion = p->allow_expansion; | |
1420 | if (!p->allow_expansion) | |
1421 | pfile->state.prevent_expansion++; | |
1422 | } | |
1423 | else | |
3da3d587 | 1424 | { |
bc4071dd RH |
1425 | /* Since the handler below doesn't get the line number, that |
1426 | it might need for diagnostics, make sure it has the right | |
3da3d587 ZW |
1427 | numbers in place. */ |
1428 | if (pfile->cb.line_change) | |
1429 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
bc4071dd | 1430 | if (p->allow_expansion) |
b5b3e36a | 1431 | pfile->state.prevent_expansion--; |
3da3d587 | 1432 | (*p->u.handler) (pfile); |
bc4071dd | 1433 | if (p->allow_expansion) |
b5b3e36a | 1434 | pfile->state.prevent_expansion++; |
3da3d587 | 1435 | } |
21b11495 | 1436 | } |
d82fc108 | 1437 | else if (pfile->cb.def_pragma) |
bdcbe496 | 1438 | { |
1c90c6f9 JJ |
1439 | if (count == 1 || pfile->context->prev == NULL) |
1440 | _cpp_backup_tokens (pfile, count); | |
1441 | else | |
1442 | { | |
1443 | /* Invalid name comes from macro expansion, _cpp_backup_tokens | |
1444 | won't allow backing 2 tokens. */ | |
1445 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook | |
1446 | reads both tokens, we could perhaps free it, but if it doesn't, | |
1447 | we don't know the exact lifespan. */ | |
1448 | cpp_token *toks = XNEWVEC (cpp_token, 2); | |
1449 | toks[0] = ns_token; | |
1450 | toks[0].flags |= NO_EXPAND; | |
1451 | toks[1] = *token; | |
1452 | toks[1].flags |= NO_EXPAND; | |
1453 | _cpp_push_token_context (pfile, NULL, toks, 2); | |
1454 | } | |
6cf87ca4 | 1455 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
bdcbe496 | 1456 | } |
a5da89c6 | 1457 | |
97293897 | 1458 | pfile->state.prevent_expansion--; |
82443371 NS |
1459 | } |
1460 | ||
5d8ebbd8 | 1461 | /* Handle #pragma once. */ |
58fea6af | 1462 | static void |
6cf87ca4 | 1463 | do_pragma_once (cpp_reader *pfile) |
a2a76ce7 | 1464 | { |
705e2d28 | 1465 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1466 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
93c80368 | 1467 | |
a5cb563b | 1468 | check_eol (pfile, false); |
49634b3a | 1469 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
a2a76ce7 | 1470 | } |
fc009f96 | 1471 | |
17e7cb85 KT |
1472 | /* Handle #pragma push_macro(STRING). */ |
1473 | static void | |
1474 | do_pragma_push_macro (cpp_reader *pfile) | |
1475 | { | |
d6874138 KT |
1476 | cpp_hashnode *node; |
1477 | size_t defnlen; | |
1478 | const uchar *defn = NULL; | |
17e7cb85 KT |
1479 | char *macroname, *dest; |
1480 | const char *limit, *src; | |
1481 | const cpp_token *txt; | |
1482 | struct def_pragma_macro *c; | |
1483 | ||
1484 | txt = get__Pragma_string (pfile); | |
1485 | if (!txt) | |
1486 | { | |
1487 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1488 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, | |
1489 | "invalid #pragma push_macro directive"); | |
1490 | check_eol (pfile, false); | |
1491 | skip_rest_of_line (pfile); | |
1492 | return; | |
1493 | } | |
1494 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1495 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1496 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1497 | while (src < limit) | |
1498 | { | |
1499 | /* We know there is a character following the backslash. */ | |
1500 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1501 | src++; | |
1502 | *dest++ = *src++; | |
1503 | } | |
1504 | *dest = 0; | |
1505 | check_eol (pfile, false); | |
1506 | skip_rest_of_line (pfile); | |
1507 | c = XNEW (struct def_pragma_macro); | |
d6874138 | 1508 | memset (c, 0, sizeof (struct def_pragma_macro)); |
17e7cb85 KT |
1509 | c->name = XNEWVAR (char, strlen (macroname) + 1); |
1510 | strcpy (c->name, macroname); | |
1511 | c->next = pfile->pushed_macros; | |
d6874138 KT |
1512 | node = _cpp_lex_identifier (pfile, c->name); |
1513 | if (node->type == NT_VOID) | |
1514 | c->is_undef = 1; | |
1515 | else | |
1516 | { | |
1517 | defn = cpp_macro_definition (pfile, node); | |
1518 | defnlen = ustrlen (defn); | |
1519 | c->definition = XNEWVEC (uchar, defnlen + 2); | |
1520 | c->definition[defnlen] = '\n'; | |
1521 | c->definition[defnlen + 1] = 0; | |
1522 | c->line = node->value.macro->line; | |
1523 | c->syshdr = node->value.macro->syshdr; | |
1524 | c->used = node->value.macro->used; | |
1525 | memcpy (c->definition, defn, defnlen); | |
1526 | } | |
1527 | ||
17e7cb85 KT |
1528 | pfile->pushed_macros = c; |
1529 | } | |
1530 | ||
1531 | /* Handle #pragma pop_macro(STRING). */ | |
1532 | static void | |
1533 | do_pragma_pop_macro (cpp_reader *pfile) | |
1534 | { | |
1535 | char *macroname, *dest; | |
1536 | const char *limit, *src; | |
1537 | const cpp_token *txt; | |
1538 | struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros; | |
1539 | txt = get__Pragma_string (pfile); | |
1540 | if (!txt) | |
1541 | { | |
1542 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1543 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, | |
1544 | "invalid #pragma pop_macro directive"); | |
1545 | check_eol (pfile, false); | |
1546 | skip_rest_of_line (pfile); | |
1547 | return; | |
1548 | } | |
1549 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1550 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1551 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1552 | while (src < limit) | |
1553 | { | |
1554 | /* We know there is a character following the backslash. */ | |
1555 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1556 | src++; | |
1557 | *dest++ = *src++; | |
1558 | } | |
1559 | *dest = 0; | |
1560 | check_eol (pfile, false); | |
1561 | skip_rest_of_line (pfile); | |
1562 | ||
1563 | while (c != NULL) | |
1564 | { | |
1565 | if (!strcmp (c->name, macroname)) | |
1566 | { | |
1567 | if (!l) | |
1568 | pfile->pushed_macros = c->next; | |
1569 | else | |
1570 | l->next = c->next; | |
d6874138 KT |
1571 | cpp_pop_definition (pfile, c); |
1572 | free (c->definition); | |
17e7cb85 KT |
1573 | free (c->name); |
1574 | free (c); | |
1575 | break; | |
1576 | } | |
1577 | l = c; | |
1578 | c = c->next; | |
1579 | } | |
1580 | } | |
1581 | ||
c3bf3e6e NB |
1582 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1583 | that the lexer produces a hard error for each subsequent usage. */ | |
58fea6af | 1584 | static void |
6cf87ca4 | 1585 | do_pragma_poison (cpp_reader *pfile) |
a2a76ce7 | 1586 | { |
345894b4 | 1587 | const cpp_token *tok; |
f8f769ea | 1588 | cpp_hashnode *hp; |
a2a76ce7 | 1589 | |
93c80368 | 1590 | pfile->state.poisoned_ok = 1; |
a2a76ce7 ZW |
1591 | for (;;) |
1592 | { | |
345894b4 NB |
1593 | tok = _cpp_lex_token (pfile); |
1594 | if (tok->type == CPP_EOF) | |
a2a76ce7 | 1595 | break; |
345894b4 | 1596 | if (tok->type != CPP_NAME) |
fc009f96 | 1597 | { |
0527bc4e JDA |
1598 | cpp_error (pfile, CPP_DL_ERROR, |
1599 | "invalid #pragma GCC poison directive"); | |
93c80368 | 1600 | break; |
fc009f96 GK |
1601 | } |
1602 | ||
9a0c6187 | 1603 | hp = tok->val.node.node; |
93c80368 NB |
1604 | if (hp->flags & NODE_POISONED) |
1605 | continue; | |
1606 | ||
1607 | if (hp->type == NT_MACRO) | |
0527bc4e | 1608 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
ebef4e8c | 1609 | NODE_NAME (hp)); |
93c80368 NB |
1610 | _cpp_free_definition (hp); |
1611 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
fc009f96 | 1612 | } |
93c80368 | 1613 | pfile->state.poisoned_ok = 0; |
7f2935c7 | 1614 | } |
2c0b35cb ZW |
1615 | |
1616 | /* Mark the current header as a system header. This will suppress | |
1617 | some categories of warnings (notably those from -pedantic). It is | |
1618 | intended for use in system libraries that cannot be implemented in | |
1619 | conforming C, but cannot be certain that their headers appear in a | |
1620 | system include directory. To prevent abuse, it is rejected in the | |
1621 | primary source file. */ | |
58fea6af | 1622 | static void |
6cf87ca4 | 1623 | do_pragma_system_header (cpp_reader *pfile) |
2c0b35cb | 1624 | { |
705e2d28 | 1625 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1626 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 1627 | "#pragma system_header ignored outside include file"); |
2c0b35cb | 1628 | else |
d82fc108 | 1629 | { |
a5cb563b | 1630 | check_eol (pfile, false); |
bdcbe496 | 1631 | skip_rest_of_line (pfile); |
d82fc108 NB |
1632 | cpp_make_system_header (pfile, 1, 0); |
1633 | } | |
2c0b35cb | 1634 | } |
f3f751ad NS |
1635 | |
1636 | /* Check the modified date of the current include file against a specified | |
1637 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1638 | determine if a fixed header should be refixed. */ | |
58fea6af | 1639 | static void |
6cf87ca4 | 1640 | do_pragma_dependency (cpp_reader *pfile) |
f3f751ad | 1641 | { |
74eb4b3e NB |
1642 | const char *fname; |
1643 | int angle_brackets, ordering; | |
a28fbdba | 1644 | source_location location; |
df383483 | 1645 | |
a28fbdba | 1646 | fname = parse_include (pfile, &angle_brackets, NULL, &location); |
74eb4b3e | 1647 | if (!fname) |
58fea6af | 1648 | return; |
041c3194 | 1649 | |
74eb4b3e | 1650 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
f3f751ad | 1651 | if (ordering < 0) |
0527bc4e | 1652 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
f3f751ad NS |
1653 | else if (ordering > 0) |
1654 | { | |
0527bc4e JDA |
1655 | cpp_error (pfile, CPP_DL_WARNING, |
1656 | "current file is older than %s", fname); | |
4ed5bcfb | 1657 | if (cpp_get_token (pfile)->type != CPP_EOF) |
bdcbe496 NB |
1658 | { |
1659 | _cpp_backup_tokens (pfile, 1); | |
87cf0651 | 1660 | do_diagnostic (pfile, CPP_DL_WARNING, 0, 0); |
bdcbe496 | 1661 | } |
f3f751ad | 1662 | } |
74eb4b3e | 1663 | |
fad205ff | 1664 | free ((void *) fname); |
f3f751ad NS |
1665 | } |
1666 | ||
f591bd8f FW |
1667 | /* Issue a diagnostic with the message taken from the pragma. If |
1668 | ERROR is true, the diagnostic is a warning, otherwise, it is an | |
1669 | error. */ | |
1670 | static void | |
1671 | do_pragma_warning_or_error (cpp_reader *pfile, bool error) | |
1672 | { | |
1673 | const cpp_token *tok = _cpp_lex_token (pfile); | |
1674 | cpp_string str; | |
1675 | if (tok->type != CPP_STRING | |
1676 | || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str, | |
1677 | CPP_STRING) | |
1678 | || str.len == 0) | |
1679 | { | |
1680 | cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive", | |
1681 | error ? "error" : "warning"); | |
1682 | return; | |
1683 | } | |
1684 | cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING, | |
1685 | "%s", str.text); | |
1686 | free ((void *)str.text); | |
1687 | } | |
1688 | ||
1689 | /* Issue a warning diagnostic. */ | |
1690 | static void | |
1691 | do_pragma_warning (cpp_reader *pfile) | |
1692 | { | |
1693 | do_pragma_warning_or_error (pfile, false); | |
1694 | } | |
1695 | ||
1696 | /* Issue an error diagnostic. */ | |
1697 | static void | |
1698 | do_pragma_error (cpp_reader *pfile) | |
1699 | { | |
1700 | do_pragma_warning_or_error (pfile, true); | |
1701 | } | |
1702 | ||
4ed5bcfb NB |
1703 | /* Get a token but skip padding. */ |
1704 | static const cpp_token * | |
6cf87ca4 | 1705 | get_token_no_padding (cpp_reader *pfile) |
a5c3cccd | 1706 | { |
4ed5bcfb NB |
1707 | for (;;) |
1708 | { | |
1709 | const cpp_token *result = cpp_get_token (pfile); | |
1710 | if (result->type != CPP_PADDING) | |
1711 | return result; | |
1712 | } | |
1713 | } | |
a5c3cccd | 1714 | |
4ed5bcfb NB |
1715 | /* Check syntax is "(string-literal)". Returns the string on success, |
1716 | or NULL on failure. */ | |
1717 | static const cpp_token * | |
6cf87ca4 | 1718 | get__Pragma_string (cpp_reader *pfile) |
4ed5bcfb NB |
1719 | { |
1720 | const cpp_token *string; | |
5b9a40df | 1721 | const cpp_token *paren; |
a5c3cccd | 1722 | |
5b9a40df TT |
1723 | paren = get_token_no_padding (pfile); |
1724 | if (paren->type == CPP_EOF) | |
1725 | _cpp_backup_tokens (pfile, 1); | |
1726 | if (paren->type != CPP_OPEN_PAREN) | |
4ed5bcfb NB |
1727 | return NULL; |
1728 | ||
1729 | string = get_token_no_padding (pfile); | |
5b9a40df TT |
1730 | if (string->type == CPP_EOF) |
1731 | _cpp_backup_tokens (pfile, 1); | |
b6baa67d | 1732 | if (string->type != CPP_STRING && string->type != CPP_WSTRING |
2c6e3f55 JJ |
1733 | && string->type != CPP_STRING32 && string->type != CPP_STRING16 |
1734 | && string->type != CPP_UTF8STRING) | |
4ed5bcfb NB |
1735 | return NULL; |
1736 | ||
5b9a40df TT |
1737 | paren = get_token_no_padding (pfile); |
1738 | if (paren->type == CPP_EOF) | |
1739 | _cpp_backup_tokens (pfile, 1); | |
1740 | if (paren->type != CPP_CLOSE_PAREN) | |
4ed5bcfb | 1741 | return NULL; |
a5c3cccd | 1742 | |
4ed5bcfb | 1743 | return string; |
a5c3cccd NB |
1744 | } |
1745 | ||
87062813 NB |
1746 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1747 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1748 | static void | |
6cf87ca4 | 1749 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
a5c3cccd NB |
1750 | { |
1751 | const unsigned char *src, *limit; | |
87062813 | 1752 | char *dest, *result; |
bc4071dd RH |
1753 | cpp_context *saved_context; |
1754 | cpp_token *saved_cur_token; | |
1755 | tokenrun *saved_cur_run; | |
1756 | cpp_token *toks; | |
1757 | int count; | |
14ccf800 | 1758 | const struct directive *save_directive; |
a5c3cccd | 1759 | |
c3f829c1 | 1760 | dest = result = (char *) alloca (in->len - 1); |
6338b358 NB |
1761 | src = in->text + 1 + (in->text[0] == 'L'); |
1762 | limit = in->text + in->len - 1; | |
1763 | while (src < limit) | |
a5c3cccd NB |
1764 | { |
1765 | /* We know there is a character following the backslash. */ | |
1766 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1767 | src++; | |
1768 | *dest++ = *src++; | |
1769 | } | |
26aea073 | 1770 | *dest = '\n'; |
a5c3cccd | 1771 | |
8128cccf NB |
1772 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1773 | tokens when in the middle of a macro expansion. Use a new | |
1774 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1775 | doesn't go beyond the end of the text. Also, remember the | |
1776 | current lexing position so we can return to it later. | |
1777 | ||
1778 | Something like line-at-a-time lexing should remove the need for | |
1779 | this. */ | |
bc4071dd RH |
1780 | saved_context = pfile->context; |
1781 | saved_cur_token = pfile->cur_token; | |
1782 | saved_cur_run = pfile->cur_run; | |
79ba5e3b | 1783 | |
3ad64f53 | 1784 | pfile->context = XCNEW (cpp_context); |
79ba5e3b | 1785 | |
bc4071dd RH |
1786 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
1787 | until we've read all of the tokens that we want. */ | |
1788 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, | |
1789 | /* from_stage3 */ true); | |
1790 | /* ??? Antique Disgusting Hack. What does this do? */ | |
1791 | if (pfile->buffer->prev) | |
1792 | pfile->buffer->file = pfile->buffer->prev->file; | |
79ba5e3b | 1793 | |
bc4071dd RH |
1794 | start_directive (pfile); |
1795 | _cpp_clean_line (pfile); | |
14ccf800 TT |
1796 | save_directive = pfile->directive; |
1797 | pfile->directive = &dtable[T_PRAGMA]; | |
bc4071dd RH |
1798 | do_pragma (pfile); |
1799 | end_directive (pfile, 1); | |
14ccf800 | 1800 | pfile->directive = save_directive; |
79ba5e3b | 1801 | |
bc4071dd RH |
1802 | /* We always insert at least one token, the directive result. It'll |
1803 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we | |
1804 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ | |
1805 | ||
1806 | /* If we're not handling the pragma internally, read all of the tokens from | |
1807 | the string buffer now, while the string buffer is still installed. */ | |
1808 | /* ??? Note that the token buffer allocated here is leaked. It's not clear | |
1809 | to me what the true lifespan of the tokens are. It would appear that | |
1810 | the lifespan is the entire parse of the main input stream, in which case | |
1811 | this may not be wrong. */ | |
1812 | if (pfile->directive_result.type == CPP_PRAGMA) | |
1813 | { | |
1814 | int maxcount; | |
1815 | ||
1816 | count = 1; | |
1817 | maxcount = 50; | |
1818 | toks = XNEWVEC (cpp_token, maxcount); | |
1819 | toks[0] = pfile->directive_result; | |
79ba5e3b | 1820 | |
bc4071dd RH |
1821 | do |
1822 | { | |
1823 | if (count == maxcount) | |
1824 | { | |
1825 | maxcount = maxcount * 3 / 2; | |
1826 | toks = XRESIZEVEC (cpp_token, toks, maxcount); | |
1827 | } | |
1c90c6f9 JJ |
1828 | toks[count] = *cpp_get_token (pfile); |
1829 | /* Macros have been already expanded by cpp_get_token | |
1830 | if the pragma allowed expansion. */ | |
1831 | toks[count++].flags |= NO_EXPAND; | |
bc4071dd RH |
1832 | } |
1833 | while (toks[count-1].type != CPP_PRAGMA_EOL); | |
1834 | } | |
1835 | else | |
1836 | { | |
1837 | count = 1; | |
1838 | toks = XNEW (cpp_token); | |
1839 | toks[0] = pfile->directive_result; | |
1840 | ||
1841 | /* If we handled the entire pragma internally, make sure we get the | |
1842 | line number correct for the next token. */ | |
1843 | if (pfile->cb.line_change) | |
1844 | pfile->cb.line_change (pfile, pfile->cur_token, false); | |
1845 | } | |
1846 | ||
1847 | /* Finish inlining run_directive. */ | |
1848 | pfile->buffer->file = NULL; | |
1849 | _cpp_pop_buffer (pfile); | |
1850 | ||
1851 | /* Reset the old macro state before ... */ | |
1852 | XDELETE (pfile->context); | |
1853 | pfile->context = saved_context; | |
1854 | pfile->cur_token = saved_cur_token; | |
1855 | pfile->cur_run = saved_cur_run; | |
1856 | ||
1857 | /* ... inserting the new tokens we collected. */ | |
1858 | _cpp_push_token_context (pfile, NULL, toks, count); | |
a5c3cccd NB |
1859 | } |
1860 | ||
5b9a40df TT |
1861 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
1862 | int | |
6cf87ca4 | 1863 | _cpp_do__Pragma (cpp_reader *pfile) |
a5c3cccd | 1864 | { |
4ed5bcfb | 1865 | const cpp_token *string = get__Pragma_string (pfile); |
21b11495 | 1866 | pfile->directive_result.type = CPP_PADDING; |
a5c3cccd | 1867 | |
79ba5e3b | 1868 | if (string) |
5b9a40df TT |
1869 | { |
1870 | destringize_and_run (pfile, &string->val.str); | |
1871 | return 1; | |
1872 | } | |
1873 | cpp_error (pfile, CPP_DL_ERROR, | |
1874 | "_Pragma takes a parenthesized string literal"); | |
1875 | return 0; | |
a5c3cccd | 1876 | } |
21b11495 | 1877 | |
5d8ebbd8 | 1878 | /* Handle #ifdef. */ |
711b8824 | 1879 | static void |
6cf87ca4 | 1880 | do_ifdef (cpp_reader *pfile) |
168d3732 | 1881 | { |
93c80368 | 1882 | int skip = 1; |
041c3194 | 1883 | |
cef0d199 | 1884 | if (! pfile->state.skipping) |
93c80368 | 1885 | { |
93d45d9e | 1886 | cpp_hashnode *node = lex_macro_node (pfile, false); |
041c3194 | 1887 | |
93c80368 | 1888 | if (node) |
a69cbaac | 1889 | { |
f3c33d9d MM |
1890 | /* Do not treat conditional macros as being defined. This is due to |
1891 | the powerpc and spu ports using conditional macros for 'vector', | |
1892 | 'bool', and 'pixel' to act as conditional keywords. This messes | |
1893 | up tests like #ifndef bool. */ | |
1894 | skip = (node->type != NT_MACRO | |
1895 | || ((node->flags & NODE_CONDITIONAL) != 0)); | |
a69cbaac | 1896 | _cpp_mark_macro_used (node); |
93d45d9e JM |
1897 | if (!(node->flags & NODE_USED)) |
1898 | { | |
1899 | node->flags |= NODE_USED; | |
1900 | if (node->type == NT_MACRO) | |
1901 | { | |
a69d2520 JJ |
1902 | if ((node->flags & NODE_BUILTIN) |
1903 | && pfile->cb.user_builtin_macro) | |
1904 | pfile->cb.user_builtin_macro (pfile, node); | |
93d45d9e JM |
1905 | if (pfile->cb.used_define) |
1906 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1907 | } | |
1908 | else | |
1909 | { | |
1910 | if (pfile->cb.used_undef) | |
1911 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1912 | } | |
1913 | } | |
3de8a540 AC |
1914 | if (pfile->cb.used) |
1915 | pfile->cb.used (pfile, pfile->directive_line, node); | |
a5cb563b | 1916 | check_eol (pfile, false); |
a69cbaac | 1917 | } |
93c80368 | 1918 | } |
168d3732 | 1919 | |
93c80368 NB |
1920 | push_conditional (pfile, skip, T_IFDEF, 0); |
1921 | } | |
168d3732 | 1922 | |
5d8ebbd8 | 1923 | /* Handle #ifndef. */ |
711b8824 | 1924 | static void |
6cf87ca4 | 1925 | do_ifndef (cpp_reader *pfile) |
168d3732 | 1926 | { |
93c80368 | 1927 | int skip = 1; |
93d45d9e | 1928 | cpp_hashnode *node = 0; |
168d3732 | 1929 | |
cef0d199 | 1930 | if (! pfile->state.skipping) |
5af7e2c2 | 1931 | { |
ee1c2a10 | 1932 | node = lex_macro_node (pfile, false); |
b43db0b3 GK |
1933 | |
1934 | if (node) | |
a69cbaac | 1935 | { |
f3c33d9d MM |
1936 | /* Do not treat conditional macros as being defined. This is due to |
1937 | the powerpc and spu ports using conditional macros for 'vector', | |
1938 | 'bool', and 'pixel' to act as conditional keywords. This messes | |
1939 | up tests like #ifndef bool. */ | |
1940 | skip = (node->type == NT_MACRO | |
1941 | && ((node->flags & NODE_CONDITIONAL) == 0)); | |
a69cbaac | 1942 | _cpp_mark_macro_used (node); |
93d45d9e JM |
1943 | if (!(node->flags & NODE_USED)) |
1944 | { | |
1945 | node->flags |= NODE_USED; | |
1946 | if (node->type == NT_MACRO) | |
1947 | { | |
a69d2520 JJ |
1948 | if ((node->flags & NODE_BUILTIN) |
1949 | && pfile->cb.user_builtin_macro) | |
1950 | pfile->cb.user_builtin_macro (pfile, node); | |
93d45d9e JM |
1951 | if (pfile->cb.used_define) |
1952 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1953 | } | |
1954 | else | |
1955 | { | |
1956 | if (pfile->cb.used_undef) | |
1957 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1958 | } | |
1959 | } | |
3de8a540 AC |
1960 | if (pfile->cb.used) |
1961 | pfile->cb.used (pfile, pfile->directive_line, node); | |
a5cb563b | 1962 | check_eol (pfile, false); |
a69cbaac | 1963 | } |
5af7e2c2 | 1964 | } |
041c3194 | 1965 | |
93c80368 | 1966 | push_conditional (pfile, skip, T_IFNDEF, node); |
7f2935c7 PB |
1967 | } |
1968 | ||
6d18adbc NB |
1969 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1970 | pfile->mi_ind_cmacro so we can handle multiple-include | |
6356f892 | 1971 | optimizations. If macro expansion occurs in the expression, we |
6d18adbc NB |
1972 | cannot treat it as a controlling conditional, since the expansion |
1973 | could change in the future. That is handled by cpp_get_token. */ | |
711b8824 | 1974 | static void |
6cf87ca4 | 1975 | do_if (cpp_reader *pfile) |
7f2935c7 | 1976 | { |
93c80368 | 1977 | int skip = 1; |
7f2935c7 | 1978 | |
cef0d199 | 1979 | if (! pfile->state.skipping) |
d750887f | 1980 | skip = _cpp_parse_expr (pfile, true) == false; |
93c80368 | 1981 | |
6d18adbc | 1982 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
7f2935c7 PB |
1983 | } |
1984 | ||
b528a07e | 1985 | /* Flip skipping state if appropriate and continue without changing |
ea4a453b ZW |
1986 | if_stack; this is so that the error message for missing #endif's |
1987 | etc. will point to the original #if. */ | |
711b8824 | 1988 | static void |
6cf87ca4 | 1989 | do_else (cpp_reader *pfile) |
ed705a82 | 1990 | { |
b528a07e NB |
1991 | cpp_buffer *buffer = pfile->buffer; |
1992 | struct if_stack *ifs = buffer->if_stack; | |
ff2b53ef | 1993 | |
ea4a453b | 1994 | if (ifs == NULL) |
0527bc4e | 1995 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
93c80368 | 1996 | else |
ff2b53ef | 1997 | { |
93c80368 NB |
1998 | if (ifs->type == T_ELSE) |
1999 | { | |
0527bc4e JDA |
2000 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
2001 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
93c80368 NB |
2002 | "the conditional began here"); |
2003 | } | |
b528a07e NB |
2004 | ifs->type = T_ELSE; |
2005 | ||
cef0d199 NB |
2006 | /* Skip any future (erroneous) #elses or #elifs. */ |
2007 | pfile->state.skipping = ifs->skip_elses; | |
2008 | ifs->skip_elses = true; | |
7f2935c7 | 2009 | |
93c80368 NB |
2010 | /* Invalidate any controlling macro. */ |
2011 | ifs->mi_cmacro = 0; | |
93c80368 | 2012 | |
cef0d199 | 2013 | /* Only check EOL if was not originally skipping. */ |
909de5da | 2014 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
81b5d104 | 2015 | check_eol_endif_labels (pfile); |
cef0d199 | 2016 | } |
7f2935c7 PB |
2017 | } |
2018 | ||
5d8ebbd8 | 2019 | /* Handle a #elif directive by not changing if_stack either. See the |
93c80368 | 2020 | comment above do_else. */ |
711b8824 | 2021 | static void |
6cf87ca4 | 2022 | do_elif (cpp_reader *pfile) |
7f2935c7 | 2023 | { |
b528a07e NB |
2024 | cpp_buffer *buffer = pfile->buffer; |
2025 | struct if_stack *ifs = buffer->if_stack; | |
7f2935c7 | 2026 | |
ea4a453b | 2027 | if (ifs == NULL) |
0527bc4e | 2028 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
b528a07e | 2029 | else |
40ea76de | 2030 | { |
b528a07e NB |
2031 | if (ifs->type == T_ELSE) |
2032 | { | |
0527bc4e JDA |
2033 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
2034 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
b528a07e NB |
2035 | "the conditional began here"); |
2036 | } | |
2037 | ifs->type = T_ELIF; | |
93c80368 | 2038 | |
d750887f | 2039 | if (! ifs->was_skipping) |
b528a07e | 2040 | { |
d750887f TT |
2041 | bool value; |
2042 | /* The standard mandates that the expression be parsed even | |
2043 | if we are skipping elses at this point -- the lexical | |
2044 | restrictions on #elif only apply to skipped groups, but | |
2045 | this group is not being skipped. Temporarily set | |
2046 | skipping to false to get lexer warnings. */ | |
cef0d199 | 2047 | pfile->state.skipping = 0; |
d750887f TT |
2048 | value = _cpp_parse_expr (pfile, false); |
2049 | if (ifs->skip_elses) | |
2050 | pfile->state.skipping = 1; | |
2051 | else | |
2052 | { | |
2053 | pfile->state.skipping = ! value; | |
2054 | ifs->skip_elses = value; | |
2055 | } | |
b528a07e | 2056 | } |
cef0d199 NB |
2057 | |
2058 | /* Invalidate any controlling macro. */ | |
2059 | ifs->mi_cmacro = 0; | |
40ea76de | 2060 | } |
7f2935c7 PB |
2061 | } |
2062 | ||
cef0d199 | 2063 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
711b8824 | 2064 | static void |
6cf87ca4 | 2065 | do_endif (cpp_reader *pfile) |
7f2935c7 | 2066 | { |
b528a07e NB |
2067 | cpp_buffer *buffer = pfile->buffer; |
2068 | struct if_stack *ifs = buffer->if_stack; | |
ea4a453b | 2069 | |
ea4a453b | 2070 | if (ifs == NULL) |
0527bc4e | 2071 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
7f2935c7 PB |
2072 | else |
2073 | { | |
cef0d199 | 2074 | /* Only check EOL if was not originally skipping. */ |
909de5da | 2075 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
81b5d104 | 2076 | check_eol_endif_labels (pfile); |
cef0d199 | 2077 | |
93c80368 NB |
2078 | /* If potential control macro, we go back outside again. */ |
2079 | if (ifs->next == 0 && ifs->mi_cmacro) | |
2080 | { | |
6d18adbc | 2081 | pfile->mi_valid = true; |
93c80368 NB |
2082 | pfile->mi_cmacro = ifs->mi_cmacro; |
2083 | } | |
2084 | ||
b528a07e | 2085 | buffer->if_stack = ifs->next; |
cef0d199 | 2086 | pfile->state.skipping = ifs->was_skipping; |
2a967f3d | 2087 | obstack_free (&pfile->buffer_ob, ifs); |
7f2935c7 | 2088 | } |
93c80368 | 2089 | } |
041c3194 | 2090 | |
5d8ebbd8 NB |
2091 | /* Push an if_stack entry for a preprocessor conditional, and set |
2092 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
2093 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
2094 | we need to check here that we are at the top of the file. */ | |
ea4a453b | 2095 | static void |
6cf87ca4 ZW |
2096 | push_conditional (cpp_reader *pfile, int skip, int type, |
2097 | const cpp_hashnode *cmacro) | |
ea4a453b ZW |
2098 | { |
2099 | struct if_stack *ifs; | |
b528a07e | 2100 | cpp_buffer *buffer = pfile->buffer; |
ea4a453b | 2101 | |
72bb2c39 | 2102 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
50410426 | 2103 | ifs->line = pfile->directive_line; |
b528a07e | 2104 | ifs->next = buffer->if_stack; |
cef0d199 NB |
2105 | ifs->skip_elses = pfile->state.skipping || !skip; |
2106 | ifs->was_skipping = pfile->state.skipping; | |
ea4a453b | 2107 | ifs->type = type; |
6d18adbc NB |
2108 | /* This condition is effectively a test for top-of-file. */ |
2109 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
93c80368 NB |
2110 | ifs->mi_cmacro = cmacro; |
2111 | else | |
2112 | ifs->mi_cmacro = 0; | |
ea4a453b | 2113 | |
cef0d199 | 2114 | pfile->state.skipping = skip; |
b528a07e | 2115 | buffer->if_stack = ifs; |
782331f4 | 2116 | } |
7061aa5a | 2117 | |
5d8ebbd8 NB |
2118 | /* Read the tokens of the answer into the macro pool, in a directive |
2119 | of type TYPE. Only commit the memory if we intend it as permanent | |
2120 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
a28fbdba MLI |
2121 | ANSWERP to point to the answer. PRED_LOC is the location of the |
2122 | predicate. */ | |
93c80368 | 2123 | static int |
a28fbdba MLI |
2124 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type, |
2125 | source_location pred_loc) | |
7f2935c7 | 2126 | { |
4ed5bcfb | 2127 | const cpp_token *paren; |
93c80368 | 2128 | struct answer *answer; |
8c3b2693 | 2129 | unsigned int acount; |
93c80368 NB |
2130 | |
2131 | /* In a conditional, it is legal to not have an open paren. We | |
2132 | should save the following token in this case. */ | |
4ed5bcfb | 2133 | paren = cpp_get_token (pfile); |
93c80368 NB |
2134 | |
2135 | /* If not a paren, see if we're OK. */ | |
4ed5bcfb | 2136 | if (paren->type != CPP_OPEN_PAREN) |
041c3194 | 2137 | { |
93c80368 NB |
2138 | /* In a conditional no answer is a test for any answer. It |
2139 | could be followed by any token. */ | |
2140 | if (type == T_IF) | |
bdcbe496 NB |
2141 | { |
2142 | _cpp_backup_tokens (pfile, 1); | |
2143 | return 0; | |
2144 | } | |
93c80368 NB |
2145 | |
2146 | /* #unassert with no answer is valid - it removes all answers. */ | |
4ed5bcfb | 2147 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
93c80368 | 2148 | return 0; |
15dad1d9 | 2149 | |
a28fbdba MLI |
2150 | cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0, |
2151 | "missing '(' after predicate"); | |
93c80368 | 2152 | return 1; |
041c3194 | 2153 | } |
7061aa5a | 2154 | |
8c3b2693 | 2155 | for (acount = 0;; acount++) |
041c3194 | 2156 | { |
8c3b2693 NB |
2157 | size_t room_needed; |
2158 | const cpp_token *token = cpp_get_token (pfile); | |
2159 | cpp_token *dest; | |
93c80368 | 2160 | |
041c3194 ZW |
2161 | if (token->type == CPP_CLOSE_PAREN) |
2162 | break; | |
a7abcbbf | 2163 | |
93c80368 | 2164 | if (token->type == CPP_EOF) |
041c3194 | 2165 | { |
0527bc4e | 2166 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
93c80368 | 2167 | return 1; |
041c3194 | 2168 | } |
8c3b2693 NB |
2169 | |
2170 | /* struct answer includes the space for one token. */ | |
2171 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
2172 | ||
2173 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
2174 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
2175 | ||
2176 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
2177 | *dest = *token; | |
2178 | ||
2179 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
2180 | if (acount == 0) | |
2181 | dest->flags &= ~PREV_WHITE; | |
041c3194 | 2182 | } |
15dad1d9 | 2183 | |
8c3b2693 | 2184 | if (acount == 0) |
7061aa5a | 2185 | { |
0527bc4e | 2186 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
93c80368 | 2187 | return 1; |
7f2935c7 | 2188 | } |
041c3194 | 2189 | |
8c3b2693 NB |
2190 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
2191 | answer->count = acount; | |
2192 | answer->next = NULL; | |
93c80368 | 2193 | *answerp = answer; |
041c3194 | 2194 | |
93c80368 NB |
2195 | return 0; |
2196 | } | |
041c3194 | 2197 | |
5d8ebbd8 NB |
2198 | /* Parses an assertion directive of type TYPE, returning a pointer to |
2199 | the hash node of the predicate, or 0 on error. If an answer was | |
2200 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
93c80368 | 2201 | static cpp_hashnode * |
6cf87ca4 | 2202 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
93c80368 NB |
2203 | { |
2204 | cpp_hashnode *result = 0; | |
4ed5bcfb | 2205 | const cpp_token *predicate; |
93c80368 NB |
2206 | |
2207 | /* We don't expand predicates or answers. */ | |
2208 | pfile->state.prevent_expansion++; | |
2209 | ||
93c80368 | 2210 | *answerp = 0; |
4ed5bcfb NB |
2211 | predicate = cpp_get_token (pfile); |
2212 | if (predicate->type == CPP_EOF) | |
0527bc4e | 2213 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
4ed5bcfb | 2214 | else if (predicate->type != CPP_NAME) |
a28fbdba MLI |
2215 | cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0, |
2216 | "predicate must be an identifier"); | |
2217 | else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0) | |
93c80368 | 2218 | { |
9a0c6187 | 2219 | unsigned int len = NODE_LEN (predicate->val.node.node); |
c3f829c1 | 2220 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
041c3194 | 2221 | |
93c80368 NB |
2222 | /* Prefix '#' to get it out of macro namespace. */ |
2223 | sym[0] = '#'; | |
9a0c6187 | 2224 | memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len); |
93c80368 NB |
2225 | result = cpp_lookup (pfile, sym, len + 1); |
2226 | } | |
7061aa5a | 2227 | |
93c80368 NB |
2228 | pfile->state.prevent_expansion--; |
2229 | return result; | |
7f2935c7 | 2230 | } |
7061aa5a | 2231 | |
5d8ebbd8 | 2232 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
041c3194 | 2233 | or a pointer to NULL if the answer is not in the chain. */ |
93c80368 | 2234 | static struct answer ** |
6cf87ca4 | 2235 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
7f2935c7 | 2236 | { |
93c80368 | 2237 | unsigned int i; |
041c3194 | 2238 | struct answer **result; |
7f2935c7 | 2239 | |
041c3194 | 2240 | for (result = &node->value.answers; *result; result = &(*result)->next) |
93c80368 NB |
2241 | { |
2242 | struct answer *answer = *result; | |
2243 | ||
2244 | if (answer->count == candidate->count) | |
2245 | { | |
2246 | for (i = 0; i < answer->count; i++) | |
2247 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
2248 | break; | |
2249 | ||
2250 | if (i == answer->count) | |
2251 | break; | |
2252 | } | |
2253 | } | |
ff2b53ef | 2254 | |
041c3194 ZW |
2255 | return result; |
2256 | } | |
15dad1d9 | 2257 | |
93c80368 | 2258 | /* Test an assertion within a preprocessor conditional. Returns |
da7d8304 | 2259 | nonzero on failure, zero on success. On success, the result of |
2402645b | 2260 | the test is written into VALUE, otherwise the value 0. */ |
93c80368 | 2261 | int |
6cf87ca4 | 2262 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
93c80368 NB |
2263 | { |
2264 | struct answer *answer; | |
2265 | cpp_hashnode *node; | |
2266 | ||
2267 | node = parse_assertion (pfile, &answer, T_IF); | |
2402645b HPN |
2268 | |
2269 | /* For recovery, an erroneous assertion expression is handled as a | |
2270 | failing assertion. */ | |
2271 | *value = 0; | |
2272 | ||
93c80368 NB |
2273 | if (node) |
2274 | *value = (node->type == NT_ASSERTION && | |
2275 | (answer == 0 || *find_answer (node, answer) != 0)); | |
91318908 NB |
2276 | else if (pfile->cur_token[-1].type == CPP_EOF) |
2277 | _cpp_backup_tokens (pfile, 1); | |
93c80368 NB |
2278 | |
2279 | /* We don't commit the memory for the answer - it's temporary only. */ | |
2280 | return node == 0; | |
2281 | } | |
2282 | ||
5d8ebbd8 | 2283 | /* Handle #assert. */ |
711b8824 | 2284 | static void |
6cf87ca4 | 2285 | do_assert (cpp_reader *pfile) |
041c3194 ZW |
2286 | { |
2287 | struct answer *new_answer; | |
2288 | cpp_hashnode *node; | |
df383483 | 2289 | |
93c80368 | 2290 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
041c3194 | 2291 | if (node) |
ff2b53ef | 2292 | { |
d8044160 GK |
2293 | size_t answer_size; |
2294 | ||
93c80368 NB |
2295 | /* Place the new answer in the answer list. First check there |
2296 | is not a duplicate. */ | |
041c3194 | 2297 | new_answer->next = 0; |
93c80368 | 2298 | if (node->type == NT_ASSERTION) |
041c3194 | 2299 | { |
93c80368 NB |
2300 | if (*find_answer (node, new_answer)) |
2301 | { | |
0527bc4e | 2302 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
ebef4e8c | 2303 | NODE_NAME (node) + 1); |
93c80368 NB |
2304 | return; |
2305 | } | |
041c3194 ZW |
2306 | new_answer->next = node->value.answers; |
2307 | } | |
8c3b2693 | 2308 | |
d8044160 GK |
2309 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
2310 | * sizeof (cpp_token)); | |
2311 | /* Commit or allocate storage for the object. */ | |
2312 | if (pfile->hash_table->alloc_subobject) | |
2313 | { | |
2314 | struct answer *temp_answer = new_answer; | |
c3f829c1 GDR |
2315 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
2316 | (answer_size); | |
d8044160 GK |
2317 | memcpy (new_answer, temp_answer, answer_size); |
2318 | } | |
2319 | else | |
2320 | BUFF_FRONT (pfile->a_buff) += answer_size; | |
2321 | ||
93c80368 | 2322 | node->type = NT_ASSERTION; |
041c3194 | 2323 | node->value.answers = new_answer; |
a5cb563b | 2324 | check_eol (pfile, false); |
ff2b53ef | 2325 | } |
041c3194 | 2326 | } |
15dad1d9 | 2327 | |
5d8ebbd8 | 2328 | /* Handle #unassert. */ |
711b8824 | 2329 | static void |
6cf87ca4 | 2330 | do_unassert (cpp_reader *pfile) |
041c3194 ZW |
2331 | { |
2332 | cpp_hashnode *node; | |
93c80368 | 2333 | struct answer *answer; |
df383483 | 2334 | |
93c80368 NB |
2335 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
2336 | /* It isn't an error to #unassert something that isn't asserted. */ | |
2337 | if (node && node->type == NT_ASSERTION) | |
7061aa5a | 2338 | { |
93c80368 | 2339 | if (answer) |
15dad1d9 | 2340 | { |
93c80368 | 2341 | struct answer **p = find_answer (node, answer), *temp; |
041c3194 | 2342 | |
93c80368 NB |
2343 | /* Remove the answer from the list. */ |
2344 | temp = *p; | |
2345 | if (temp) | |
2346 | *p = temp->next; | |
a7abcbbf | 2347 | |
93c80368 NB |
2348 | /* Did we free the last answer? */ |
2349 | if (node->value.answers == 0) | |
2350 | node->type = NT_VOID; | |
8c3b2693 | 2351 | |
a5cb563b | 2352 | check_eol (pfile, false); |
93c80368 NB |
2353 | } |
2354 | else | |
2355 | _cpp_free_definition (node); | |
041c3194 | 2356 | } |
93c80368 NB |
2357 | |
2358 | /* We don't commit the memory for the answer - it's temporary only. */ | |
7f2935c7 | 2359 | } |
7f2935c7 | 2360 | |
45b966db ZW |
2361 | /* These are for -D, -U, -A. */ |
2362 | ||
2363 | /* Process the string STR as if it appeared as the body of a #define. | |
2364 | If STR is just an identifier, define it with value 1. | |
2365 | If STR has anything after the identifier, then it should | |
ec5c56db | 2366 | be identifier=definition. */ |
0b22d65c | 2367 | void |
6cf87ca4 | 2368 | cpp_define (cpp_reader *pfile, const char *str) |
0b22d65c | 2369 | { |
86373e7e JM |
2370 | char *buf; |
2371 | const char *p; | |
45b966db ZW |
2372 | size_t count; |
2373 | ||
df383483 | 2374 | /* Copy the entire option so we can modify it. |
45b966db | 2375 | Change the first "=" in the string to a space. If there is none, |
86368122 NB |
2376 | tack " 1" on the end. */ |
2377 | ||
86368122 | 2378 | count = strlen (str); |
c3f829c1 | 2379 | buf = (char *) alloca (count + 3); |
86368122 NB |
2380 | memcpy (buf, str, count); |
2381 | ||
2382 | p = strchr (str, '='); | |
45b966db | 2383 | if (p) |
86368122 | 2384 | buf[p - str] = ' '; |
45b966db ZW |
2385 | else |
2386 | { | |
86368122 NB |
2387 | buf[count++] = ' '; |
2388 | buf[count++] = '1'; | |
0b22d65c | 2389 | } |
26aea073 | 2390 | buf[count] = '\n'; |
cf4ed945 | 2391 | |
29401c30 | 2392 | run_directive (pfile, T_DEFINE, buf, count); |
2c8f0515 ZW |
2393 | } |
2394 | ||
28f68625 DF |
2395 | |
2396 | /* Use to build macros to be run through cpp_define() as | |
2397 | described above. | |
2398 | Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ | |
2399 | ||
2400 | void | |
2401 | cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) | |
2402 | { | |
2403 | char *ptr = NULL; | |
2404 | ||
2405 | va_list ap; | |
2406 | va_start (ap, fmt); | |
2407 | vasprintf (&ptr, fmt, ap); | |
2408 | va_end (ap); | |
2409 | ||
2410 | cpp_define (pfile, ptr); | |
2411 | free (ptr); | |
2412 | } | |
2413 | ||
2414 | ||
ad2a084d | 2415 | /* Slight variant of the above for use by initialize_builtins. */ |
2c8f0515 | 2416 | void |
6cf87ca4 | 2417 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
2c8f0515 | 2418 | { |
26aea073 | 2419 | size_t len = strlen (str); |
c3f829c1 | 2420 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2421 | memcpy (buf, str, len); |
2422 | buf[len] = '\n'; | |
2423 | run_directive (pfile, T_DEFINE, buf, len); | |
45b966db | 2424 | } |
0f41302f | 2425 | |
45b966db ZW |
2426 | /* Process MACRO as if it appeared as the body of an #undef. */ |
2427 | void | |
6cf87ca4 | 2428 | cpp_undef (cpp_reader *pfile, const char *macro) |
7f2935c7 | 2429 | { |
26aea073 | 2430 | size_t len = strlen (macro); |
c3f829c1 | 2431 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2432 | memcpy (buf, macro, len); |
2433 | buf[len] = '\n'; | |
2434 | run_directive (pfile, T_UNDEF, buf, len); | |
7f2935c7 PB |
2435 | } |
2436 | ||
d6874138 KT |
2437 | /* Replace a previous definition DEF of the macro STR. If DEF is NULL, |
2438 | or first element is zero, then the macro should be undefined. */ | |
2439 | static void | |
2440 | cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c) | |
121de39f | 2441 | { |
d6874138 | 2442 | cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name); |
121de39f RH |
2443 | if (node == NULL) |
2444 | return; | |
2445 | ||
93d45d9e JM |
2446 | if (pfile->cb.before_define) |
2447 | pfile->cb.before_define (pfile); | |
2448 | ||
121de39f RH |
2449 | if (node->type == NT_MACRO) |
2450 | { | |
2451 | if (pfile->cb.undef) | |
2452 | pfile->cb.undef (pfile, pfile->directive_line, node); | |
2453 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
2454 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
2455 | } | |
2456 | if (node->type != NT_VOID) | |
2457 | _cpp_free_definition (node); | |
2458 | ||
d6874138 KT |
2459 | if (c->is_undef) |
2460 | return; | |
2461 | { | |
2462 | size_t namelen; | |
2463 | const uchar *dn; | |
2464 | cpp_hashnode *h = NULL; | |
2465 | cpp_buffer *nbuf; | |
2466 | ||
2467 | namelen = ustrcspn (c->definition, "( \n"); | |
2468 | h = cpp_lookup (pfile, c->definition, namelen); | |
2469 | dn = c->definition + namelen; | |
2470 | ||
2471 | h->type = NT_VOID; | |
2472 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); | |
2473 | nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true); | |
2474 | if (nbuf != NULL) | |
2475 | { | |
2476 | _cpp_clean_line (pfile); | |
2477 | nbuf->sysp = 1; | |
2478 | if (!_cpp_create_definition (pfile, h)) | |
2479 | abort (); | |
2480 | _cpp_pop_buffer (pfile); | |
2481 | } | |
2482 | else | |
2483 | abort (); | |
2484 | h->value.macro->line = c->line; | |
2485 | h->value.macro->syshdr = c->syshdr; | |
2486 | h->value.macro->used = c->used; | |
2487 | } | |
121de39f RH |
2488 | } |
2489 | ||
ec5c56db | 2490 | /* Process the string STR as if it appeared as the body of a #assert. */ |
45b966db | 2491 | void |
6cf87ca4 | 2492 | cpp_assert (cpp_reader *pfile, const char *str) |
45b966db | 2493 | { |
86368122 | 2494 | handle_assertion (pfile, str, T_ASSERT); |
45b966db | 2495 | } |
7f2935c7 | 2496 | |
ec5c56db | 2497 | /* Process STR as if it appeared as the body of an #unassert. */ |
45b966db | 2498 | void |
6cf87ca4 | 2499 | cpp_unassert (cpp_reader *pfile, const char *str) |
7f2935c7 | 2500 | { |
86368122 | 2501 | handle_assertion (pfile, str, T_UNASSERT); |
df383483 | 2502 | } |
3fdc651f | 2503 | |
86368122 NB |
2504 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
2505 | static void | |
6cf87ca4 | 2506 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
86368122 NB |
2507 | { |
2508 | size_t count = strlen (str); | |
2509 | const char *p = strchr (str, '='); | |
2510 | ||
26aea073 NB |
2511 | /* Copy the entire option so we can modify it. Change the first |
2512 | "=" in the string to a '(', and tack a ')' on the end. */ | |
c3f829c1 | 2513 | char *buf = (char *) alloca (count + 2); |
26aea073 NB |
2514 | |
2515 | memcpy (buf, str, count); | |
86368122 NB |
2516 | if (p) |
2517 | { | |
86368122 NB |
2518 | buf[p - str] = '('; |
2519 | buf[count++] = ')'; | |
86368122 | 2520 | } |
26aea073 NB |
2521 | buf[count] = '\n'; |
2522 | str = buf; | |
86368122 | 2523 | |
29401c30 | 2524 | run_directive (pfile, type, str, count); |
86368122 NB |
2525 | } |
2526 | ||
7e96d768 NB |
2527 | /* The options structure. */ |
2528 | cpp_options * | |
6cf87ca4 | 2529 | cpp_get_options (cpp_reader *pfile) |
7e96d768 NB |
2530 | { |
2531 | return &pfile->opts; | |
2532 | } | |
2533 | ||
2534 | /* The callbacks structure. */ | |
2535 | cpp_callbacks * | |
6cf87ca4 | 2536 | cpp_get_callbacks (cpp_reader *pfile) |
7e96d768 NB |
2537 | { |
2538 | return &pfile->cb; | |
2539 | } | |
2540 | ||
2541 | /* Copy the given callbacks structure to our own. */ | |
2542 | void | |
6cf87ca4 | 2543 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
7e96d768 NB |
2544 | { |
2545 | pfile->cb = *cb; | |
2546 | } | |
2547 | ||
c6e83800 ZW |
2548 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
2549 | struct deps * | |
2550 | cpp_get_deps (cpp_reader *pfile) | |
2551 | { | |
2552 | if (!pfile->deps) | |
2553 | pfile->deps = deps_init (); | |
2554 | return pfile->deps; | |
2555 | } | |
2556 | ||
eb1f4d9d NB |
2557 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
2558 | doesn't fail. It does not generate a file change call back; that | |
2559 | is the responsibility of the caller. */ | |
c71f835b | 2560 | cpp_buffer * |
6cf87ca4 | 2561 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
40de9f76 | 2562 | int from_stage3) |
c71f835b | 2563 | { |
c3f829c1 | 2564 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
93c80368 | 2565 | |
fde84349 | 2566 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
c3f829c1 | 2567 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
fde84349 | 2568 | |
c3f829c1 GDR |
2569 | new_buffer->next_line = new_buffer->buf = buffer; |
2570 | new_buffer->rlimit = buffer + len; | |
2571 | new_buffer->from_stage3 = from_stage3; | |
2572 | new_buffer->prev = pfile->buffer; | |
2573 | new_buffer->need_line = true; | |
0bda4760 | 2574 | |
c3f829c1 | 2575 | pfile->buffer = new_buffer; |
cf551fba | 2576 | |
c3f829c1 | 2577 | return new_buffer; |
c71f835b ZW |
2578 | } |
2579 | ||
af0d16cd NB |
2580 | /* Pops a single buffer, with a file change call-back if appropriate. |
2581 | Then pushes the next -include file, if any remain. */ | |
ef6e958a | 2582 | void |
6cf87ca4 | 2583 | _cpp_pop_buffer (cpp_reader *pfile) |
c71f835b | 2584 | { |
fde84349 | 2585 | cpp_buffer *buffer = pfile->buffer; |
8f9b4009 | 2586 | struct _cpp_file *inc = buffer->file; |
ad2a084d | 2587 | struct if_stack *ifs; |
28937f11 | 2588 | const unsigned char *to_free; |
c71f835b | 2589 | |
fde84349 NB |
2590 | /* Walk back up the conditional stack till we reach its level at |
2591 | entry to this file, issuing error messages. */ | |
2592 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
0527bc4e | 2593 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
fde84349 | 2594 | "unterminated #%s", dtable[ifs->type].name); |
eb1f4d9d | 2595 | |
97293897 | 2596 | /* In case of a missing #endif. */ |
67821e3a | 2597 | pfile->state.skipping = 0; |
29401c30 | 2598 | |
af0d16cd | 2599 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
29401c30 NB |
2600 | pfile->buffer = buffer->prev; |
2601 | ||
28937f11 | 2602 | to_free = buffer->to_free; |
26aea073 NB |
2603 | free (buffer->notes); |
2604 | ||
af0d16cd NB |
2605 | /* Free the buffer object now; we may want to push a new buffer |
2606 | in _cpp_push_next_include_file. */ | |
2607 | obstack_free (&pfile->buffer_ob, buffer); | |
29401c30 | 2608 | |
af0d16cd NB |
2609 | if (inc) |
2610 | { | |
28937f11 | 2611 | _cpp_pop_file_buffer (pfile, inc, to_free); |
af0d16cd | 2612 | |
40de9f76 | 2613 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
af0d16cd | 2614 | } |
c71f835b ZW |
2615 | } |
2616 | ||
05713b80 | 2617 | /* Enter all recognized directives in the hash table. */ |
c71f835b | 2618 | void |
6cf87ca4 | 2619 | _cpp_init_directives (cpp_reader *pfile) |
c71f835b | 2620 | { |
766ee681 | 2621 | unsigned int i; |
93c80368 | 2622 | cpp_hashnode *node; |
bfb9dc7f | 2623 | |
37b8524c | 2624 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
93c80368 | 2625 | { |
766ee681 | 2626 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
4977bab6 ZW |
2627 | node->is_directive = 1; |
2628 | node->directive_index = i; | |
93c80368 | 2629 | } |
c71f835b | 2630 | } |
a15f7cb8 ESR |
2631 | |
2632 | /* Extract header file from a bracket include. Parsing starts after '<'. | |
2633 | The string is malloced and must be freed by the caller. */ | |
2634 | char * | |
2635 | _cpp_bracket_include(cpp_reader *pfile) | |
2636 | { | |
2637 | return glue_header_name (pfile); | |
2638 | } | |
2639 |