static void skip_rest_of_line PARAMS ((cpp_reader *));
static void check_eol PARAMS ((cpp_reader *));
+static void start_directive PARAMS ((cpp_reader *));
+static void end_directive PARAMS ((cpp_reader *, int));
static void run_directive PARAMS ((cpp_reader *, int,
- const char *, size_t,
- const char *));
+ enum cpp_buffer_type,
+ const char *, size_t));
static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
static int parse_include PARAMS ((cpp_reader *, cpp_token *));
static void push_conditional PARAMS ((cpp_reader *, int, int,
const cpp_hashnode *));
-static int read_line_number PARAMS ((cpp_reader *, int *));
+static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
unsigned long *));
-static void do_diagnostic PARAMS ((cpp_reader *, enum error_type));
+static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
static void do_pragma_once PARAMS ((cpp_reader *));
static void do_pragma_poison PARAMS ((cpp_reader *));
{
cpp_token token;
- /* Discard all lookaheads. */
+ /* Discard all input lookaheads. */
while (pfile->la_read)
_cpp_release_lookahead (pfile);
}
}
+/* Called when entering a directive, _Pragma or command-line directive. */
+static void
+start_directive (pfile)
+ cpp_reader *pfile;
+{
+ cpp_buffer *buffer = pfile->buffer;
+
+ /* Setup in-directive state. */
+ pfile->state.in_directive = 1;
+ pfile->state.save_comments = 0;
+
+ /* Some handlers need the position of the # for diagnostics. */
+ pfile->directive_pos = pfile->lexer_pos;
+
+ /* Don't save directive tokens for external clients. */
+ pfile->la_saved = pfile->la_write;
+ pfile->la_write = 0;
+
+ /* Turn off skipping. */
+ buffer->was_skipping = pfile->skipping;
+ pfile->skipping = 0;
+}
+
+/* Called when leaving a directive, _Pragma or command-line directive. */
+static void
+end_directive (pfile, skip_line)
+ cpp_reader *pfile;
+ int skip_line;
+{
+ cpp_buffer *buffer = pfile->buffer;
+
+ /* Restore pfile->skipping before skip_rest_of_line, so that e.g.
+ __VA_ARGS__ in the rest of the directive doesn't warn. */
+ pfile->skipping = buffer->was_skipping;
+
+ /* We don't skip for an assembler #. */
+ if (skip_line)
+ skip_rest_of_line (pfile);
+
+ /* Restore state. */
+ pfile->la_write = pfile->la_saved;
+ pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
+ pfile->state.in_directive = 0;
+ pfile->state.angled_headers = 0;
+ pfile->state.line_extension = 0;
+ pfile->directive = 0;
+}
+
/* Check if a token's name matches that of a known directive. Put in
this file to save exporting dtable and other unneeded information. */
int
cpp_buffer *buffer = pfile->buffer;
const directive *dir = 0;
cpp_token dname;
- int not_asm = 1;
+ int skip = 1;
- /* Some handlers need the position of the # for diagnostics. */
- pfile->directive_pos = pfile->lexer_pos;
-
- /* We're now in a directive. This ensures we get pedantic warnings
- about /v and /f in whitespace. */
- pfile->state.in_directive = 1;
- pfile->state.save_comments = 0;
+ start_directive (pfile);
/* Lex the directive name directly. */
_cpp_lex_token (pfile, &dname);
skipped conditional groups. Complain about this form if
we're being pedantic, but not if this is regurgitated input
(preprocessed or fed back in by the C++ frontend). */
- if (! pfile->skipping && !CPP_OPTION (pfile, lang_asm))
+ if (! buffer->was_skipping && CPP_OPTION (pfile, lang) != CLK_ASM)
{
dir = &dtable[T_LINE];
+ pfile->state.line_extension = 1;
_cpp_push_token (pfile, &dname, &pfile->directive_pos);
- if (CPP_PEDANTIC (pfile) && buffer->inc
- && ! CPP_OPTION (pfile, preprocessed))
+ if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed))
cpp_pedwarn (pfile, "# followed by integer");
}
}
/* If we are skipping a failed conditional group, all
non-conditional directives are ignored. */
- if (! pfile->skipping || (dir->flags & COND))
+ if (! buffer->was_skipping || (dir->flags & COND))
{
/* Issue -pedantic warnings for extensions. */
if (CPP_PEDANTIC (pfile) && dir->origin == EXTENSION)
if (! (dir->flags & IF_COND))
pfile->mi_state = MI_FAILED;
- buffer->was_skipping = pfile->skipping;
- pfile->skipping = 0;
(*dir->handler) (pfile);
- pfile->skipping = buffer->was_skipping;
}
}
}
- else if (dname.type == CPP_EOF)
- {
- /* The null directive. */
- if (indented && CPP_WTRADITIONAL (pfile))
- cpp_warning (pfile, "traditional C ignores #\\n with the # indented");
- }
- else if (!pfile->skipping)
+ else if (dname.type != CPP_EOF && ! pfile->skipping)
{
/* An unknown directive. Don't complain about it in assembly
source: we don't know where the comments are, and # may
introduce assembler pseudo-ops. Don't complain about invalid
directives in skipped conditional groups (6.10 p4). */
- if (CPP_OPTION (pfile, lang_asm))
+ if (CPP_OPTION (pfile, lang) == CLK_ASM)
{
/* Output the # and lookahead token for the assembler. */
- not_asm = 0;
_cpp_push_token (pfile, &dname, &pfile->directive_pos);
+ skip = 0;
}
else
cpp_error (pfile, "invalid preprocessing directive #%s",
cpp_token_as_text (pfile, &dname));
}
- /* Save the lookahead token for assembler. */
- if (not_asm)
- skip_rest_of_line (pfile);
- pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
- pfile->state.in_directive = 0;
- pfile->state.angled_headers = 0;
- pfile->directive = 0;
-
- return not_asm;
+ end_directive (pfile, skip);
+ return skip;
}
/* Directive handler wrapper used by the command line option
processor. */
static void
-run_directive (pfile, dir_no, buf, count, name)
+run_directive (pfile, dir_no, type, buf, count)
cpp_reader *pfile;
int dir_no;
+ enum cpp_buffer_type type;
const char *buf;
size_t count;
- const char *name;
{
unsigned int output_line = pfile->lexer_pos.output_line;
+ cpp_buffer *buffer;
- if (cpp_push_buffer (pfile, (const U_CHAR *) buf, count) != NULL)
- {
- const struct directive *dir = &dtable[dir_no], *orig_dir;
- unsigned char orig_in_directive;
-
- if (name)
- CPP_BUFFER (pfile)->nominal_fname = name;
- else
- CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
+ buffer = cpp_push_buffer (pfile, (const U_CHAR *) buf, count, type, 0);
+ if (dir_no == T_PRAGMA)
+ {
/* A kludge to avoid line markers for _Pragma. */
- if (dir_no == T_PRAGMA)
- pfile->lexer_pos.output_line = output_line;
-
- /* Save any in-process directive; _Pragma can appear in one. */
- orig_dir = pfile->directive;
- orig_in_directive = pfile->state.in_directive;
-
- /* For _Pragma, the text is passed through preprocessing stage 3
- only, i.e. no trigraphs, no escaped newline removal, and no
- macro expansion. Do the same for command-line directives. */
- pfile->buffer->from_stage3 = 1;
- pfile->state.in_directive = 1;
- pfile->directive = dir;
- pfile->state.prevent_expansion++;
- (void) (*dir->handler) (pfile);
- pfile->state.prevent_expansion--;
- pfile->directive = orig_dir;
- pfile->state.in_directive = orig_in_directive;
-
- skip_rest_of_line (pfile);
- if (pfile->buffer->cur != pfile->buffer->rlimit)
- cpp_error (pfile, "extra text after end of #%s directive",
- dtable[dir_no].name);
- cpp_pop_buffer (pfile);
+ pfile->lexer_pos.output_line = output_line;
+ /* Avoid interpretation of directives in a _Pragma string. */
+ pfile->state.next_bol = 0;
}
+
+ start_directive (pfile);
+ pfile->state.prevent_expansion++;
+ (void) (*dtable[dir_no].handler) (pfile);
+ pfile->state.prevent_expansion--;
+ check_eol (pfile);
+ end_directive (pfile, 1);
+
+ cpp_pop_buffer (pfile);
}
/* Checks for validity the macro name in #define, #undef, #ifdef and
if (node)
{
- /* Use the permanent pool for storage. */
- pfile->string_pool = &pfile->ident_pool;
-
if (_cpp_create_definition (pfile, node))
if (pfile->cb.define)
(*pfile->cb.define) (pfile, node);
-
- /* Revert to the temporary pool. */
- pfile->string_pool = &pfile->temp_string_pool;
}
}
buffer = (unsigned char *) xmalloc (capacity);
for (;;)
{
- _cpp_get_token (pfile, &token);
+ cpp_get_token (pfile, &token);
if (token.type == CPP_GREATER || token.type == CPP_EOF)
break;
if (total_len + len > capacity)
{
capacity = (capacity + len) * 2;
- buffer = (unsigned char *) realloc (buffer, capacity);
+ buffer = (unsigned char *) xrealloc (buffer, capacity);
}
if (token.flags & PREV_WHITE)
cpp_error (pfile, "missing terminating > character");
else
{
- token_mem = _cpp_pool_alloc (pfile->string_pool, total_len);
+ token_mem = _cpp_pool_alloc (&pfile->ident_pool, total_len);
memcpy (token_mem, buffer, total_len);
header->type = CPP_HEADER_NAME;
cpp_reader *pfile;
{
cpp_token header;
- struct file_name_list *search_start = 0;
-
- if (parse_include (pfile, &header))
- return;
-
- /* For #include_next, skip in the search path past the dir in which
- the current file was found. If this is the last directory in the
- search path, don't include anything. If the current file was
- specified with an absolute path, use the normal search logic. If
- this is the primary source file, use the normal search logic and
- generate a warning. */
- if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
- {
- if (CPP_BUFFER (pfile)->inc->foundhere)
- {
- search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
- if (!search_start)
- return;
- }
- }
- else
- cpp_warning (pfile, "#include_next in primary source file");
- _cpp_execute_include (pfile, &header, 0, search_start);
+ if (!parse_include (pfile, &header))
+ _cpp_execute_include (pfile, &header, 0, 1);
}
-/* Subroutine of do_line. Read next token from PFILE without adding it to
- the output buffer. If it is a number between 1 and 4, store it in *NUM
- and return 1; otherwise, return 0 and complain if we aren't at the end
- of the directive. */
+/* Subroutine of do_line. Read possible flags after file name. LAST
+ is the last flag seen; 0 if this is the first flag. Return the flag
+ if it is valid, 0 at the end of the directive. Otherwise complain. */
-static int
-read_line_number (pfile, num)
+static unsigned int
+read_flag (pfile, last)
cpp_reader *pfile;
- int *num;
+ unsigned int last;
{
cpp_token token;
- unsigned int val;
_cpp_lex_token (pfile, &token);
if (token.type == CPP_NUMBER && token.val.str.len == 1)
{
- val = token.val.str.text[0] - '1';
- if (val <= 3)
- {
- *num = val + 1;
- return 1;
- }
+ unsigned int flag = token.val.str.text[0] - '0';
+
+ if (flag > last && flag <= 4
+ && (flag != 4 || last == 3)
+ && (flag != 2 || last == 0))
+ return flag;
}
if (token.type != CPP_EOF)
- cpp_error (pfile, "invalid format #line");
+ cpp_error (pfile, "invalid flag \"%s\" in line directive",
+ cpp_token_as_text (pfile, &token));
return 0;
}
do_line (pfile)
cpp_reader *pfile;
{
- cpp_buffer *ip = CPP_BUFFER (pfile);
+ cpp_buffer *buffer = pfile->buffer;
+ const char *filename = buffer->nominal_fname;
+ unsigned int lineno = buffer->lineno;
+ enum cpp_fc_reason reason = FC_RENAME;
unsigned long new_lineno;
- /* C99 raised the minimum limit on #line numbers. */
- unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
- int enter = 0, leave = 0, rename = 0;
+ unsigned int cap;
cpp_token token;
+ /* C99 raised the minimum limit on #line numbers. */
+ cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
+
/* #line commands expand macros. */
- _cpp_get_token (pfile, &token);
+ cpp_get_token (pfile, &token);
if (token.type != CPP_NUMBER
|| strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
{
if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
cpp_pedwarn (pfile, "line number out of range");
- _cpp_get_token (pfile, &token);
-
- if (token.type != CPP_EOF)
+ cpp_get_token (pfile, &token);
+ if (token.type == CPP_STRING)
{
char *fname;
unsigned int len;
- int action_number = 0;
-
- if (token.type != CPP_STRING)
- {
- cpp_error (pfile, "\"%s\" is not a valid filename",
- cpp_token_as_text (pfile, &token));
- return;
- }
+ /* FIXME: memory leak. */
len = token.val.str.len;
- fname = alloca (len + 1);
+ fname = xmalloc (len + 1);
memcpy (fname, token.val.str.text, len);
fname[len] = '\0';
- if (strcmp (fname, ip->nominal_fname))
- {
- rename = 1;
- if (!strcmp (fname, ip->inc->name))
- ip->nominal_fname = ip->inc->name;
- else
- ip->nominal_fname = _cpp_fake_include (pfile, fname);
- }
+ _cpp_simplify_pathname (fname);
- if (read_line_number (pfile, &action_number) != 0)
+ if (! pfile->state.line_extension)
+ check_eol (pfile);
+ else
{
- if (! CPP_OPTION (pfile, preprocessed) && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "extra tokens at end of #line directive");
+ int flag = 0, sysp = 0;
- if (action_number == 1)
+ flag = read_flag (pfile, flag);
+ if (flag == 1)
{
- enter = 1;
- cpp_make_system_header (pfile, ip, 0);
- read_line_number (pfile, &action_number);
+ reason = FC_ENTER;
+ flag = read_flag (pfile, flag);
}
- else if (action_number == 2)
+ else if (flag == 2)
{
- leave = 1;
- cpp_make_system_header (pfile, ip, 0);
- read_line_number (pfile, &action_number);
+ reason = FC_LEAVE;
+ flag = read_flag (pfile, flag);
}
- if (action_number == 3)
+ if (flag == 3)
{
- cpp_make_system_header (pfile, ip, 1);
- read_line_number (pfile, &action_number);
+ sysp = 1;
+ flag = read_flag (pfile, flag);
+ if (flag == 4)
+ sysp = 2, read_flag (pfile, flag);
}
- if (action_number == 4)
+
+ if (reason == FC_ENTER)
+ {
+ cpp_push_buffer (pfile, 0, 0, BUF_FAKE, fname);
+ buffer = pfile->buffer;
+ }
+ else if (reason == FC_LEAVE)
{
- cpp_make_system_header (pfile, ip, 2);
- read_line_number (pfile, &action_number);
+ if (buffer->type != BUF_FAKE)
+ cpp_warning (pfile, "file \"%s\" left but not entered",
+ buffer->nominal_fname);
+ else
+ {
+ cpp_pop_buffer (pfile);
+ buffer = pfile->buffer;
+ if (strcmp (buffer->nominal_fname, fname))
+ cpp_warning (pfile, "expected to return to file \"%s\"",
+ buffer->nominal_fname);
+ if (buffer->lineno + 1 != new_lineno)
+ cpp_warning (pfile, "expected to return to line number %u",
+ buffer->lineno + 1);
+ if (buffer->sysp != sysp)
+ cpp_warning (pfile, "header flags for \"%s\" have changed",
+ buffer->nominal_fname);
+ }
}
+ buffer->sysp = sysp;
}
- check_eol (pfile);
+ buffer->nominal_fname = fname;
+ }
+ else if (token.type != CPP_EOF)
+ {
+ cpp_error (pfile, "\"%s\" is not a valid filename",
+ cpp_token_as_text (pfile, &token));
+ return;
}
/* Our line number is incremented after the directive is processed. */
- ip->lineno = new_lineno - 1;
- pfile->lexer_pos.output_line = ip->lineno;
- if (enter && pfile->cb.enter_file)
- (*pfile->cb.enter_file) (pfile);
- if (leave && pfile->cb.leave_file)
- (*pfile->cb.leave_file) (pfile);
- if (rename && pfile->cb.rename_file)
- (*pfile->cb.rename_file) (pfile);
+ buffer->lineno = new_lineno - 1;
+ _cpp_do_file_change (pfile, reason, filename, lineno);
+}
+
+/* Arrange the file_change callback. */
+void
+_cpp_do_file_change (pfile, reason, from_file, from_lineno)
+ cpp_reader *pfile;
+ enum cpp_fc_reason reason;
+ const char *from_file;
+ unsigned int from_lineno;
+{
+ if (pfile->cb.file_change)
+ {
+ cpp_file_change fc;
+ cpp_buffer *buffer = pfile->buffer;
+
+ fc.reason = reason;
+ fc.to.filename = buffer->nominal_fname;
+ fc.to.lineno = buffer->lineno + 1;
+ fc.sysp = buffer->sysp;
+ fc.externc = CPP_OPTION (pfile, cplusplus) && buffer->sysp == 2;
+
+ /* Caller doesn't need to handle FC_ENTER. */
+ if (reason == FC_ENTER)
+ {
+ if (buffer->prev)
+ {
+ from_file = buffer->prev->nominal_fname;
+ from_lineno = buffer->prev->lineno;
+ }
+ else
+ from_file = 0;
+ }
+ /* Special case for file "foo.i" with "# 1 foo.c" on first line. */
+ else if (reason == FC_RENAME && ! buffer->prev
+ && pfile->directive_pos.line == 1)
+ from_file = 0;
+
+ fc.from.filename = from_file;
+ fc.from.lineno = from_lineno;
+ pfile->cb.file_change (pfile, &fc);
+ }
}
/*
*/
static void
-do_diagnostic (pfile, code)
+do_diagnostic (pfile, code, print_dir)
cpp_reader *pfile;
enum error_type code;
+ int print_dir;
{
if (_cpp_begin_message (pfile, code, NULL, 0))
{
- fprintf (stderr, "#%s ", pfile->directive->name);
+ if (print_dir)
+ fprintf (stderr, "#%s ", pfile->directive->name);
pfile->state.prevent_expansion++;
cpp_output_line (pfile, stderr);
pfile->state.prevent_expansion--;
do_error (pfile)
cpp_reader *pfile;
{
- do_diagnostic (pfile, ERROR);
+ do_diagnostic (pfile, ERROR, 1);
}
static void
do_warning (pfile)
cpp_reader *pfile;
{
- do_diagnostic (pfile, WARNING);
+ do_diagnostic (pfile, WARNING, 1);
}
/* Report program identification. */
{
cpp_token str;
- _cpp_get_token (pfile, &str);
+ cpp_get_token (pfile, &str);
if (str.type != CPP_STRING)
cpp_error (pfile, "invalid #ident");
else if (pfile->cb.ident)
do_pragma_once (pfile)
cpp_reader *pfile;
{
- cpp_buffer *ip = CPP_BUFFER (pfile);
-
cpp_warning (pfile, "#pragma once is obsolete");
- if (CPP_PREV_BUFFER (ip) == NULL)
+ if (pfile->buffer->prev == NULL)
cpp_warning (pfile, "#pragma once in main file");
else
- ip->inc->cmacro = NEVER_REREAD;
+ _cpp_never_reread (pfile->buffer->inc);
check_eol (pfile);
}
do_pragma_system_header (pfile)
cpp_reader *pfile;
{
- cpp_buffer *ip = CPP_BUFFER (pfile);
- if (CPP_PREV_BUFFER (ip) == NULL)
- cpp_warning (pfile, "#pragma system_header outside include file");
+ cpp_buffer *buffer = pfile->buffer;
+
+ if (buffer->prev == 0)
+ cpp_warning (pfile, "#pragma system_header ignored outside include file");
else
- cpp_make_system_header (pfile, ip, 1);
+ cpp_make_system_header (pfile, 1, 0);
check_eol (pfile);
}
cpp_start_lookahead (pfile);
cpp_get_token (pfile, &msg);
cpp_stop_lookahead (pfile, msg.type == CPP_EOF);
- if (msg.type != CPP_EOF && _cpp_begin_message (pfile, WARNING, NULL, 0))
- cpp_output_line (pfile, stderr);
+ if (msg.type != CPP_EOF)
+ do_diagnostic (pfile, WARNING, 0);
}
}
}
buffer = destringize (&string.val.str, &len);
- run_directive (pfile, T_PRAGMA, (char *) buffer, len, _("<_Pragma>"));
+ run_directive (pfile, T_PRAGMA, BUF_PRAGMA, (char *) buffer, len);
free ((PTR) buffer);
}
if (node)
skip = node->type != NT_MACRO;
+
+ if (node)
+ check_eol (pfile);
}
push_conditional (pfile, skip, T_IFDEF, 0);
node = lex_macro_node (pfile);
if (node)
skip = node->type == NT_MACRO;
+
+ if (node)
+ check_eol (pfile);
}
push_conditional (pfile, skip, T_IFNDEF, node);
token = &answer->first[answer->count];
}
- _cpp_get_token (pfile, token);
+ cpp_get_token (pfile, token);
if (token->type == CPP_CLOSE_PAREN)
break;
/* Parses an assertion, returning a pointer to the hash node of the
predicate, or 0 on error. If an answer was supplied, it is placed
- in ANSWERP, otherwise it is set to 0. We use _cpp_get_raw_token,
- since we cannot assume tokens are consecutive in a #if statement
- (we may be in a macro), and we don't want to macro expand. */
+ in ANSWERP, otherwise it is set to 0. */
static cpp_hashnode *
parse_assertion (pfile, answerp, type)
cpp_reader *pfile;
/* We don't expand predicates or answers. */
pfile->state.prevent_expansion++;
- /* Use the permanent pool for storage (for the answers). */
- pfile->string_pool = &pfile->ident_pool;
-
*answerp = 0;
- _cpp_get_token (pfile, &predicate);
+ cpp_get_token (pfile, &predicate);
if (predicate.type == CPP_EOF)
cpp_error (pfile, "assertion without predicate");
else if (predicate.type != CPP_NAME)
result = cpp_lookup (pfile, sym, len + 1);
}
- pfile->string_pool = &pfile->temp_string_pool;
pfile->state.prevent_expansion--;
return result;
}
buf[count++] = '1';
}
- run_directive (pfile, T_DEFINE, buf, count, 0);
+ run_directive (pfile, T_DEFINE, BUF_CL_OPTION, buf, count);
}
-/* Slight variant of the above for use by initialize_builtins, which (a)
- knows how to set up the buffer itself, (b) needs a different "filename"
- tag. */
+/* Slight variant of the above for use by initialize_builtins. */
void
_cpp_define_builtin (pfile, str)
cpp_reader *pfile;
const char *str;
{
- run_directive (pfile, T_DEFINE, str, strlen (str), _("<builtin>"));
+ run_directive (pfile, T_DEFINE, BUF_BUILTIN, str, strlen (str));
}
/* Process MACRO as if it appeared as the body of an #undef. */
cpp_reader *pfile;
const char *macro;
{
- run_directive (pfile, T_UNDEF, macro, strlen (macro), 0);
+ run_directive (pfile, T_UNDEF, BUF_CL_OPTION, macro, strlen (macro));
}
/* Process the string STR as if it appeared as the body of a #assert. */
str = buf;
}
- run_directive (pfile, type, str, count, 0);
+ run_directive (pfile, type, BUF_CL_OPTION, str, count);
}
-/* Allocate a new cpp_buffer for PFILE, and push it on the input
- buffer stack. If BUFFER != NULL, then use the LENGTH characters in
- BUFFER as the new input buffer. Return the new buffer, or NULL on
- failure. */
-
+/* Push a new buffer on the buffer stack. Returns the new buffer; it
+ doesn't fail. It does not generate a file change call back; that
+ is the responsibility of the caller. */
cpp_buffer *
-cpp_push_buffer (pfile, buffer, length)
+cpp_push_buffer (pfile, buffer, len, type, filename)
cpp_reader *pfile;
const U_CHAR *buffer;
- long length;
+ size_t len;
+ enum cpp_buffer_type type;
+ const char *filename;
{
- cpp_buffer *buf = CPP_BUFFER (pfile);
- cpp_buffer *new;
- if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
+ cpp_buffer *new = xobnew (pfile->buffer_ob, cpp_buffer);
+
+ if (type == BUF_FAKE)
{
- cpp_fatal (pfile, "#include nested too deeply");
- return NULL;
+ /* A copy of the current buffer, just with a new name and type. */
+ memcpy (new, pfile->buffer, sizeof (cpp_buffer));
+ new->type = BUF_FAKE;
}
+ else
+ {
+ if (type == BUF_BUILTIN)
+ filename = _("<builtin>");
+ else if (type == BUF_CL_OPTION)
+ filename = _("<command line>");
+ else if (type == BUF_PRAGMA)
+ filename = "<_Pragma>";
+
+ /* Clears, amongst other things, if_stack and mi_cmacro. */
+ memset (new, 0, sizeof (cpp_buffer));
+
+ new->line_base = new->buf = new->cur = buffer;
+ new->rlimit = buffer + len;
+ new->sysp = 0;
+
+ /* No read ahead or extra char initially. */
+ new->read_ahead = EOF;
+ new->extra_char = EOF;
- new = xobnew (pfile->buffer_ob, cpp_buffer);
- /* Clears, amongst other things, if_stack and mi_cmacro. */
- memset (new, 0, sizeof (cpp_buffer));
+ /* Preprocessed files, builtins, _Pragma and command line
+ options don't do trigraph and escaped newline processing. */
+ new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
- pfile->lexer_pos.output_line = 1;
- new->line_base = new->buf = new->cur = buffer;
- new->rlimit = buffer + length;
- new->prev = buf;
+ pfile->lexer_pos.output_line = 1;
+ }
+
+ new->nominal_fname = filename;
+ new->type = type;
+ new->prev = pfile->buffer;
new->pfile = pfile;
- new->was_skipping = 0;
- /* Preprocessed files don't do trigraph and escaped newline processing. */
- new->from_stage3 = CPP_OPTION (pfile, preprocessed);
- /* No read ahead or extra char initially. */
- new->read_ahead = EOF;
- new->extra_char = EOF;
+ new->include_stack_listed = 0;
+ new->lineno = 1;
+
pfile->state.next_bol = 1;
+ pfile->buffer_stack_depth++;
+ pfile->buffer = new;
- CPP_BUFFER (pfile) = new;
return new;
}
+/* If called from do_line, pops a single buffer. Otherwise pops all
+ buffers until a real file is reached. Generates appropriate
+ call-backs. */
cpp_buffer *
cpp_pop_buffer (pfile)
cpp_reader *pfile;
{
- cpp_buffer *buffer = pfile->buffer;
- struct if_stack *ifs = buffer->if_stack;
- int wfb;
+ cpp_buffer *buffer;
+ struct if_stack *ifs;
- /* Walk back up the conditional stack till we reach its level at
- entry to this file, issuing error messages. */
- for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
- cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
- "unterminated #%s", dtable[ifs->type].name);
+ for (;;)
+ {
+ buffer = pfile->buffer;
+ /* Walk back up the conditional stack till we reach its level at
+ entry to this file, issuing error messages. */
+ for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
+ cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
+ "unterminated #%s", dtable[ifs->type].name);
+
+ if (buffer->type == BUF_FAKE)
+ buffer->prev->cur = buffer->cur;
+ else if (buffer->type == BUF_FILE)
+ _cpp_pop_file_buffer (pfile, buffer);
+
+ pfile->buffer = buffer->prev;
+ pfile->buffer_stack_depth--;
+
+ /* Callbacks only generated for faked or real files. */
+ if (buffer->type != BUF_FILE && buffer->type != BUF_FAKE)
+ break;
+
+ /* No callback for EOF of last file. */
+ if (!pfile->buffer)
+ break;
- wfb = (buffer->inc != 0);
- if (wfb)
- _cpp_pop_file_buffer (pfile, buffer);
+ /* do_line does its own call backs. */
+ pfile->buffer->include_stack_listed = 0;
+ if (pfile->directive == &dtable[T_LINE])
+ break;
- pfile->buffer = buffer->prev;
- obstack_free (pfile->buffer_ob, buffer);
- pfile->buffer_stack_depth--;
+ _cpp_do_file_change (pfile, FC_LEAVE, buffer->nominal_fname,
+ buffer->lineno);
+ if (pfile->buffer->type == BUF_FILE)
+ break;
- if (pfile->buffer && wfb && pfile->cb.leave_file)
- (*pfile->cb.leave_file) (pfile);
-
+ cpp_warning (pfile, "file \"%s\" entered but not left",
+ buffer->nominal_fname);
+ }
+
+ obstack_free (pfile->buffer_ob, buffer);
return pfile->buffer;
}