/* CPP Library. (Directive handling.)
Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
- 1999, 2000 Free Software Foundation, Inc.
+ 1999, 2000, 2001 Free Software Foundation, Inc.
Contributed by Per Bothner, 1994-95.
Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
#include "cpphash.h"
#include "intl.h"
#include "obstack.h"
-#include "symcat.h"
/* Chained list of answers to an assertion. */
struct answer
unsigned long *));
static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
+static void do_include_common PARAMS ((cpp_reader *, enum include_type));
static void do_pragma_once PARAMS ((cpp_reader *));
static void do_pragma_poison PARAMS ((cpp_reader *));
static void do_pragma_system_header PARAMS ((cpp_reader *));
D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
-D(elif, T_ELIF, KANDR, COND) /* 610 */ \
+D(elif, T_ELIF, STDC89, COND) /* 610 */ \
D(error, T_ERROR, STDC89, 0) /* 475 */ \
D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
#define D(n, tag, o, f) tag,
enum
{
- T_BAD_DIRECTIVE,
DIRECTIVE_TABLE
N_DIRECTIVES
};
/* If we are rescanning preprocessed input, only directives tagged
with IN_I are honored, and the warnings below are suppressed. */
- if (! CPP_OPTION (pfile, preprocessed) || dir->flags & IN_I)
+ if (CPP_OPTION (pfile, preprocessed))
+ {
+ /* Kluge alert. In order to be sure that code like this
+ #define HASH #
+ HASH define foo bar
+ does not cause '#define foo bar' to get executed when
+ compiled with -save-temps, we recognize directives in
+ -fpreprocessed mode only if the # is in column 1 and the
+ directive name starts in column 2. This output can only
+ be generated by the directive callbacks in cppmain.c (see
+ also the special case in scan_buffer). */
+ if (dir->flags & IN_I && !indented && !(dname.flags & PREV_WHITE))
+ (*dir->handler) (pfile);
+ /* That check misses '# 123' linemarkers. Let them through too. */
+ else if (dname.type == CPP_NUMBER)
+ (*dir->handler) (pfile);
+ else
+ {
+ /* We don't want to process this directive. Put back the
+ tokens so caller will see them (and issue an error,
+ probably). */
+ _cpp_push_token (pfile, &dname, &pfile->directive_pos);
+ skip = 0;
+ }
+ }
+ else
{
/* Traditionally, a directive is ignored unless its # is in
column 1. Therefore in code intended to work with K+R
conditional blocks. */
if (CPP_WTRADITIONAL (pfile))
{
- if (indented && dir->origin == KANDR)
+ if (dir == &dtable[T_ELIF])
+ cpp_warning (pfile,
+ "suggest not using #elif in traditional C");
+ else if (indented && dir->origin == KANDR)
cpp_warning (pfile,
"traditional C ignores #%s with the # indented",
dir->name);
}
}
}
- else if (dname.type != CPP_EOF && ! pfile->skipping)
+ else if (dname.type != CPP_EOF && ! buffer->was_skipping)
{
/* An unknown directive. Don't complain about it in assembly
source: we don't know where the comments are, and # may
else if (token.flags & NAMED_OP)
cpp_error (pfile,
"\"%s\" cannot be used as a macro name as it is an operator in C++",
- token.val.node->name);
+ NODE_NAME (token.val.node));
else
cpp_error (pfile, "macro names must be identifiers");
}
/* In Objective C, some keywords begin with '@', but general
identifiers do not, and you're not allowed to #define them. */
- if (node == pfile->spec_nodes.n_defined || node->name[0] == '@')
- cpp_error (pfile, "\"%s\" cannot be used as a macro name", node->name);
+ if (node == pfile->spec_nodes.n_defined || NODE_NAME (node)[0] == '@')
+ cpp_error (pfile, "\"%s\" cannot be used as a macro name",
+ NODE_NAME (node));
else if (!(node->flags & NODE_POISONED))
return node;
}
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;
}
}
if (pfile->cb.undef)
(*pfile->cb.undef) (pfile, node);
- if (node->flags & NODE_BUILTIN)
- cpp_warning (pfile, "undefining \"%s\"", node->name);
+ if (node->flags & NODE_WARN)
+ cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
_cpp_free_definition (node);
}
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 + 1);
memcpy (token_mem, buffer, total_len);
+ token_mem[total_len] = '\0';
header->type = CPP_HEADER_NAME;
header->flags &= ~PREV_WHITE;
return 0;
}
+/* Handle #include, #include_next and #import. */
static void
-do_include (pfile)
+do_include_common (pfile, type)
cpp_reader *pfile;
+ enum include_type type;
{
cpp_token header;
if (!parse_include (pfile, &header))
- _cpp_execute_include (pfile, &header, 0, 0);
+ {
+ /* Prevent #include recursion. */
+ if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
+ cpp_fatal (pfile, "#include nested too deeply");
+ else if (pfile->context->prev)
+ cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
+ else
+ {
+ /* For #include_next, if this is the primary source file,
+ warn and use the normal search logic. */
+ if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
+ {
+ cpp_warning (pfile, "#include_next in primary source file");
+ type = IT_INCLUDE;
+ }
+
+ _cpp_execute_include (pfile, &header, type);
+ }
+ }
}
static void
-do_import (pfile)
+do_include (pfile)
cpp_reader *pfile;
{
- cpp_token header;
+ do_include_common (pfile, IT_INCLUDE);
+}
+static void
+do_import (pfile)
+ cpp_reader *pfile;
+{
if (!pfile->import_warning && CPP_OPTION (pfile, warn_import))
{
pfile->import_warning = 1;
"#import is obsolete, use an #ifndef wrapper in the header file");
}
- if (!parse_include (pfile, &header))
- _cpp_execute_include (pfile, &header, 1, 0);
+ do_include_common (pfile, IT_IMPORT);
}
static void
do_include_next (pfile)
cpp_reader *pfile;
{
- cpp_token header;
-
- if (!parse_include (pfile, &header))
- _cpp_execute_include (pfile, &header, 0, 1);
+ do_include_common (pfile, IT_INCLUDE_NEXT);
}
/* Subroutine of do_line. Read possible flags after file name. LAST
cpp_get_token (pfile, &token);
if (token.type == CPP_STRING)
{
- char *fname;
- unsigned int len;
-
- /* FIXME: memory leak. */
- len = token.val.str.len;
- fname = xmalloc (len + 1);
- memcpy (fname, token.val.str.text, len);
- fname[len] = '\0';
-
- _cpp_simplify_pathname (fname);
- buffer->nominal_fname = fname;
+ const char *fname = (const char *) token.val.str.text;
+ /* Only accept flags for the # 55 form. */
if (! pfile->state.line_extension)
check_eol (pfile);
else
sysp = 2, read_flag (pfile, flag);
}
- cpp_make_system_header (pfile, sysp, sysp == 2);
+ if (reason == FC_ENTER)
+ {
+ /* Fake a buffer stack for diagnostics. */
+ cpp_push_buffer (pfile, 0, 0, BUF_FAKE, fname);
+ /* Fake an include for cpp_included. */
+ _cpp_fake_include (pfile, fname);
+ buffer = pfile->buffer;
+ }
+ else if (reason == FC_LEAVE)
+ {
+ 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;
+#ifdef ENABLE_CHECKING
+ 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);
+#endif
+ }
+ }
+ buffer->sysp = sysp;
}
+ buffer->nominal_fname = fname;
}
else if (token.type != CPP_EOF)
{
_cpp_do_file_change (pfile, reason, filename, lineno);
}
-/* Arrange the file_change callback. The assumption is that the
- current buffer's lineno is one less than the next line. */
+/* Arrange the file_change callback. */
void
_cpp_do_file_change (pfile, reason, from_file, from_lineno)
cpp_reader *pfile;
const char *from_file;
unsigned int from_lineno;
{
- if (pfile->cb.change_file)
+ if (pfile->cb.file_change)
{
cpp_file_change fc;
cpp_buffer *buffer = pfile->buffer;
fc.reason = reason;
- fc.from.filename = from_file;
- fc.from.lineno = from_lineno;
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;
- pfile->cb.change_file (pfile, &fc);
+
+ /* 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);
}
}
do_warning (pfile)
cpp_reader *pfile;
{
- do_diagnostic (pfile, WARNING, 1);
+ /* We want #warning diagnostics to be emitted in system headers too. */
+ do_diagnostic (pfile, WARNING_SYSHDR, 1);
}
/* Report program identification. */
{
const struct pragma_entry *p;
cpp_token tok;
- const cpp_hashnode *node;
- const U_CHAR *name;
- size_t len;
int drop = 0;
p = pfile->pragmas;
cpp_get_token (pfile, &tok);
if (tok.type == CPP_NAME)
{
- node = tok.val.node;
- name = node->name;
- len = node->length;
+ const cpp_hashnode *node = tok.val.node;
+ size_t len = NODE_LEN (node);
+
while (p)
{
- if (strlen (p->name) == len && !memcmp (p->name, name, len))
+ if (strlen (p->name) == len
+ && !memcmp (p->name, NODE_NAME (node), len))
{
if (p->isnspace)
{
continue;
if (hp->type == NT_MACRO)
- cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
+ cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
_cpp_free_definition (hp);
hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
}
buffer->if_stack = ifs->next;
buffer->was_skipping = ifs->was_skipping;
- obstack_free (pfile->buffer_ob, ifs);
+ obstack_free (&pfile->buffer_ob, ifs);
}
check_eol (pfile);
struct if_stack *ifs;
cpp_buffer *buffer = pfile->buffer;
- ifs = xobnew (pfile->buffer_ob, struct if_stack);
+ ifs = xobnew (&pfile->buffer_ob, struct if_stack);
ifs->pos = pfile->directive_pos;
ifs->next = buffer->if_stack;
ifs->was_skipping = buffer->was_skipping;
/* 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);
if (predicate.type == CPP_EOF)
cpp_error (pfile, "predicate must be an identifier");
else if (parse_answer (pfile, answerp, type) == 0)
{
- unsigned int len = predicate.val.node->length;
+ unsigned int len = NODE_LEN (predicate.val.node);
unsigned char *sym = alloca (len + 1);
/* Prefix '#' to get it out of macro namespace. */
sym[0] = '#';
- memcpy (sym + 1, predicate.val.node->name, len);
+ memcpy (sym + 1, NODE_NAME (predicate.val.node), len);
result = cpp_lookup (pfile, sym, len + 1);
}
- pfile->string_pool = &pfile->temp_string_pool;
pfile->state.prevent_expansion--;
return result;
}
{
if (*find_answer (node, new_answer))
{
- cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
+ cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
return;
}
new_answer->next = node->value.answers;
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;
run_directive (pfile, type, BUF_CL_OPTION, str, count);
}
-/* Push a new buffer on the buffer stack. Buffer can be NULL, but
- then LEN should be 0. Returns the new buffer; it doesn't fail. */
+/* The number of errors for a given reader. */
+unsigned int
+cpp_errors (pfile)
+ cpp_reader *pfile;
+{
+ return pfile->errors;
+}
+
+/* The options structure. */
+cpp_options *
+cpp_get_options (pfile)
+ cpp_reader *pfile;
+{
+ return &pfile->opts;
+}
+/* The callbacks structure. */
+cpp_callbacks *
+cpp_get_callbacks (pfile)
+ cpp_reader *pfile;
+{
+ return &pfile->cb;
+}
+
+/* Copy the given callbacks structure to our own. */
+void
+cpp_set_callbacks (pfile, cb)
+ cpp_reader *pfile;
+ cpp_callbacks *cb;
+{
+ pfile->cb = *cb;
+}
+
+/* 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, len, type, filename)
cpp_reader *pfile;
enum cpp_buffer_type type;
const char *filename;
{
- cpp_buffer *new = xobnew (pfile->buffer_ob, cpp_buffer);
+ cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
- /* Clears, amongst other things, if_stack and mi_cmacro. */
- memset (new, 0, sizeof (cpp_buffer));
-
- switch (type)
+ if (type == BUF_FAKE)
{
- case BUF_FILE: new->nominal_fname = filename; break;
- case BUF_BUILTIN: new->nominal_fname = _("<builtin>"); break;
- case BUF_CL_OPTION: new->nominal_fname = _("<command line>"); break;
- case BUF_PRAGMA: new->nominal_fname = _("<_Pragma>"); break;
+ /* 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;
+
+ /* 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;
+ }
+
+ if (*filename == '\0')
+ new->nominal_fname = _("<stdin>");
+ else
+ new->nominal_fname = filename;
new->type = type;
- new->line_base = new->buf = new->cur = buffer;
- new->rlimit = buffer + len;
new->prev = pfile->buffer;
new->pfile = pfile;
-
- /* No read ahead or extra char initially. */
- new->read_ahead = EOF;
- new->extra_char = EOF;
-
- /* 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);
+ new->include_stack_listed = 0;
+ new->lineno = 1;
pfile->state.next_bol = 1;
pfile->buffer_stack_depth++;
- pfile->lexer_pos.output_line = 1;
pfile->buffer = 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;
- const char *filename = buffer->nominal_fname;
- unsigned int lineno = buffer->lineno;
- struct if_stack *ifs = buffer->if_stack;
- int file_buffer_p = buffer->type == BUF_FILE;
+ 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;
- if (file_buffer_p)
- _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 && file_buffer_p)
- {
- _cpp_do_file_change (pfile, FC_LEAVE, filename, lineno);
- pfile->buffer->include_stack_listed = 0;
+ cpp_warning (pfile, "file \"%s\" entered but not left",
+ buffer->nominal_fname);
}
-
+
+ obstack_free (&pfile->buffer_ob, buffer);
return pfile->buffer;
}
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
void
-_cpp_init_stacks (pfile)
+_cpp_init_directives (pfile)
cpp_reader *pfile;
{
- int i;
+ unsigned int i;
cpp_hashnode *node;
- pfile->buffer_ob = xnew (struct obstack);
- obstack_init (pfile->buffer_ob);
-
/* Register the directives. */
- for (i = 1; i < N_DIRECTIVES; i++)
+ for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
{
- node = cpp_lookup (pfile, dtable[i - 1].name, dtable[i - 1].length);
- node->directive_index = i;
+ node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
+ node->directive_index = i + 1;
}
}
-
-void
-_cpp_cleanup_stacks (pfile)
- cpp_reader *pfile;
-{
- obstack_free (pfile->buffer_ob, 0);
- free (pfile->buffer_ob);
-}