136ac495dSmrg /* CPP Library. (Directive handling.)
28feb0f0bSmrg Copyright (C) 1986-2020 Free Software Foundation, Inc.
336ac495dSmrg Contributed by Per Bothner, 1994-95.
436ac495dSmrg Based on CCCP program by Paul Rubin, June 1986
536ac495dSmrg Adapted to ANSI C, Richard Stallman, Jan 1987
636ac495dSmrg
736ac495dSmrg This program is free software; you can redistribute it and/or modify it
836ac495dSmrg under the terms of the GNU General Public License as published by the
936ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
1036ac495dSmrg later version.
1136ac495dSmrg
1236ac495dSmrg This program is distributed in the hope that it will be useful,
1336ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1436ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1536ac495dSmrg GNU General Public License for more details.
1636ac495dSmrg
1736ac495dSmrg You should have received a copy of the GNU General Public License
1836ac495dSmrg along with this program; see the file COPYING3. If not see
1936ac495dSmrg <http://www.gnu.org/licenses/>. */
2036ac495dSmrg
2136ac495dSmrg #include "config.h"
2236ac495dSmrg #include "system.h"
2336ac495dSmrg #include "cpplib.h"
2436ac495dSmrg #include "internal.h"
2536ac495dSmrg #include "mkdeps.h"
2636ac495dSmrg #include "obstack.h"
2736ac495dSmrg
2836ac495dSmrg /* Stack of conditionals currently in progress
2936ac495dSmrg (including both successful and failing conditionals). */
3036ac495dSmrg struct if_stack
3136ac495dSmrg {
3236ac495dSmrg struct if_stack *next;
33c0a68be4Smrg location_t line; /* Line where condition started. */
3436ac495dSmrg const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
3536ac495dSmrg bool skip_elses; /* Can future #else / #elif be skipped? */
3636ac495dSmrg bool was_skipping; /* If were skipping on entry. */
3736ac495dSmrg int type; /* Most recent conditional for diagnostics. */
3836ac495dSmrg };
3936ac495dSmrg
4036ac495dSmrg /* Contains a registered pragma or pragma namespace. */
4136ac495dSmrg typedef void (*pragma_cb) (cpp_reader *);
4236ac495dSmrg struct pragma_entry
4336ac495dSmrg {
4436ac495dSmrg struct pragma_entry *next;
4536ac495dSmrg const cpp_hashnode *pragma; /* Name and length. */
4636ac495dSmrg bool is_nspace;
4736ac495dSmrg bool is_internal;
4836ac495dSmrg bool is_deferred;
4936ac495dSmrg bool allow_expansion;
5036ac495dSmrg union {
5136ac495dSmrg pragma_cb handler;
5236ac495dSmrg struct pragma_entry *space;
5336ac495dSmrg unsigned int ident;
5436ac495dSmrg } u;
5536ac495dSmrg };
5636ac495dSmrg
5736ac495dSmrg /* Values for the origin field of struct directive. KANDR directives
5836ac495dSmrg come from traditional (K&R) C. STDC89 directives come from the
5936ac495dSmrg 1989 C standard. EXTENSION directives are extensions. */
6036ac495dSmrg #define KANDR 0
6136ac495dSmrg #define STDC89 1
6236ac495dSmrg #define EXTENSION 2
6336ac495dSmrg
6436ac495dSmrg /* Values for the flags field of struct directive. COND indicates a
6536ac495dSmrg conditional; IF_COND an opening conditional. INCL means to treat
6636ac495dSmrg "..." and <...> as q-char and h-char sequences respectively. IN_I
6736ac495dSmrg means this directive should be handled even if -fpreprocessed is in
6836ac495dSmrg effect (these are the directives with callback hooks).
6936ac495dSmrg
7036ac495dSmrg EXPAND is set on directives that are always macro-expanded. */
7136ac495dSmrg #define COND (1 << 0)
7236ac495dSmrg #define IF_COND (1 << 1)
7336ac495dSmrg #define INCL (1 << 2)
7436ac495dSmrg #define IN_I (1 << 3)
7536ac495dSmrg #define EXPAND (1 << 4)
7636ac495dSmrg #define DEPRECATED (1 << 5)
7736ac495dSmrg
7836ac495dSmrg /* Defines one #-directive, including how to handle it. */
7936ac495dSmrg typedef void (*directive_handler) (cpp_reader *);
8036ac495dSmrg typedef struct directive directive;
8136ac495dSmrg struct directive
8236ac495dSmrg {
8336ac495dSmrg directive_handler handler; /* Function to handle directive. */
8436ac495dSmrg const uchar *name; /* Name of directive. */
8536ac495dSmrg unsigned short length; /* Length of name. */
8636ac495dSmrg unsigned char origin; /* Origin of directive. */
8736ac495dSmrg unsigned char flags; /* Flags describing this directive. */
8836ac495dSmrg };
8936ac495dSmrg
9036ac495dSmrg /* Forward declarations. */
9136ac495dSmrg
9236ac495dSmrg static void skip_rest_of_line (cpp_reader *);
9336ac495dSmrg static void check_eol (cpp_reader *, bool);
9436ac495dSmrg static void start_directive (cpp_reader *);
9536ac495dSmrg static void prepare_directive_trad (cpp_reader *);
9636ac495dSmrg static void end_directive (cpp_reader *, int);
9736ac495dSmrg static void directive_diagnostics (cpp_reader *, const directive *, int);
9836ac495dSmrg static void run_directive (cpp_reader *, int, const char *, size_t);
9936ac495dSmrg static char *glue_header_name (cpp_reader *);
10036ac495dSmrg static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
101c0a68be4Smrg location_t *);
10236ac495dSmrg static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
10336ac495dSmrg static unsigned int read_flag (cpp_reader *, unsigned int);
10436ac495dSmrg static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
105c0a68be4Smrg static void do_diagnostic (cpp_reader *, enum cpp_diagnostic_level code,
106c0a68be4Smrg enum cpp_warning_reason reason, int);
10736ac495dSmrg static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
10836ac495dSmrg static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
10936ac495dSmrg static void do_include_common (cpp_reader *, enum include_type);
11036ac495dSmrg static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
11136ac495dSmrg const cpp_hashnode *);
11236ac495dSmrg static int count_registered_pragmas (struct pragma_entry *);
11336ac495dSmrg static char ** save_registered_pragmas (struct pragma_entry *, char **);
11436ac495dSmrg static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
11536ac495dSmrg char **);
11636ac495dSmrg static void do_pragma_once (cpp_reader *);
11736ac495dSmrg static void do_pragma_poison (cpp_reader *);
11836ac495dSmrg static void do_pragma_system_header (cpp_reader *);
11936ac495dSmrg static void do_pragma_dependency (cpp_reader *);
12036ac495dSmrg static void do_pragma_warning_or_error (cpp_reader *, bool error);
12136ac495dSmrg static void do_pragma_warning (cpp_reader *);
12236ac495dSmrg static void do_pragma_error (cpp_reader *);
12336ac495dSmrg static void do_linemarker (cpp_reader *);
12436ac495dSmrg static const cpp_token *get_token_no_padding (cpp_reader *);
12536ac495dSmrg static const cpp_token *get__Pragma_string (cpp_reader *);
12636ac495dSmrg static void destringize_and_run (cpp_reader *, const cpp_string *,
127c0a68be4Smrg location_t);
128c0a68be4Smrg static bool parse_answer (cpp_reader *, int, location_t, cpp_macro **);
129c0a68be4Smrg static cpp_hashnode *parse_assertion (cpp_reader *, int, cpp_macro **);
130c0a68be4Smrg static cpp_macro **find_answer (cpp_hashnode *, const cpp_macro *);
13136ac495dSmrg static void handle_assertion (cpp_reader *, const char *, int);
13236ac495dSmrg static void do_pragma_push_macro (cpp_reader *);
13336ac495dSmrg static void do_pragma_pop_macro (cpp_reader *);
13436ac495dSmrg static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
13536ac495dSmrg
136c0a68be4Smrg /* This is the table of directive handlers. All extensions other than
137c0a68be4Smrg #warning, #include_next, and #import are deprecated. The name is
138c0a68be4Smrg where the extension appears to have come from. */
13936ac495dSmrg
14036ac495dSmrg #define DIRECTIVE_TABLE \
141c0a68be4Smrg D(define, T_DEFINE = 0, KANDR, IN_I) \
142c0a68be4Smrg D(include, T_INCLUDE, KANDR, INCL | EXPAND) \
143c0a68be4Smrg D(endif, T_ENDIF, KANDR, COND) \
144c0a68be4Smrg D(ifdef, T_IFDEF, KANDR, COND | IF_COND) \
145c0a68be4Smrg D(if, T_IF, KANDR, COND | IF_COND | EXPAND) \
146c0a68be4Smrg D(else, T_ELSE, KANDR, COND) \
147c0a68be4Smrg D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) \
148c0a68be4Smrg D(undef, T_UNDEF, KANDR, IN_I) \
149c0a68be4Smrg D(line, T_LINE, KANDR, EXPAND) \
150c0a68be4Smrg D(elif, T_ELIF, STDC89, COND | EXPAND) \
151c0a68be4Smrg D(error, T_ERROR, STDC89, 0) \
152c0a68be4Smrg D(pragma, T_PRAGMA, STDC89, IN_I) \
153c0a68be4Smrg D(warning, T_WARNING, EXTENSION, 0) \
154c0a68be4Smrg D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) \
155c0a68be4Smrg D(ident, T_IDENT, EXTENSION, IN_I) \
156c0a68be4Smrg D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* ObjC */ \
157c0a68be4Smrg D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
158c0a68be4Smrg D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* SVR4 */ \
159c0a68be4Smrg D(sccs, T_SCCS, EXTENSION, IN_I) /* SVR4? */
16036ac495dSmrg
16136ac495dSmrg /* #sccs is synonymous with #ident. */
16236ac495dSmrg #define do_sccs do_ident
16336ac495dSmrg
16436ac495dSmrg /* Use the table to generate a series of prototypes, an enum for the
16536ac495dSmrg directive names, and an array of directive handlers. */
16636ac495dSmrg
16736ac495dSmrg #define D(name, t, o, f) static void do_##name (cpp_reader *);
16836ac495dSmrg DIRECTIVE_TABLE
16936ac495dSmrg #undef D
17036ac495dSmrg
17136ac495dSmrg #define D(n, tag, o, f) tag,
17236ac495dSmrg enum
17336ac495dSmrg {
17436ac495dSmrg DIRECTIVE_TABLE
17536ac495dSmrg N_DIRECTIVES
17636ac495dSmrg };
17736ac495dSmrg #undef D
17836ac495dSmrg
17936ac495dSmrg #define D(name, t, origin, flags) \
18036ac495dSmrg { do_##name, (const uchar *) #name, \
18136ac495dSmrg sizeof #name - 1, origin, flags },
18236ac495dSmrg static const directive dtable[] =
18336ac495dSmrg {
18436ac495dSmrg DIRECTIVE_TABLE
18536ac495dSmrg };
18636ac495dSmrg #undef D
18736ac495dSmrg
18836ac495dSmrg /* A NULL-terminated array of directive names for use
18936ac495dSmrg when suggesting corrections for misspelled directives. */
19036ac495dSmrg #define D(name, t, origin, flags) #name,
19136ac495dSmrg static const char * const directive_names[] = {
19236ac495dSmrg DIRECTIVE_TABLE
19336ac495dSmrg NULL
19436ac495dSmrg };
19536ac495dSmrg #undef D
19636ac495dSmrg
19736ac495dSmrg #undef DIRECTIVE_TABLE
19836ac495dSmrg
19936ac495dSmrg /* Wrapper struct directive for linemarkers.
20036ac495dSmrg The origin is more or less true - the original K+R cpp
20136ac495dSmrg did use this notation in its preprocessed output. */
20236ac495dSmrg static const directive linemarker_dir =
20336ac495dSmrg {
20436ac495dSmrg do_linemarker, UC"#", 1, KANDR, IN_I
20536ac495dSmrg };
20636ac495dSmrg
20736ac495dSmrg /* Skip any remaining tokens in a directive. */
20836ac495dSmrg static void
skip_rest_of_line(cpp_reader * pfile)20936ac495dSmrg skip_rest_of_line (cpp_reader *pfile)
21036ac495dSmrg {
21136ac495dSmrg /* Discard all stacked contexts. */
21236ac495dSmrg while (pfile->context->prev)
21336ac495dSmrg _cpp_pop_context (pfile);
21436ac495dSmrg
21536ac495dSmrg /* Sweep up all tokens remaining on the line. */
21636ac495dSmrg if (! SEEN_EOL ())
21736ac495dSmrg while (_cpp_lex_token (pfile)->type != CPP_EOF)
21836ac495dSmrg ;
21936ac495dSmrg }
22036ac495dSmrg
22136ac495dSmrg /* Helper function for check_oel. */
22236ac495dSmrg
22336ac495dSmrg static void
check_eol_1(cpp_reader * pfile,bool expand,enum cpp_warning_reason reason)224c0a68be4Smrg check_eol_1 (cpp_reader *pfile, bool expand, enum cpp_warning_reason reason)
22536ac495dSmrg {
22636ac495dSmrg if (! SEEN_EOL () && (expand
22736ac495dSmrg ? cpp_get_token (pfile)
22836ac495dSmrg : _cpp_lex_token (pfile))->type != CPP_EOF)
22936ac495dSmrg cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive",
23036ac495dSmrg pfile->directive->name);
23136ac495dSmrg }
23236ac495dSmrg
23336ac495dSmrg /* Variant of check_eol used for Wendif-labels warnings. */
23436ac495dSmrg
23536ac495dSmrg static void
check_eol_endif_labels(cpp_reader * pfile)23636ac495dSmrg check_eol_endif_labels (cpp_reader *pfile)
23736ac495dSmrg {
23836ac495dSmrg check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS);
23936ac495dSmrg }
24036ac495dSmrg
24136ac495dSmrg /* Ensure there are no stray tokens at the end of a directive. If
24236ac495dSmrg EXPAND is true, tokens macro-expanding to nothing are allowed. */
24336ac495dSmrg
24436ac495dSmrg static void
check_eol(cpp_reader * pfile,bool expand)24536ac495dSmrg check_eol (cpp_reader *pfile, bool expand)
24636ac495dSmrg {
24736ac495dSmrg check_eol_1 (pfile, expand, CPP_W_NONE);
24836ac495dSmrg }
24936ac495dSmrg
25036ac495dSmrg /* Ensure there are no stray tokens other than comments at the end of
25136ac495dSmrg a directive, and gather the comments. */
25236ac495dSmrg static const cpp_token **
check_eol_return_comments(cpp_reader * pfile)25336ac495dSmrg check_eol_return_comments (cpp_reader *pfile)
25436ac495dSmrg {
25536ac495dSmrg size_t c;
25636ac495dSmrg size_t capacity = 8;
25736ac495dSmrg const cpp_token **buf;
25836ac495dSmrg
25936ac495dSmrg buf = XNEWVEC (const cpp_token *, capacity);
26036ac495dSmrg c = 0;
26136ac495dSmrg if (! SEEN_EOL ())
26236ac495dSmrg {
26336ac495dSmrg while (1)
26436ac495dSmrg {
26536ac495dSmrg const cpp_token *tok;
26636ac495dSmrg
26736ac495dSmrg tok = _cpp_lex_token (pfile);
26836ac495dSmrg if (tok->type == CPP_EOF)
26936ac495dSmrg break;
27036ac495dSmrg if (tok->type != CPP_COMMENT)
27136ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
27236ac495dSmrg "extra tokens at end of #%s directive",
27336ac495dSmrg pfile->directive->name);
27436ac495dSmrg else
27536ac495dSmrg {
27636ac495dSmrg if (c + 1 >= capacity)
27736ac495dSmrg {
27836ac495dSmrg capacity *= 2;
27936ac495dSmrg buf = XRESIZEVEC (const cpp_token *, buf, capacity);
28036ac495dSmrg }
28136ac495dSmrg buf[c] = tok;
28236ac495dSmrg ++c;
28336ac495dSmrg }
28436ac495dSmrg }
28536ac495dSmrg }
28636ac495dSmrg buf[c] = NULL;
28736ac495dSmrg return buf;
28836ac495dSmrg }
28936ac495dSmrg
29036ac495dSmrg /* Called when entering a directive, _Pragma or command-line directive. */
29136ac495dSmrg static void
start_directive(cpp_reader * pfile)29236ac495dSmrg start_directive (cpp_reader *pfile)
29336ac495dSmrg {
29436ac495dSmrg /* Setup in-directive state. */
29536ac495dSmrg pfile->state.in_directive = 1;
29636ac495dSmrg pfile->state.save_comments = 0;
29736ac495dSmrg pfile->directive_result.type = CPP_PADDING;
29836ac495dSmrg
29936ac495dSmrg /* Some handlers need the position of the # for diagnostics. */
30036ac495dSmrg pfile->directive_line = pfile->line_table->highest_line;
30136ac495dSmrg }
30236ac495dSmrg
30336ac495dSmrg /* Called when leaving a directive, _Pragma or command-line directive. */
30436ac495dSmrg static void
end_directive(cpp_reader * pfile,int skip_line)30536ac495dSmrg end_directive (cpp_reader *pfile, int skip_line)
30636ac495dSmrg {
30736ac495dSmrg if (CPP_OPTION (pfile, traditional))
30836ac495dSmrg {
30936ac495dSmrg /* Revert change of prepare_directive_trad. */
31036ac495dSmrg if (!pfile->state.in_deferred_pragma)
31136ac495dSmrg pfile->state.prevent_expansion--;
31236ac495dSmrg
31336ac495dSmrg if (pfile->directive != &dtable[T_DEFINE])
31436ac495dSmrg _cpp_remove_overlay (pfile);
31536ac495dSmrg }
31636ac495dSmrg else if (pfile->state.in_deferred_pragma)
31736ac495dSmrg ;
31836ac495dSmrg /* We don't skip for an assembler #. */
31936ac495dSmrg else if (skip_line)
32036ac495dSmrg {
32136ac495dSmrg skip_rest_of_line (pfile);
32236ac495dSmrg if (!pfile->keep_tokens)
32336ac495dSmrg {
32436ac495dSmrg pfile->cur_run = &pfile->base_run;
32536ac495dSmrg pfile->cur_token = pfile->base_run.base;
32636ac495dSmrg }
32736ac495dSmrg }
32836ac495dSmrg
32936ac495dSmrg /* Restore state. */
33036ac495dSmrg pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
33136ac495dSmrg pfile->state.in_directive = 0;
33236ac495dSmrg pfile->state.in_expression = 0;
33336ac495dSmrg pfile->state.angled_headers = 0;
33436ac495dSmrg pfile->directive = 0;
33536ac495dSmrg }
33636ac495dSmrg
33736ac495dSmrg /* Prepare to handle the directive in pfile->directive. */
33836ac495dSmrg static void
prepare_directive_trad(cpp_reader * pfile)33936ac495dSmrg prepare_directive_trad (cpp_reader *pfile)
34036ac495dSmrg {
34136ac495dSmrg if (pfile->directive != &dtable[T_DEFINE])
34236ac495dSmrg {
34336ac495dSmrg bool no_expand = (pfile->directive
34436ac495dSmrg && ! (pfile->directive->flags & EXPAND));
34536ac495dSmrg bool was_skipping = pfile->state.skipping;
34636ac495dSmrg
34736ac495dSmrg pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
34836ac495dSmrg || pfile->directive == &dtable[T_ELIF]);
34936ac495dSmrg if (pfile->state.in_expression)
35036ac495dSmrg pfile->state.skipping = false;
35136ac495dSmrg
35236ac495dSmrg if (no_expand)
35336ac495dSmrg pfile->state.prevent_expansion++;
35436ac495dSmrg _cpp_scan_out_logical_line (pfile, NULL, false);
35536ac495dSmrg if (no_expand)
35636ac495dSmrg pfile->state.prevent_expansion--;
35736ac495dSmrg
35836ac495dSmrg pfile->state.skipping = was_skipping;
35936ac495dSmrg _cpp_overlay_buffer (pfile, pfile->out.base,
36036ac495dSmrg pfile->out.cur - pfile->out.base);
36136ac495dSmrg }
36236ac495dSmrg
36336ac495dSmrg /* Stop ISO C from expanding anything. */
36436ac495dSmrg pfile->state.prevent_expansion++;
36536ac495dSmrg }
36636ac495dSmrg
36736ac495dSmrg /* Output diagnostics for a directive DIR. INDENTED is nonzero if
36836ac495dSmrg the '#' was indented. */
36936ac495dSmrg static void
directive_diagnostics(cpp_reader * pfile,const directive * dir,int indented)37036ac495dSmrg directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
37136ac495dSmrg {
37236ac495dSmrg /* Issue -pedantic or deprecated warnings for extensions. We let
37336ac495dSmrg -pedantic take precedence if both are applicable. */
37436ac495dSmrg if (! pfile->state.skipping)
37536ac495dSmrg {
37636ac495dSmrg if (dir->origin == EXTENSION
37736ac495dSmrg && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
37836ac495dSmrg && CPP_PEDANTIC (pfile))
37936ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
38036ac495dSmrg else if (((dir->flags & DEPRECATED) != 0
38136ac495dSmrg || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
38236ac495dSmrg && CPP_OPTION (pfile, cpp_warn_deprecated))
38336ac495dSmrg cpp_warning (pfile, CPP_W_DEPRECATED,
38436ac495dSmrg "#%s is a deprecated GCC extension", dir->name);
38536ac495dSmrg }
38636ac495dSmrg
38736ac495dSmrg /* Traditionally, a directive is ignored unless its # is in
38836ac495dSmrg column 1. Therefore in code intended to work with K+R
38936ac495dSmrg compilers, directives added by C89 must have their #
39036ac495dSmrg indented, and directives present in traditional C must not.
39136ac495dSmrg This is true even of directives in skipped conditional
39236ac495dSmrg blocks. #elif cannot be used at all. */
39336ac495dSmrg if (CPP_WTRADITIONAL (pfile))
39436ac495dSmrg {
39536ac495dSmrg if (dir == &dtable[T_ELIF])
39636ac495dSmrg cpp_warning (pfile, CPP_W_TRADITIONAL,
39736ac495dSmrg "suggest not using #elif in traditional C");
39836ac495dSmrg else if (indented && dir->origin == KANDR)
39936ac495dSmrg cpp_warning (pfile, CPP_W_TRADITIONAL,
40036ac495dSmrg "traditional C ignores #%s with the # indented",
40136ac495dSmrg dir->name);
40236ac495dSmrg else if (!indented && dir->origin != KANDR)
40336ac495dSmrg cpp_warning (pfile, CPP_W_TRADITIONAL,
40436ac495dSmrg "suggest hiding #%s from traditional C with an indented #",
40536ac495dSmrg dir->name);
40636ac495dSmrg }
40736ac495dSmrg }
40836ac495dSmrg
4098feb0f0bSmrg /* Check if we have a known directive. INDENTED is true if the
41036ac495dSmrg '#' of the directive was indented. This function is in this file
41136ac495dSmrg to save unnecessarily exporting dtable etc. to lex.c. Returns
41236ac495dSmrg nonzero if the line of tokens has been handled, zero if we should
41336ac495dSmrg continue processing the line. */
41436ac495dSmrg int
_cpp_handle_directive(cpp_reader * pfile,bool indented)4158feb0f0bSmrg _cpp_handle_directive (cpp_reader *pfile, bool indented)
41636ac495dSmrg {
41736ac495dSmrg const directive *dir = 0;
41836ac495dSmrg const cpp_token *dname;
41936ac495dSmrg bool was_parsing_args = pfile->state.parsing_args;
42036ac495dSmrg bool was_discarding_output = pfile->state.discarding_output;
42136ac495dSmrg int skip = 1;
42236ac495dSmrg
42336ac495dSmrg if (was_discarding_output)
42436ac495dSmrg pfile->state.prevent_expansion = 0;
42536ac495dSmrg
42636ac495dSmrg if (was_parsing_args)
42736ac495dSmrg {
42836ac495dSmrg if (CPP_OPTION (pfile, cpp_pedantic))
42936ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
43036ac495dSmrg "embedding a directive within macro arguments is not portable");
43136ac495dSmrg pfile->state.parsing_args = 0;
43236ac495dSmrg pfile->state.prevent_expansion = 0;
43336ac495dSmrg }
43436ac495dSmrg start_directive (pfile);
43536ac495dSmrg dname = _cpp_lex_token (pfile);
43636ac495dSmrg
43736ac495dSmrg if (dname->type == CPP_NAME)
43836ac495dSmrg {
43936ac495dSmrg if (dname->val.node.node->is_directive)
44036ac495dSmrg dir = &dtable[dname->val.node.node->directive_index];
44136ac495dSmrg }
44236ac495dSmrg /* We do not recognize the # followed by a number extension in
44336ac495dSmrg assembler code. */
44436ac495dSmrg else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
44536ac495dSmrg {
44636ac495dSmrg dir = &linemarker_dir;
44736ac495dSmrg if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
44836ac495dSmrg && ! pfile->state.skipping)
44936ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN,
45036ac495dSmrg "style of line directive is a GCC extension");
45136ac495dSmrg }
45236ac495dSmrg
45336ac495dSmrg if (dir)
45436ac495dSmrg {
45536ac495dSmrg /* If we have a directive that is not an opening conditional,
45636ac495dSmrg invalidate any control macro. */
45736ac495dSmrg if (! (dir->flags & IF_COND))
45836ac495dSmrg pfile->mi_valid = false;
45936ac495dSmrg
46036ac495dSmrg /* Kluge alert. In order to be sure that code like this
46136ac495dSmrg
46236ac495dSmrg #define HASH #
46336ac495dSmrg HASH define foo bar
46436ac495dSmrg
46536ac495dSmrg does not cause '#define foo bar' to get executed when
46636ac495dSmrg compiled with -save-temps, we recognize directives in
46736ac495dSmrg -fpreprocessed mode only if the # is in column 1. macro.c
46836ac495dSmrg puts a space in front of any '#' at the start of a macro.
46936ac495dSmrg
47036ac495dSmrg We exclude the -fdirectives-only case because macro expansion
47136ac495dSmrg has not been performed yet, and block comments can cause spaces
47236ac495dSmrg to precede the directive. */
47336ac495dSmrg if (CPP_OPTION (pfile, preprocessed)
47436ac495dSmrg && !CPP_OPTION (pfile, directives_only)
47536ac495dSmrg && (indented || !(dir->flags & IN_I)))
47636ac495dSmrg {
47736ac495dSmrg skip = 0;
47836ac495dSmrg dir = 0;
47936ac495dSmrg }
48036ac495dSmrg else
48136ac495dSmrg {
48236ac495dSmrg /* In failed conditional groups, all non-conditional
48336ac495dSmrg directives are ignored. Before doing that, whether
48436ac495dSmrg skipping or not, we should lex angle-bracketed headers
48536ac495dSmrg correctly, and maybe output some diagnostics. */
48636ac495dSmrg pfile->state.angled_headers = dir->flags & INCL;
48736ac495dSmrg pfile->state.directive_wants_padding = dir->flags & INCL;
48836ac495dSmrg if (! CPP_OPTION (pfile, preprocessed))
48936ac495dSmrg directive_diagnostics (pfile, dir, indented);
49036ac495dSmrg if (pfile->state.skipping && !(dir->flags & COND))
49136ac495dSmrg dir = 0;
49236ac495dSmrg }
49336ac495dSmrg }
49436ac495dSmrg else if (dname->type == CPP_EOF)
49536ac495dSmrg ; /* CPP_EOF is the "null directive". */
49636ac495dSmrg else
49736ac495dSmrg {
49836ac495dSmrg /* An unknown directive. Don't complain about it in assembly
49936ac495dSmrg source: we don't know where the comments are, and # may
50036ac495dSmrg introduce assembler pseudo-ops. Don't complain about invalid
50136ac495dSmrg directives in skipped conditional groups (6.10 p4). */
50236ac495dSmrg if (CPP_OPTION (pfile, lang) == CLK_ASM)
50336ac495dSmrg skip = 0;
50436ac495dSmrg else if (!pfile->state.skipping)
50536ac495dSmrg {
50636ac495dSmrg const char *unrecognized
50736ac495dSmrg = (const char *)cpp_token_as_text (pfile, dname);
50836ac495dSmrg const char *hint = NULL;
50936ac495dSmrg
51036ac495dSmrg /* Call back into gcc to get a spelling suggestion. Ideally
51136ac495dSmrg we'd just use best_match from gcc/spellcheck.h (and filter
51236ac495dSmrg out the uncommon directives), but that requires moving it
51336ac495dSmrg to a support library. */
51436ac495dSmrg if (pfile->cb.get_suggestion)
51536ac495dSmrg hint = pfile->cb.get_suggestion (pfile, unrecognized,
51636ac495dSmrg directive_names);
51736ac495dSmrg
51836ac495dSmrg if (hint)
51936ac495dSmrg {
52036ac495dSmrg rich_location richloc (pfile->line_table, dname->src_loc);
52136ac495dSmrg source_range misspelled_token_range
52236ac495dSmrg = get_range_from_loc (pfile->line_table, dname->src_loc);
52336ac495dSmrg richloc.add_fixit_replace (misspelled_token_range, hint);
524a2dc1f3fSmrg cpp_error_at (pfile, CPP_DL_ERROR, &richloc,
52536ac495dSmrg "invalid preprocessing directive #%s;"
52636ac495dSmrg " did you mean #%s?",
52736ac495dSmrg unrecognized, hint);
52836ac495dSmrg }
52936ac495dSmrg else
53036ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
53136ac495dSmrg "invalid preprocessing directive #%s",
53236ac495dSmrg unrecognized);
53336ac495dSmrg }
53436ac495dSmrg }
53536ac495dSmrg
53636ac495dSmrg pfile->directive = dir;
53736ac495dSmrg if (CPP_OPTION (pfile, traditional))
53836ac495dSmrg prepare_directive_trad (pfile);
53936ac495dSmrg
54036ac495dSmrg if (dir)
54136ac495dSmrg pfile->directive->handler (pfile);
54236ac495dSmrg else if (skip == 0)
54336ac495dSmrg _cpp_backup_tokens (pfile, 1);
54436ac495dSmrg
54536ac495dSmrg end_directive (pfile, skip);
54636ac495dSmrg if (was_parsing_args && !pfile->state.in_deferred_pragma)
54736ac495dSmrg {
54836ac495dSmrg /* Restore state when within macro args. */
54936ac495dSmrg pfile->state.parsing_args = 2;
55036ac495dSmrg pfile->state.prevent_expansion = 1;
55136ac495dSmrg }
55236ac495dSmrg if (was_discarding_output)
55336ac495dSmrg pfile->state.prevent_expansion = 1;
55436ac495dSmrg return skip;
55536ac495dSmrg }
55636ac495dSmrg
55736ac495dSmrg /* Directive handler wrapper used by the command line option
55836ac495dSmrg processor. BUF is \n terminated. */
55936ac495dSmrg static void
run_directive(cpp_reader * pfile,int dir_no,const char * buf,size_t count)56036ac495dSmrg run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
56136ac495dSmrg {
56236ac495dSmrg cpp_push_buffer (pfile, (const uchar *) buf, count,
56336ac495dSmrg /* from_stage3 */ true);
56436ac495dSmrg start_directive (pfile);
56536ac495dSmrg
56636ac495dSmrg /* This is a short-term fix to prevent a leading '#' being
56736ac495dSmrg interpreted as a directive. */
56836ac495dSmrg _cpp_clean_line (pfile);
56936ac495dSmrg
57036ac495dSmrg pfile->directive = &dtable[dir_no];
57136ac495dSmrg if (CPP_OPTION (pfile, traditional))
57236ac495dSmrg prepare_directive_trad (pfile);
57336ac495dSmrg pfile->directive->handler (pfile);
57436ac495dSmrg end_directive (pfile, 1);
57536ac495dSmrg _cpp_pop_buffer (pfile);
57636ac495dSmrg }
57736ac495dSmrg
57836ac495dSmrg /* Checks for validity the macro name in #define, #undef, #ifdef and
57936ac495dSmrg #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
58036ac495dSmrg processing a #define or #undefine directive, and false
58136ac495dSmrg otherwise. */
58236ac495dSmrg static cpp_hashnode *
lex_macro_node(cpp_reader * pfile,bool is_def_or_undef)58336ac495dSmrg lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
58436ac495dSmrg {
58536ac495dSmrg const cpp_token *token = _cpp_lex_token (pfile);
58636ac495dSmrg
58736ac495dSmrg /* The token immediately after #define must be an identifier. That
58836ac495dSmrg identifier may not be "defined", per C99 6.10.8p4.
58936ac495dSmrg In C++, it may not be any of the "named operators" either,
59036ac495dSmrg per C++98 [lex.digraph], [lex.key].
59136ac495dSmrg Finally, the identifier may not have been poisoned. (In that case
59236ac495dSmrg the lexer has issued the error message for us.) */
59336ac495dSmrg
59436ac495dSmrg if (token->type == CPP_NAME)
59536ac495dSmrg {
59636ac495dSmrg cpp_hashnode *node = token->val.node.node;
59736ac495dSmrg
5988feb0f0bSmrg if (is_def_or_undef
5998feb0f0bSmrg && node == pfile->spec_nodes.n_defined)
60036ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
6018feb0f0bSmrg "\"%s\" cannot be used as a macro name",
6028feb0f0bSmrg NODE_NAME (node));
60336ac495dSmrg else if (! (node->flags & NODE_POISONED))
60436ac495dSmrg return node;
60536ac495dSmrg }
60636ac495dSmrg else if (token->flags & NAMED_OP)
60736ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
60836ac495dSmrg "\"%s\" cannot be used as a macro name as it is an operator in C++",
60936ac495dSmrg NODE_NAME (token->val.node.node));
61036ac495dSmrg else if (token->type == CPP_EOF)
61136ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
61236ac495dSmrg pfile->directive->name);
61336ac495dSmrg else
61436ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
61536ac495dSmrg
61636ac495dSmrg return NULL;
61736ac495dSmrg }
61836ac495dSmrg
61936ac495dSmrg /* Process a #define directive. Most work is done in macro.c. */
62036ac495dSmrg static void
do_define(cpp_reader * pfile)62136ac495dSmrg do_define (cpp_reader *pfile)
62236ac495dSmrg {
62336ac495dSmrg cpp_hashnode *node = lex_macro_node (pfile, true);
62436ac495dSmrg
62536ac495dSmrg if (node)
62636ac495dSmrg {
62736ac495dSmrg /* If we have been requested to expand comments into macros,
62836ac495dSmrg then re-enable saving of comments. */
62936ac495dSmrg pfile->state.save_comments =
63036ac495dSmrg ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
63136ac495dSmrg
63236ac495dSmrg if (pfile->cb.before_define)
63336ac495dSmrg pfile->cb.before_define (pfile);
63436ac495dSmrg
63536ac495dSmrg if (_cpp_create_definition (pfile, node))
63636ac495dSmrg if (pfile->cb.define)
63736ac495dSmrg pfile->cb.define (pfile, pfile->directive_line, node);
63836ac495dSmrg
63936ac495dSmrg node->flags &= ~NODE_USED;
64036ac495dSmrg }
64136ac495dSmrg }
64236ac495dSmrg
64336ac495dSmrg /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
64436ac495dSmrg static void
do_undef(cpp_reader * pfile)64536ac495dSmrg do_undef (cpp_reader *pfile)
64636ac495dSmrg {
64736ac495dSmrg cpp_hashnode *node = lex_macro_node (pfile, true);
64836ac495dSmrg
64936ac495dSmrg if (node)
65036ac495dSmrg {
65136ac495dSmrg if (pfile->cb.before_define)
65236ac495dSmrg pfile->cb.before_define (pfile);
65336ac495dSmrg
65436ac495dSmrg if (pfile->cb.undef)
65536ac495dSmrg pfile->cb.undef (pfile, pfile->directive_line, node);
65636ac495dSmrg
65736ac495dSmrg /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
65836ac495dSmrg identifier is not currently defined as a macro name. */
659c0a68be4Smrg if (cpp_macro_p (node))
66036ac495dSmrg {
66136ac495dSmrg if (node->flags & NODE_WARN)
66236ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
66336ac495dSmrg "undefining \"%s\"", NODE_NAME (node));
664c0a68be4Smrg else if (cpp_builtin_macro_p (node)
66536ac495dSmrg && CPP_OPTION (pfile, warn_builtin_macro_redefined))
66636ac495dSmrg cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
66736ac495dSmrg pfile->directive_line, 0,
66836ac495dSmrg "undefining \"%s\"", NODE_NAME (node));
66936ac495dSmrg
67036ac495dSmrg if (CPP_OPTION (pfile, warn_unused_macros))
67136ac495dSmrg _cpp_warn_if_unused_macro (pfile, node, NULL);
67236ac495dSmrg
67336ac495dSmrg _cpp_free_definition (node);
67436ac495dSmrg }
67536ac495dSmrg }
67636ac495dSmrg
67736ac495dSmrg check_eol (pfile, false);
67836ac495dSmrg }
67936ac495dSmrg
68036ac495dSmrg /* Undefine a single macro/assertion/whatever. */
68136ac495dSmrg
68236ac495dSmrg static int
undefine_macros(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * h,void * data_p ATTRIBUTE_UNUSED)68336ac495dSmrg undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
68436ac495dSmrg void *data_p ATTRIBUTE_UNUSED)
68536ac495dSmrg {
68636ac495dSmrg /* Body of _cpp_free_definition inlined here for speed.
68736ac495dSmrg Macros and assertions no longer have anything to free. */
68836ac495dSmrg h->type = NT_VOID;
689c0a68be4Smrg h->value.answers = NULL;
690c0a68be4Smrg h->flags &= ~(NODE_POISONED|NODE_DISABLED|NODE_USED);
69136ac495dSmrg return 1;
69236ac495dSmrg }
69336ac495dSmrg
69436ac495dSmrg /* Undefine all macros and assertions. */
69536ac495dSmrg
69636ac495dSmrg void
cpp_undef_all(cpp_reader * pfile)69736ac495dSmrg cpp_undef_all (cpp_reader *pfile)
69836ac495dSmrg {
69936ac495dSmrg cpp_forall_identifiers (pfile, undefine_macros, NULL);
70036ac495dSmrg }
70136ac495dSmrg
70236ac495dSmrg
70336ac495dSmrg /* Helper routine used by parse_include. Reinterpret the current line
70436ac495dSmrg as an h-char-sequence (< ... >); we are looking at the first token
70536ac495dSmrg after the <. Returns a malloced filename. */
70636ac495dSmrg static char *
glue_header_name(cpp_reader * pfile)70736ac495dSmrg glue_header_name (cpp_reader *pfile)
70836ac495dSmrg {
70936ac495dSmrg const cpp_token *token;
71036ac495dSmrg char *buffer;
71136ac495dSmrg size_t len, total_len = 0, capacity = 1024;
71236ac495dSmrg
71336ac495dSmrg /* To avoid lexed tokens overwriting our glued name, we can only
71436ac495dSmrg allocate from the string pool once we've lexed everything. */
71536ac495dSmrg buffer = XNEWVEC (char, capacity);
71636ac495dSmrg for (;;)
71736ac495dSmrg {
71836ac495dSmrg token = get_token_no_padding (pfile);
71936ac495dSmrg
72036ac495dSmrg if (token->type == CPP_GREATER)
72136ac495dSmrg break;
72236ac495dSmrg if (token->type == CPP_EOF)
72336ac495dSmrg {
72436ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
72536ac495dSmrg break;
72636ac495dSmrg }
72736ac495dSmrg
72836ac495dSmrg len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
72936ac495dSmrg if (total_len + len > capacity)
73036ac495dSmrg {
73136ac495dSmrg capacity = (capacity + len) * 2;
73236ac495dSmrg buffer = XRESIZEVEC (char, buffer, capacity);
73336ac495dSmrg }
73436ac495dSmrg
73536ac495dSmrg if (token->flags & PREV_WHITE)
73636ac495dSmrg buffer[total_len++] = ' ';
73736ac495dSmrg
73836ac495dSmrg total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
73936ac495dSmrg true)
74036ac495dSmrg - (uchar *) buffer);
74136ac495dSmrg }
74236ac495dSmrg
74336ac495dSmrg buffer[total_len] = '\0';
74436ac495dSmrg return buffer;
74536ac495dSmrg }
74636ac495dSmrg
74736ac495dSmrg /* Returns the file name of #include, #include_next, #import and
74836ac495dSmrg #pragma dependency. The string is malloced and the caller should
74936ac495dSmrg free it. Returns NULL on error. LOCATION is the source location
75036ac495dSmrg of the file name. */
75136ac495dSmrg
75236ac495dSmrg static const char *
parse_include(cpp_reader * pfile,int * pangle_brackets,const cpp_token *** buf,location_t * location)75336ac495dSmrg parse_include (cpp_reader *pfile, int *pangle_brackets,
754c0a68be4Smrg const cpp_token ***buf, location_t *location)
75536ac495dSmrg {
75636ac495dSmrg char *fname;
75736ac495dSmrg const cpp_token *header;
75836ac495dSmrg
75936ac495dSmrg /* Allow macro expansion. */
76036ac495dSmrg header = get_token_no_padding (pfile);
76136ac495dSmrg *location = header->src_loc;
76236ac495dSmrg if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
76336ac495dSmrg || header->type == CPP_HEADER_NAME)
76436ac495dSmrg {
76536ac495dSmrg fname = XNEWVEC (char, header->val.str.len - 1);
76636ac495dSmrg memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
76736ac495dSmrg fname[header->val.str.len - 2] = '\0';
76836ac495dSmrg *pangle_brackets = header->type == CPP_HEADER_NAME;
76936ac495dSmrg }
77036ac495dSmrg else if (header->type == CPP_LESS)
77136ac495dSmrg {
77236ac495dSmrg fname = glue_header_name (pfile);
77336ac495dSmrg *pangle_brackets = 1;
77436ac495dSmrg }
77536ac495dSmrg else
77636ac495dSmrg {
77736ac495dSmrg const unsigned char *dir;
77836ac495dSmrg
77936ac495dSmrg if (pfile->directive == &dtable[T_PRAGMA])
78036ac495dSmrg dir = UC"pragma dependency";
78136ac495dSmrg else
78236ac495dSmrg dir = pfile->directive->name;
78336ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
78436ac495dSmrg dir);
78536ac495dSmrg
78636ac495dSmrg return NULL;
78736ac495dSmrg }
78836ac495dSmrg
78936ac495dSmrg if (pfile->directive == &dtable[T_PRAGMA])
79036ac495dSmrg {
79136ac495dSmrg /* This pragma allows extra tokens after the file name. */
79236ac495dSmrg }
79336ac495dSmrg else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
79436ac495dSmrg check_eol (pfile, true);
79536ac495dSmrg else
79636ac495dSmrg {
79736ac495dSmrg /* If we are not discarding comments, then gather them while
79836ac495dSmrg doing the eol check. */
79936ac495dSmrg *buf = check_eol_return_comments (pfile);
80036ac495dSmrg }
80136ac495dSmrg
80236ac495dSmrg return fname;
80336ac495dSmrg }
80436ac495dSmrg
80536ac495dSmrg /* Handle #include, #include_next and #import. */
80636ac495dSmrg static void
do_include_common(cpp_reader * pfile,enum include_type type)80736ac495dSmrg do_include_common (cpp_reader *pfile, enum include_type type)
80836ac495dSmrg {
80936ac495dSmrg const char *fname;
81036ac495dSmrg int angle_brackets;
81136ac495dSmrg const cpp_token **buf = NULL;
812c0a68be4Smrg location_t location;
81336ac495dSmrg
81436ac495dSmrg /* Re-enable saving of comments if requested, so that the include
81536ac495dSmrg callback can dump comments which follow #include. */
81636ac495dSmrg pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
81736ac495dSmrg
8188feb0f0bSmrg /* Tell the lexer this is an include directive -- we want it to
8198feb0f0bSmrg increment the line number even if this is the last line of a file. */
8208feb0f0bSmrg pfile->state.in_directive = 2;
8218feb0f0bSmrg
82236ac495dSmrg fname = parse_include (pfile, &angle_brackets, &buf, &location);
82336ac495dSmrg if (!fname)
824c0a68be4Smrg goto done;
82536ac495dSmrg
82636ac495dSmrg if (!*fname)
82736ac495dSmrg {
82836ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
82936ac495dSmrg "empty filename in #%s",
83036ac495dSmrg pfile->directive->name);
831c0a68be4Smrg goto done;
83236ac495dSmrg }
83336ac495dSmrg
83436ac495dSmrg /* Prevent #include recursion. */
8358feb0f0bSmrg if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth))
8368feb0f0bSmrg cpp_error (pfile,
8378feb0f0bSmrg CPP_DL_ERROR,
8388feb0f0bSmrg "#include nested depth %u exceeds maximum of %u"
8398feb0f0bSmrg " (use -fmax-include-depth=DEPTH to increase the maximum)",
8408feb0f0bSmrg pfile->line_table->depth,
8418feb0f0bSmrg CPP_OPTION (pfile, max_include_depth));
84236ac495dSmrg else
84336ac495dSmrg {
84436ac495dSmrg /* Get out of macro context, if we are. */
84536ac495dSmrg skip_rest_of_line (pfile);
84636ac495dSmrg
84736ac495dSmrg if (pfile->cb.include)
84836ac495dSmrg pfile->cb.include (pfile, pfile->directive_line,
84936ac495dSmrg pfile->directive->name, fname, angle_brackets,
85036ac495dSmrg buf);
85136ac495dSmrg
85236ac495dSmrg _cpp_stack_include (pfile, fname, angle_brackets, type, location);
85336ac495dSmrg }
85436ac495dSmrg
855c0a68be4Smrg done:
85636ac495dSmrg XDELETEVEC (fname);
85736ac495dSmrg if (buf)
85836ac495dSmrg XDELETEVEC (buf);
85936ac495dSmrg }
86036ac495dSmrg
86136ac495dSmrg static void
do_include(cpp_reader * pfile)86236ac495dSmrg do_include (cpp_reader *pfile)
86336ac495dSmrg {
86436ac495dSmrg do_include_common (pfile, IT_INCLUDE);
86536ac495dSmrg }
86636ac495dSmrg
86736ac495dSmrg static void
do_import(cpp_reader * pfile)86836ac495dSmrg do_import (cpp_reader *pfile)
86936ac495dSmrg {
87036ac495dSmrg do_include_common (pfile, IT_IMPORT);
87136ac495dSmrg }
87236ac495dSmrg
87336ac495dSmrg static void
do_include_next(cpp_reader * pfile)87436ac495dSmrg do_include_next (cpp_reader *pfile)
87536ac495dSmrg {
87636ac495dSmrg enum include_type type = IT_INCLUDE_NEXT;
87736ac495dSmrg
87836ac495dSmrg /* If this is the primary source file, warn and use the normal
87936ac495dSmrg search logic. */
88036ac495dSmrg if (cpp_in_primary_file (pfile))
88136ac495dSmrg {
88236ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
88336ac495dSmrg "#include_next in primary source file");
88436ac495dSmrg type = IT_INCLUDE;
88536ac495dSmrg }
88636ac495dSmrg do_include_common (pfile, type);
88736ac495dSmrg }
88836ac495dSmrg
88936ac495dSmrg /* Subroutine of do_linemarker. Read possible flags after file name.
89036ac495dSmrg LAST is the last flag seen; 0 if this is the first flag. Return the
89136ac495dSmrg flag if it is valid, 0 at the end of the directive. Otherwise
89236ac495dSmrg complain. */
89336ac495dSmrg static unsigned int
read_flag(cpp_reader * pfile,unsigned int last)89436ac495dSmrg read_flag (cpp_reader *pfile, unsigned int last)
89536ac495dSmrg {
89636ac495dSmrg const cpp_token *token = _cpp_lex_token (pfile);
89736ac495dSmrg
89836ac495dSmrg if (token->type == CPP_NUMBER && token->val.str.len == 1)
89936ac495dSmrg {
90036ac495dSmrg unsigned int flag = token->val.str.text[0] - '0';
90136ac495dSmrg
90236ac495dSmrg if (flag > last && flag <= 4
90336ac495dSmrg && (flag != 4 || last == 3)
90436ac495dSmrg && (flag != 2 || last == 0))
90536ac495dSmrg return flag;
90636ac495dSmrg }
90736ac495dSmrg
90836ac495dSmrg if (token->type != CPP_EOF)
90936ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
91036ac495dSmrg cpp_token_as_text (pfile, token));
91136ac495dSmrg return 0;
91236ac495dSmrg }
91336ac495dSmrg
91436ac495dSmrg /* Subroutine of do_line and do_linemarker. Convert a number in STR,
91536ac495dSmrg of length LEN, to binary; store it in NUMP, and return false if the
91636ac495dSmrg number was well-formed, true if not. WRAPPED is set to true if the
91736ac495dSmrg number did not fit into 'unsigned long'. */
91836ac495dSmrg static bool
strtolinenum(const uchar * str,size_t len,linenum_type * nump,bool * wrapped)91936ac495dSmrg strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
92036ac495dSmrg {
92136ac495dSmrg linenum_type reg = 0;
92236ac495dSmrg linenum_type reg_prev = 0;
92336ac495dSmrg
92436ac495dSmrg uchar c;
92536ac495dSmrg *wrapped = false;
92636ac495dSmrg while (len--)
92736ac495dSmrg {
92836ac495dSmrg c = *str++;
92936ac495dSmrg if (!ISDIGIT (c))
93036ac495dSmrg return true;
93136ac495dSmrg reg *= 10;
93236ac495dSmrg reg += c - '0';
93336ac495dSmrg if (reg < reg_prev)
93436ac495dSmrg *wrapped = true;
93536ac495dSmrg reg_prev = reg;
93636ac495dSmrg }
93736ac495dSmrg *nump = reg;
93836ac495dSmrg return false;
93936ac495dSmrg }
94036ac495dSmrg
94136ac495dSmrg /* Interpret #line command.
94236ac495dSmrg Note that the filename string (if any) is a true string constant
94336ac495dSmrg (escapes are interpreted), unlike in #line. */
94436ac495dSmrg static void
do_line(cpp_reader * pfile)94536ac495dSmrg do_line (cpp_reader *pfile)
94636ac495dSmrg {
9478feb0f0bSmrg class line_maps *line_table = pfile->line_table;
94836ac495dSmrg const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
94936ac495dSmrg
95036ac495dSmrg /* skip_rest_of_line() may cause line table to be realloc()ed so note down
95136ac495dSmrg sysp right now. */
95236ac495dSmrg
95336ac495dSmrg unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
95436ac495dSmrg const cpp_token *token;
95536ac495dSmrg const char *new_file = ORDINARY_MAP_FILE_NAME (map);
95636ac495dSmrg linenum_type new_lineno;
95736ac495dSmrg
95836ac495dSmrg /* C99 raised the minimum limit on #line numbers. */
95936ac495dSmrg linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
96036ac495dSmrg bool wrapped;
96136ac495dSmrg
96236ac495dSmrg /* #line commands expand macros. */
96336ac495dSmrg token = cpp_get_token (pfile);
96436ac495dSmrg if (token->type != CPP_NUMBER
96536ac495dSmrg || strtolinenum (token->val.str.text, token->val.str.len,
96636ac495dSmrg &new_lineno, &wrapped))
96736ac495dSmrg {
96836ac495dSmrg if (token->type == CPP_EOF)
96936ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
97036ac495dSmrg else
97136ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
97236ac495dSmrg "\"%s\" after #line is not a positive integer",
97336ac495dSmrg cpp_token_as_text (pfile, token));
97436ac495dSmrg return;
97536ac495dSmrg }
97636ac495dSmrg
97736ac495dSmrg if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
97836ac495dSmrg cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
97936ac495dSmrg else if (wrapped)
98036ac495dSmrg cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
98136ac495dSmrg
98236ac495dSmrg token = cpp_get_token (pfile);
98336ac495dSmrg if (token->type == CPP_STRING)
98436ac495dSmrg {
98536ac495dSmrg cpp_string s = { 0, 0 };
98636ac495dSmrg if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
98736ac495dSmrg &s, CPP_STRING))
98836ac495dSmrg new_file = (const char *)s.text;
98936ac495dSmrg check_eol (pfile, true);
99036ac495dSmrg }
99136ac495dSmrg else if (token->type != CPP_EOF)
99236ac495dSmrg {
99336ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
99436ac495dSmrg cpp_token_as_text (pfile, token));
99536ac495dSmrg return;
99636ac495dSmrg }
99736ac495dSmrg
99836ac495dSmrg skip_rest_of_line (pfile);
99936ac495dSmrg _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
100036ac495dSmrg map_sysp);
100136ac495dSmrg line_table->seen_line_directive = true;
100236ac495dSmrg }
100336ac495dSmrg
100436ac495dSmrg /* Interpret the # 44 "file" [flags] notation, which has slightly
100536ac495dSmrg different syntax and semantics from #line: Flags are allowed,
100636ac495dSmrg and we never complain about the line number being too big. */
100736ac495dSmrg static void
do_linemarker(cpp_reader * pfile)100836ac495dSmrg do_linemarker (cpp_reader *pfile)
100936ac495dSmrg {
10108feb0f0bSmrg class line_maps *line_table = pfile->line_table;
101136ac495dSmrg const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
101236ac495dSmrg const cpp_token *token;
101336ac495dSmrg const char *new_file = ORDINARY_MAP_FILE_NAME (map);
101436ac495dSmrg linenum_type new_lineno;
101536ac495dSmrg unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
101636ac495dSmrg enum lc_reason reason = LC_RENAME_VERBATIM;
101736ac495dSmrg int flag;
101836ac495dSmrg bool wrapped;
101936ac495dSmrg
102036ac495dSmrg /* Back up so we can get the number again. Putting this in
102136ac495dSmrg _cpp_handle_directive risks two calls to _cpp_backup_tokens in
102236ac495dSmrg some circumstances, which can segfault. */
102336ac495dSmrg _cpp_backup_tokens (pfile, 1);
102436ac495dSmrg
102536ac495dSmrg /* #line commands expand macros. */
102636ac495dSmrg token = cpp_get_token (pfile);
102736ac495dSmrg if (token->type != CPP_NUMBER
102836ac495dSmrg || strtolinenum (token->val.str.text, token->val.str.len,
102936ac495dSmrg &new_lineno, &wrapped))
103036ac495dSmrg {
103136ac495dSmrg /* Unlike #line, there does not seem to be a way to get an EOF
103236ac495dSmrg here. So, it should be safe to always spell the token. */
103336ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
103436ac495dSmrg "\"%s\" after # is not a positive integer",
103536ac495dSmrg cpp_token_as_text (pfile, token));
103636ac495dSmrg return;
103736ac495dSmrg }
103836ac495dSmrg
103936ac495dSmrg token = cpp_get_token (pfile);
104036ac495dSmrg if (token->type == CPP_STRING)
104136ac495dSmrg {
104236ac495dSmrg cpp_string s = { 0, 0 };
104336ac495dSmrg if (cpp_interpret_string_notranslate (pfile, &token->val.str,
104436ac495dSmrg 1, &s, CPP_STRING))
104536ac495dSmrg new_file = (const char *)s.text;
104636ac495dSmrg
104736ac495dSmrg new_sysp = 0;
104836ac495dSmrg flag = read_flag (pfile, 0);
104936ac495dSmrg if (flag == 1)
105036ac495dSmrg {
105136ac495dSmrg reason = LC_ENTER;
105236ac495dSmrg /* Fake an include for cpp_included (). */
105336ac495dSmrg _cpp_fake_include (pfile, new_file);
105436ac495dSmrg flag = read_flag (pfile, flag);
105536ac495dSmrg }
105636ac495dSmrg else if (flag == 2)
105736ac495dSmrg {
105836ac495dSmrg reason = LC_LEAVE;
105936ac495dSmrg flag = read_flag (pfile, flag);
106036ac495dSmrg }
106136ac495dSmrg if (flag == 3)
106236ac495dSmrg {
106336ac495dSmrg new_sysp = 1;
106436ac495dSmrg flag = read_flag (pfile, flag);
106536ac495dSmrg if (flag == 4)
106636ac495dSmrg new_sysp = 2;
106736ac495dSmrg }
106836ac495dSmrg pfile->buffer->sysp = new_sysp;
106936ac495dSmrg
107036ac495dSmrg check_eol (pfile, false);
107136ac495dSmrg }
107236ac495dSmrg else if (token->type != CPP_EOF)
107336ac495dSmrg {
107436ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
107536ac495dSmrg cpp_token_as_text (pfile, token));
107636ac495dSmrg return;
107736ac495dSmrg }
107836ac495dSmrg
107936ac495dSmrg skip_rest_of_line (pfile);
108036ac495dSmrg
108136ac495dSmrg if (reason == LC_LEAVE)
108236ac495dSmrg {
108336ac495dSmrg /* Reread map since cpp_get_token can invalidate it with a
108436ac495dSmrg reallocation. */
108536ac495dSmrg map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
1086c0a68be4Smrg const line_map_ordinary *from
1087c0a68be4Smrg = linemap_included_from_linemap (line_table, map);
10888feb0f0bSmrg
10898feb0f0bSmrg if (!from)
10908feb0f0bSmrg /* Not nested. */;
10918feb0f0bSmrg else if (!new_file[0])
10928feb0f0bSmrg /* Leaving to "" means fill in the popped-to name. */
10938feb0f0bSmrg new_file = ORDINARY_MAP_FILE_NAME (from);
10948feb0f0bSmrg else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0)
10958feb0f0bSmrg /* It's the wrong name, Grommit! */
10968feb0f0bSmrg from = NULL;
10978feb0f0bSmrg
10988feb0f0bSmrg if (!from)
109936ac495dSmrg {
110036ac495dSmrg cpp_warning (pfile, CPP_W_NONE,
110136ac495dSmrg "file \"%s\" linemarker ignored due to "
110236ac495dSmrg "incorrect nesting", new_file);
110336ac495dSmrg return;
110436ac495dSmrg }
110536ac495dSmrg }
11068feb0f0bSmrg
110736ac495dSmrg /* Compensate for the increment in linemap_add that occurs in
110836ac495dSmrg _cpp_do_file_change. We're currently at the start of the line
1109c0a68be4Smrg *following* the #line directive. A separate location_t for this
111036ac495dSmrg location makes no sense (until we do the LC_LEAVE), and
111136ac495dSmrg complicates LAST_SOURCE_LINE_LOCATION. */
111236ac495dSmrg pfile->line_table->highest_location--;
111336ac495dSmrg
111436ac495dSmrg _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
111536ac495dSmrg line_table->seen_line_directive = true;
111636ac495dSmrg }
111736ac495dSmrg
111836ac495dSmrg /* Arrange the file_change callback. pfile->line has changed to
111936ac495dSmrg FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
112036ac495dSmrg header, 2 for a system header that needs to be extern "C" protected,
112136ac495dSmrg and zero otherwise. */
112236ac495dSmrg void
_cpp_do_file_change(cpp_reader * pfile,enum lc_reason reason,const char * to_file,linenum_type file_line,unsigned int sysp)112336ac495dSmrg _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
112436ac495dSmrg const char *to_file, linenum_type file_line,
112536ac495dSmrg unsigned int sysp)
112636ac495dSmrg {
112736ac495dSmrg linemap_assert (reason != LC_ENTER_MACRO);
112836ac495dSmrg const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
112936ac495dSmrg to_file, file_line);
113036ac495dSmrg const line_map_ordinary *ord_map = NULL;
113136ac495dSmrg if (map != NULL)
113236ac495dSmrg {
113336ac495dSmrg ord_map = linemap_check_ordinary (map);
113436ac495dSmrg linemap_line_start (pfile->line_table,
113536ac495dSmrg ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
113636ac495dSmrg 127);
113736ac495dSmrg }
113836ac495dSmrg
113936ac495dSmrg if (pfile->cb.file_change)
114036ac495dSmrg pfile->cb.file_change (pfile, ord_map);
114136ac495dSmrg }
114236ac495dSmrg
114336ac495dSmrg /* Report a warning or error detected by the program we are
114436ac495dSmrg processing. Use the directive's tokens in the error message. */
114536ac495dSmrg static void
do_diagnostic(cpp_reader * pfile,enum cpp_diagnostic_level code,enum cpp_warning_reason reason,int print_dir)1146c0a68be4Smrg do_diagnostic (cpp_reader *pfile, enum cpp_diagnostic_level code,
1147c0a68be4Smrg enum cpp_warning_reason reason, int print_dir)
114836ac495dSmrg {
114936ac495dSmrg const unsigned char *dir_name;
115036ac495dSmrg unsigned char *line;
1151c0a68be4Smrg location_t src_loc = pfile->cur_token[-1].src_loc;
115236ac495dSmrg
115336ac495dSmrg if (print_dir)
115436ac495dSmrg dir_name = pfile->directive->name;
115536ac495dSmrg else
115636ac495dSmrg dir_name = NULL;
115736ac495dSmrg pfile->state.prevent_expansion++;
115836ac495dSmrg line = cpp_output_line_to_string (pfile, dir_name);
115936ac495dSmrg pfile->state.prevent_expansion--;
116036ac495dSmrg
116136ac495dSmrg if (code == CPP_DL_WARNING_SYSHDR && reason)
116236ac495dSmrg cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
116336ac495dSmrg else if (code == CPP_DL_WARNING && reason)
116436ac495dSmrg cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
116536ac495dSmrg else
116636ac495dSmrg cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
116736ac495dSmrg free (line);
116836ac495dSmrg }
116936ac495dSmrg
117036ac495dSmrg static void
do_error(cpp_reader * pfile)117136ac495dSmrg do_error (cpp_reader *pfile)
117236ac495dSmrg {
1173c0a68be4Smrg do_diagnostic (pfile, CPP_DL_ERROR, CPP_W_NONE, 1);
117436ac495dSmrg }
117536ac495dSmrg
117636ac495dSmrg static void
do_warning(cpp_reader * pfile)117736ac495dSmrg do_warning (cpp_reader *pfile)
117836ac495dSmrg {
117936ac495dSmrg /* We want #warning diagnostics to be emitted in system headers too. */
118036ac495dSmrg do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
118136ac495dSmrg }
118236ac495dSmrg
118336ac495dSmrg /* Report program identification. */
118436ac495dSmrg static void
do_ident(cpp_reader * pfile)118536ac495dSmrg do_ident (cpp_reader *pfile)
118636ac495dSmrg {
118736ac495dSmrg const cpp_token *str = cpp_get_token (pfile);
118836ac495dSmrg
118936ac495dSmrg if (str->type != CPP_STRING)
119036ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
119136ac495dSmrg pfile->directive->name);
119236ac495dSmrg else if (pfile->cb.ident)
119336ac495dSmrg pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
119436ac495dSmrg
119536ac495dSmrg check_eol (pfile, false);
119636ac495dSmrg }
119736ac495dSmrg
119836ac495dSmrg /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
119936ac495dSmrg matching entry, or NULL if none is found. The returned entry could
120036ac495dSmrg be the start of a namespace chain, or a pragma. */
120136ac495dSmrg static struct pragma_entry *
lookup_pragma_entry(struct pragma_entry * chain,const cpp_hashnode * pragma)120236ac495dSmrg lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
120336ac495dSmrg {
120436ac495dSmrg while (chain && chain->pragma != pragma)
120536ac495dSmrg chain = chain->next;
120636ac495dSmrg
120736ac495dSmrg return chain;
120836ac495dSmrg }
120936ac495dSmrg
121036ac495dSmrg /* Create and insert a blank pragma entry at the beginning of a
121136ac495dSmrg singly-linked CHAIN. */
121236ac495dSmrg static struct pragma_entry *
new_pragma_entry(cpp_reader * pfile,struct pragma_entry ** chain)121336ac495dSmrg new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
121436ac495dSmrg {
121536ac495dSmrg struct pragma_entry *new_entry;
121636ac495dSmrg
121736ac495dSmrg new_entry = (struct pragma_entry *)
121836ac495dSmrg _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
121936ac495dSmrg
122036ac495dSmrg memset (new_entry, 0, sizeof (struct pragma_entry));
122136ac495dSmrg new_entry->next = *chain;
122236ac495dSmrg
122336ac495dSmrg *chain = new_entry;
122436ac495dSmrg return new_entry;
122536ac495dSmrg }
122636ac495dSmrg
122736ac495dSmrg /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
122836ac495dSmrg goes in the global namespace. */
122936ac495dSmrg static struct pragma_entry *
register_pragma_1(cpp_reader * pfile,const char * space,const char * name,bool allow_name_expansion)123036ac495dSmrg register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
123136ac495dSmrg bool allow_name_expansion)
123236ac495dSmrg {
123336ac495dSmrg struct pragma_entry **chain = &pfile->pragmas;
123436ac495dSmrg struct pragma_entry *entry;
123536ac495dSmrg const cpp_hashnode *node;
123636ac495dSmrg
123736ac495dSmrg if (space)
123836ac495dSmrg {
123936ac495dSmrg node = cpp_lookup (pfile, UC space, strlen (space));
124036ac495dSmrg entry = lookup_pragma_entry (*chain, node);
124136ac495dSmrg if (!entry)
124236ac495dSmrg {
124336ac495dSmrg entry = new_pragma_entry (pfile, chain);
124436ac495dSmrg entry->pragma = node;
124536ac495dSmrg entry->is_nspace = true;
124636ac495dSmrg entry->allow_expansion = allow_name_expansion;
124736ac495dSmrg }
124836ac495dSmrg else if (!entry->is_nspace)
124936ac495dSmrg goto clash;
125036ac495dSmrg else if (entry->allow_expansion != allow_name_expansion)
125136ac495dSmrg {
125236ac495dSmrg cpp_error (pfile, CPP_DL_ICE,
125336ac495dSmrg "registering pragmas in namespace \"%s\" with mismatched "
125436ac495dSmrg "name expansion", space);
125536ac495dSmrg return NULL;
125636ac495dSmrg }
125736ac495dSmrg chain = &entry->u.space;
125836ac495dSmrg }
125936ac495dSmrg else if (allow_name_expansion)
126036ac495dSmrg {
126136ac495dSmrg cpp_error (pfile, CPP_DL_ICE,
126236ac495dSmrg "registering pragma \"%s\" with name expansion "
126336ac495dSmrg "and no namespace", name);
126436ac495dSmrg return NULL;
126536ac495dSmrg }
126636ac495dSmrg
126736ac495dSmrg /* Check for duplicates. */
126836ac495dSmrg node = cpp_lookup (pfile, UC name, strlen (name));
126936ac495dSmrg entry = lookup_pragma_entry (*chain, node);
127036ac495dSmrg if (entry == NULL)
127136ac495dSmrg {
127236ac495dSmrg entry = new_pragma_entry (pfile, chain);
127336ac495dSmrg entry->pragma = node;
127436ac495dSmrg return entry;
127536ac495dSmrg }
127636ac495dSmrg
127736ac495dSmrg if (entry->is_nspace)
127836ac495dSmrg clash:
127936ac495dSmrg cpp_error (pfile, CPP_DL_ICE,
128036ac495dSmrg "registering \"%s\" as both a pragma and a pragma namespace",
128136ac495dSmrg NODE_NAME (node));
128236ac495dSmrg else if (space)
128336ac495dSmrg cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
128436ac495dSmrg space, name);
128536ac495dSmrg else
128636ac495dSmrg cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
128736ac495dSmrg
128836ac495dSmrg return NULL;
128936ac495dSmrg }
129036ac495dSmrg
129136ac495dSmrg /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
129236ac495dSmrg static void
register_pragma_internal(cpp_reader * pfile,const char * space,const char * name,pragma_cb handler)129336ac495dSmrg register_pragma_internal (cpp_reader *pfile, const char *space,
129436ac495dSmrg const char *name, pragma_cb handler)
129536ac495dSmrg {
129636ac495dSmrg struct pragma_entry *entry;
129736ac495dSmrg
129836ac495dSmrg entry = register_pragma_1 (pfile, space, name, false);
129936ac495dSmrg entry->is_internal = true;
130036ac495dSmrg entry->u.handler = handler;
130136ac495dSmrg }
130236ac495dSmrg
130336ac495dSmrg /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
130436ac495dSmrg goes in the global namespace. HANDLER is the handler it will call,
130536ac495dSmrg which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
130636ac495dSmrg expansion while parsing pragma NAME. This function is exported
130736ac495dSmrg from libcpp. */
130836ac495dSmrg void
cpp_register_pragma(cpp_reader * pfile,const char * space,const char * name,pragma_cb handler,bool allow_expansion)130936ac495dSmrg cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
131036ac495dSmrg pragma_cb handler, bool allow_expansion)
131136ac495dSmrg {
131236ac495dSmrg struct pragma_entry *entry;
131336ac495dSmrg
131436ac495dSmrg if (!handler)
131536ac495dSmrg {
131636ac495dSmrg cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
131736ac495dSmrg return;
131836ac495dSmrg }
131936ac495dSmrg
132036ac495dSmrg entry = register_pragma_1 (pfile, space, name, false);
132136ac495dSmrg if (entry)
132236ac495dSmrg {
132336ac495dSmrg entry->allow_expansion = allow_expansion;
132436ac495dSmrg entry->u.handler = handler;
132536ac495dSmrg }
132636ac495dSmrg }
132736ac495dSmrg
132836ac495dSmrg /* Similarly, but create mark the pragma for deferred processing.
132936ac495dSmrg When found, a CPP_PRAGMA token will be insertted into the stream
133036ac495dSmrg with IDENT in the token->u.pragma slot. */
133136ac495dSmrg void
cpp_register_deferred_pragma(cpp_reader * pfile,const char * space,const char * name,unsigned int ident,bool allow_expansion,bool allow_name_expansion)133236ac495dSmrg cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
133336ac495dSmrg const char *name, unsigned int ident,
133436ac495dSmrg bool allow_expansion, bool allow_name_expansion)
133536ac495dSmrg {
133636ac495dSmrg struct pragma_entry *entry;
133736ac495dSmrg
133836ac495dSmrg entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
133936ac495dSmrg if (entry)
134036ac495dSmrg {
134136ac495dSmrg entry->is_deferred = true;
134236ac495dSmrg entry->allow_expansion = allow_expansion;
134336ac495dSmrg entry->u.ident = ident;
134436ac495dSmrg }
134536ac495dSmrg }
134636ac495dSmrg
134736ac495dSmrg /* Register the pragmas the preprocessor itself handles. */
134836ac495dSmrg void
_cpp_init_internal_pragmas(cpp_reader * pfile)134936ac495dSmrg _cpp_init_internal_pragmas (cpp_reader *pfile)
135036ac495dSmrg {
135136ac495dSmrg /* Pragmas in the global namespace. */
135236ac495dSmrg register_pragma_internal (pfile, 0, "once", do_pragma_once);
135336ac495dSmrg register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
135436ac495dSmrg register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
135536ac495dSmrg
135636ac495dSmrg /* New GCC-specific pragmas should be put in the GCC namespace. */
135736ac495dSmrg register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
135836ac495dSmrg register_pragma_internal (pfile, "GCC", "system_header",
135936ac495dSmrg do_pragma_system_header);
136036ac495dSmrg register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
136136ac495dSmrg register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
136236ac495dSmrg register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
136336ac495dSmrg }
136436ac495dSmrg
136536ac495dSmrg /* Return the number of registered pragmas in PE. */
136636ac495dSmrg
136736ac495dSmrg static int
count_registered_pragmas(struct pragma_entry * pe)136836ac495dSmrg count_registered_pragmas (struct pragma_entry *pe)
136936ac495dSmrg {
137036ac495dSmrg int ct = 0;
137136ac495dSmrg for (; pe != NULL; pe = pe->next)
137236ac495dSmrg {
137336ac495dSmrg if (pe->is_nspace)
137436ac495dSmrg ct += count_registered_pragmas (pe->u.space);
137536ac495dSmrg ct++;
137636ac495dSmrg }
137736ac495dSmrg return ct;
137836ac495dSmrg }
137936ac495dSmrg
138036ac495dSmrg /* Save into SD the names of the registered pragmas referenced by PE,
138136ac495dSmrg and return a pointer to the next free space in SD. */
138236ac495dSmrg
138336ac495dSmrg static char **
save_registered_pragmas(struct pragma_entry * pe,char ** sd)138436ac495dSmrg save_registered_pragmas (struct pragma_entry *pe, char **sd)
138536ac495dSmrg {
138636ac495dSmrg for (; pe != NULL; pe = pe->next)
138736ac495dSmrg {
138836ac495dSmrg if (pe->is_nspace)
138936ac495dSmrg sd = save_registered_pragmas (pe->u.space, sd);
139036ac495dSmrg *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
139136ac495dSmrg HT_LEN (&pe->pragma->ident),
139236ac495dSmrg HT_LEN (&pe->pragma->ident) + 1);
139336ac495dSmrg }
139436ac495dSmrg return sd;
139536ac495dSmrg }
139636ac495dSmrg
139736ac495dSmrg /* Return a newly-allocated array which saves the names of the
139836ac495dSmrg registered pragmas. */
139936ac495dSmrg
140036ac495dSmrg char **
_cpp_save_pragma_names(cpp_reader * pfile)140136ac495dSmrg _cpp_save_pragma_names (cpp_reader *pfile)
140236ac495dSmrg {
140336ac495dSmrg int ct = count_registered_pragmas (pfile->pragmas);
140436ac495dSmrg char **result = XNEWVEC (char *, ct);
140536ac495dSmrg (void) save_registered_pragmas (pfile->pragmas, result);
140636ac495dSmrg return result;
140736ac495dSmrg }
140836ac495dSmrg
140936ac495dSmrg /* Restore from SD the names of the registered pragmas referenced by PE,
141036ac495dSmrg and return a pointer to the next unused name in SD. */
141136ac495dSmrg
141236ac495dSmrg static char **
restore_registered_pragmas(cpp_reader * pfile,struct pragma_entry * pe,char ** sd)141336ac495dSmrg restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
141436ac495dSmrg char **sd)
141536ac495dSmrg {
141636ac495dSmrg for (; pe != NULL; pe = pe->next)
141736ac495dSmrg {
141836ac495dSmrg if (pe->is_nspace)
141936ac495dSmrg sd = restore_registered_pragmas (pfile, pe->u.space, sd);
142036ac495dSmrg pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
142136ac495dSmrg free (*sd);
142236ac495dSmrg sd++;
142336ac495dSmrg }
142436ac495dSmrg return sd;
142536ac495dSmrg }
142636ac495dSmrg
142736ac495dSmrg /* Restore the names of the registered pragmas from SAVED. */
142836ac495dSmrg
142936ac495dSmrg void
_cpp_restore_pragma_names(cpp_reader * pfile,char ** saved)143036ac495dSmrg _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
143136ac495dSmrg {
143236ac495dSmrg (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
143336ac495dSmrg free (saved);
143436ac495dSmrg }
143536ac495dSmrg
143636ac495dSmrg /* Pragmata handling. We handle some, and pass the rest on to the
143736ac495dSmrg front end. C99 defines three pragmas and says that no macro
143836ac495dSmrg expansion is to be performed on them; whether or not macro
143936ac495dSmrg expansion happens for other pragmas is implementation defined.
144036ac495dSmrg This implementation allows for a mix of both, since GCC did not
144136ac495dSmrg traditionally macro expand its (few) pragmas, whereas OpenMP
144236ac495dSmrg specifies that macro expansion should happen. */
144336ac495dSmrg static void
do_pragma(cpp_reader * pfile)144436ac495dSmrg do_pragma (cpp_reader *pfile)
144536ac495dSmrg {
144636ac495dSmrg const struct pragma_entry *p = NULL;
144736ac495dSmrg const cpp_token *token, *pragma_token;
1448c0a68be4Smrg location_t pragma_token_virt_loc = 0;
144936ac495dSmrg cpp_token ns_token;
145036ac495dSmrg unsigned int count = 1;
145136ac495dSmrg
145236ac495dSmrg pfile->state.prevent_expansion++;
145336ac495dSmrg
145436ac495dSmrg pragma_token = token = cpp_get_token_with_location (pfile,
145536ac495dSmrg &pragma_token_virt_loc);
145636ac495dSmrg ns_token = *token;
145736ac495dSmrg if (token->type == CPP_NAME)
145836ac495dSmrg {
145936ac495dSmrg p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
146036ac495dSmrg if (p && p->is_nspace)
146136ac495dSmrg {
146236ac495dSmrg bool allow_name_expansion = p->allow_expansion;
146336ac495dSmrg if (allow_name_expansion)
146436ac495dSmrg pfile->state.prevent_expansion--;
146536ac495dSmrg
146636ac495dSmrg token = cpp_get_token (pfile);
146736ac495dSmrg if (token->type == CPP_NAME)
146836ac495dSmrg p = lookup_pragma_entry (p->u.space, token->val.node.node);
146936ac495dSmrg else
147036ac495dSmrg p = NULL;
147136ac495dSmrg if (allow_name_expansion)
147236ac495dSmrg pfile->state.prevent_expansion++;
147336ac495dSmrg count = 2;
147436ac495dSmrg }
147536ac495dSmrg }
147636ac495dSmrg
147736ac495dSmrg if (p)
147836ac495dSmrg {
147936ac495dSmrg if (p->is_deferred)
148036ac495dSmrg {
148136ac495dSmrg pfile->directive_result.src_loc = pragma_token_virt_loc;
148236ac495dSmrg pfile->directive_result.type = CPP_PRAGMA;
148336ac495dSmrg pfile->directive_result.flags = pragma_token->flags;
148436ac495dSmrg pfile->directive_result.val.pragma = p->u.ident;
148536ac495dSmrg pfile->state.in_deferred_pragma = true;
148636ac495dSmrg pfile->state.pragma_allow_expansion = p->allow_expansion;
148736ac495dSmrg if (!p->allow_expansion)
148836ac495dSmrg pfile->state.prevent_expansion++;
148936ac495dSmrg }
149036ac495dSmrg else
149136ac495dSmrg {
149236ac495dSmrg /* Since the handler below doesn't get the line number, that
149336ac495dSmrg it might need for diagnostics, make sure it has the right
149436ac495dSmrg numbers in place. */
149536ac495dSmrg if (pfile->cb.line_change)
149636ac495dSmrg (*pfile->cb.line_change) (pfile, pragma_token, false);
149736ac495dSmrg if (p->allow_expansion)
149836ac495dSmrg pfile->state.prevent_expansion--;
149936ac495dSmrg (*p->u.handler) (pfile);
150036ac495dSmrg if (p->allow_expansion)
150136ac495dSmrg pfile->state.prevent_expansion++;
150236ac495dSmrg }
150336ac495dSmrg }
150436ac495dSmrg else if (pfile->cb.def_pragma)
150536ac495dSmrg {
150636ac495dSmrg if (count == 1 || pfile->context->prev == NULL)
150736ac495dSmrg _cpp_backup_tokens (pfile, count);
150836ac495dSmrg else
150936ac495dSmrg {
151036ac495dSmrg /* Invalid name comes from macro expansion, _cpp_backup_tokens
151136ac495dSmrg won't allow backing 2 tokens. */
151236ac495dSmrg /* ??? The token buffer is leaked. Perhaps if def_pragma hook
151336ac495dSmrg reads both tokens, we could perhaps free it, but if it doesn't,
151436ac495dSmrg we don't know the exact lifespan. */
151536ac495dSmrg cpp_token *toks = XNEWVEC (cpp_token, 2);
151636ac495dSmrg toks[0] = ns_token;
151736ac495dSmrg toks[0].flags |= NO_EXPAND;
151836ac495dSmrg toks[1] = *token;
151936ac495dSmrg toks[1].flags |= NO_EXPAND;
152036ac495dSmrg _cpp_push_token_context (pfile, NULL, toks, 2);
152136ac495dSmrg }
152236ac495dSmrg pfile->cb.def_pragma (pfile, pfile->directive_line);
152336ac495dSmrg }
152436ac495dSmrg
152536ac495dSmrg pfile->state.prevent_expansion--;
152636ac495dSmrg }
152736ac495dSmrg
152836ac495dSmrg /* Handle #pragma once. */
152936ac495dSmrg static void
do_pragma_once(cpp_reader * pfile)153036ac495dSmrg do_pragma_once (cpp_reader *pfile)
153136ac495dSmrg {
153236ac495dSmrg if (cpp_in_primary_file (pfile))
153336ac495dSmrg cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
153436ac495dSmrg
153536ac495dSmrg check_eol (pfile, false);
153636ac495dSmrg _cpp_mark_file_once_only (pfile, pfile->buffer->file);
153736ac495dSmrg }
153836ac495dSmrg
153936ac495dSmrg /* Handle #pragma push_macro(STRING). */
154036ac495dSmrg static void
do_pragma_push_macro(cpp_reader * pfile)154136ac495dSmrg do_pragma_push_macro (cpp_reader *pfile)
154236ac495dSmrg {
154336ac495dSmrg cpp_hashnode *node;
154436ac495dSmrg size_t defnlen;
154536ac495dSmrg const uchar *defn = NULL;
154636ac495dSmrg char *macroname, *dest;
154736ac495dSmrg const char *limit, *src;
154836ac495dSmrg const cpp_token *txt;
154936ac495dSmrg struct def_pragma_macro *c;
155036ac495dSmrg
155136ac495dSmrg txt = get__Pragma_string (pfile);
155236ac495dSmrg if (!txt)
155336ac495dSmrg {
1554c0a68be4Smrg location_t src_loc = pfile->cur_token[-1].src_loc;
155536ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
155636ac495dSmrg "invalid #pragma push_macro directive");
155736ac495dSmrg check_eol (pfile, false);
155836ac495dSmrg skip_rest_of_line (pfile);
155936ac495dSmrg return;
156036ac495dSmrg }
156136ac495dSmrg dest = macroname = (char *) alloca (txt->val.str.len + 2);
156236ac495dSmrg src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
156336ac495dSmrg limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
156436ac495dSmrg while (src < limit)
156536ac495dSmrg {
156636ac495dSmrg /* We know there is a character following the backslash. */
156736ac495dSmrg if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
156836ac495dSmrg src++;
156936ac495dSmrg *dest++ = *src++;
157036ac495dSmrg }
157136ac495dSmrg *dest = 0;
157236ac495dSmrg check_eol (pfile, false);
157336ac495dSmrg skip_rest_of_line (pfile);
157436ac495dSmrg c = XNEW (struct def_pragma_macro);
157536ac495dSmrg memset (c, 0, sizeof (struct def_pragma_macro));
157636ac495dSmrg c->name = XNEWVAR (char, strlen (macroname) + 1);
157736ac495dSmrg strcpy (c->name, macroname);
157836ac495dSmrg c->next = pfile->pushed_macros;
157936ac495dSmrg node = _cpp_lex_identifier (pfile, c->name);
158036ac495dSmrg if (node->type == NT_VOID)
158136ac495dSmrg c->is_undef = 1;
1582c0a68be4Smrg else if (node->type == NT_BUILTIN_MACRO)
1583a2dc1f3fSmrg c->is_builtin = 1;
158436ac495dSmrg else
158536ac495dSmrg {
158636ac495dSmrg defn = cpp_macro_definition (pfile, node);
158736ac495dSmrg defnlen = ustrlen (defn);
158836ac495dSmrg c->definition = XNEWVEC (uchar, defnlen + 2);
158936ac495dSmrg c->definition[defnlen] = '\n';
159036ac495dSmrg c->definition[defnlen + 1] = 0;
159136ac495dSmrg c->line = node->value.macro->line;
159236ac495dSmrg c->syshdr = node->value.macro->syshdr;
159336ac495dSmrg c->used = node->value.macro->used;
159436ac495dSmrg memcpy (c->definition, defn, defnlen);
159536ac495dSmrg }
159636ac495dSmrg
159736ac495dSmrg pfile->pushed_macros = c;
159836ac495dSmrg }
159936ac495dSmrg
160036ac495dSmrg /* Handle #pragma pop_macro(STRING). */
160136ac495dSmrg static void
do_pragma_pop_macro(cpp_reader * pfile)160236ac495dSmrg do_pragma_pop_macro (cpp_reader *pfile)
160336ac495dSmrg {
160436ac495dSmrg char *macroname, *dest;
160536ac495dSmrg const char *limit, *src;
160636ac495dSmrg const cpp_token *txt;
160736ac495dSmrg struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
160836ac495dSmrg txt = get__Pragma_string (pfile);
160936ac495dSmrg if (!txt)
161036ac495dSmrg {
1611c0a68be4Smrg location_t src_loc = pfile->cur_token[-1].src_loc;
161236ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
161336ac495dSmrg "invalid #pragma pop_macro directive");
161436ac495dSmrg check_eol (pfile, false);
161536ac495dSmrg skip_rest_of_line (pfile);
161636ac495dSmrg return;
161736ac495dSmrg }
161836ac495dSmrg dest = macroname = (char *) alloca (txt->val.str.len + 2);
161936ac495dSmrg src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
162036ac495dSmrg limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
162136ac495dSmrg while (src < limit)
162236ac495dSmrg {
162336ac495dSmrg /* We know there is a character following the backslash. */
162436ac495dSmrg if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
162536ac495dSmrg src++;
162636ac495dSmrg *dest++ = *src++;
162736ac495dSmrg }
162836ac495dSmrg *dest = 0;
162936ac495dSmrg check_eol (pfile, false);
163036ac495dSmrg skip_rest_of_line (pfile);
163136ac495dSmrg
163236ac495dSmrg while (c != NULL)
163336ac495dSmrg {
163436ac495dSmrg if (!strcmp (c->name, macroname))
163536ac495dSmrg {
163636ac495dSmrg if (!l)
163736ac495dSmrg pfile->pushed_macros = c->next;
163836ac495dSmrg else
163936ac495dSmrg l->next = c->next;
164036ac495dSmrg cpp_pop_definition (pfile, c);
164136ac495dSmrg free (c->definition);
164236ac495dSmrg free (c->name);
164336ac495dSmrg free (c);
164436ac495dSmrg break;
164536ac495dSmrg }
164636ac495dSmrg l = c;
164736ac495dSmrg c = c->next;
164836ac495dSmrg }
164936ac495dSmrg }
165036ac495dSmrg
165136ac495dSmrg /* Handle #pragma GCC poison, to poison one or more identifiers so
165236ac495dSmrg that the lexer produces a hard error for each subsequent usage. */
165336ac495dSmrg static void
do_pragma_poison(cpp_reader * pfile)165436ac495dSmrg do_pragma_poison (cpp_reader *pfile)
165536ac495dSmrg {
165636ac495dSmrg const cpp_token *tok;
165736ac495dSmrg cpp_hashnode *hp;
165836ac495dSmrg
165936ac495dSmrg pfile->state.poisoned_ok = 1;
166036ac495dSmrg for (;;)
166136ac495dSmrg {
166236ac495dSmrg tok = _cpp_lex_token (pfile);
166336ac495dSmrg if (tok->type == CPP_EOF)
166436ac495dSmrg break;
166536ac495dSmrg if (tok->type != CPP_NAME)
166636ac495dSmrg {
166736ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
166836ac495dSmrg "invalid #pragma GCC poison directive");
166936ac495dSmrg break;
167036ac495dSmrg }
167136ac495dSmrg
167236ac495dSmrg hp = tok->val.node.node;
167336ac495dSmrg if (hp->flags & NODE_POISONED)
167436ac495dSmrg continue;
167536ac495dSmrg
1676c0a68be4Smrg if (cpp_macro_p (hp))
167736ac495dSmrg cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
167836ac495dSmrg NODE_NAME (hp));
167936ac495dSmrg _cpp_free_definition (hp);
168036ac495dSmrg hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
168136ac495dSmrg }
168236ac495dSmrg pfile->state.poisoned_ok = 0;
168336ac495dSmrg }
168436ac495dSmrg
168536ac495dSmrg /* Mark the current header as a system header. This will suppress
168636ac495dSmrg some categories of warnings (notably those from -pedantic). It is
168736ac495dSmrg intended for use in system libraries that cannot be implemented in
168836ac495dSmrg conforming C, but cannot be certain that their headers appear in a
168936ac495dSmrg system include directory. To prevent abuse, it is rejected in the
169036ac495dSmrg primary source file. */
169136ac495dSmrg static void
do_pragma_system_header(cpp_reader * pfile)169236ac495dSmrg do_pragma_system_header (cpp_reader *pfile)
169336ac495dSmrg {
169436ac495dSmrg if (cpp_in_primary_file (pfile))
169536ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
169636ac495dSmrg "#pragma system_header ignored outside include file");
169736ac495dSmrg else
169836ac495dSmrg {
169936ac495dSmrg check_eol (pfile, false);
170036ac495dSmrg skip_rest_of_line (pfile);
170136ac495dSmrg cpp_make_system_header (pfile, 1, 0);
170236ac495dSmrg }
170336ac495dSmrg }
170436ac495dSmrg
170536ac495dSmrg /* Check the modified date of the current include file against a specified
170636ac495dSmrg file. Issue a diagnostic, if the specified file is newer. We use this to
170736ac495dSmrg determine if a fixed header should be refixed. */
170836ac495dSmrg static void
do_pragma_dependency(cpp_reader * pfile)170936ac495dSmrg do_pragma_dependency (cpp_reader *pfile)
171036ac495dSmrg {
171136ac495dSmrg const char *fname;
171236ac495dSmrg int angle_brackets, ordering;
1713c0a68be4Smrg location_t location;
171436ac495dSmrg
171536ac495dSmrg fname = parse_include (pfile, &angle_brackets, NULL, &location);
171636ac495dSmrg if (!fname)
171736ac495dSmrg return;
171836ac495dSmrg
171936ac495dSmrg ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
172036ac495dSmrg if (ordering < 0)
172136ac495dSmrg cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
172236ac495dSmrg else if (ordering > 0)
172336ac495dSmrg {
172436ac495dSmrg cpp_error (pfile, CPP_DL_WARNING,
172536ac495dSmrg "current file is older than %s", fname);
172636ac495dSmrg if (cpp_get_token (pfile)->type != CPP_EOF)
172736ac495dSmrg {
172836ac495dSmrg _cpp_backup_tokens (pfile, 1);
1729c0a68be4Smrg do_diagnostic (pfile, CPP_DL_WARNING, CPP_W_NONE, 0);
173036ac495dSmrg }
173136ac495dSmrg }
173236ac495dSmrg
173336ac495dSmrg free ((void *) fname);
173436ac495dSmrg }
173536ac495dSmrg
173636ac495dSmrg /* Issue a diagnostic with the message taken from the pragma. If
173736ac495dSmrg ERROR is true, the diagnostic is a warning, otherwise, it is an
173836ac495dSmrg error. */
173936ac495dSmrg static void
do_pragma_warning_or_error(cpp_reader * pfile,bool error)174036ac495dSmrg do_pragma_warning_or_error (cpp_reader *pfile, bool error)
174136ac495dSmrg {
174236ac495dSmrg const cpp_token *tok = _cpp_lex_token (pfile);
174336ac495dSmrg cpp_string str;
174436ac495dSmrg if (tok->type != CPP_STRING
174536ac495dSmrg || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
174636ac495dSmrg CPP_STRING)
174736ac495dSmrg || str.len == 0)
174836ac495dSmrg {
174936ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
175036ac495dSmrg error ? "error" : "warning");
175136ac495dSmrg return;
175236ac495dSmrg }
175336ac495dSmrg cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
175436ac495dSmrg "%s", str.text);
175536ac495dSmrg free ((void *)str.text);
175636ac495dSmrg }
175736ac495dSmrg
175836ac495dSmrg /* Issue a warning diagnostic. */
175936ac495dSmrg static void
do_pragma_warning(cpp_reader * pfile)176036ac495dSmrg do_pragma_warning (cpp_reader *pfile)
176136ac495dSmrg {
176236ac495dSmrg do_pragma_warning_or_error (pfile, false);
176336ac495dSmrg }
176436ac495dSmrg
176536ac495dSmrg /* Issue an error diagnostic. */
176636ac495dSmrg static void
do_pragma_error(cpp_reader * pfile)176736ac495dSmrg do_pragma_error (cpp_reader *pfile)
176836ac495dSmrg {
176936ac495dSmrg do_pragma_warning_or_error (pfile, true);
177036ac495dSmrg }
177136ac495dSmrg
177236ac495dSmrg /* Get a token but skip padding. */
177336ac495dSmrg static const cpp_token *
get_token_no_padding(cpp_reader * pfile)177436ac495dSmrg get_token_no_padding (cpp_reader *pfile)
177536ac495dSmrg {
177636ac495dSmrg for (;;)
177736ac495dSmrg {
177836ac495dSmrg const cpp_token *result = cpp_get_token (pfile);
177936ac495dSmrg if (result->type != CPP_PADDING)
178036ac495dSmrg return result;
178136ac495dSmrg }
178236ac495dSmrg }
178336ac495dSmrg
178436ac495dSmrg /* Check syntax is "(string-literal)". Returns the string on success,
178536ac495dSmrg or NULL on failure. */
178636ac495dSmrg static const cpp_token *
get__Pragma_string(cpp_reader * pfile)178736ac495dSmrg get__Pragma_string (cpp_reader *pfile)
178836ac495dSmrg {
178936ac495dSmrg const cpp_token *string;
179036ac495dSmrg const cpp_token *paren;
179136ac495dSmrg
179236ac495dSmrg paren = get_token_no_padding (pfile);
179336ac495dSmrg if (paren->type == CPP_EOF)
179436ac495dSmrg _cpp_backup_tokens (pfile, 1);
179536ac495dSmrg if (paren->type != CPP_OPEN_PAREN)
179636ac495dSmrg return NULL;
179736ac495dSmrg
179836ac495dSmrg string = get_token_no_padding (pfile);
179936ac495dSmrg if (string->type == CPP_EOF)
180036ac495dSmrg _cpp_backup_tokens (pfile, 1);
180136ac495dSmrg if (string->type != CPP_STRING && string->type != CPP_WSTRING
180236ac495dSmrg && string->type != CPP_STRING32 && string->type != CPP_STRING16
180336ac495dSmrg && string->type != CPP_UTF8STRING)
180436ac495dSmrg return NULL;
180536ac495dSmrg
180636ac495dSmrg paren = get_token_no_padding (pfile);
180736ac495dSmrg if (paren->type == CPP_EOF)
180836ac495dSmrg _cpp_backup_tokens (pfile, 1);
180936ac495dSmrg if (paren->type != CPP_CLOSE_PAREN)
181036ac495dSmrg return NULL;
181136ac495dSmrg
181236ac495dSmrg return string;
181336ac495dSmrg }
181436ac495dSmrg
181536ac495dSmrg /* Destringize IN into a temporary buffer, by removing the first \ of
181636ac495dSmrg \" and \\ sequences, and process the result as a #pragma directive. */
181736ac495dSmrg static void
destringize_and_run(cpp_reader * pfile,const cpp_string * in,location_t expansion_loc)181836ac495dSmrg destringize_and_run (cpp_reader *pfile, const cpp_string *in,
1819c0a68be4Smrg location_t expansion_loc)
182036ac495dSmrg {
182136ac495dSmrg const unsigned char *src, *limit;
182236ac495dSmrg char *dest, *result;
182336ac495dSmrg cpp_context *saved_context;
182436ac495dSmrg cpp_token *saved_cur_token;
182536ac495dSmrg tokenrun *saved_cur_run;
182636ac495dSmrg cpp_token *toks;
182736ac495dSmrg int count;
182836ac495dSmrg const struct directive *save_directive;
182936ac495dSmrg
183036ac495dSmrg dest = result = (char *) alloca (in->len - 1);
183136ac495dSmrg src = in->text + 1 + (in->text[0] == 'L');
183236ac495dSmrg limit = in->text + in->len - 1;
183336ac495dSmrg while (src < limit)
183436ac495dSmrg {
183536ac495dSmrg /* We know there is a character following the backslash. */
183636ac495dSmrg if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
183736ac495dSmrg src++;
183836ac495dSmrg *dest++ = *src++;
183936ac495dSmrg }
184036ac495dSmrg *dest = '\n';
184136ac495dSmrg
184236ac495dSmrg /* Ugh; an awful kludge. We are really not set up to be lexing
184336ac495dSmrg tokens when in the middle of a macro expansion. Use a new
184436ac495dSmrg context to force cpp_get_token to lex, and so skip_rest_of_line
184536ac495dSmrg doesn't go beyond the end of the text. Also, remember the
184636ac495dSmrg current lexing position so we can return to it later.
184736ac495dSmrg
184836ac495dSmrg Something like line-at-a-time lexing should remove the need for
184936ac495dSmrg this. */
185036ac495dSmrg saved_context = pfile->context;
185136ac495dSmrg saved_cur_token = pfile->cur_token;
185236ac495dSmrg saved_cur_run = pfile->cur_run;
185336ac495dSmrg
185436ac495dSmrg pfile->context = XCNEW (cpp_context);
185536ac495dSmrg
185636ac495dSmrg /* Inline run_directive, since we need to delay the _cpp_pop_buffer
185736ac495dSmrg until we've read all of the tokens that we want. */
185836ac495dSmrg cpp_push_buffer (pfile, (const uchar *) result, dest - result,
185936ac495dSmrg /* from_stage3 */ true);
186036ac495dSmrg /* ??? Antique Disgusting Hack. What does this do? */
186136ac495dSmrg if (pfile->buffer->prev)
186236ac495dSmrg pfile->buffer->file = pfile->buffer->prev->file;
186336ac495dSmrg
186436ac495dSmrg start_directive (pfile);
186536ac495dSmrg _cpp_clean_line (pfile);
186636ac495dSmrg save_directive = pfile->directive;
186736ac495dSmrg pfile->directive = &dtable[T_PRAGMA];
186836ac495dSmrg do_pragma (pfile);
186936ac495dSmrg end_directive (pfile, 1);
187036ac495dSmrg pfile->directive = save_directive;
187136ac495dSmrg
187236ac495dSmrg /* We always insert at least one token, the directive result. It'll
187336ac495dSmrg either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
187436ac495dSmrg need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
187536ac495dSmrg
187636ac495dSmrg /* If we're not handling the pragma internally, read all of the tokens from
187736ac495dSmrg the string buffer now, while the string buffer is still installed. */
187836ac495dSmrg /* ??? Note that the token buffer allocated here is leaked. It's not clear
187936ac495dSmrg to me what the true lifespan of the tokens are. It would appear that
188036ac495dSmrg the lifespan is the entire parse of the main input stream, in which case
188136ac495dSmrg this may not be wrong. */
188236ac495dSmrg if (pfile->directive_result.type == CPP_PRAGMA)
188336ac495dSmrg {
188436ac495dSmrg int maxcount;
188536ac495dSmrg
188636ac495dSmrg count = 1;
188736ac495dSmrg maxcount = 50;
188836ac495dSmrg toks = XNEWVEC (cpp_token, maxcount);
188936ac495dSmrg toks[0] = pfile->directive_result;
1890*23f5f463Smrg toks[0].src_loc = expansion_loc;
189136ac495dSmrg
189236ac495dSmrg do
189336ac495dSmrg {
189436ac495dSmrg if (count == maxcount)
189536ac495dSmrg {
189636ac495dSmrg maxcount = maxcount * 3 / 2;
189736ac495dSmrg toks = XRESIZEVEC (cpp_token, toks, maxcount);
189836ac495dSmrg }
189936ac495dSmrg toks[count] = *cpp_get_token (pfile);
190036ac495dSmrg /* _Pragma is a builtin, so we're not within a macro-map, and so
190136ac495dSmrg the token locations are set to bogus ordinary locations
190236ac495dSmrg near to, but after that of the "_Pragma".
190336ac495dSmrg Paper over this by setting them equal to the location of the
190436ac495dSmrg _Pragma itself (PR preprocessor/69126). */
190536ac495dSmrg toks[count].src_loc = expansion_loc;
190636ac495dSmrg /* Macros have been already expanded by cpp_get_token
190736ac495dSmrg if the pragma allowed expansion. */
190836ac495dSmrg toks[count++].flags |= NO_EXPAND;
190936ac495dSmrg }
191036ac495dSmrg while (toks[count-1].type != CPP_PRAGMA_EOL);
191136ac495dSmrg }
191236ac495dSmrg else
191336ac495dSmrg {
191436ac495dSmrg count = 1;
19158feb0f0bSmrg toks = &pfile->avoid_paste;
191636ac495dSmrg
191736ac495dSmrg /* If we handled the entire pragma internally, make sure we get the
191836ac495dSmrg line number correct for the next token. */
191936ac495dSmrg if (pfile->cb.line_change)
192036ac495dSmrg pfile->cb.line_change (pfile, pfile->cur_token, false);
192136ac495dSmrg }
192236ac495dSmrg
192336ac495dSmrg /* Finish inlining run_directive. */
192436ac495dSmrg pfile->buffer->file = NULL;
192536ac495dSmrg _cpp_pop_buffer (pfile);
192636ac495dSmrg
192736ac495dSmrg /* Reset the old macro state before ... */
192836ac495dSmrg XDELETE (pfile->context);
192936ac495dSmrg pfile->context = saved_context;
193036ac495dSmrg pfile->cur_token = saved_cur_token;
193136ac495dSmrg pfile->cur_run = saved_cur_run;
193236ac495dSmrg
193336ac495dSmrg /* ... inserting the new tokens we collected. */
193436ac495dSmrg _cpp_push_token_context (pfile, NULL, toks, count);
193536ac495dSmrg }
193636ac495dSmrg
193736ac495dSmrg /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
193836ac495dSmrg int
_cpp_do__Pragma(cpp_reader * pfile,location_t expansion_loc)1939c0a68be4Smrg _cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc)
194036ac495dSmrg {
194136ac495dSmrg const cpp_token *string = get__Pragma_string (pfile);
194236ac495dSmrg pfile->directive_result.type = CPP_PADDING;
194336ac495dSmrg
194436ac495dSmrg if (string)
194536ac495dSmrg {
194636ac495dSmrg destringize_and_run (pfile, &string->val.str, expansion_loc);
194736ac495dSmrg return 1;
194836ac495dSmrg }
194936ac495dSmrg cpp_error (pfile, CPP_DL_ERROR,
195036ac495dSmrg "_Pragma takes a parenthesized string literal");
195136ac495dSmrg return 0;
195236ac495dSmrg }
195336ac495dSmrg
195436ac495dSmrg /* Handle #ifdef. */
195536ac495dSmrg static void
do_ifdef(cpp_reader * pfile)195636ac495dSmrg do_ifdef (cpp_reader *pfile)
195736ac495dSmrg {
195836ac495dSmrg int skip = 1;
195936ac495dSmrg
196036ac495dSmrg if (! pfile->state.skipping)
196136ac495dSmrg {
196236ac495dSmrg cpp_hashnode *node = lex_macro_node (pfile, false);
196336ac495dSmrg
196436ac495dSmrg if (node)
196536ac495dSmrg {
19668feb0f0bSmrg skip = !_cpp_defined_macro_p (node);
196736ac495dSmrg _cpp_mark_macro_used (node);
1968c0a68be4Smrg _cpp_maybe_notify_macro_use (pfile, node);
196936ac495dSmrg if (pfile->cb.used)
197036ac495dSmrg pfile->cb.used (pfile, pfile->directive_line, node);
197136ac495dSmrg check_eol (pfile, false);
197236ac495dSmrg }
197336ac495dSmrg }
197436ac495dSmrg
197536ac495dSmrg push_conditional (pfile, skip, T_IFDEF, 0);
197636ac495dSmrg }
197736ac495dSmrg
197836ac495dSmrg /* Handle #ifndef. */
197936ac495dSmrg static void
do_ifndef(cpp_reader * pfile)198036ac495dSmrg do_ifndef (cpp_reader *pfile)
198136ac495dSmrg {
198236ac495dSmrg int skip = 1;
198336ac495dSmrg cpp_hashnode *node = 0;
198436ac495dSmrg
198536ac495dSmrg if (! pfile->state.skipping)
198636ac495dSmrg {
198736ac495dSmrg node = lex_macro_node (pfile, false);
198836ac495dSmrg
198936ac495dSmrg if (node)
199036ac495dSmrg {
199136ac495dSmrg /* Do not treat conditional macros as being defined. This is due to
19928feb0f0bSmrg the powerpc port using conditional macros for 'vector', 'bool',
19938feb0f0bSmrg and 'pixel' to act as conditional keywords. This messes up tests
19948feb0f0bSmrg like #ifndef bool. */
19958feb0f0bSmrg skip = _cpp_defined_macro_p (node);
199636ac495dSmrg _cpp_mark_macro_used (node);
1997c0a68be4Smrg _cpp_maybe_notify_macro_use (pfile, node);
199836ac495dSmrg if (pfile->cb.used)
199936ac495dSmrg pfile->cb.used (pfile, pfile->directive_line, node);
200036ac495dSmrg check_eol (pfile, false);
200136ac495dSmrg }
200236ac495dSmrg }
200336ac495dSmrg
200436ac495dSmrg push_conditional (pfile, skip, T_IFNDEF, node);
200536ac495dSmrg }
200636ac495dSmrg
200736ac495dSmrg /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
200836ac495dSmrg pfile->mi_ind_cmacro so we can handle multiple-include
200936ac495dSmrg optimizations. If macro expansion occurs in the expression, we
201036ac495dSmrg cannot treat it as a controlling conditional, since the expansion
201136ac495dSmrg could change in the future. That is handled by cpp_get_token. */
201236ac495dSmrg static void
do_if(cpp_reader * pfile)201336ac495dSmrg do_if (cpp_reader *pfile)
201436ac495dSmrg {
201536ac495dSmrg int skip = 1;
201636ac495dSmrg
201736ac495dSmrg if (! pfile->state.skipping)
201836ac495dSmrg skip = _cpp_parse_expr (pfile, true) == false;
201936ac495dSmrg
202036ac495dSmrg push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
202136ac495dSmrg }
202236ac495dSmrg
202336ac495dSmrg /* Flip skipping state if appropriate and continue without changing
202436ac495dSmrg if_stack; this is so that the error message for missing #endif's
202536ac495dSmrg etc. will point to the original #if. */
202636ac495dSmrg static void
do_else(cpp_reader * pfile)202736ac495dSmrg do_else (cpp_reader *pfile)
202836ac495dSmrg {
202936ac495dSmrg cpp_buffer *buffer = pfile->buffer;
203036ac495dSmrg struct if_stack *ifs = buffer->if_stack;
203136ac495dSmrg
203236ac495dSmrg if (ifs == NULL)
203336ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
203436ac495dSmrg else
203536ac495dSmrg {
203636ac495dSmrg if (ifs->type == T_ELSE)
203736ac495dSmrg {
203836ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
203936ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
204036ac495dSmrg "the conditional began here");
204136ac495dSmrg }
204236ac495dSmrg ifs->type = T_ELSE;
204336ac495dSmrg
204436ac495dSmrg /* Skip any future (erroneous) #elses or #elifs. */
204536ac495dSmrg pfile->state.skipping = ifs->skip_elses;
204636ac495dSmrg ifs->skip_elses = true;
204736ac495dSmrg
204836ac495dSmrg /* Invalidate any controlling macro. */
204936ac495dSmrg ifs->mi_cmacro = 0;
205036ac495dSmrg
205136ac495dSmrg /* Only check EOL if was not originally skipping. */
205236ac495dSmrg if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
205336ac495dSmrg check_eol_endif_labels (pfile);
205436ac495dSmrg }
205536ac495dSmrg }
205636ac495dSmrg
205736ac495dSmrg /* Handle a #elif directive by not changing if_stack either. See the
205836ac495dSmrg comment above do_else. */
205936ac495dSmrg static void
do_elif(cpp_reader * pfile)206036ac495dSmrg do_elif (cpp_reader *pfile)
206136ac495dSmrg {
206236ac495dSmrg cpp_buffer *buffer = pfile->buffer;
206336ac495dSmrg struct if_stack *ifs = buffer->if_stack;
206436ac495dSmrg
206536ac495dSmrg if (ifs == NULL)
206636ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
206736ac495dSmrg else
206836ac495dSmrg {
206936ac495dSmrg if (ifs->type == T_ELSE)
207036ac495dSmrg {
207136ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
207236ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
207336ac495dSmrg "the conditional began here");
207436ac495dSmrg }
207536ac495dSmrg ifs->type = T_ELIF;
207636ac495dSmrg
207736ac495dSmrg /* See DR#412: "Only the first group whose control condition
207836ac495dSmrg evaluates to true (nonzero) is processed; any following groups
207936ac495dSmrg are skipped and their controlling directives are processed as
208036ac495dSmrg if they were in a group that is skipped." */
208136ac495dSmrg if (ifs->skip_elses)
208236ac495dSmrg pfile->state.skipping = 1;
208336ac495dSmrg else
208436ac495dSmrg {
208536ac495dSmrg pfile->state.skipping = ! _cpp_parse_expr (pfile, false);
208636ac495dSmrg ifs->skip_elses = ! pfile->state.skipping;
208736ac495dSmrg }
208836ac495dSmrg
208936ac495dSmrg /* Invalidate any controlling macro. */
209036ac495dSmrg ifs->mi_cmacro = 0;
209136ac495dSmrg }
209236ac495dSmrg }
209336ac495dSmrg
209436ac495dSmrg /* #endif pops the if stack and resets pfile->state.skipping. */
209536ac495dSmrg static void
do_endif(cpp_reader * pfile)209636ac495dSmrg do_endif (cpp_reader *pfile)
209736ac495dSmrg {
209836ac495dSmrg cpp_buffer *buffer = pfile->buffer;
209936ac495dSmrg struct if_stack *ifs = buffer->if_stack;
210036ac495dSmrg
210136ac495dSmrg if (ifs == NULL)
210236ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
210336ac495dSmrg else
210436ac495dSmrg {
210536ac495dSmrg /* Only check EOL if was not originally skipping. */
210636ac495dSmrg if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
210736ac495dSmrg check_eol_endif_labels (pfile);
210836ac495dSmrg
210936ac495dSmrg /* If potential control macro, we go back outside again. */
211036ac495dSmrg if (ifs->next == 0 && ifs->mi_cmacro)
211136ac495dSmrg {
211236ac495dSmrg pfile->mi_valid = true;
211336ac495dSmrg pfile->mi_cmacro = ifs->mi_cmacro;
211436ac495dSmrg }
211536ac495dSmrg
211636ac495dSmrg buffer->if_stack = ifs->next;
211736ac495dSmrg pfile->state.skipping = ifs->was_skipping;
211836ac495dSmrg obstack_free (&pfile->buffer_ob, ifs);
211936ac495dSmrg }
212036ac495dSmrg }
212136ac495dSmrg
212236ac495dSmrg /* Push an if_stack entry for a preprocessor conditional, and set
212336ac495dSmrg pfile->state.skipping to SKIP. If TYPE indicates the conditional
212436ac495dSmrg is #if or #ifndef, CMACRO is a potentially controlling macro, and
212536ac495dSmrg we need to check here that we are at the top of the file. */
212636ac495dSmrg static void
push_conditional(cpp_reader * pfile,int skip,int type,const cpp_hashnode * cmacro)212736ac495dSmrg push_conditional (cpp_reader *pfile, int skip, int type,
212836ac495dSmrg const cpp_hashnode *cmacro)
212936ac495dSmrg {
213036ac495dSmrg struct if_stack *ifs;
213136ac495dSmrg cpp_buffer *buffer = pfile->buffer;
213236ac495dSmrg
213336ac495dSmrg ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
213436ac495dSmrg ifs->line = pfile->directive_line;
213536ac495dSmrg ifs->next = buffer->if_stack;
213636ac495dSmrg ifs->skip_elses = pfile->state.skipping || !skip;
213736ac495dSmrg ifs->was_skipping = pfile->state.skipping;
213836ac495dSmrg ifs->type = type;
213936ac495dSmrg /* This condition is effectively a test for top-of-file. */
214036ac495dSmrg if (pfile->mi_valid && pfile->mi_cmacro == 0)
214136ac495dSmrg ifs->mi_cmacro = cmacro;
214236ac495dSmrg else
214336ac495dSmrg ifs->mi_cmacro = 0;
214436ac495dSmrg
214536ac495dSmrg pfile->state.skipping = skip;
214636ac495dSmrg buffer->if_stack = ifs;
214736ac495dSmrg }
214836ac495dSmrg
214936ac495dSmrg /* Read the tokens of the answer into the macro pool, in a directive
215036ac495dSmrg of type TYPE. Only commit the memory if we intend it as permanent
215136ac495dSmrg storage, i.e. the #assert case. Returns 0 on success, and sets
215236ac495dSmrg ANSWERP to point to the answer. PRED_LOC is the location of the
215336ac495dSmrg predicate. */
2154c0a68be4Smrg static bool
parse_answer(cpp_reader * pfile,int type,location_t pred_loc,cpp_macro ** answer_ptr)2155c0a68be4Smrg parse_answer (cpp_reader *pfile, int type, location_t pred_loc,
2156c0a68be4Smrg cpp_macro **answer_ptr)
215736ac495dSmrg {
215836ac495dSmrg /* In a conditional, it is legal to not have an open paren. We
215936ac495dSmrg should save the following token in this case. */
2160c0a68be4Smrg const cpp_token *paren = cpp_get_token (pfile);
216136ac495dSmrg
216236ac495dSmrg /* If not a paren, see if we're OK. */
216336ac495dSmrg if (paren->type != CPP_OPEN_PAREN)
216436ac495dSmrg {
216536ac495dSmrg /* In a conditional no answer is a test for any answer. It
216636ac495dSmrg could be followed by any token. */
216736ac495dSmrg if (type == T_IF)
216836ac495dSmrg {
216936ac495dSmrg _cpp_backup_tokens (pfile, 1);
2170c0a68be4Smrg return true;
217136ac495dSmrg }
217236ac495dSmrg
217336ac495dSmrg /* #unassert with no answer is valid - it removes all answers. */
217436ac495dSmrg if (type == T_UNASSERT && paren->type == CPP_EOF)
2175c0a68be4Smrg return true;
217636ac495dSmrg
217736ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
217836ac495dSmrg "missing '(' after predicate");
2179c0a68be4Smrg return false;
218036ac495dSmrg }
218136ac495dSmrg
2182c0a68be4Smrg cpp_macro *answer = _cpp_new_macro (pfile, cmk_assert,
2183c0a68be4Smrg _cpp_reserve_room (pfile, 0,
2184c0a68be4Smrg sizeof (cpp_macro)));
2185c0a68be4Smrg answer->parm.next = NULL;
2186c0a68be4Smrg unsigned count = 0;
2187c0a68be4Smrg for (;;)
218836ac495dSmrg {
218936ac495dSmrg const cpp_token *token = cpp_get_token (pfile);
219036ac495dSmrg
219136ac495dSmrg if (token->type == CPP_CLOSE_PAREN)
219236ac495dSmrg break;
219336ac495dSmrg
219436ac495dSmrg if (token->type == CPP_EOF)
219536ac495dSmrg {
219636ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2197c0a68be4Smrg return false;
219836ac495dSmrg }
219936ac495dSmrg
2200c0a68be4Smrg answer = (cpp_macro *)_cpp_reserve_room
2201c0a68be4Smrg (pfile, sizeof (cpp_macro) + count * sizeof (cpp_token),
2202c0a68be4Smrg sizeof (cpp_token));
2203c0a68be4Smrg answer->exp.tokens[count++] = *token;
220436ac495dSmrg }
220536ac495dSmrg
2206c0a68be4Smrg if (!count)
220736ac495dSmrg {
220836ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2209c0a68be4Smrg return false;
221036ac495dSmrg }
221136ac495dSmrg
2212c0a68be4Smrg /* Drop whitespace at start, for answer equivalence purposes. */
2213c0a68be4Smrg answer->exp.tokens[0].flags &= ~PREV_WHITE;
221436ac495dSmrg
2215c0a68be4Smrg answer->count = count;
2216c0a68be4Smrg *answer_ptr = answer;
2217c0a68be4Smrg
2218c0a68be4Smrg return true;
221936ac495dSmrg }
222036ac495dSmrg
222136ac495dSmrg /* Parses an assertion directive of type TYPE, returning a pointer to
2222c0a68be4Smrg the hash node of the predicate, or 0 on error. The node is
2223c0a68be4Smrg guaranteed to be disjoint from the macro namespace, so can only
2224c0a68be4Smrg have type 'NT_VOID'. If an answer was supplied, it is placed in
2225c0a68be4Smrg *ANSWER_PTR, which is otherwise set to 0. */
222636ac495dSmrg static cpp_hashnode *
parse_assertion(cpp_reader * pfile,int type,cpp_macro ** answer_ptr)2227c0a68be4Smrg parse_assertion (cpp_reader *pfile, int type, cpp_macro **answer_ptr)
222836ac495dSmrg {
222936ac495dSmrg cpp_hashnode *result = 0;
223036ac495dSmrg
223136ac495dSmrg /* We don't expand predicates or answers. */
223236ac495dSmrg pfile->state.prevent_expansion++;
223336ac495dSmrg
2234c0a68be4Smrg *answer_ptr = NULL;
2235c0a68be4Smrg
2236c0a68be4Smrg const cpp_token *predicate = cpp_get_token (pfile);
223736ac495dSmrg if (predicate->type == CPP_EOF)
223836ac495dSmrg cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
223936ac495dSmrg else if (predicate->type != CPP_NAME)
224036ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
224136ac495dSmrg "predicate must be an identifier");
2242c0a68be4Smrg else if (parse_answer (pfile, type, predicate->src_loc, answer_ptr))
224336ac495dSmrg {
224436ac495dSmrg unsigned int len = NODE_LEN (predicate->val.node.node);
224536ac495dSmrg unsigned char *sym = (unsigned char *) alloca (len + 1);
224636ac495dSmrg
224736ac495dSmrg /* Prefix '#' to get it out of macro namespace. */
224836ac495dSmrg sym[0] = '#';
224936ac495dSmrg memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
225036ac495dSmrg result = cpp_lookup (pfile, sym, len + 1);
225136ac495dSmrg }
225236ac495dSmrg
225336ac495dSmrg pfile->state.prevent_expansion--;
2254c0a68be4Smrg
225536ac495dSmrg return result;
225636ac495dSmrg }
225736ac495dSmrg
225836ac495dSmrg /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
225936ac495dSmrg or a pointer to NULL if the answer is not in the chain. */
2260c0a68be4Smrg static cpp_macro **
find_answer(cpp_hashnode * node,const cpp_macro * candidate)2261c0a68be4Smrg find_answer (cpp_hashnode *node, const cpp_macro *candidate)
226236ac495dSmrg {
226336ac495dSmrg unsigned int i;
2264c0a68be4Smrg cpp_macro **result = NULL;
226536ac495dSmrg
2266c0a68be4Smrg for (result = &node->value.answers; *result; result = &(*result)->parm.next)
226736ac495dSmrg {
2268c0a68be4Smrg cpp_macro *answer = *result;
226936ac495dSmrg
227036ac495dSmrg if (answer->count == candidate->count)
227136ac495dSmrg {
227236ac495dSmrg for (i = 0; i < answer->count; i++)
2273c0a68be4Smrg if (!_cpp_equiv_tokens (&answer->exp.tokens[i],
2274c0a68be4Smrg &candidate->exp.tokens[i]))
227536ac495dSmrg break;
227636ac495dSmrg
227736ac495dSmrg if (i == answer->count)
227836ac495dSmrg break;
227936ac495dSmrg }
228036ac495dSmrg }
228136ac495dSmrg
228236ac495dSmrg return result;
228336ac495dSmrg }
228436ac495dSmrg
228536ac495dSmrg /* Test an assertion within a preprocessor conditional. Returns
228636ac495dSmrg nonzero on failure, zero on success. On success, the result of
228736ac495dSmrg the test is written into VALUE, otherwise the value 0. */
228836ac495dSmrg int
_cpp_test_assertion(cpp_reader * pfile,unsigned int * value)228936ac495dSmrg _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
229036ac495dSmrg {
2291c0a68be4Smrg cpp_macro *answer;
2292c0a68be4Smrg cpp_hashnode *node = parse_assertion (pfile, T_IF, &answer);
229336ac495dSmrg
229436ac495dSmrg /* For recovery, an erroneous assertion expression is handled as a
229536ac495dSmrg failing assertion. */
229636ac495dSmrg *value = 0;
229736ac495dSmrg
229836ac495dSmrg if (node)
2299c0a68be4Smrg {
2300c0a68be4Smrg if (node->value.answers)
2301c0a68be4Smrg *value = !answer || *find_answer (node, answer);
2302c0a68be4Smrg }
230336ac495dSmrg else if (pfile->cur_token[-1].type == CPP_EOF)
230436ac495dSmrg _cpp_backup_tokens (pfile, 1);
230536ac495dSmrg
230636ac495dSmrg /* We don't commit the memory for the answer - it's temporary only. */
230736ac495dSmrg return node == 0;
230836ac495dSmrg }
230936ac495dSmrg
231036ac495dSmrg /* Handle #assert. */
231136ac495dSmrg static void
do_assert(cpp_reader * pfile)231236ac495dSmrg do_assert (cpp_reader *pfile)
231336ac495dSmrg {
2314c0a68be4Smrg cpp_macro *answer;
2315c0a68be4Smrg cpp_hashnode *node = parse_assertion (pfile, T_ASSERT, &answer);
231636ac495dSmrg
231736ac495dSmrg if (node)
231836ac495dSmrg {
231936ac495dSmrg /* Place the new answer in the answer list. First check there
232036ac495dSmrg is not a duplicate. */
2321c0a68be4Smrg if (*find_answer (node, answer))
232236ac495dSmrg {
232336ac495dSmrg cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
232436ac495dSmrg NODE_NAME (node) + 1);
232536ac495dSmrg return;
232636ac495dSmrg }
232736ac495dSmrg
2328c0a68be4Smrg /* Commit or allocate storage for the answer. */
2329c0a68be4Smrg answer = (cpp_macro *)_cpp_commit_buff
2330c0a68be4Smrg (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
2331c0a68be4Smrg + sizeof (cpp_token) * answer->count);
233236ac495dSmrg
2333c0a68be4Smrg /* Chain into the list. */
2334c0a68be4Smrg answer->parm.next = node->value.answers;
2335c0a68be4Smrg node->value.answers = answer;
2336c0a68be4Smrg
233736ac495dSmrg check_eol (pfile, false);
233836ac495dSmrg }
233936ac495dSmrg }
234036ac495dSmrg
234136ac495dSmrg /* Handle #unassert. */
234236ac495dSmrg static void
do_unassert(cpp_reader * pfile)234336ac495dSmrg do_unassert (cpp_reader *pfile)
234436ac495dSmrg {
2345c0a68be4Smrg cpp_macro *answer;
2346c0a68be4Smrg cpp_hashnode *node = parse_assertion (pfile, T_UNASSERT, &answer);
234736ac495dSmrg
234836ac495dSmrg /* It isn't an error to #unassert something that isn't asserted. */
2349c0a68be4Smrg if (node)
235036ac495dSmrg {
235136ac495dSmrg if (answer)
235236ac495dSmrg {
2353c0a68be4Smrg cpp_macro **p = find_answer (node, answer);
235436ac495dSmrg
2355c0a68be4Smrg /* Remove the assert from the list. */
2356c0a68be4Smrg if (cpp_macro *temp = *p)
2357c0a68be4Smrg *p = temp->parm.next;
235836ac495dSmrg
235936ac495dSmrg check_eol (pfile, false);
236036ac495dSmrg }
236136ac495dSmrg else
236236ac495dSmrg _cpp_free_definition (node);
236336ac495dSmrg }
236436ac495dSmrg
236536ac495dSmrg /* We don't commit the memory for the answer - it's temporary only. */
236636ac495dSmrg }
236736ac495dSmrg
236836ac495dSmrg /* These are for -D, -U, -A. */
236936ac495dSmrg
237036ac495dSmrg /* Process the string STR as if it appeared as the body of a #define.
237136ac495dSmrg If STR is just an identifier, define it with value 1.
237236ac495dSmrg If STR has anything after the identifier, then it should
237336ac495dSmrg be identifier=definition. */
237436ac495dSmrg void
cpp_define(cpp_reader * pfile,const char * str)237536ac495dSmrg cpp_define (cpp_reader *pfile, const char *str)
237636ac495dSmrg {
237736ac495dSmrg char *buf;
237836ac495dSmrg const char *p;
237936ac495dSmrg size_t count;
238036ac495dSmrg
238136ac495dSmrg /* Copy the entire option so we can modify it.
238236ac495dSmrg Change the first "=" in the string to a space. If there is none,
238336ac495dSmrg tack " 1" on the end. */
238436ac495dSmrg
238536ac495dSmrg count = strlen (str);
238636ac495dSmrg buf = (char *) alloca (count + 3);
238736ac495dSmrg memcpy (buf, str, count);
238836ac495dSmrg
238936ac495dSmrg p = strchr (str, '=');
239036ac495dSmrg if (p)
239136ac495dSmrg buf[p - str] = ' ';
239236ac495dSmrg else
239336ac495dSmrg {
239436ac495dSmrg buf[count++] = ' ';
239536ac495dSmrg buf[count++] = '1';
239636ac495dSmrg }
239736ac495dSmrg buf[count] = '\n';
239836ac495dSmrg
239936ac495dSmrg run_directive (pfile, T_DEFINE, buf, count);
240036ac495dSmrg }
240136ac495dSmrg
240236ac495dSmrg
240336ac495dSmrg /* Use to build macros to be run through cpp_define() as
240436ac495dSmrg described above.
240536ac495dSmrg Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
240636ac495dSmrg
240736ac495dSmrg void
cpp_define_formatted(cpp_reader * pfile,const char * fmt,...)240836ac495dSmrg cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
240936ac495dSmrg {
241036ac495dSmrg char *ptr;
241136ac495dSmrg
241236ac495dSmrg va_list ap;
241336ac495dSmrg va_start (ap, fmt);
241436ac495dSmrg ptr = xvasprintf (fmt, ap);
241536ac495dSmrg va_end (ap);
241636ac495dSmrg
241736ac495dSmrg cpp_define (pfile, ptr);
241836ac495dSmrg free (ptr);
241936ac495dSmrg }
242036ac495dSmrg
242136ac495dSmrg
242236ac495dSmrg /* Slight variant of the above for use by initialize_builtins. */
242336ac495dSmrg void
_cpp_define_builtin(cpp_reader * pfile,const char * str)242436ac495dSmrg _cpp_define_builtin (cpp_reader *pfile, const char *str)
242536ac495dSmrg {
242636ac495dSmrg size_t len = strlen (str);
242736ac495dSmrg char *buf = (char *) alloca (len + 1);
242836ac495dSmrg memcpy (buf, str, len);
242936ac495dSmrg buf[len] = '\n';
243036ac495dSmrg run_directive (pfile, T_DEFINE, buf, len);
243136ac495dSmrg }
243236ac495dSmrg
243336ac495dSmrg /* Process MACRO as if it appeared as the body of an #undef. */
243436ac495dSmrg void
cpp_undef(cpp_reader * pfile,const char * macro)243536ac495dSmrg cpp_undef (cpp_reader *pfile, const char *macro)
243636ac495dSmrg {
243736ac495dSmrg size_t len = strlen (macro);
243836ac495dSmrg char *buf = (char *) alloca (len + 1);
243936ac495dSmrg memcpy (buf, macro, len);
244036ac495dSmrg buf[len] = '\n';
244136ac495dSmrg run_directive (pfile, T_UNDEF, buf, len);
244236ac495dSmrg }
244336ac495dSmrg
244436ac495dSmrg /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
244536ac495dSmrg or first element is zero, then the macro should be undefined. */
244636ac495dSmrg static void
cpp_pop_definition(cpp_reader * pfile,struct def_pragma_macro * c)244736ac495dSmrg cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
244836ac495dSmrg {
244936ac495dSmrg cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
245036ac495dSmrg if (node == NULL)
245136ac495dSmrg return;
2452c0a68be4Smrg
2453c0a68be4Smrg if (pfile->cb.before_define)
2454c0a68be4Smrg pfile->cb.before_define (pfile);
2455c0a68be4Smrg
2456c0a68be4Smrg if (cpp_macro_p (node))
2457c0a68be4Smrg {
2458c0a68be4Smrg if (pfile->cb.undef)
2459c0a68be4Smrg pfile->cb.undef (pfile, pfile->directive_line, node);
2460c0a68be4Smrg if (CPP_OPTION (pfile, warn_unused_macros))
2461c0a68be4Smrg _cpp_warn_if_unused_macro (pfile, node, NULL);
2462c0a68be4Smrg _cpp_free_definition (node);
2463c0a68be4Smrg }
2464c0a68be4Smrg
2465c0a68be4Smrg if (c->is_undef)
2466c0a68be4Smrg return;
2467a2dc1f3fSmrg if (c->is_builtin)
2468a2dc1f3fSmrg {
2469a2dc1f3fSmrg _cpp_restore_special_builtin (pfile, c);
2470a2dc1f3fSmrg return;
2471a2dc1f3fSmrg }
247236ac495dSmrg
247336ac495dSmrg {
247436ac495dSmrg size_t namelen;
247536ac495dSmrg const uchar *dn;
247636ac495dSmrg cpp_hashnode *h = NULL;
247736ac495dSmrg cpp_buffer *nbuf;
247836ac495dSmrg
247936ac495dSmrg namelen = ustrcspn (c->definition, "( \n");
248036ac495dSmrg h = cpp_lookup (pfile, c->definition, namelen);
248136ac495dSmrg dn = c->definition + namelen;
248236ac495dSmrg
248336ac495dSmrg nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
248436ac495dSmrg if (nbuf != NULL)
248536ac495dSmrg {
248636ac495dSmrg _cpp_clean_line (pfile);
248736ac495dSmrg nbuf->sysp = 1;
248836ac495dSmrg if (!_cpp_create_definition (pfile, h))
248936ac495dSmrg abort ();
249036ac495dSmrg _cpp_pop_buffer (pfile);
249136ac495dSmrg }
249236ac495dSmrg else
249336ac495dSmrg abort ();
249436ac495dSmrg h->value.macro->line = c->line;
249536ac495dSmrg h->value.macro->syshdr = c->syshdr;
249636ac495dSmrg h->value.macro->used = c->used;
249736ac495dSmrg }
249836ac495dSmrg }
249936ac495dSmrg
250036ac495dSmrg /* Process the string STR as if it appeared as the body of a #assert. */
250136ac495dSmrg void
cpp_assert(cpp_reader * pfile,const char * str)250236ac495dSmrg cpp_assert (cpp_reader *pfile, const char *str)
250336ac495dSmrg {
250436ac495dSmrg handle_assertion (pfile, str, T_ASSERT);
250536ac495dSmrg }
250636ac495dSmrg
250736ac495dSmrg /* Process STR as if it appeared as the body of an #unassert. */
250836ac495dSmrg void
cpp_unassert(cpp_reader * pfile,const char * str)250936ac495dSmrg cpp_unassert (cpp_reader *pfile, const char *str)
251036ac495dSmrg {
251136ac495dSmrg handle_assertion (pfile, str, T_UNASSERT);
251236ac495dSmrg }
251336ac495dSmrg
251436ac495dSmrg /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
251536ac495dSmrg static void
handle_assertion(cpp_reader * pfile,const char * str,int type)251636ac495dSmrg handle_assertion (cpp_reader *pfile, const char *str, int type)
251736ac495dSmrg {
251836ac495dSmrg size_t count = strlen (str);
251936ac495dSmrg const char *p = strchr (str, '=');
252036ac495dSmrg
252136ac495dSmrg /* Copy the entire option so we can modify it. Change the first
252236ac495dSmrg "=" in the string to a '(', and tack a ')' on the end. */
252336ac495dSmrg char *buf = (char *) alloca (count + 2);
252436ac495dSmrg
252536ac495dSmrg memcpy (buf, str, count);
252636ac495dSmrg if (p)
252736ac495dSmrg {
252836ac495dSmrg buf[p - str] = '(';
252936ac495dSmrg buf[count++] = ')';
253036ac495dSmrg }
253136ac495dSmrg buf[count] = '\n';
253236ac495dSmrg str = buf;
253336ac495dSmrg
253436ac495dSmrg run_directive (pfile, type, str, count);
253536ac495dSmrg }
253636ac495dSmrg
253736ac495dSmrg /* The options structure. */
253836ac495dSmrg cpp_options *
cpp_get_options(cpp_reader * pfile)253936ac495dSmrg cpp_get_options (cpp_reader *pfile)
254036ac495dSmrg {
254136ac495dSmrg return &pfile->opts;
254236ac495dSmrg }
254336ac495dSmrg
254436ac495dSmrg /* The callbacks structure. */
254536ac495dSmrg cpp_callbacks *
cpp_get_callbacks(cpp_reader * pfile)254636ac495dSmrg cpp_get_callbacks (cpp_reader *pfile)
254736ac495dSmrg {
254836ac495dSmrg return &pfile->cb;
254936ac495dSmrg }
255036ac495dSmrg
255136ac495dSmrg /* Copy the given callbacks structure to our own. */
255236ac495dSmrg void
cpp_set_callbacks(cpp_reader * pfile,cpp_callbacks * cb)255336ac495dSmrg cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
255436ac495dSmrg {
255536ac495dSmrg pfile->cb = *cb;
255636ac495dSmrg }
255736ac495dSmrg
255836ac495dSmrg /* The dependencies structure. (Creates one if it hasn't already been.) */
25598feb0f0bSmrg class mkdeps *
cpp_get_deps(cpp_reader * pfile)256036ac495dSmrg cpp_get_deps (cpp_reader *pfile)
256136ac495dSmrg {
256236ac495dSmrg if (!pfile->deps)
256336ac495dSmrg pfile->deps = deps_init ();
256436ac495dSmrg return pfile->deps;
256536ac495dSmrg }
256636ac495dSmrg
256736ac495dSmrg /* Push a new buffer on the buffer stack. Returns the new buffer; it
256836ac495dSmrg doesn't fail. It does not generate a file change call back; that
256936ac495dSmrg is the responsibility of the caller. */
257036ac495dSmrg cpp_buffer *
cpp_push_buffer(cpp_reader * pfile,const uchar * buffer,size_t len,int from_stage3)257136ac495dSmrg cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
257236ac495dSmrg int from_stage3)
257336ac495dSmrg {
257436ac495dSmrg cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
257536ac495dSmrg
257636ac495dSmrg /* Clears, amongst other things, if_stack and mi_cmacro. */
257736ac495dSmrg memset (new_buffer, 0, sizeof (cpp_buffer));
257836ac495dSmrg
257936ac495dSmrg new_buffer->next_line = new_buffer->buf = buffer;
258036ac495dSmrg new_buffer->rlimit = buffer + len;
258136ac495dSmrg new_buffer->from_stage3 = from_stage3;
258236ac495dSmrg new_buffer->prev = pfile->buffer;
258336ac495dSmrg new_buffer->need_line = true;
258436ac495dSmrg
258536ac495dSmrg pfile->buffer = new_buffer;
258636ac495dSmrg
258736ac495dSmrg return new_buffer;
258836ac495dSmrg }
258936ac495dSmrg
259036ac495dSmrg /* Pops a single buffer, with a file change call-back if appropriate.
259136ac495dSmrg Then pushes the next -include file, if any remain. */
259236ac495dSmrg void
_cpp_pop_buffer(cpp_reader * pfile)259336ac495dSmrg _cpp_pop_buffer (cpp_reader *pfile)
259436ac495dSmrg {
259536ac495dSmrg cpp_buffer *buffer = pfile->buffer;
259636ac495dSmrg struct _cpp_file *inc = buffer->file;
259736ac495dSmrg struct if_stack *ifs;
259836ac495dSmrg const unsigned char *to_free;
259936ac495dSmrg
260036ac495dSmrg /* Walk back up the conditional stack till we reach its level at
260136ac495dSmrg entry to this file, issuing error messages. */
260236ac495dSmrg for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
260336ac495dSmrg cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
260436ac495dSmrg "unterminated #%s", dtable[ifs->type].name);
260536ac495dSmrg
260636ac495dSmrg /* In case of a missing #endif. */
260736ac495dSmrg pfile->state.skipping = 0;
260836ac495dSmrg
260936ac495dSmrg /* _cpp_do_file_change expects pfile->buffer to be the new one. */
261036ac495dSmrg pfile->buffer = buffer->prev;
261136ac495dSmrg
261236ac495dSmrg to_free = buffer->to_free;
261336ac495dSmrg free (buffer->notes);
261436ac495dSmrg
261536ac495dSmrg /* Free the buffer object now; we may want to push a new buffer
261636ac495dSmrg in _cpp_push_next_include_file. */
261736ac495dSmrg obstack_free (&pfile->buffer_ob, buffer);
261836ac495dSmrg
261936ac495dSmrg if (inc)
262036ac495dSmrg {
262136ac495dSmrg _cpp_pop_file_buffer (pfile, inc, to_free);
262236ac495dSmrg
262336ac495dSmrg _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
262436ac495dSmrg }
2625c0a68be4Smrg else if (to_free)
2626c0a68be4Smrg free ((void *)to_free);
262736ac495dSmrg }
262836ac495dSmrg
262936ac495dSmrg /* Enter all recognized directives in the hash table. */
263036ac495dSmrg void
_cpp_init_directives(cpp_reader * pfile)263136ac495dSmrg _cpp_init_directives (cpp_reader *pfile)
263236ac495dSmrg {
26338feb0f0bSmrg for (int i = 0; i < N_DIRECTIVES; i++)
263436ac495dSmrg {
26358feb0f0bSmrg cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
263636ac495dSmrg node->is_directive = 1;
263736ac495dSmrg node->directive_index = i;
263836ac495dSmrg }
263936ac495dSmrg }
264036ac495dSmrg
264136ac495dSmrg /* Extract header file from a bracket include. Parsing starts after '<'.
264236ac495dSmrg The string is malloced and must be freed by the caller. */
264336ac495dSmrg char *
_cpp_bracket_include(cpp_reader * pfile)264436ac495dSmrg _cpp_bracket_include(cpp_reader *pfile)
264536ac495dSmrg {
264636ac495dSmrg return glue_header_name (pfile);
264736ac495dSmrg }
264836ac495dSmrg
2649