along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+/*
+
+Cleanups to do:-
+
+o Distinguish integers, floats, and 'other' pp-numbers.
+o Store ints and char constants as binary values.
+o New command-line assertion syntax.
+o Comment all functions, and describe macro expansion algorithm.
+o Move as much out of header files as possible.
+o Remove single quote pairs `', and some '', from diagnostics.
+o Correct pastability test for CPP_NAME and CPP_NUMBER.
+
+*/
+
#include "config.h"
#include "system.h"
#include "intl.h"
-#include "hashtab.h"
#include "cpplib.h"
#include "cpphash.h"
+#include "symcat.h"
+
+const unsigned char *_cpp_digraph_spellings [] = {U"%:", U"%:%:", U"<:",
+ U":>", U"<%", U"%>"};
+static const cpp_token placemarker_token = {0, 0, CPP_PLACEMARKER,
+ 0 UNION_INIT_ZERO};
+static const cpp_token eof_token = {0, 0, CPP_EOF, 0 UNION_INIT_ZERO};
+
+/* Flags for cpp_context. */
+#define CONTEXT_PASTEL (1 << 0) /* An argument context on LHS of ##. */
+#define CONTEXT_PASTER (1 << 1) /* An argument context on RHS of ##. */
+#define CONTEXT_RAW (1 << 2) /* If argument tokens already expanded. */
+#define CONTEXT_ARG (1 << 3) /* If an argument context. */
+#define CONTEXT_VARARGS (1 << 4) /* If a varargs argument context. */
+
+typedef struct cpp_context cpp_context;
+struct cpp_context
+{
+ union
+ {
+ const cpp_toklist *list; /* Used for macro contexts only. */
+ const cpp_token **arg; /* Used for arg contexts only. */
+ } u;
+
+ /* Pushed token to be returned by next call to get_raw_token. */
+ const cpp_token *pushed_token;
+
+ struct macro_args *args; /* The arguments for a function-like
+ macro. NULL otherwise. */
+ unsigned short posn; /* Current posn, index into u. */
+ unsigned short count; /* No. of tokens in u. */
+ unsigned short level;
+ unsigned char flags;
+};
+
+typedef struct macro_args macro_args;
+struct macro_args
+{
+ unsigned int *ends;
+ const cpp_token **tokens;
+ unsigned int capacity;
+ unsigned int used;
+ unsigned short level;
+};
+
+static const cpp_token *get_raw_token PARAMS ((cpp_reader *));
+static const cpp_token *parse_arg PARAMS ((cpp_reader *, int, unsigned int,
+ macro_args *, unsigned int *));
+static int parse_args PARAMS ((cpp_reader *, cpp_hashnode *, macro_args *));
+static void save_token PARAMS ((macro_args *, const cpp_token *));
+static int pop_context PARAMS ((cpp_reader *));
+static int push_macro_context PARAMS ((cpp_reader *, const cpp_token *));
+static void push_arg_context PARAMS ((cpp_reader *, const cpp_token *));
+static void free_macro_args PARAMS ((macro_args *));
+
+static cppchar_t handle_newline PARAMS ((cpp_buffer *, cppchar_t));
+static cppchar_t skip_escaped_newlines PARAMS ((cpp_buffer *, cppchar_t));
+static cppchar_t get_effective_char PARAMS ((cpp_buffer *));
-#define PEEKBUF(BUFFER, N) \
- ((BUFFER)->rlimit - (BUFFER)->cur > (N) ? (BUFFER)->cur[N] : EOF)
-#define GETBUF(BUFFER) \
- ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
-#define FORWARDBUF(BUFFER, N) ((BUFFER)->cur += (N))
-
-#define PEEKN(N) PEEKBUF (CPP_BUFFER (pfile), N)
-#define FORWARD(N) FORWARDBUF (CPP_BUFFER (pfile), (N))
-#define GETC() GETBUF (CPP_BUFFER (pfile))
-#define PEEKC() PEEKBUF (CPP_BUFFER (pfile), 0)
-
-static void skip_block_comment PARAMS ((cpp_reader *));
-static void skip_line_comment PARAMS ((cpp_reader *));
-static int maybe_macroexpand PARAMS ((cpp_reader *, long));
-static int skip_comment PARAMS ((cpp_reader *, int));
-static int copy_comment PARAMS ((cpp_reader *, int));
-static void skip_string PARAMS ((cpp_reader *, int));
-static void parse_string PARAMS ((cpp_reader *, int));
-static U_CHAR *find_position PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
-static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
-static void null_warning PARAMS ((cpp_reader *, unsigned int));
-
-static void safe_fwrite PARAMS ((cpp_reader *, const U_CHAR *,
- size_t, FILE *));
-static void output_line_command PARAMS ((cpp_reader *, cpp_printer *,
- unsigned int));
-static void bump_column PARAMS ((cpp_printer *, unsigned int,
- unsigned int));
-static void expand_name_space PARAMS ((cpp_toklist *, unsigned int));
-static void expand_token_space PARAMS ((cpp_toklist *));
-static void init_token_list PARAMS ((cpp_reader *, cpp_toklist *, int));
-static void pedantic_whitespace PARAMS ((cpp_reader *, U_CHAR *,
- unsigned int));
-
-#define auto_expand_name_space(list) \
- expand_name_space ((list), (list)->name_cap / 2)
-
-/* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
+static int skip_block_comment PARAMS ((cpp_reader *));
+static int skip_line_comment PARAMS ((cpp_reader *));
+static void adjust_column PARAMS ((cpp_reader *));
+static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
+static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *, cppchar_t));
+static void parse_number PARAMS ((cpp_reader *, cpp_string *, cppchar_t));
+static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
+static void unterminated PARAMS ((cpp_reader *, unsigned int, int));
+static int trigraph_ok PARAMS ((cpp_reader *, cppchar_t));
+static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *));
+static void lex_percent PARAMS ((cpp_buffer *, cpp_token *));
+static void lex_dot PARAMS ((cpp_reader *, cpp_token *));
+static void lex_line PARAMS ((cpp_reader *, cpp_toklist *));
+static void lex_token PARAMS ((cpp_reader *, cpp_token *));
+static int lex_next PARAMS ((cpp_reader *, int));
+
+static int is_macro_disabled PARAMS ((cpp_reader *, const cpp_toklist *,
+ const cpp_token *));
+
+static cpp_token *stringify_arg PARAMS ((cpp_reader *, const cpp_token *));
+static void expand_context_stack PARAMS ((cpp_reader *));
+static unsigned char * spell_token PARAMS ((cpp_reader *, const cpp_token *,
+ unsigned char *));
+typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
+ cpp_token *));
+static cpp_token *make_string_token PARAMS ((cpp_token *, const U_CHAR *,
+ unsigned int));
+static cpp_token *alloc_number_token PARAMS ((cpp_reader *, int number));
+static const cpp_token *special_symbol PARAMS ((cpp_reader *, cpp_hashnode *,
+ const cpp_token *));
+static cpp_token *duplicate_token PARAMS ((cpp_reader *, const cpp_token *));
+static const cpp_token *maybe_paste_with_next PARAMS ((cpp_reader *,
+ const cpp_token *));
+static unsigned int prevent_macro_expansion PARAMS ((cpp_reader *));
+static void restore_macro_expansion PARAMS ((cpp_reader *, unsigned int));
+static cpp_token *get_temp_token PARAMS ((cpp_reader *));
+static void release_temp_tokens PARAMS ((cpp_reader *));
+static U_CHAR * quote_string PARAMS ((U_CHAR *, const U_CHAR *, unsigned int));
-void
-_cpp_grow_token_buffer (pfile, n)
- cpp_reader *pfile;
- long n;
-{
- long old_written = CPP_WRITTEN (pfile);
- pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
- pfile->token_buffer = (U_CHAR *)
- xrealloc(pfile->token_buffer, pfile->token_buffer_size);
- CPP_SET_WRITTEN (pfile, old_written);
-}
+#define VALID_SIGN(c, prevc) \
+ (((c) == '+' || (c) == '-') && \
+ ((prevc) == 'e' || (prevc) == 'E' \
+ || (((prevc) == 'p' || (prevc) == 'P') && !CPP_OPTION (pfile, c89))))
-static int
-null_cleanup (pbuf, pfile)
- cpp_buffer *pbuf ATTRIBUTE_UNUSED;
- cpp_reader *pfile ATTRIBUTE_UNUSED;
+/* An upper bound on the number of bytes needed to spell a token,
+ including preceding whitespace. */
+static inline size_t TOKEN_LEN PARAMS ((const cpp_token *));
+static inline size_t
+TOKEN_LEN (token)
+ const cpp_token *token;
{
- return 0;
+ size_t len;
+
+ switch (TOKEN_SPELL (token))
+ {
+ default: len = 0; break;
+ case SPELL_STRING: len = token->val.str.len; break;
+ case SPELL_IDENT: len = token->val.node->length; break;
+ }
+ return len + 5;
}
-/* 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. */
+#define IS_ARG_CONTEXT(c) ((c)->flags & CONTEXT_ARG)
+#define CURRENT_CONTEXT(pfile) ((pfile)->contexts + (pfile)->cur_context)
+
+#define ASSIGN_FLAGS_AND_POS(d, s) \
+ do {(d)->flags = (s)->flags & (PREV_WHITE | BOL | PASTE_LEFT); \
+ if ((d)->flags & BOL) {(d)->col = (s)->col; (d)->line = (s)->line;} \
+ } while (0)
+
+/* f is flags, just consisting of PREV_WHITE | BOL. */
+#define MODIFY_FLAGS_AND_POS(d, s, f) \
+ do {(d)->flags &= ~(PREV_WHITE | BOL); (d)->flags |= (f); \
+ if ((f) & BOL) {(d)->col = (s)->col; (d)->line = (s)->line;} \
+ } while (0)
+
+#define OP(e, s) { SPELL_OPERATOR, U s },
+#define TK(e, s) { s, U STRINGX (e) },
-cpp_buffer *
-cpp_push_buffer (pfile, buffer, length)
+const struct token_spelling
+_cpp_token_spellings [N_TTYPES] = {TTYPE_TABLE };
+
+#undef OP
+#undef TK
+
+/* Helper routine used by parse_include, which can't see spell_token.
+ Reinterpret the current line as an h-char-sequence (< ... >); we are
+ looking at the first token after the <. */
+const cpp_token *
+_cpp_glue_header_name (pfile)
cpp_reader *pfile;
- const U_CHAR *buffer;
- long length;
{
- cpp_buffer *buf = CPP_BUFFER (pfile);
- cpp_buffer *new;
- if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
+ const cpp_token *t;
+ cpp_token *hdr;
+ U_CHAR *buf, *p;
+ size_t len, avail;
+
+ avail = 40;
+ len = 0;
+ buf = xmalloc (avail);
+
+ for (;;)
{
- cpp_fatal (pfile, "macro or `#include' recursion too deep");
- return NULL;
+ t = _cpp_get_token (pfile);
+ if (t->type == CPP_GREATER || t->type == CPP_EOF)
+ break;
+
+ if (len + TOKEN_LEN (t) > avail)
+ {
+ avail = len + TOKEN_LEN (t) + 40;
+ buf = xrealloc (buf, avail);
+ }
+
+ if (t->flags & PREV_WHITE)
+ buf[len++] = ' ';
+
+ p = spell_token (pfile, t, buf + len);
+ len = (size_t) (p - buf); /* p known >= buf */
}
- new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
+ if (t->type == CPP_EOF)
+ cpp_error (pfile, "missing terminating > character");
- new->if_stack = pfile->if_stack;
- new->cleanup = null_cleanup;
- new->buf = new->cur = buffer;
- new->rlimit = buffer + length;
- new->prev = buf;
- new->mark = NULL;
- new->line_base = NULL;
+ buf = xrealloc (buf, len);
- CPP_BUFFER (pfile) = new;
- return new;
+ hdr = get_temp_token (pfile);
+ hdr->type = CPP_HEADER_NAME;
+ hdr->flags = 0;
+ hdr->val.str.text = buf;
+ hdr->val.str.len = len;
+ return hdr;
}
-cpp_buffer *
-cpp_pop_buffer (pfile)
- cpp_reader *pfile;
-{
- cpp_buffer *buf = CPP_BUFFER (pfile);
- if (ACTIVE_MARK_P (pfile))
- cpp_ice (pfile, "mark active in cpp_pop_buffer");
- (*buf->cleanup) (buf, pfile);
- CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
- free (buf);
- pfile->buffer_stack_depth--;
- return CPP_BUFFER (pfile);
-}
+/* Token-buffer helper functions. */
-/* Deal with the annoying semantics of fwrite. */
-static void
-safe_fwrite (pfile, buf, len, fp)
- cpp_reader *pfile;
- const U_CHAR *buf;
- size_t len;
- FILE *fp;
+/* Expand a token list's string space. It is *vital* that
+ list->tokens_used is correct, to get pointer fix-up right. */
+void
+_cpp_expand_name_space (list, len)
+ cpp_toklist *list;
+ unsigned int len;
{
- size_t count;
+ const U_CHAR *old_namebuf;
- while (len)
+ old_namebuf = list->namebuf;
+ list->name_cap += len;
+ list->namebuf = (unsigned char *) xrealloc (list->namebuf, list->name_cap);
+
+ /* Fix up token text pointers. */
+ if (list->namebuf != old_namebuf)
{
- count = fwrite (buf, 1, len, fp);
- if (count == 0)
- goto error;
- len -= count;
- buf += count;
- }
- return;
+ unsigned int i;
- error:
- cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
+ for (i = 0; i < list->tokens_used; i++)
+ if (TOKEN_SPELL (&list->tokens[i]) == SPELL_STRING)
+ list->tokens[i].val.str.text += (list->namebuf - old_namebuf);
+ }
}
-/* Notify the compiler proper that the current line number has jumped,
- or the current file name has changed. */
-
-static void
-output_line_command (pfile, print, line)
- cpp_reader *pfile;
- cpp_printer *print;
- unsigned int line;
+/* If there is not enough room for LEN more characters, expand the
+ list by just enough to have room for LEN characters. */
+void
+_cpp_reserve_name_space (list, len)
+ cpp_toklist *list;
+ unsigned int len;
{
- cpp_buffer *ip = cpp_file_buffer (pfile);
- enum { same = 0, enter, leave, rname } change;
- static const char * const codes[] = { "", " 1", " 2", "" };
+ unsigned int room = list->name_cap - list->name_used;
- if (CPP_OPTION (pfile, no_line_commands))
- return;
+ if (room < len)
+ _cpp_expand_name_space (list, len - room);
+}
- /* Determine whether the current filename has changed, and if so,
- how. 'nominal_fname' values are unique, so they can be compared
- by comparing pointers. */
- if (ip->nominal_fname == print->last_fname)
- change = same;
- else
+/* Expand the number of tokens in a list. */
+void
+_cpp_expand_token_space (list, count)
+ cpp_toklist *list;
+ unsigned int count;
+{
+ list->tokens_cap += count;
+ list->tokens = (cpp_token *)
+ xrealloc (list->tokens, list->tokens_cap * sizeof (cpp_token));
+}
+
+/* Initialize a token list. If EMPTY is false, some token and name
+ space is provided. */
+void
+_cpp_init_toklist (list, empty)
+ cpp_toklist *list;
+ int empty;
+{
+ if (empty)
{
- if (pfile->buffer_stack_depth == print->last_bsd)
- change = rname;
- else
- {
- if (pfile->buffer_stack_depth > print->last_bsd)
- change = enter;
- else
- change = leave;
- print->last_bsd = pfile->buffer_stack_depth;
- }
- print->last_fname = ip->nominal_fname;
+ list->tokens_cap = 0;
+ list->tokens = 0;
+ list->name_cap = 0;
+ list->namebuf = 0;
}
- /* If the current file has not changed, we can output a few newlines
- instead if we want to increase the line number by a small amount.
- We cannot do this if print->lineno is zero, because that means we
- haven't output any line commands yet. (The very first line
- command output is a `same_file' command.) */
- if (change == same && print->lineno != 0
- && line >= print->lineno && line < print->lineno + 8)
+ else
{
- while (line > print->lineno)
- {
- putc ('\n', print->outf);
- print->lineno++;
- }
- return;
+ /* Initialize token space. */
+ list->tokens_cap = 256; /* 4K's worth. */
+ list->tokens = (cpp_token *)
+ xmalloc ((list->tokens_cap + 1) * sizeof (cpp_token));
+
+ /* Initialize name space. */
+ list->name_cap = 1024;
+ list->namebuf = (unsigned char *) xmalloc (list->name_cap);
}
-#ifndef NO_IMPLICIT_EXTERN_C
- if (CPP_OPTION (pfile, cplusplus))
- fprintf (print->outf, "# %u \"%s\"%s%s%s\n", line, ip->nominal_fname,
- codes[change],
- ip->system_header_p ? " 3" : "",
- (ip->system_header_p == 2) ? " 4" : "");
- else
-#endif
- fprintf (print->outf, "# %u \"%s\"%s%s\n", line, ip->nominal_fname,
- codes[change],
- ip->system_header_p ? " 3" : "");
- print->lineno = line;
+ _cpp_clear_toklist (list);
}
-/* Write the contents of the token_buffer to the output stream, and
- clear the token_buffer. Also handles generating line commands and
- keeping track of file transitions. */
-
+/* Clear a token list. */
void
-cpp_output_tokens (pfile, print)
- cpp_reader *pfile;
- cpp_printer *print;
+_cpp_clear_toklist (list)
+ cpp_toklist *list;
{
- cpp_buffer *ip;
+ list->tokens_used = 0;
+ list->name_used = 0;
+ list->directive = 0;
+ list->paramc = 0;
+ list->params_len = 0;
+ list->flags = 0;
+}
- if (CPP_WRITTEN (pfile) - print->written)
- {
- if (CPP_PWRITTEN (pfile)[-1] == '\n' && print->lineno)
- print->lineno++;
- safe_fwrite (pfile, pfile->token_buffer,
- CPP_WRITTEN (pfile) - print->written, print->outf);
- }
+/* Free a token list. Does not free the list itself, which may be
+ embedded in a larger structure. */
+void
+_cpp_free_toklist (list)
+ const cpp_toklist *list;
+{
+ free (list->tokens);
+ free (list->namebuf);
+}
- ip = cpp_file_buffer (pfile);
- if (ip)
- output_line_command (pfile, print, CPP_BUF_LINE (ip));
+/* Compare two tokens. */
+int
+_cpp_equiv_tokens (a, b)
+ const cpp_token *a, *b;
+{
+ if (a->type == b->type && a->flags == b->flags)
+ switch (TOKEN_SPELL (a))
+ {
+ default: /* Keep compiler happy. */
+ case SPELL_OPERATOR:
+ return 1;
+ case SPELL_CHAR:
+ case SPELL_NONE:
+ return a->val.aux == b->val.aux; /* arg_no or character. */
+ case SPELL_IDENT:
+ return a->val.node == b->val.node;
+ case SPELL_STRING:
+ return (a->val.str.len == b->val.str.len
+ && !memcmp (a->val.str.text, b->val.str.text,
+ a->val.str.len));
+ }
- CPP_SET_WRITTEN (pfile, print->written);
+ return 0;
}
-/* Helper for cpp_output_list - increases the column number to match
- what we expect it to be. */
-
-static void
-bump_column (print, from, to)
- cpp_printer *print;
- unsigned int from, to;
+/* Compare two token lists. */
+int
+_cpp_equiv_toklists (a, b)
+ const cpp_toklist *a, *b;
{
- unsigned int tabs, spcs;
- unsigned int delta = to - from;
+ unsigned int i;
- /* Only if FROM is 0, advance by tabs. */
- if (from == 0)
- tabs = delta / 8, spcs = delta % 8;
- else
- tabs = 0, spcs = delta;
+ if (a->tokens_used != b->tokens_used
+ || a->flags != b->flags
+ || a->paramc != b->paramc)
+ return 0;
- while (tabs--) putc ('\t', print->outf);
- while (spcs--) putc (' ', print->outf);
+ for (i = 0; i < a->tokens_used; i++)
+ if (! _cpp_equiv_tokens (&a->tokens[i], &b->tokens[i]))
+ return 0;
+ return 1;
}
-/* Write out the list L onto pfile->token_buffer. This function is
- incomplete:
+/* Utility routine:
- 1) pfile->token_buffer is not going to continue to exist.
- 2) At the moment, tokens don't carry the information described
- in cpplib.h; they are all strings.
- 3) The list has to be a complete line, and has to be written starting
- at the beginning of a line. */
+ Compares, the token TOKEN to the NUL-terminated string STRING.
+ TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
-void
-cpp_output_list (pfile, print, list)
- cpp_reader *pfile;
- cpp_printer *print;
- const cpp_toklist *list;
+int
+cpp_ideq (token, string)
+ const cpp_token *token;
+ const char *string;
{
- unsigned int i;
- unsigned int curcol = 1;
+ if (token->type != CPP_NAME)
+ return 0;
- /* XXX Probably does not do what is intended. */
- if (print->lineno != list->line)
- output_line_command (pfile, print, list->line);
-
- for (i = 0; i < list->tokens_used; i++)
+ return !ustrcmp (token->val.node->name, (const U_CHAR *)string);
+}
+
+/* Call when meeting a newline. Returns the character after the newline
+ (or carriage-return newline combination), or EOF. */
+static cppchar_t
+handle_newline (buffer, newline_char)
+ cpp_buffer *buffer;
+ cppchar_t newline_char;
+{
+ cppchar_t next = EOF;
+
+ buffer->col_adjust = 0;
+ buffer->lineno++;
+ buffer->line_base = buffer->cur;
+
+ /* Handle CR-LF and LF-CR combinations, get the next character. */
+ if (buffer->cur < buffer->rlimit)
{
- if (list->tokens[i].type == CPP_VSPACE)
- {
- output_line_command (pfile, print, list->tokens[i].aux);
- continue;
- }
-
- if (curcol < list->tokens[i].col)
+ next = *buffer->cur++;
+ if (next + newline_char == '\r' + '\n')
{
- /* Insert space to bring the column to what it should be. */
- bump_column (print, curcol - 1, list->tokens[i].col);
- curcol = list->tokens[i].col;
+ buffer->line_base = buffer->cur;
+ if (buffer->cur < buffer->rlimit)
+ next = *buffer->cur++;
+ else
+ next = EOF;
}
- /* XXX We may have to insert space to prevent an accidental
- token paste. */
- safe_fwrite (pfile, list->namebuf + list->tokens[i].val.name.offset,
- list->tokens[i].val.name.len, print->outf);
- curcol += list->tokens[i].val.name.len;
}
-}
-/* Scan a string (which may have escape marks), perform macro expansion,
- and write the result to the token_buffer. */
+ buffer->read_ahead = next;
+ return next;
+}
-void
-_cpp_expand_to_buffer (pfile, buf, length)
+/* Subroutine of skip_escaped_newlines; called when a trigraph is
+ encountered. It warns if necessary, and returns true if the
+ trigraph should be honoured. FROM_CHAR is the third character of a
+ trigraph, and presumed to be the previous character for position
+ reporting. */
+static int
+trigraph_ok (pfile, from_char)
cpp_reader *pfile;
- const U_CHAR *buf;
- int length;
+ cppchar_t from_char;
{
- cpp_buffer *ip;
- enum cpp_ttype token;
- U_CHAR *buf1;
-
- if (length < 0)
+ int accept = CPP_OPTION (pfile, trigraphs);
+
+ /* Don't warn about trigraphs in comments. */
+ if (CPP_OPTION (pfile, warn_trigraphs) && !pfile->state.lexing_comment)
{
- cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
- return;
+ cpp_buffer *buffer = pfile->buffer;
+ if (accept)
+ cpp_warning_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer) - 2,
+ "trigraph ??%c converted to %c",
+ (int) from_char,
+ (int) _cpp_trigraph_map[from_char]);
+ else
+ cpp_warning_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer) - 2,
+ "trigraph ??%c ignored", (int) from_char);
}
- /* Copy the buffer, because it might be in an unsafe place - for
- example, a sequence on the token_buffer, where the pointers will
- be invalidated if we enlarge the token_buffer. */
- buf1 = alloca (length);
- memcpy (buf1, buf, length);
+ return accept;
+}
- /* Set up the input on the input stack. */
- ip = cpp_push_buffer (pfile, buf1, length);
- if (ip == NULL)
- return;
- ip->has_escapes = 1;
+/* Assumes local variables buffer and result. */
+#define ACCEPT_CHAR(t) \
+ do { result->type = t; buffer->read_ahead = EOF; } while (0)
+
+/* When we move to multibyte character sets, add to these something
+ that saves and restores the state of the multibyte conversion
+ library. This probably involves saving and restoring a "cookie".
+ In the case of glibc it is an 8-byte structure, so is not a high
+ overhead operation. In any case, it's out of the fast path. */
+#define SAVE_STATE() do { saved_cur = buffer->cur; } while (0)
+#define RESTORE_STATE() do { buffer->cur = saved_cur; } while (0)
+
+/* Skips any escaped newlines introduced by NEXT, which is either a
+ '?' or a '\\'. Returns the next character, which will also have
+ been placed in buffer->read_ahead. */
+static cppchar_t
+skip_escaped_newlines (buffer, next)
+ cpp_buffer *buffer;
+ cppchar_t next;
+{
+ cppchar_t next1;
+ const unsigned char *saved_cur;
+ int space;
- /* Scan the input, create the output. */
- for (;;)
+ do
{
- token = cpp_get_token (pfile);
- if (token == CPP_EOF)
+ if (buffer->cur == buffer->rlimit)
break;
- if (token == CPP_POP && CPP_BUFFER (pfile) == ip)
+
+ SAVE_STATE ();
+ if (next == '?')
{
- cpp_pop_buffer (pfile);
- break;
+ next1 = *buffer->cur++;
+ if (next1 != '?' || buffer->cur == buffer->rlimit)
+ {
+ RESTORE_STATE ();
+ break;
+ }
+
+ next1 = *buffer->cur++;
+ if (!_cpp_trigraph_map[next1] || !trigraph_ok (buffer->pfile, next1))
+ {
+ RESTORE_STATE ();
+ break;
+ }
+
+ /* We have a full trigraph here. */
+ next = _cpp_trigraph_map[next1];
+ if (next != '\\' || buffer->cur == buffer->rlimit)
+ break;
+ SAVE_STATE ();
}
- }
-}
-/* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output.
- Then pop the buffer. */
+ /* We have a backslash, and room for at least one more character. */
+ space = 0;
+ do
+ {
+ next1 = *buffer->cur++;
+ if (!is_nvspace (next1))
+ break;
+ space = 1;
+ }
+ while (buffer->cur < buffer->rlimit);
-void
-cpp_scan_buffer_nooutput (pfile)
- cpp_reader *pfile;
-{
- cpp_buffer *buffer = CPP_BUFFER (pfile);
- enum cpp_ttype token;
- unsigned int old_written = CPP_WRITTEN (pfile);
- /* In no-output mode, we can ignore everything but directives. */
- for (;;)
- {
- if (! pfile->only_seen_white)
- _cpp_skip_rest_of_line (pfile);
- token = cpp_get_token (pfile);
- if (token == CPP_EOF)
- break;
- if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
+ if (!is_vspace (next1))
{
- cpp_pop_buffer (pfile);
+ RESTORE_STATE ();
break;
}
+
+ if (space)
+ cpp_warning (buffer->pfile,
+ "backslash and newline separated by space");
+
+ next = handle_newline (buffer, next1);
+ if (next == EOF)
+ cpp_pedwarn (buffer->pfile, "backslash-newline at end of file");
}
- CPP_SET_WRITTEN (pfile, old_written);
+ while (next == '\\' || next == '?');
+
+ buffer->read_ahead = next;
+ return next;
}
-/* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT.
- Then pop the buffer. */
+/* Obtain the next character, after trigraph conversion and skipping
+ an arbitrary string of escaped newlines. The common case of no
+ trigraphs or escaped newlines falls through quickly. */
+static cppchar_t
+get_effective_char (buffer)
+ cpp_buffer *buffer;
+{
+ cppchar_t next = EOF;
-void
-cpp_scan_buffer (pfile, print)
+ if (buffer->cur < buffer->rlimit)
+ {
+ next = *buffer->cur++;
+
+ /* '?' can introduce trigraphs (and therefore backslash); '\\'
+ can introduce escaped newlines, which we want to skip, or
+ UCNs, which, depending upon lexer state, we will handle in
+ the future. */
+ if (next == '?' || next == '\\')
+ next = skip_escaped_newlines (buffer, next);
+ }
+
+ buffer->read_ahead = next;
+ return next;
+}
+
+/* Skip a C-style block comment. We find the end of the comment by
+ seeing if an asterisk is before every '/' we encounter. Returns
+ non-zero if comment terminated by EOF, zero otherwise. */
+static int
+skip_block_comment (pfile)
cpp_reader *pfile;
- cpp_printer *print;
{
- cpp_buffer *buffer = CPP_BUFFER (pfile);
- enum cpp_ttype token;
+ cpp_buffer *buffer = pfile->buffer;
+ cppchar_t c = EOF, prevc = EOF;
- for (;;)
+ pfile->state.lexing_comment = 1;
+ while (buffer->cur != buffer->rlimit)
{
- token = cpp_get_token (pfile);
- if ((token == CPP_POP && !CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
- || token == CPP_EOF || token == CPP_VSPACE
- /* XXX Temporary kluge - force flush after #include only */
- || (token == CPP_DIRECTIVE
- && CPP_BUFFER (pfile)->nominal_fname != print->last_fname))
- {
- cpp_output_tokens (pfile, print);
- if (token == CPP_EOF)
- return;
- if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
+ prevc = c, c = *buffer->cur++;
+
+ next_char:
+ /* FIXME: For speed, create a new character class of characters
+ of no interest inside block comments. */
+ if (c == '?' || c == '\\')
+ c = skip_escaped_newlines (buffer, c);
+
+ /* People like decorating comments with '*', so check for '/'
+ instead for efficiency. */
+ if (c == '/')
+ {
+ if (prevc == '*')
+ break;
+
+ /* Warn about potential nested comments, but not if the '/'
+ comes immediately before the true comment delimeter.
+ Don't bother to get it right across escaped newlines. */
+ if (CPP_OPTION (pfile, warn_comments)
+ && buffer->cur != buffer->rlimit)
{
- cpp_pop_buffer (pfile);
- return;
+ prevc = c, c = *buffer->cur++;
+ if (c == '*' && buffer->cur != buffer->rlimit)
+ {
+ prevc = c, c = *buffer->cur++;
+ if (c != '/')
+ cpp_warning_with_line (pfile, CPP_BUF_LINE (buffer),
+ CPP_BUF_COL (buffer),
+ "\"/*\" within comment");
+ }
+ goto next_char;
}
}
+ else if (is_vspace (c))
+ {
+ prevc = c, c = handle_newline (buffer, c);
+ goto next_char;
+ }
+ else if (c == '\t')
+ adjust_column (pfile);
}
-}
-/* Return the topmost cpp_buffer that corresponds to a file (not a macro). */
+ pfile->state.lexing_comment = 0;
+ buffer->read_ahead = EOF;
+ return c != '/' || prevc != '*';
+}
-cpp_buffer *
-cpp_file_buffer (pfile)
+/* Skip a C++ line comment. Handles escaped newlines. Returns
+ non-zero if a multiline comment. The following new line, if any,
+ is left in buffer->read_ahead. */
+static int
+skip_line_comment (pfile)
cpp_reader *pfile;
{
- cpp_buffer *ip;
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int orig_lineno = buffer->lineno;
+ cppchar_t c;
- for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
- if (ip->ihash != NULL)
- return ip;
- return NULL;
-}
+ pfile->state.lexing_comment = 1;
+ do
+ {
+ c = EOF;
+ if (buffer->cur == buffer->rlimit)
+ break;
-/* Token-buffer helper functions. */
+ c = *buffer->cur++;
+ if (c == '?' || c == '\\')
+ c = skip_escaped_newlines (buffer, c);
+ }
+ while (!is_vspace (c));
-/* Expand a token list's string space. */
-static void
-expand_name_space (list, len)
- cpp_toklist *list;
- unsigned int len;
-{
- list->name_cap += len;
- list->namebuf = (unsigned char *) xrealloc (list->namebuf, list->name_cap);
+ pfile->state.lexing_comment = 0;
+ buffer->read_ahead = c; /* Leave any newline for caller. */
+ return orig_lineno != buffer->lineno;
}
-/* Expand the number of tokens in a list. */
+/* pfile->buffer->cur is one beyond the \t character. Update
+ col_adjust so we track the column correctly. */
static void
-expand_token_space (list)
- cpp_toklist *list;
+adjust_column (pfile)
+ cpp_reader *pfile;
{
- list->tokens_cap *= 2;
- list->tokens = (cpp_token *)
- xrealloc (list->tokens - 1, (list->tokens_cap + 1) * sizeof (cpp_token));
- list->tokens++; /* Skip the dummy. */
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int col = CPP_BUF_COL (buffer) - 1; /* Zero-based column. */
+
+ /* Round it up to multiple of the tabstop, but subtract 1 since the
+ tab itself occupies a character position. */
+ buffer->col_adjust += (CPP_OPTION (pfile, tabstop)
+ - col % CPP_OPTION (pfile, tabstop)) - 1;
}
-/* Initialize a token list. We allocate an extra token in front of
- the token list, as this allows us to always peek at the previous
- token without worrying about underflowing the list. */
+/* Skips whitespace, saving the next non-whitespace character.
+ Adjusts pfile->col_adjust to account for tabs. Without this,
+ tokens might be assigned an incorrect column. */
static void
-init_token_list (pfile, list, recycle)
+skip_whitespace (pfile, c)
cpp_reader *pfile;
- cpp_toklist *list;
- int recycle;
+ cppchar_t c;
{
- /* Recycling a used list saves 3 free-malloc pairs. */
- if (!recycle)
- {
- /* Initialize token space. Put a dummy token before the start
- that will fail matches. */
- list->tokens_cap = 256; /* 4K's worth. */
- list->tokens = (cpp_token *)
- xmalloc ((list->tokens_cap + 1) * sizeof (cpp_token));
- list->tokens[0].type = CPP_EOF;
- list->tokens++;
-
- /* Initialize name space. */
- list->name_cap = 1024;
- list->namebuf = (unsigned char *) xmalloc (list->name_cap);
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int warned = 0;
- /* Only create a comment space on demand. */
- list->comments_cap = 0;
- list->comments = 0;
+ do
+ {
+ /* Horizontal space always OK. */
+ if (c == ' ')
+ ;
+ else if (c == '\t')
+ adjust_column (pfile);
+ /* Just \f \v or \0 left. */
+ else if (c == '\0')
+ {
+ if (!warned)
+ {
+ cpp_warning (pfile, "null character(s) ignored");
+ warned = 1;
+ }
+ }
+ else if (IN_DIRECTIVE (pfile) && CPP_PEDANTIC (pfile))
+ cpp_pedwarn_with_line (pfile, CPP_BUF_LINE (buffer),
+ CPP_BUF_COL (buffer),
+ "%s in preprocessing directive",
+ c == '\f' ? "form feed" : "vertical tab");
+
+ c = EOF;
+ if (buffer->cur == buffer->rlimit)
+ break;
+ c = *buffer->cur++;
}
+ /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
+ while (is_nvspace (c));
- list->tokens_used = 0;
- list->name_used = 0;
- list->comments_used = 0;
- if (pfile->buffer)
- list->line = pfile->buffer->lineno;
- list->dir_handler = 0;
- list->dir_flags = 0;
+ /* Remember the next character. */
+ buffer->read_ahead = c;
}
-/* Scan an entire line and create a token list for it. Does not
- macro-expand or execute directives. */
+/* Parse an identifier, skipping embedded backslash-newlines.
+ Calculate the hash value of the token while parsing, for improved
+ performance. The hashing algorithm *must* match cpp_lookup(). */
-void
-_cpp_scan_line (pfile, list)
+static cpp_hashnode *
+parse_identifier (pfile, c)
cpp_reader *pfile;
- cpp_toklist *list;
+ cppchar_t c;
{
- int i, col;
- long written, len;
- enum cpp_ttype type;
- int space_before;
-
- init_token_list (pfile, list, 1);
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int r = 0, saw_dollar = 0;
+ unsigned int orig_used = pfile->token_list.name_used;
- written = CPP_WRITTEN (pfile);
- i = 0;
- space_before = 0;
- for (;;)
+ do
{
- col = CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base;
- type = _cpp_lex_token (pfile);
- len = CPP_WRITTEN (pfile) - written;
- CPP_SET_WRITTEN (pfile, written);
- if (type == CPP_HSPACE)
- {
- if (CPP_PEDANTIC (pfile))
- pedantic_whitespace (pfile, pfile->token_buffer + written, len);
- space_before = 1;
- continue;
- }
- else if (type == CPP_COMMENT)
- /* Only happens when processing -traditional macro definitions.
- Do not give this a token entry, but do not change space_before
- either. */
- continue;
+ do
+ {
+ if (pfile->token_list.name_used == pfile->token_list.name_cap)
+ _cpp_expand_name_space (&pfile->token_list,
+ pfile->token_list.name_used + 256);
+ pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
+ r = HASHSTEP (r, c);
- if (list->tokens_used >= list->tokens_cap)
- expand_token_space (list);
- if (list->name_used + len >= list->name_cap)
- auto_expand_name_space (list);
+ if (c == '$')
+ saw_dollar++;
- if (type == CPP_MACRO)
- type = CPP_NAME;
+ c = EOF;
+ if (buffer->cur == buffer->rlimit)
+ break;
- list->tokens_used++;
- list->tokens[i].type = type;
- list->tokens[i].col = col;
- list->tokens[i].flags = space_before ? PREV_WHITESPACE : 0;
-
- if (type == CPP_VSPACE)
- break;
+ c = *buffer->cur++;
+ }
+ while (is_idchar (c));
- list->tokens[i].val.name.len = len;
- list->tokens[i].val.name.offset = list->name_used;
- memcpy (list->namebuf + list->name_used, CPP_PWRITTEN (pfile), len);
- list->name_used += len;
- i++;
- space_before = 0;
+ /* Potential escaped newline? */
+ if (c != '?' && c != '\\')
+ break;
+ c = skip_escaped_newlines (buffer, c);
}
- list->tokens[i].aux = CPP_BUFFER (pfile)->lineno + 1;
-
- /* XXX Temporary kluge: put back the newline. */
- FORWARD(-1);
+ while (is_idchar (c));
+
+ /* $ is not a identifier character in the standard, but is commonly
+ accepted as an extension. Don't warn about it in skipped
+ conditional blocks. */
+ if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->skipping)
+ cpp_pedwarn (pfile, "'$' character(s) in identifier");
+
+ /* Remember the next character. */
+ buffer->read_ahead = c;
+ return _cpp_lookup_with_hash (pfile, &pfile->token_list.namebuf[orig_used],
+ pfile->token_list.name_used - orig_used, r);
}
-
-/* Skip a C-style block comment. We know it's a comment, and point is
- at the second character of the starter. */
+/* Parse a number, skipping embedded backslash-newlines. */
static void
-skip_block_comment (pfile)
+parse_number (pfile, number, c)
cpp_reader *pfile;
+ cpp_string *number;
+ cppchar_t c;
{
- unsigned int line, col;
- const U_CHAR *limit, *cur;
+ cppchar_t prevc;
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int orig_used = pfile->token_list.name_used;
- FORWARD(1);
- line = CPP_BUF_LINE (CPP_BUFFER (pfile));
- col = CPP_BUF_COL (CPP_BUFFER (pfile));
- limit = CPP_BUFFER (pfile)->rlimit;
- cur = CPP_BUFFER (pfile)->cur;
+ /* Reserve space for a leading period. */
+ if (pfile->state.seen_dot)
+ pfile->token_list.name_used++;
- while (cur < limit)
+ do
{
- char c = *cur++;
- if (c == '\n' || c == '\r')
+ do
{
- /* \r cannot be a macro escape marker here. */
- if (!ACTIVE_MARK_P (pfile))
- CPP_BUMP_LINE_CUR (pfile, cur);
- }
- else if (c == '*')
- {
- /* Check for teminator. */
- if (cur < limit && *cur == '/')
- goto out;
+ if (pfile->token_list.name_used >= pfile->token_list.name_cap)
+ _cpp_expand_name_space (&pfile->token_list,
+ pfile->token_list.name_used + 256);
+ pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
+
+ prevc = c;
+ c = EOF;
+ if (buffer->cur == buffer->rlimit)
+ break;
- /* Warn about comment starter embedded in comment. */
- if (cur[-2] == '/' && CPP_OPTION (pfile, warn_comments))
- cpp_warning_with_line (pfile, CPP_BUFFER (pfile)->lineno,
- cur - CPP_BUFFER (pfile)->line_base,
- "'/*' within comment");
+ c = *buffer->cur++;
}
+ while (is_numchar (c) || c == '.' || VALID_SIGN (c, prevc));
+
+ /* Potential escaped newline? */
+ if (c != '?' && c != '\\')
+ break;
+ c = skip_escaped_newlines (buffer, c);
}
+ while (is_numchar (c) || c == '.' || VALID_SIGN (c, prevc));
- cpp_error_with_line (pfile, line, col, "unterminated comment");
- cur--;
- out:
- CPP_BUFFER (pfile)->cur = cur + 1;
+ /* Put any leading period in place, now we have the room. */
+ if (pfile->state.seen_dot)
+ pfile->token_list.namebuf[orig_used] = '.';
+
+ /* Remember the next character. */
+ buffer->read_ahead = c;
+
+ number->text = &pfile->token_list.namebuf[orig_used];
+ number->len = pfile->token_list.name_used - orig_used;
}
-/* Skip a C++/Chill line comment. We know it's a comment, and point
- is at the second character of the initiator. */
+/* Subroutine of parse_string. Emits error for unterminated strings. */
static void
-skip_line_comment (pfile)
+unterminated (pfile, line, term)
cpp_reader *pfile;
+ unsigned int line;
+ int term;
{
- FORWARD(1);
- for (;;)
- {
- int c = GETC ();
+ cpp_error (pfile, "missing terminating %c character", term);
- /* We don't have to worry about EOF in here. */
- if (c == '\n')
- {
- /* Don't consider final '\n' to be part of comment. */
- FORWARD(-1);
- return;
- }
- else if (c == '\r')
- {
- /* \r cannot be a macro escape marker here. */
- if (!ACTIVE_MARK_P (pfile))
- CPP_BUMP_LINE (pfile);
- if (CPP_OPTION (pfile, warn_comments))
- cpp_warning (pfile, "backslash-newline within line comment");
- }
+ if (term == '\"' && pfile->mls_line && pfile->mls_line != line)
+ {
+ cpp_error_with_line (pfile, pfile->mls_line, pfile->mls_column,
+ "possible start of unterminated string literal");
+ pfile->mls_line = 0;
}
}
-/* Skip a comment - C, C++, or Chill style. M is the first character
- of the comment marker. If this really is a comment, skip to its
- end and return ' '. If this is not a comment, return M (which will
- be '/' or '-'). */
+/* Parses a string, character constant, or angle-bracketed header file
+ name. Handles embedded trigraphs and escaped newlines.
-static int
-skip_comment (pfile, m)
+ Multi-line strings are allowed, but they are deprecated within
+ directives. */
+static void
+parse_string (pfile, token, terminator)
cpp_reader *pfile;
- int m;
+ cpp_token *token;
+ cppchar_t terminator;
{
- if (m == '/' && PEEKC() == '*')
- {
- skip_block_comment (pfile);
- return ' ';
- }
- else if (m == '/' && PEEKC() == '/')
+ cpp_buffer *buffer = pfile->buffer;
+ unsigned int orig_used = pfile->token_list.name_used;
+ cppchar_t c;
+ unsigned int nulls = 0;
+
+ for (;;)
{
- if (CPP_BUFFER (pfile)->system_header_p)
- {
- /* We silently allow C++ comments in system headers, irrespective
- of conformance mode, because lots of busted systems do that
- and trying to clean it up in fixincludes is a nightmare. */
- skip_line_comment (pfile);
- return ' ';
- }
- else if (CPP_OPTION (pfile, cplusplus_comments))
+ if (buffer->cur == buffer->rlimit)
{
- if (! CPP_BUFFER (pfile)->warned_cplusplus_comments)
- {
- if (CPP_WTRADITIONAL (pfile))
- cpp_pedwarn (pfile,
- "C++ style comments are not allowed in traditional C");
- else if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile,
- "C++ style comments are not allowed in ISO C89");
- if (CPP_WTRADITIONAL (pfile)
- || (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)))
- cpp_pedwarn (pfile,
- "(this will be reported only once per input file)");
- CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
- }
- skip_line_comment (pfile);
- return ' ';
+ c = EOF;
+ unterminated (pfile, token->line, terminator);
+ break;
}
- else
- return m;
- }
- else if (m == '-' && PEEKC() == '-'
- && CPP_OPTION (pfile, chill))
- {
- skip_line_comment (pfile);
- return ' ';
- }
- else
- return m;
-}
-
-/* Identical to skip_comment except that it copies the comment into the
- token_buffer. This is used if !discard_comments. */
-static int
-copy_comment (pfile, m)
- cpp_reader *pfile;
- int m;
-{
- const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
- const U_CHAR *limit;
-
- if (skip_comment (pfile, m) == m)
- return m;
+ c = *buffer->cur++;
- limit = CPP_BUFFER (pfile)->cur;
- CPP_RESERVE (pfile, limit - start + 2);
- CPP_PUTC_Q (pfile, m);
- for (; start <= limit; start++)
- if (*start != '\r')
- CPP_PUTC_Q (pfile, *start);
+ have_char:
+ /* Handle trigraphs, escaped newlines etc. */
+ if (c == '?' || c == '\\')
+ c = skip_escaped_newlines (buffer, c);
- return ' ';
-}
-
-static void
-null_warning (pfile, count)
- cpp_reader *pfile;
- unsigned int count;
-{
- if (count == 1)
- cpp_warning (pfile, "embedded null character ignored");
- else
- cpp_warning (pfile, "embedded null characters ignored");
-}
-
-/* Skip whitespace \-newline and comments. Does not macro-expand. */
+ if (c == terminator)
+ {
+ unsigned int u = pfile->token_list.name_used;
-void
-_cpp_skip_hspace (pfile)
- cpp_reader *pfile;
-{
- unsigned int null_count = 0;
- int c;
+ /* An odd number of consecutive backslashes represents an
+ escaped terminator. */
+ while (u > orig_used && pfile->token_list.namebuf[u - 1] == '\\')
+ u--;
- while (1)
- {
- c = GETC();
- if (c == EOF)
- goto out;
- else if (is_hspace(c))
- {
- if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "%s in preprocessing directive",
- c == '\f' ? "formfeed" : "vertical tab");
- else if (c == '\0')
- null_count++;
- }
- else if (c == '\r')
- {
- /* \r is a backslash-newline marker if !has_escapes, and
- a deletable-whitespace or no-reexpansion marker otherwise. */
- if (CPP_BUFFER (pfile)->has_escapes)
+ if ((pfile->token_list.name_used - u) % 2 == 0)
{
- if (PEEKC() == ' ')
- FORWARD(1);
- else
- break;
+ c = EOF;
+ break;
}
- else
- CPP_BUMP_LINE (pfile);
}
- else if (c == '/' || c == '-')
+ else if (is_vspace (c))
{
- c = skip_comment (pfile, c);
- if (c != ' ')
+ /* In assembly language, silently terminate string and
+ character literals at end of line. This is a kludge
+ around not knowing where comments are. */
+ if (CPP_OPTION (pfile, lang_asm) && terminator != '>')
break;
- }
- else
- break;
- }
- FORWARD(-1);
- out:
- if (null_count)
- null_warning (pfile, null_count);
-}
-/* Read and discard the rest of the current line. */
+ /* Character constants and header names may not extend over
+ multiple lines. In Standard C, neither may strings.
+ Unfortunately, we accept multiline strings as an
+ extension. (Deprecatedly even in directives - otherwise,
+ glibc's longlong.h breaks.) */
+ if (terminator != '"')
+ {
+ unterminated (pfile, token->line, terminator);
+ break;
+ }
-void
-_cpp_skip_rest_of_line (pfile)
- cpp_reader *pfile;
-{
- for (;;)
- {
- int c = GETC();
- switch (c)
+ if (pfile->mls_line == 0)
+ {
+ pfile->mls_line = token->line;
+ pfile->mls_column = token->col;
+ if (CPP_PEDANTIC (pfile))
+ cpp_pedwarn (pfile, "multi-line string constant");
+ }
+
+ handle_newline (buffer, c); /* Stores to read_ahead. */
+ c = '\n';
+ }
+ else if (c == '\0')
{
- case '\n':
- FORWARD(-1);
- case EOF:
- return;
+ if (nulls++ == 0)
+ cpp_warning (pfile, "null character(s) preserved in literal");
+ }
- case '\r':
- if (! CPP_BUFFER (pfile)->has_escapes)
- CPP_BUMP_LINE (pfile);
- break;
-
- case '\'':
- case '\"':
- skip_string (pfile, c);
- break;
+ if (pfile->token_list.name_used == pfile->token_list.name_cap)
+ _cpp_expand_name_space (&pfile->token_list,
+ pfile->token_list.name_used + 256);
- case '/':
- case '-':
- skip_comment (pfile, c);
- break;
+ pfile->token_list.namebuf[pfile->token_list.name_used++] = c;
+ /* If we had a new line, the next character is in read_ahead. */
+ if (c != '\n')
+ continue;
+ c = buffer->read_ahead;
+ if (c != EOF)
+ goto have_char;
+ }
- case '\f':
- case '\v':
- if (CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "%s in preprocessing directive",
- c == '\f' ? "formfeed" : "vertical tab");
- break;
+ buffer->read_ahead = c;
- }
- }
+ token->val.str.text = &pfile->token_list.namebuf[orig_used];
+ token->val.str.len = pfile->token_list.name_used - orig_used;
}
-/* Parse an identifier starting with C. */
-
-void
-_cpp_parse_name (pfile, c)
+/* For output routine simplicity, the stored comment includes the
+ comment start and any terminator. */
+static void
+save_comment (pfile, token, from)
cpp_reader *pfile;
- int c;
+ cpp_token *token;
+ const unsigned char *from;
{
- for (;;)
- {
- if (! is_idchar(c))
- {
- FORWARD (-1);
- break;
- }
-
- if (c == '$' && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "`$' in identifier");
+ unsigned char *buffer;
+ unsigned int len;
+ cpp_toklist *list = &pfile->token_list;
+
+ len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
+ _cpp_reserve_name_space (list, len);
+ buffer = list->namebuf + list->name_used;
+ list->name_used += len;
+
+ token->type = CPP_COMMENT;
+ token->val.str.len = len;
+ token->val.str.text = buffer;
- CPP_RESERVE(pfile, 2); /* One more for final NUL. */
- CPP_PUTC_Q (pfile, c);
- c = GETC();
- if (c == EOF)
- break;
- }
- return;
+ buffer[0] = '/';
+ memcpy (buffer + 1, from, len - 1);
}
-/* Parse and skip over a string starting with C. A single quoted
- string is treated like a double -- some programs (e.g., troff) are
- perverse this way. (However, a single quoted string is not allowed
- to extend over multiple lines.) */
+/* Subroutine of lex_token to handle '%'. A little tricky, since we
+ want to avoid stepping back when lexing %:%X. */
static void
-skip_string (pfile, c)
- cpp_reader *pfile;
- int c;
+lex_percent (buffer, result)
+ cpp_buffer *buffer;
+ cpp_token *result;
{
- unsigned int start_line, start_column;
- unsigned int null_count = 0;
+ cppchar_t c;
- start_line = CPP_BUF_LINE (CPP_BUFFER (pfile));
- start_column = CPP_BUF_COL (CPP_BUFFER (pfile));
- while (1)
+ result->type = CPP_MOD;
+ /* Parsing %:%X could leave an extra character. */
+ if (buffer->extra_char == EOF)
+ c = get_effective_char (buffer);
+ else
{
- int cc = GETC();
- switch (cc)
- {
- case EOF:
- cpp_error_with_line (pfile, start_line, start_column,
- "unterminated string or character constant");
- if (pfile->multiline_string_line != start_line
- && pfile->multiline_string_line != 0)
- cpp_error_with_line (pfile,
- pfile->multiline_string_line, -1,
- "possible real start of unterminated constant");
- pfile->multiline_string_line = 0;
- goto out;
-
- case '\0':
- null_count++;
- break;
-
- case '\n':
- CPP_BUMP_LINE (pfile);
- /* In Fortran and assembly language, silently terminate
- strings of either variety at end of line. This is a
- kludge around not knowing where comments are in these
- languages. */
- if (CPP_OPTION (pfile, lang_fortran)
- || CPP_OPTION (pfile, lang_asm))
- {
- FORWARD(-1);
- goto out;
- }
- /* Character constants may not extend over multiple lines.
- In Standard C, neither may strings. We accept multiline
- strings as an extension. */
- if (c == '\'')
- {
- cpp_error_with_line (pfile, start_line, start_column,
- "unterminated character constant");
- FORWARD(-1);
- goto out;
- }
- if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
- cpp_pedwarn_with_line (pfile, start_line, start_column,
- "string constant runs past end of line");
- if (pfile->multiline_string_line == 0)
- pfile->multiline_string_line = start_line;
- break;
+ c = buffer->read_ahead = buffer->extra_char;
+ buffer->extra_char = EOF;
+ }
- case '\r':
- if (CPP_BUFFER (pfile)->has_escapes)
+ if (c == '=')
+ ACCEPT_CHAR (CPP_MOD_EQ);
+ else if (CPP_OPTION (buffer->pfile, digraphs))
+ {
+ if (c == ':')
+ {
+ result->flags |= DIGRAPH;
+ ACCEPT_CHAR (CPP_HASH);
+ if (get_effective_char (buffer) == '%')
{
- cpp_ice (pfile, "\\r escape inside string constant");
- FORWARD(1);
+ buffer->extra_char = get_effective_char (buffer);
+ if (buffer->extra_char == ':')
+ {
+ buffer->extra_char = EOF;
+ ACCEPT_CHAR (CPP_PASTE);
+ }
+ else
+ /* We'll catch the extra_char when we're called back. */
+ buffer->read_ahead = '%';
}
- else
- /* Backslash newline is replaced by nothing at all. */
- CPP_BUMP_LINE (pfile);
- break;
-
- case '\\':
- FORWARD(1);
- break;
-
- case '\"':
- case '\'':
- if (cc == c)
- goto out;
- break;
+ }
+ else if (c == '>')
+ {
+ result->flags |= DIGRAPH;
+ ACCEPT_CHAR (CPP_CLOSE_BRACE);
}
}
-
- out:
- if (null_count == 1)
- cpp_warning (pfile, "null character in string or character constant");
- else if (null_count > 1)
- cpp_warning (pfile, "null characters in string or character constant");
}
-/* Parse a string and copy it to the output. */
-
+/* Subroutine of lex_token to handle '.'. This is tricky, since we
+ want to avoid stepping back when lexing '...' or '.123'. In the
+ latter case we should also set a flag for parse_number. */
static void
-parse_string (pfile, c)
+lex_dot (pfile, result)
cpp_reader *pfile;
- int c;
+ cpp_token *result;
{
- const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
- const U_CHAR *limit;
-
- skip_string (pfile, c);
-
- limit = CPP_BUFFER (pfile)->cur;
- CPP_RESERVE (pfile, limit - start + 2);
- CPP_PUTC_Q (pfile, c);
- for (; start < limit; start++)
- if (*start != '\r')
- CPP_PUTC_Q (pfile, *start);
-}
+ cpp_buffer *buffer = pfile->buffer;
+ cppchar_t c;
-/* Read an assertion into the token buffer, converting to
- canonical form: `#predicate(a n swe r)' The next non-whitespace
- character to read should be the first letter of the predicate.
- Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
- with answer (see callers for why). In case of 0, an error has been
- printed. */
-int
-_cpp_parse_assertion (pfile)
- cpp_reader *pfile;
-{
- int c, dropwhite;
- _cpp_skip_hspace (pfile);
- c = PEEKC();
- if (c == '\n')
- {
- cpp_error (pfile, "assertion without predicate");
- return 0;
- }
- else if (! is_idstart(c))
+ /* Parsing ..X could leave an extra character. */
+ if (buffer->extra_char == EOF)
+ c = get_effective_char (buffer);
+ else
{
- cpp_error (pfile, "assertion predicate is not an identifier");
- return 0;
+ c = buffer->read_ahead = buffer->extra_char;
+ buffer->extra_char = EOF;
}
- CPP_PUTC(pfile, '#');
- FORWARD(1);
- _cpp_parse_name (pfile, c);
- c = PEEKC();
- if (c != '(')
+ /* All known character sets have 0...9 contiguous. */
+ if (c >= '0' && c <= '9')
{
- if (is_hspace(c) || c == '\r')
- _cpp_skip_hspace (pfile);
- c = PEEKC();
+ result->type = CPP_NUMBER;
+ buffer->pfile->state.seen_dot = 1;
+ parse_number (pfile, &result->val.str, c);
+ buffer->pfile->state.seen_dot = 0;
}
- if (c != '(')
- return 1;
-
- CPP_PUTC(pfile, '(');
- FORWARD(1);
- dropwhite = 1;
- while ((c = GETC()) != ')')
+ else
{
- if (is_space(c))
+ result->type = CPP_DOT;
+ if (c == '.')
{
- if (! dropwhite)
+ buffer->extra_char = get_effective_char (buffer);
+ if (buffer->extra_char == '.')
{
- CPP_PUTC(pfile, ' ');
- dropwhite = 1;
+ buffer->extra_char = EOF;
+ ACCEPT_CHAR (CPP_ELLIPSIS);
}
+ else
+ /* We'll catch the extra_char when we're called back. */
+ buffer->read_ahead = '.';
}
- else if (c == '\n' || c == EOF)
- {
- if (c == '\n') FORWARD(-1);
- cpp_error (pfile, "un-terminated assertion answer");
- return 0;
- }
- else if (c == '\r')
- /* \r cannot be a macro escape here. */
- CPP_BUMP_LINE (pfile);
- else
- {
- CPP_PUTC (pfile, c);
- dropwhite = 0;
- }
+ else if (c == '*' && CPP_OPTION (pfile, cplusplus))
+ ACCEPT_CHAR (CPP_DOT_STAR);
}
-
- if (pfile->limit[-1] == ' ')
- pfile->limit[-1] = ')';
- else if (pfile->limit[-1] == '(')
- {
- cpp_error (pfile, "empty token sequence in assertion");
- return 0;
- }
- else
- CPP_PUTC (pfile, ')');
-
- return 2;
}
-/* Get the next token, and add it to the text in pfile->token_buffer.
- Return the kind of token we got. */
-
-enum cpp_ttype
-_cpp_lex_token (pfile)
+static void
+lex_token (pfile, result)
cpp_reader *pfile;
+ cpp_token *result;
{
- register int c, c2;
- enum cpp_ttype token;
+ cppchar_t c;
+ cpp_buffer *buffer = pfile->buffer;
+ const unsigned char *comment_start;
- if (CPP_BUFFER (pfile) == NULL)
- return CPP_EOF;
+ result->flags = 0;
+ next_char:
+ result->line = CPP_BUF_LINE (buffer);
+ next_char2:
+ result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
+
+ c = buffer->read_ahead;
+ if (c == EOF && buffer->cur < buffer->rlimit)
+ {
+ c = *buffer->cur++;
+ result->col++;
+ }
- get_next:
- c = GETC();
+ do_switch:
+ buffer->read_ahead = EOF;
switch (c)
{
case EOF:
- return CPP_EOF;
+ /* Non-empty files should end in a newline. Testing
+ skip_newlines ensures we only emit the warning once. */
+ if (buffer->cur != buffer->line_base && buffer->cur != buffer->buf
+ && pfile->state.skip_newlines)
+ cpp_pedwarn_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer),
+ "no newline at end of file");
+ result->type = CPP_EOF;
+ break;
+
+ case ' ': case '\t': case '\f': case '\v': case '\0':
+ skip_whitespace (pfile, c);
+ result->flags |= PREV_WHITE;
+ goto next_char2;
+
+ case '\n': case '\r':
+ result->type = CPP_EOF;
+ handle_newline (buffer, c);
+ /* Handling here will change significantly when moving to
+ token-at-a-time. */
+ if (pfile->state.skip_newlines)
+ {
+ result->flags &= ~PREV_WHITE; /* Clear any whitespace flag. */
+ goto next_char;
+ }
+ break;
- case '/':
- if (PEEKC () == '=')
- goto op2;
+ case '?':
+ case '\\':
+ /* These could start an escaped newline, or '?' a trigraph. Let
+ skip_escaped_newlines do all the work. */
+ {
+ unsigned int lineno = buffer->lineno;
+
+ c = skip_escaped_newlines (buffer, c);
+ if (lineno != buffer->lineno)
+ /* We had at least one escaped newline of some sort, and the
+ next character is in buffer->read_ahead. Update the
+ token's line and column. */
+ goto next_char;
+
+ /* We are either the original '?' or '\\', or a trigraph. */
+ result->type = CPP_QUERY;
+ buffer->read_ahead = EOF;
+ if (c == '\\')
+ result->type = CPP_BACKSLASH;
+ else if (c != '?')
+ goto do_switch;
+ }
+ break;
- comment:
- if (CPP_OPTION (pfile, discard_comments))
- c = skip_comment (pfile, c);
- else
- c = copy_comment (pfile, c);
- if (c != ' ')
- goto randomchar;
-
- /* Comments are equivalent to spaces.
- For -traditional, a comment is equivalent to nothing. */
- if (!CPP_OPTION (pfile, discard_comments))
- return CPP_COMMENT;
- else if (CPP_TRADITIONAL (pfile))
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ result->type = CPP_NUMBER;
+ parse_number (pfile, &result->val.str, c);
+ break;
+
+ case '$':
+ if (!CPP_OPTION (pfile, dollars_in_ident))
+ goto random_char;
+ /* Fall through... */
+
+ case '_':
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
+ case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
+ case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
+ case 's': case 't': case 'u': case 'v': case 'w': case 'x':
+ case 'y': case 'z':
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
+ case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
+ case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
+ case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
+ case 'Y': case 'Z':
+ result->type = CPP_NAME;
+ result->val.node = parse_identifier (pfile, c);
+
+ /* 'L' may introduce wide characters or strings. */
+ if (result->val.node == pfile->spec_nodes->n_L)
{
- if (pfile->parsing_define_directive)
- return CPP_COMMENT;
- goto get_next;
+ c = buffer->read_ahead; /* For make_string. */
+ if (c == '\'' || c == '"')
+ {
+ ACCEPT_CHAR (c == '"' ? CPP_WSTRING: CPP_WCHAR);
+ goto make_string;
+ }
}
- else
+ /* Convert named operators to their proper types. */
+ else if (result->val.node->type == T_OPERATOR)
{
- CPP_PUTC (pfile, c);
- return CPP_HSPACE;
+ result->flags |= NAMED_OP;
+ result->type = result->val.node->value.code;
}
+ break;
- case '#':
- CPP_PUTC (pfile, c);
+ case '\'':
+ case '"':
+ result->type = c == '"' ? CPP_STRING: CPP_CHAR;
+ make_string:
+ parse_string (pfile, result, c);
+ break;
+
+ case '/':
+ /* A potential block or line comment. */
+ comment_start = buffer->cur;
+ result->type = CPP_DIV;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_DIV_EQ);
+ if (c != '/' && c != '*')
+ break;
- hash:
- if (pfile->parsing_if_directive)
+ if (c == '*')
{
- CPP_ADJUST_WRITTEN (pfile, -1);
- if (_cpp_parse_assertion (pfile))
- return CPP_ASSERTION;
- return CPP_OTHER;
+ if (skip_block_comment (pfile))
+ cpp_error_with_line (pfile, result->line, result->col,
+ "unterminated comment");
}
-
- if (pfile->parsing_define_directive)
+ else
{
- c2 = PEEKC ();
- if (c2 == '#')
- {
- FORWARD (1);
- CPP_PUTC (pfile, c2);
- }
- else if (c2 == '%' && PEEKN (1) == ':')
+ if (!CPP_OPTION (pfile, cplusplus_comments)
+ && !CPP_IN_SYSTEM_HEADER (pfile))
+ break;
+
+ /* We silently allow C++ comments in system headers,
+ irrespective of conformance mode, because lots of
+ broken systems do that and trying to clean it up in
+ fixincludes is a nightmare. */
+ if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)
+ && ! buffer->warned_cplusplus_comments)
{
- /* Digraph: "%:" == "#". */
- FORWARD (1);
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c2);
- CPP_PUTC_Q (pfile, GETC ());
+ cpp_pedwarn (pfile,
+ "C++ style comments are not allowed in ISO C89");
+ cpp_pedwarn (pfile,
+ "(this will be reported only once per input file)");
+ buffer->warned_cplusplus_comments = 1;
}
- else
- return CPP_HASH;
- return CPP_PASTE;
+ /* Skip_line_comment updates buffer->read_ahead. */
+ if (skip_line_comment (pfile))
+ cpp_warning_with_line (pfile, result->line, result->col,
+ "multi-line comment");
}
- if (!pfile->only_seen_white)
- return CPP_OTHER;
-
- /* Remove the "#" or "%:" from the token buffer. */
- CPP_ADJUST_WRITTEN (pfile, (c == '#' ? -1 : -2));
- return CPP_DIRECTIVE;
-
- case '\"':
- case '\'':
- parse_string (pfile, c);
- return c == '\'' ? CPP_CHAR : CPP_STRING;
-
- case '$':
- if (!CPP_OPTION (pfile, dollars_in_ident))
- goto randomchar;
- goto letter;
+ /* Skipping the comment has updated buffer->read_ahead. */
+ if (!pfile->state.save_comments)
+ {
+ result->flags |= PREV_WHITE;
+ goto next_char;
+ }
- case ':':
- c2 = PEEKC ();
- /* Digraph: ":>" == "]". */
- if (c2 == '>'
- || (c2 == ':' && CPP_OPTION (pfile, cplusplus)))
- goto op2;
- goto randomchar;
+ /* Save the comment as a token in its own right. */
+ save_comment (pfile, result, comment_start);
+ break;
- case '&':
- case '+':
- case '|':
- c2 = PEEKC ();
- if (c2 == c || c2 == '=')
- goto op2;
- goto randomchar;
+ case '<':
+ if (pfile->state.angled_headers)
+ {
+ result->type = CPP_HEADER_NAME;
+ c = '>'; /* terminator. */
+ goto make_string;
+ }
- case '%':
- /* Digraphs: "%:" == "#", "%>" == "}". */
- c2 = PEEKC ();
- if (c2 == ':')
+ result->type = CPP_LESS;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_LESS_EQ);
+ else if (c == '<')
{
- FORWARD (1);
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, c2);
- goto hash;
+ ACCEPT_CHAR (CPP_LSHIFT);
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_LSHIFT_EQ);
}
- else if (c2 == '>')
+ else if (c == '?' && CPP_OPTION (pfile, cplusplus))
{
- FORWARD (1);
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, c2);
- return CPP_OPEN_BRACE;
+ ACCEPT_CHAR (CPP_MIN);
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_MIN_EQ);
}
- /* else fall through */
-
- case '*':
- case '!':
- case '=':
- case '^':
- if (PEEKC () == '=')
- goto op2;
- goto randomchar;
-
- case '-':
- c2 = PEEKC ();
- if (c2 == '-')
+ else if (c == ':' && CPP_OPTION (pfile, digraphs))
{
- if (CPP_OPTION (pfile, chill))
- goto comment; /* Chill style comment */
- else
- goto op2;
+ ACCEPT_CHAR (CPP_OPEN_SQUARE);
+ result->flags |= DIGRAPH;
}
- else if (c2 == '=')
- goto op2;
- else if (c2 == '>')
+ else if (c == '%' && CPP_OPTION (pfile, digraphs))
{
- if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
- {
- /* In C++, there's a ->* operator. */
- token = CPP_OTHER;
- CPP_RESERVE (pfile, 4);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, GETC ());
- CPP_PUTC_Q (pfile, GETC ());
- return token;
- }
- goto op2;
+ ACCEPT_CHAR (CPP_OPEN_BRACE);
+ result->flags |= DIGRAPH;
}
- goto randomchar;
+ break;
- case '<':
- if (pfile->parsing_include_directive)
+ case '>':
+ result->type = CPP_GREATER;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_GREATER_EQ);
+ else if (c == '>')
{
- for (;;)
- {
- CPP_PUTC (pfile, c);
- if (c == '>')
- break;
- c = GETC ();
- if (c == '\n' || c == EOF)
- {
- cpp_error (pfile,
- "missing '>' in `#include <FILENAME>'");
- break;
- }
- else if (c == '\r')
- {
- if (!CPP_BUFFER (pfile)->has_escapes)
- {
- /* Backslash newline is replaced by nothing. */
- CPP_ADJUST_WRITTEN (pfile, -1);
- CPP_BUMP_LINE (pfile);
- }
- else
- {
- /* We might conceivably get \r- or \r<space> in
- here. Just delete 'em. */
- int d = GETC();
- if (d != '-' && d != ' ')
- cpp_ice (pfile, "unrecognized escape \\r%c", d);
- CPP_ADJUST_WRITTEN (pfile, -1);
- }
- }
- }
- return CPP_STRING;
+ ACCEPT_CHAR (CPP_RSHIFT);
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_RSHIFT_EQ);
}
- /* Digraphs: "<%" == "{", "<:" == "[". */
- c2 = PEEKC ();
- if (c2 == '%')
+ else if (c == '?' && CPP_OPTION (pfile, cplusplus))
{
- FORWARD (1);
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, c2);
- return CPP_CLOSE_BRACE;
+ ACCEPT_CHAR (CPP_MAX);
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_MAX_EQ);
}
- else if (c2 == ':')
- goto op2;
- /* else fall through */
- case '>':
- c2 = PEEKC ();
- if (c2 == '=')
- goto op2;
- /* GNU C++ supports MIN and MAX operators <? and >?. */
- if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
- goto randomchar;
- FORWARD(1);
- CPP_RESERVE (pfile, 3);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, c2);
- if (PEEKC () == '=')
- CPP_PUTC_Q (pfile, GETC ());
- return CPP_OTHER;
+ break;
+
+ case '%':
+ lex_percent (buffer, result);
+ break;
case '.':
- c2 = PEEKC ();
- if (ISDIGIT (c2))
- {
- CPP_PUTC (pfile, c);
- c = GETC ();
- goto number;
- }
+ lex_dot (pfile, result);
+ break;
- /* In C++ there's a .* operator. */
- if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
- goto op2;
+ case '+':
+ result->type = CPP_PLUS;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_PLUS_EQ);
+ else if (c == '+')
+ ACCEPT_CHAR (CPP_PLUS_PLUS);
+ break;
- if (c2 == '.' && PEEKN(1) == '.')
+ case '-':
+ result->type = CPP_MINUS;
+ c = get_effective_char (buffer);
+ if (c == '>')
{
- CPP_RESERVE (pfile, 3);
- CPP_PUTC_Q (pfile, '.');
- CPP_PUTC_Q (pfile, '.');
- CPP_PUTC_Q (pfile, '.');
- FORWARD (2);
- return CPP_ELLIPSIS;
+ ACCEPT_CHAR (CPP_DEREF);
+ if (CPP_OPTION (pfile, cplusplus)
+ && get_effective_char (buffer) == '*')
+ ACCEPT_CHAR (CPP_DEREF_STAR);
}
- goto randomchar;
+ else if (c == '=')
+ ACCEPT_CHAR (CPP_MINUS_EQ);
+ else if (c == '-')
+ ACCEPT_CHAR (CPP_MINUS_MINUS);
+ break;
- op2:
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, GETC ());
- return CPP_OTHER;
+ case '*':
+ result->type = CPP_MULT;
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_MULT_EQ);
+ break;
- case 'L':
- c2 = PEEKC ();
- if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
- {
- CPP_PUTC (pfile, c);
- c = GETC ();
- parse_string (pfile, c);
- return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
- }
- goto letter;
+ case '=':
+ result->type = CPP_EQ;
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_EQ_EQ);
+ break;
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- number:
- c2 = '.';
- for (;;)
- {
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- c = PEEKC ();
- if (c == EOF)
- break;
- if (!is_numchar(c) && c != '.'
- && ((c2 != 'e' && c2 != 'E'
- && ((c2 != 'p' && c2 != 'P')
- || CPP_OPTION (pfile, c89)))
- || (c != '+' && c != '-')))
- break;
- FORWARD(1);
- c2= c;
- }
- return CPP_NUMBER;
- case 'b': case 'c': case 'd': case 'h': case 'o':
- case 'B': case 'C': case 'D': case 'H': case 'O':
- if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
- {
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- CPP_PUTC_Q (pfile, '\'');
- FORWARD(1);
- for (;;)
- {
- c = GETC();
- if (c == EOF)
- goto chill_number_eof;
- if (!is_numchar(c))
- break;
- CPP_PUTC (pfile, c);
- }
- if (c == '\'')
- {
- CPP_RESERVE (pfile, 2);
- CPP_PUTC_Q (pfile, c);
- return CPP_STRING;
- }
- else
+ case '!':
+ result->type = CPP_NOT;
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_NOT_EQ);
+ break;
+
+ case '&':
+ result->type = CPP_AND;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_AND_EQ);
+ else if (c == '&')
+ ACCEPT_CHAR (CPP_AND_AND);
+ break;
+
+ case '#':
+ result->type = CPP_HASH;
+ if (get_effective_char (buffer) == '#')
+ ACCEPT_CHAR (CPP_PASTE);
+ break;
+
+ case '|':
+ result->type = CPP_OR;
+ c = get_effective_char (buffer);
+ if (c == '=')
+ ACCEPT_CHAR (CPP_OR_EQ);
+ else if (c == '|')
+ ACCEPT_CHAR (CPP_OR_OR);
+ break;
+
+ case '^':
+ result->type = CPP_XOR;
+ if (get_effective_char (buffer) == '=')
+ ACCEPT_CHAR (CPP_XOR_EQ);
+ break;
+
+ case ':':
+ result->type = CPP_COLON;
+ c = get_effective_char (buffer);
+ if (c == ':' && CPP_OPTION (pfile, cplusplus))
+ ACCEPT_CHAR (CPP_SCOPE);
+ else if (c == '>' && CPP_OPTION (pfile, digraphs))
+ {
+ result->flags |= DIGRAPH;
+ ACCEPT_CHAR (CPP_CLOSE_SQUARE);
+ }
+ break;
+
+ case '~': result->type = CPP_COMPL; break;
+ case ',': result->type = CPP_COMMA; break;
+ case '(': result->type = CPP_OPEN_PAREN; break;
+ case ')': result->type = CPP_CLOSE_PAREN; break;
+ case '[': result->type = CPP_OPEN_SQUARE; break;
+ case ']': result->type = CPP_CLOSE_SQUARE; break;
+ case '{': result->type = CPP_OPEN_BRACE; break;
+ case '}': result->type = CPP_CLOSE_BRACE; break;
+ case ';': result->type = CPP_SEMICOLON; break;
+
+ case '@':
+ if (CPP_OPTION (pfile, objc))
+ {
+ /* In Objective C, '@' may begin keywords or strings, like
+ @keyword or @"string". It would be nice to call
+ get_effective_char here and test the result. However, we
+ would then need to pass 2 characters to parse_identifier,
+ making it ugly and slowing down its main loop. Instead,
+ we assume we have an identifier, and recover if not. */
+ result->type = CPP_NAME;
+ result->val.node = parse_identifier (pfile, c);
+ if (result->val.node->length != 1)
+ break;
+
+ /* OK, so it wasn't an identifier. Maybe a string? */
+ if (buffer->read_ahead == '"')
{
- FORWARD(-1);
- chill_number_eof:
- return CPP_NUMBER;
+ c = '"';
+ ACCEPT_CHAR (CPP_OSTRING);
+ goto make_string;
}
}
- else
- goto letter;
- case '_':
- case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
- case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
- case 'r': case 's': case 't': case 'u': case 'v': case 'w':
- case 'x': case 'y': case 'z':
- case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
- case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
- case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- letter:
- _cpp_parse_name (pfile, c);
- return CPP_MACRO;
+ goto random_char;
- case ' ': case '\t': case '\v': case '\f': case '\0':
- {
- int null_count = 0;
+ random_char:
+ default:
+ result->type = CPP_OTHER;
+ result->val.aux = c;
+ break;
+ }
+}
- for (;;)
- {
- if (c == '\0')
- null_count++;
- else
- CPP_PUTC (pfile, c);
- c = PEEKC ();
- if (c == EOF || !is_hspace(c))
- break;
- FORWARD(1);
- }
- if (null_count)
- null_warning (pfile, null_count);
- return CPP_HSPACE;
- }
+/*
+ * The tokenizer's main loop. Returns a token list, representing a
+ * logical line in the input file. On EOF after some tokens have
+ * been processed, we return immediately. Then in next call, or if
+ * EOF occurred at the beginning of a logical line, a single CPP_EOF
+ * token is placed in the list.
+ */
+
+static void
+lex_line (pfile, list)
+ cpp_reader *pfile;
+ cpp_toklist *list;
+{
+ unsigned int first_token;
+ cpp_token *cur_token, *first;
+ cpp_buffer *buffer = pfile->buffer;
+
+ pfile->state.in_lex_line = 1;
+ if (pfile->buffer->cur == pfile->buffer->buf)
+ list->flags |= BEG_OF_FILE;
+
+ retry:
+ pfile->state.in_directive = 0;
+ pfile->state.angled_headers = 0;
+ pfile->state.skip_newlines = 1;
+ pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
+ first_token = list->tokens_used;
+ list->file = buffer->nominal_fname;
+
+ do
+ {
+ if (list->tokens_used >= list->tokens_cap)
+ _cpp_expand_token_space (list, 256);
+
+ cur_token = list->tokens + list->tokens_used;
+ lex_token (pfile, cur_token);
- case '\r':
- if (CPP_BUFFER (pfile)->has_escapes)
+ if (pfile->state.skip_newlines)
{
- c = GETC ();
- if (c == '-')
- {
- if (pfile->output_escapes)
- CPP_PUTS (pfile, "\r-", 2);
- _cpp_parse_name (pfile, GETC ());
- return CPP_NAME;
- }
- else if (c == ' ')
+ pfile->state.skip_newlines = 0;
+ list->line = buffer->lineno;
+ if (cur_token->type == CPP_HASH)
{
- /* "\r " means a space, but only if necessary to prevent
- accidental token concatenation. */
- CPP_RESERVE (pfile, 2);
- if (pfile->output_escapes)
- CPP_PUTC_Q (pfile, '\r');
- CPP_PUTC_Q (pfile, c);
- return CPP_HSPACE;
+ pfile->state.in_directive = 1;
+ pfile->state.save_comments = 0;
+ pfile->state.indented = cur_token->flags & PREV_WHITE;
}
+ /* 6.10.3.10: Within the sequence of preprocessing tokens
+ making up the invocation of a function-like macro, new
+ line is considered a normal white-space character. */
+ else if (first_token != 0)
+ cur_token->flags |= PREV_WHITE;
+ }
+ else if (IN_DIRECTIVE (pfile) && list->tokens_used == first_token + 1)
+ {
+ if (cur_token->type == CPP_NUMBER)
+ list->directive = _cpp_check_linemarker (pfile, cur_token);
else
- {
- cpp_ice (pfile, "unrecognized escape \\r%c", c);
- goto get_next;
- }
+ list->directive = _cpp_check_directive (pfile, cur_token);
}
- else
+
+ /* _cpp_get_line assumes list->tokens_used refers to the current
+ token being lexed. So do this after _cpp_check_directive to
+ get the warnings therein correct. */
+ list->tokens_used++;
+ }
+ while (cur_token->type != CPP_EOF);
+
+ /* All tokens are allocated, so the memory location is fixed. */
+ first = &list->tokens[first_token];
+ first->flags |= BOL;
+ pfile->first_directive_token = first;
+
+ /* Don't complain about the null directive, nor directives 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 (IN_DIRECTIVE (pfile) && !KNOWN_DIRECTIVE (list) && !pfile->skipping
+ && !CPP_OPTION (pfile, lang_asm))
+ {
+ if (cur_token > first + 1)
{
- /* Backslash newline is ignored. */
- if (!ACTIVE_MARK_P (pfile))
- CPP_BUMP_LINE (pfile);
- goto get_next;
+ if (first[1].type == CPP_NAME)
+ cpp_error_with_line (pfile, first->line, first->col,
+ "invalid preprocessing directive #%s",
+ first[1].val.node->name);
+ else
+ cpp_error_with_line (pfile, first->line, first->col,
+ "invalid preprocessing directive");
}
- case '\n':
- CPP_PUTC (pfile, c);
- return CPP_VSPACE;
+ /* Discard this line to prevent further errors from cc1. */
+ _cpp_clear_toklist (list);
+ goto retry;
+ }
+
+ /* Drop the EOF unless really at EOF or in a directive. */
+ if (cur_token != first && !KNOWN_DIRECTIVE (list)
+ && pfile->done_initializing)
+ list->tokens_used--;
- case '(': token = CPP_OPEN_PAREN; goto char1;
- case ')': token = CPP_CLOSE_PAREN; goto char1;
- case '{': token = CPP_OPEN_BRACE; goto char1;
- case '}': token = CPP_CLOSE_BRACE; goto char1;
- case ',': token = CPP_COMMA; goto char1;
- case ';': token = CPP_SEMICOLON; goto char1;
+ pfile->state.in_lex_line = 0;
+}
- randomchar:
- default:
- token = CPP_OTHER;
- char1:
- CPP_PUTC (pfile, c);
- return token;
+/* Write the spelling of a token TOKEN to BUFFER. The buffer must
+ already contain the enough space to hold the token's spelling.
+ Returns a pointer to the character after the last character
+ written. */
+
+static unsigned char *
+spell_token (pfile, token, buffer)
+ cpp_reader *pfile; /* Would be nice to be rid of this... */
+ const cpp_token *token;
+ unsigned char *buffer;
+{
+ switch (TOKEN_SPELL (token))
+ {
+ case SPELL_OPERATOR:
+ {
+ const unsigned char *spelling;
+ unsigned char c;
+
+ if (token->flags & DIGRAPH)
+ spelling = _cpp_digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
+ else if (token->flags & NAMED_OP)
+ goto spell_ident;
+ else
+ spelling = TOKEN_NAME (token);
+
+ while ((c = *spelling++) != '\0')
+ *buffer++ = c;
+ }
+ break;
+
+ case SPELL_IDENT:
+ spell_ident:
+ memcpy (buffer, token->val.node->name, token->val.node->length);
+ buffer += token->val.node->length;
+ break;
+
+ case SPELL_STRING:
+ {
+ int left, right, tag;
+ switch (token->type)
+ {
+ case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
+ case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
+ case CPP_OSTRING: left = '"'; right = '"'; tag = '@'; break;
+ case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
+ case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
+ case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
+ default: left = '\0'; right = '\0'; tag = '\0'; break;
+ }
+ if (tag) *buffer++ = tag;
+ if (left) *buffer++ = left;
+ memcpy (buffer, token->val.str.text, token->val.str.len);
+ buffer += token->val.str.len;
+ if (right) *buffer++ = right;
+ }
+ break;
+
+ case SPELL_CHAR:
+ *buffer++ = token->val.aux;
+ break;
+
+ case SPELL_NONE:
+ cpp_ice (pfile, "Unspellable token %s", TOKEN_NAME (token));
+ break;
}
+
+ return buffer;
+}
+
+/* Macro expansion algorithm.
+
+Macro expansion is implemented by a single-pass algorithm; there are
+no rescan passes involved. cpp_get_token expands just enough to be
+able to return a token to the caller, a consequence is that when it
+returns the preprocessor can be in a state of mid-expansion. The
+algorithm does not work by fully expanding a macro invocation into
+some kind of token list, and then returning them one by one.
+
+Our expansion state is recorded in a context stack. We start out with
+a single context on the stack, let's call it base context. This
+consists of the token list returned by lex_line that forms the next
+logical line in the source file.
+
+The current level in the context stack is stored in the cur_context
+member of the cpp_reader structure. The context it references keeps,
+amongst other things, a count of how many tokens form that context and
+our position within those tokens.
+
+Fundamentally, calling cpp_get_token will return the next token from
+the current context. If we're at the end of the current context, that
+context is popped from the stack first, unless it is the base context,
+in which case the next logical line is lexed from the source file.
+
+However, before returning the token, if it is a CPP_NAME token
+_cpp_get_token checks to see if it is a macro and if it is enabled.
+Each time it encounters a macro name, it calls push_macro_context.
+This function checks that the macro should be expanded (with
+is_macro_enabled), and if so pushes a new macro context on the stack
+which becomes the current context. It then loops back to read the
+first token of the macro context.
+
+A macro context basically consists of the token list representing the
+macro's replacement list, which was saved in the hash table by
+save_macro_expansion when its #define statement was parsed. If the
+macro is function-like, it also contains the tokens that form the
+arguments to the macro. I say more about macro arguments below, but
+for now just saying that each argument is a set of pointers to tokens
+is enough.
+
+When taking tokens from a macro context, we may get a CPP_MACRO_ARG
+token. This represents an argument passed to the macro, with the
+argument number stored in the token's AUX field. The argument should
+be substituted, this is achieved by pushing an "argument context". An
+argument context is just refers to the tokens forming the argument,
+which are obtained directly from the macro context. The STRINGIFY
+flag on a CPP_MACRO_ARG token indicates that the argument should be
+stringified.
+
+Here's a few simple rules the context stack obeys:-
+
+ 1) The lex_line token list is always context zero.
+
+ 2) Context 1, if it exists, must be a macro context.
+
+ 3) An argument context can only appear above a macro context.
+
+ 4) A macro context can appear above the base context, another macro
+ context, or an argument context.
+
+ 5) These imply that the minimal level of an argument context is 2.
+
+The only tricky thing left is ensuring that macros are enabled and
+disabled correctly. The algorithm controls macro expansion by the
+level of the context a token is taken from in the context stack. If a
+token is taken from a level equal to no_expand_level (a member of
+struct cpp_reader), no expansion is performed.
+
+When popping a context off the stack, if no_expand_level equals the
+level of the popped context, it is reduced by one to match the new
+context level, so that expansion is still disabled. It does not
+increase if a context is pushed, though. It starts out life as
+UINT_MAX, which has the effect that initially macro expansion is
+enabled. I explain how this mechanism works below.
+
+The standard requires:-
+
+ 1) Arguments to be fully expanded before substitution.
+
+ 2) Stringified arguments to not be expanded, nor the tokens
+ immediately surrounding a ## operator.
+
+ 3) Continual rescanning until there are no more macros left to
+ replace.
+
+ 4) Once a macro has been expanded in stage 1) or 3), it cannot be
+ expanded again during later rescans. This prevents infinite
+ recursion.
+
+The first thing to observe is that stage 3) is mostly redundant.
+Since a macro is disabled once it has been expanded, how can a rescan
+find an unexpanded macro name? There are only two cases where this is
+possible:-
+
+ a) If the macro name results from a token paste operation.
+
+ b) If the macro in question is a function-like macro that hasn't
+ already been expanded because previously there was not the required
+ '(' token immediately following it. This is only possible when an
+ argument is substituted, and after substitution the last token of
+ the argument can bind with a parenthesis appearing in the tokens
+ following the substitution. Note that if the '(' appears within the
+ argument, the ')' must too, as expanding macro arguments cannot
+ "suck in" tokens outside the argument.
+
+So we tackle this as follows. When parsing the macro invocation for
+arguments, we record the tokens forming each argument as a list of
+pointers to those tokens. We do not expand any tokens that are "raw",
+i.e. directly from the macro invocation, but other tokens that come
+from (nested) argument substitution are fully expanded.
+
+This is achieved by setting the no_expand_level to that of the macro
+invocation. A CPP_MACRO_ARG token never appears in the list of tokens
+forming an argument, because parse_args (indirectly) calls
+get_raw_token which automatically pushes argument contexts and traces
+into them. Since these contexts are at a higher level than the
+no_expand_level, they get fully macro expanded.
+
+"Raw" and non-raw tokens are separated in arguments by null pointers,
+with the policy that the initial state of an argument is raw. If the
+first token is not raw, it should be preceded by a null pointer. When
+tracing through the tokens of an argument context, each time
+get_raw_token encounters a null pointer, it toggles the flag
+CONTEXT_RAW.
+
+This flag, when set, indicates to is_macro_disabled that we are
+reading raw tokens which should be macro-expanded. Similarly, if
+clear, is_macro_disabled suppresses re-expansion.
+
+It's probably time for an example.
+
+#define hash #
+#define str(x) #x
+#define xstr(y) str(y hash)
+str(hash) // "hash"
+xstr(hash) // "# hash"
+
+In the invocation of str, parse_args turns off macro expansion and so
+parses the argument as <hash>. This is the only token (pointer)
+passed as the argument to str. Since <hash> is raw there is no need
+for an initial null pointer. stringify_arg is called from
+get_raw_token when tracing through the expansion of str, since the
+argument has the STRINGIFY flag set. stringify_arg turns off
+macro_expansion by setting the no_expand_level to that of the argument
+context. Thus it gets the token <hash> and stringifies it to "hash"
+correctly.
+
+Similary xstr is passed <hash>. However, when parse_args is parsing
+the invocation of str() in xstr's expansion, get_raw_token encounters
+a CPP_MACRO_ARG token for y. Transparently to parse_args, it pushes
+an argument context, and enters the tokens of the argument,
+i.e. <hash>. This is at a higher context level than parse_args
+disabled, and so is_macro_disabled permits expansion of it and a macro
+context is pushed on top of the argument context. This contains the
+<#> token, and the end result is that <hash> is macro expanded.
+However, after popping off the argument context, the <hash> of xstr's
+expansion does not get macro expanded because we're back at the
+no_expand_level. The end result is that the argument passed to str is
+<NULL> <#> <NULL> <hash>. Note the nulls - policy is we start off
+raw, <#> is not raw, but then <hash> is.
+
+*/
+
+
+/* Free the storage allocated for macro arguments. */
+static void
+free_macro_args (args)
+ macro_args *args;
+{
+ if (args->tokens)
+ free ((PTR) args->tokens);
+ free (args->ends);
+ free (args);
}
-/* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
- Caller is expected to have checked no_macro_expand. */
+/* Determines if a macro has been already used (and is therefore
+ disabled). */
static int
-maybe_macroexpand (pfile, written)
+is_macro_disabled (pfile, expansion, token)
cpp_reader *pfile;
- long written;
+ const cpp_toklist *expansion;
+ const cpp_token *token;
{
- U_CHAR *macro = pfile->token_buffer + written;
- size_t len = CPP_WRITTEN (pfile) - written;
- HASHNODE *hp = _cpp_lookup (pfile, macro, len);
-
- if (!hp)
- return 0;
- if (hp->disabled || hp->type == T_IDENTITY)
+ cpp_context *context = CURRENT_CONTEXT (pfile);
+
+ /* Arguments on either side of ## are inserted in place without
+ macro expansion (6.10.3.3.2). Conceptually, any macro expansion
+ occurs during a later rescan pass. The effect is that we expand
+ iff we would as part of the macro's expansion list, so we should
+ drop to the macro's context. */
+ if (IS_ARG_CONTEXT (context))
{
- if (pfile->output_escapes)
- {
- /* Insert a no-reexpand marker before IDENT. */
- CPP_RESERVE (pfile, 2);
- CPP_ADJUST_WRITTEN (pfile, 2);
- macro = pfile->token_buffer + written;
-
- memmove (macro + 2, macro, len);
- macro[0] = '\r';
- macro[1] = '-';
- }
- return 0;
+ if (token->flags & PASTED)
+ context--;
+ else if (!(context->flags & CONTEXT_RAW))
+ return 1;
+ else if (context->flags & (CONTEXT_PASTEL | CONTEXT_PASTER))
+ context--;
}
- if (hp->type == T_EMPTY)
+
+ /* Have we already used this macro? */
+ while (context->level > 0)
{
- /* Special case optimization: macro expands to nothing. */
- CPP_SET_WRITTEN (pfile, written);
- CPP_PUTC_Q (pfile, ' ');
- return 1;
+ if (!IS_ARG_CONTEXT (context) && context->u.list == expansion)
+ return 1;
+ /* Raw argument tokens are judged based on the token list they
+ came from. */
+ if (context->flags & CONTEXT_RAW)
+ context = pfile->contexts + context->level;
+ else
+ context--;
}
- /* If macro wants an arglist, verify that a '(' follows. */
- if (hp->type == T_FMACRO)
+ /* Function-like macros may be disabled if the '(' is not in the
+ current context. We check this without disrupting the context
+ stack. */
+ if (expansion->paramc >= 0)
{
- int macbuf_whitespace = 0;
- int c;
+ const cpp_token *next;
+ unsigned int prev_nme;
- while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
+ context = CURRENT_CONTEXT (pfile);
+ /* Drop down any contexts we're at the end of: the '(' may
+ appear in lower macro expansions, or in the rest of the file. */
+ while (context->posn == context->count && context > pfile->contexts)
{
- const U_CHAR *point = CPP_BUFFER (pfile)->cur;
- for (;;)
- {
- _cpp_skip_hspace (pfile);
- c = PEEKC ();
- if (c == '\n')
- FORWARD(1);
- else
- break;
- }
- if (point != CPP_BUFFER (pfile)->cur)
- macbuf_whitespace = 1;
- if (c == '(')
- goto is_macro_call;
- else if (c != EOF)
- goto not_macro_call;
- cpp_pop_buffer (pfile);
- }
-
- CPP_SET_MARK (pfile);
- for (;;)
- {
- _cpp_skip_hspace (pfile);
- c = PEEKC ();
- if (c == '\n')
- FORWARD(1);
- else
- break;
+ context--;
+ /* If we matched, we are disabled, as we appear in the
+ expansion of each macro we meet. */
+ if (!IS_ARG_CONTEXT (context) && context->u.list == expansion)
+ return 1;
}
- CPP_GOTO_MARK (pfile);
- if (c != '(')
+ prev_nme = pfile->no_expand_level;
+ pfile->no_expand_level = context - pfile->contexts;
+ next = _cpp_get_token (pfile);
+ restore_macro_expansion (pfile, prev_nme);
+
+ if (next->type != CPP_OPEN_PAREN)
{
- not_macro_call:
- if (macbuf_whitespace)
- CPP_PUTC (pfile, ' ');
- return 0;
+ _cpp_push_token (pfile, next);
+ if (CPP_WTRADITIONAL (pfile))
+ cpp_warning (pfile,
+ "function macro %s must be used with arguments in traditional C",
+ token->val.node->name);
+ return 1;
}
}
- is_macro_call:
- /* This is now known to be a macro call.
- Expand the macro, reading arguments as needed,
- and push the expansion on the input stack. */
- _cpp_macroexpand (pfile, hp);
- CPP_SET_WRITTEN (pfile, written);
- return 1;
+ return 0;
}
-/* Complain about \v or \f in a preprocessing directive (constraint
- violation, C99 6.10 para 5). Caller has checked CPP_PEDANTIC. */
+/* Add a token to the set of tokens forming the arguments to the macro
+ being parsed in parse_args. */
static void
-pedantic_whitespace (pfile, p, len)
- cpp_reader *pfile;
- U_CHAR *p;
- unsigned int len;
+save_token (args, token)
+ macro_args *args;
+ const cpp_token *token;
{
- while (len)
+ if (args->used == args->capacity)
{
- if (*p == '\v')
- cpp_pedwarn (pfile, "vertical tab in preprocessing directive");
- else if (*p == '\f')
- cpp_pedwarn (pfile, "form feed in preprocessing directive");
- p++;
- len--;
+ args->capacity += args->capacity + 100;
+ args->tokens = (const cpp_token **)
+ xrealloc ((PTR) args->tokens,
+ args->capacity * sizeof (const cpp_token *));
}
+ args->tokens[args->used++] = token;
}
-
-enum cpp_ttype
-cpp_get_token (pfile)
+/* Take and save raw tokens until we finish one argument. Empty
+ arguments are saved as a single CPP_PLACEMARKER token. */
+static const cpp_token *
+parse_arg (pfile, var_args, paren_context, args, pcount)
cpp_reader *pfile;
+ int var_args;
+ unsigned int paren_context;
+ macro_args *args;
+ unsigned int *pcount;
{
- enum cpp_ttype token;
- long written = CPP_WRITTEN (pfile);
-
- get_next:
- token = _cpp_lex_token (pfile);
-
- switch (token)
+ const cpp_token *token;
+ unsigned int paren = 0, count = 0;
+ int raw, was_raw = 1;
+
+ for (count = 0;; count++)
{
- default:
- pfile->potential_control_macro = 0;
- pfile->only_seen_white = 0;
- return token;
+ token = _cpp_get_token (pfile);
- case CPP_VSPACE:
- if (pfile->only_seen_white == 0)
- pfile->only_seen_white = 1;
- CPP_BUMP_LINE (pfile);
- return token;
-
- case CPP_HSPACE:
- case CPP_COMMENT:
- return token;
+ switch (token->type)
+ {
+ default:
+ break;
- case CPP_DIRECTIVE:
- pfile->potential_control_macro = 0;
- if (_cpp_handle_directive (pfile))
- return CPP_DIRECTIVE;
- pfile->only_seen_white = 0;
- CPP_PUTC (pfile, '#');
- return CPP_OTHER;
+ case CPP_OPEN_PAREN:
+ paren++;
+ break;
- case CPP_MACRO:
- pfile->potential_control_macro = 0;
- pfile->only_seen_white = 0;
- if (! pfile->no_macro_expand
- && maybe_macroexpand (pfile, written))
- goto get_next;
- return CPP_NAME;
+ case CPP_CLOSE_PAREN:
+ if (paren-- != 0)
+ break;
+ goto out;
- case CPP_EOF:
- if (CPP_BUFFER (pfile) == NULL)
- return CPP_EOF;
- if (CPP_BUFFER (pfile)->manual_pop)
- /* If we've been reading from redirected input, the
- frontend will pop the buffer. */
- return CPP_EOF;
+ case CPP_COMMA:
+ /* Commas are not terminators within parantheses or var_args. */
+ if (paren || var_args)
+ break;
+ goto out;
- if (CPP_BUFFER (pfile)->seen_eof)
- {
- cpp_pop_buffer (pfile);
- goto get_next;
+ case CPP_EOF: /* Error reported by caller. */
+ goto out;
}
- else
+
+ raw = pfile->cur_context <= paren_context;
+ if (raw != was_raw)
{
- _cpp_handle_eof (pfile);
- return CPP_POP;
+ was_raw = raw;
+ save_token (args, 0);
+ count++;
}
+ save_token (args, token);
}
-}
-/* Like cpp_get_token, but skip spaces and comments. */
-
-enum cpp_ttype
-cpp_get_non_space_token (pfile)
- cpp_reader *pfile;
-{
- int old_written = CPP_WRITTEN (pfile);
- for (;;)
+ out:
+ if (count == 0)
{
- enum cpp_ttype token = cpp_get_token (pfile);
- if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
- return token;
- CPP_SET_WRITTEN (pfile, old_written);
+ /* Duplicate the placemarker. Then we can set its flags and
+ position and safely be using more than one. */
+ save_token (args, duplicate_token (pfile, &placemarker_token));
+ count++;
}
+
+ *pcount = count;
+ return token;
}
-/* Like cpp_get_token, except that it does not execute directives,
- does not consume vertical space, discards horizontal space, and
- automatically pops off macro buffers. */
-enum cpp_ttype
-_cpp_get_directive_token (pfile)
+/* This macro returns true if the argument starting at offset O of arglist
+ A is empty - that is, it's either a single PLACEMARKER token, or a null
+ pointer followed by a PLACEMARKER. */
+
+#define empty_argument(A, O) \
+ ((A)->tokens[O] ? (A)->tokens[O]->type == CPP_PLACEMARKER \
+ : (A)->tokens[(O)+1]->type == CPP_PLACEMARKER)
+
+/* Parse the arguments making up a macro invocation. Nested arguments
+ are automatically macro expanded, but immediate macros are not
+ expanded; this enables e.g. operator # to work correctly. Returns
+ non-zero on error. */
+static int
+parse_args (pfile, hp, args)
cpp_reader *pfile;
+ cpp_hashnode *hp;
+ macro_args *args;
{
- long old_written;
- enum cpp_ttype token;
-
- get_next:
- old_written = CPP_WRITTEN (pfile);
- token = _cpp_lex_token (pfile);
- switch (token)
+ const cpp_token *token;
+ const cpp_toklist *macro;
+ unsigned int total = 0;
+ unsigned int paren_context = pfile->cur_context;
+ int argc = 0;
+
+ macro = hp->value.expansion;
+ do
{
- default:
- return token;
+ unsigned int count;
- case CPP_VSPACE:
- /* Put it back and return VSPACE. */
- FORWARD(-1);
- CPP_ADJUST_WRITTEN (pfile, -1);
- return CPP_VSPACE;
-
- case CPP_HSPACE:
- if (CPP_PEDANTIC (pfile))
- pedantic_whitespace (pfile, pfile->token_buffer + old_written,
- CPP_WRITTEN (pfile) - old_written);
- CPP_SET_WRITTEN (pfile, old_written);
- goto get_next;
- return CPP_HSPACE;
-
- case CPP_DIRECTIVE:
- /* Don't execute the directive, but don't smash it to OTHER either. */
- CPP_PUTC (pfile, '#');
- return CPP_DIRECTIVE;
-
- case CPP_MACRO:
- if (! pfile->no_macro_expand
- && maybe_macroexpand (pfile, old_written))
- goto get_next;
- return CPP_NAME;
-
- case CPP_EOF:
- if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
- {
- cpp_pop_buffer (pfile);
- goto get_next;
+ token = parse_arg (pfile, (argc + 1 == macro->paramc
+ && (macro->flags & VAR_ARGS)),
+ paren_context, args, &count);
+ if (argc < macro->paramc)
+ {
+ total += count;
+ args->ends[argc] = total;
}
- else
- /* This can happen for files that don't end with a newline,
- and for cpp_define and friends. Pretend they do, so
- callers don't have to deal. A warning will be issued by
- someone else, if necessary. */
- return CPP_VSPACE;
+ argc++;
}
-}
+ while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
-/* Determine the current line and column. Used only by read_and_prescan. */
-static U_CHAR *
-find_position (start, limit, linep)
- U_CHAR *start;
- U_CHAR *limit;
- unsigned long *linep;
-{
- unsigned long line = *linep;
- U_CHAR *lbase = start;
- while (start < limit)
+ if (token->type == CPP_EOF)
{
- U_CHAR ch = *start++;
- if (ch == '\n' || ch == '\r')
- {
- line++;
- lbase = start;
- }
+ cpp_error(pfile, "unterminated argument list for macro \"%s\"", hp->name);
+ return 1;
}
- *linep = line;
- return lbase;
-}
-
-/* The following table is used by _cpp_read_and_prescan. If we have
- designated initializers, it can be constant data; otherwise, it is
- set up at runtime by _cpp_init_input_buffer. */
+ else if (argc < macro->paramc)
+ {
+ /* A rest argument is allowed to not appear in the invocation at all.
+ e.g. #define debug(format, args...) ...
+ debug("string");
+ This is exactly the same as if the rest argument had received no
+ tokens - debug("string",); This extension is deprecated. */
-#ifndef UCHAR_MAX
-#define UCHAR_MAX 255 /* assume 8-bit bytes */
-#endif
+ if (argc + 1 == macro->paramc && (macro->flags & VAR_ARGS))
+ {
+ /* Duplicate the placemarker. Then we can set its flags and
+ position and safely be using more than one. */
+ save_token (args, duplicate_token (pfile, &placemarker_token));
+ args->ends[argc] = total + 1;
-#if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
-#define init_chartab() /* nothing */
-#define CHARTAB static const unsigned char chartab[UCHAR_MAX + 1] = {
-#define END };
-#define s(p, v) [p] = v,
-#else
-#define CHARTAB static unsigned char chartab[UCHAR_MAX + 1] = { 0 }; \
- static void init_chartab PARAMS ((void)) { \
- unsigned char *x = chartab;
-#define END }
-#define s(p, v) x[p] = v;
-#endif
+ if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile))
+ cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
-/* Table of characters that can't be handled in the inner loop.
- Also contains the mapping between trigraph third characters and their
- replacements. */
-#define SPECCASE_CR 1
-#define SPECCASE_BACKSLASH 2
-#define SPECCASE_QUESTION 3
-
-CHARTAB
- s('\r', SPECCASE_CR)
- s('\\', SPECCASE_BACKSLASH)
- s('?', SPECCASE_QUESTION)
-
- s('=', '#') s(')', ']') s('!', '|')
- s('(', '[') s('\'', '^') s('>', '}')
- s('/', '\\') s('<', '{') s('-', '~')
-END
-
-#undef CHARTAB
-#undef END
-#undef s
-
-#define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
-#define NONTRI(c) ((c) <= SPECCASE_QUESTION)
-
-/* Read the entire contents of file DESC into buffer BUF. LEN is how
- much memory to allocate initially; more will be allocated if
- necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
- canonical form (\n). If enabled, convert and/or warn about
- trigraphs. Convert backslash-newline to a one-character escape
- (\r) and remove it from "embarrassing" places (i.e. the middle of a
- token). If there is no newline at the end of the file, add one and
- warn. Returns -1 on failure, or the actual length of the data to
- be scanned.
-
- This function does a lot of work, and can be a serious performance
- bottleneck. It has been tuned heavily; make sure you understand it
- before hacking. The common case - no trigraphs, Unix style line
- breaks, backslash-newline set off by whitespace, newline at EOF -
- has been optimized at the expense of the others. The performance
- penalty for DOS style line breaks (\r\n) is about 15%.
-
- Warnings lose particularly heavily since we have to determine the
- line number, which involves scanning from the beginning of the file
- or from the last warning. The penalty for the absence of a newline
- at the end of reload1.c is about 60%. (reload1.c is 329k.)
+ return 0;
+ }
+ else
+ {
+ cpp_error (pfile, "%u arguments is not enough for macro \"%s\"",
+ argc, hp->name);
+ return 1;
+ }
+ }
+ /* An empty argument to an empty function-like macro is fine. */
+ else if (argc > macro->paramc
+ && !(macro->paramc == 0 && argc == 1 && empty_argument (args, 0)))
+ {
+ cpp_error (pfile, "%u arguments is too many for macro \"%s\"",
+ argc, hp->name);
+ return 1;
+ }
- If your file has more than one kind of end-of-line marker, you
- will get messed-up line numbering.
-
- So that the cases of the switch statement do not have to concern
- themselves with the complications of reading beyond the end of the
- buffer, the buffer is guaranteed to have at least 3 characters in
- it (or however many are left in the file, if less) on entry to the
- switch. This is enough to handle trigraphs and the "\\\n\r" and
- "\\\r\n" cases.
-
- The end of the buffer is marked by a '\\', which, being a special
- character, guarantees we will exit the fast-scan loops and perform
- a refill. */
-
-long
-_cpp_read_and_prescan (pfile, fp, desc, len)
- cpp_reader *pfile;
- cpp_buffer *fp;
- int desc;
- size_t len;
-{
- U_CHAR *buf = (U_CHAR *) xmalloc (len);
- U_CHAR *ip, *op, *line_base;
- U_CHAR *ibase;
- unsigned long line;
- unsigned int deferred_newlines;
- size_t offset;
- int count = 0;
-
- offset = 0;
- deferred_newlines = 0;
- op = buf;
- line_base = buf;
- line = 1;
- ibase = pfile->input_buffer + 3;
- ip = ibase;
- ip[-1] = '\0'; /* Guarantee no match with \n for SPECCASE_CR */
+ return 0;
+}
- for (;;)
+/* Adds backslashes before all backslashes and double quotes appearing
+ in strings. Non-printable characters are converted to octal. */
+static U_CHAR *
+quote_string (dest, src, len)
+ U_CHAR *dest;
+ const U_CHAR *src;
+ unsigned int len;
+{
+ while (len--)
{
- U_CHAR *near_buff_end;
+ U_CHAR c = *src++;
- count = read (desc, ibase, pfile->input_buffer_len);
- if (count < 0)
- goto error;
-
- ibase[count] = '\\'; /* Marks end of buffer */
- if (count)
+ if (c == '\\' || c == '"')
{
- near_buff_end = pfile->input_buffer + count;
- offset += count;
- if (offset > len)
- {
- size_t delta_op;
- size_t delta_line_base;
- len = offset * 2;
- if (offset > len)
- /* len overflowed.
- This could happen if the file is larger than half the
- maximum address space of the machine. */
- goto too_big;
-
- delta_op = op - buf;
- delta_line_base = line_base - buf;
- buf = (U_CHAR *) xrealloc (buf, len);
- op = buf + delta_op;
- line_base = buf + delta_line_base;
- }
+ *dest++ = '\\';
+ *dest++ = c;
}
else
{
- if (ip == ibase)
- break;
- /* Allow normal processing of the (at most 2) remaining
- characters. The end-of-buffer marker is still present
- and prevents false matches within the switch. */
- near_buff_end = ibase - 1;
- }
-
- for (;;)
- {
- unsigned int span;
-
- /* Deal with \-newline, potentially in the middle of a token. */
- if (deferred_newlines)
+ if (ISPRINT (c))
+ *dest++ = c;
+ else
{
- if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
- {
- /* Previous was not white space. Skip to white
- space, if we can, before outputting the \r's */
- span = 0;
- while (ip[span] != ' '
- && ip[span] != '\t'
- && ip[span] != '\n'
- && NORMAL(ip[span]))
- span++;
- memcpy (op, ip, span);
- op += span;
- ip += span;
- if (! NORMAL(ip[0]))
- goto do_speccase;
- }
- while (deferred_newlines)
- deferred_newlines--, *op++ = '\r';
+ sprintf ((char *) dest, "\\%03o", c);
+ dest += 4;
}
+ }
+ }
- /* Copy as much as we can without special treatment. */
- span = 0;
- while (NORMAL (ip[span])) span++;
- memcpy (op, ip, span);
- op += span;
- ip += span;
+ return dest;
+}
- do_speccase:
- if (ip > near_buff_end) /* Do we have enough chars? */
- break;
- switch (chartab[*ip++])
- {
- case SPECCASE_CR: /* \r */
- if (ip[-2] != '\n')
- {
- if (*ip == '\n')
- ip++;
- *op++ = '\n';
- }
- break;
+/* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a
+ CPP_STRING token containing TEXT in quoted form. */
+static cpp_token *
+make_string_token (token, text, len)
+ cpp_token *token;
+ const U_CHAR *text;
+ unsigned int len;
+{
+ U_CHAR *buf;
+
+ buf = (U_CHAR *) xmalloc (len * 4);
+ token->type = CPP_STRING;
+ token->flags = 0;
+ token->val.str.text = buf;
+ token->val.str.len = quote_string (buf, text, len) - buf;
+ return token;
+}
- case SPECCASE_BACKSLASH: /* \ */
- if (*ip == '\n')
- {
- deferred_newlines++;
- ip++;
- if (*ip == '\r') ip++;
- }
- else if (*ip == '\r')
- {
- deferred_newlines++;
- ip++;
- if (*ip == '\n') ip++;
- }
- else
- *op++ = '\\';
- break;
+/* Allocates and converts a temporary token to a CPP_NUMBER token,
+ evaluating to NUMBER. */
+static cpp_token *
+alloc_number_token (pfile, number)
+ cpp_reader *pfile;
+ int number;
+{
+ cpp_token *result;
+ char *buf;
+
+ result = get_temp_token (pfile);
+ buf = xmalloc (20);
+ sprintf (buf, "%d", number);
+
+ result->type = CPP_NUMBER;
+ result->flags = 0;
+ result->val.str.text = (U_CHAR *) buf;
+ result->val.str.len = strlen (buf);
+ return result;
+}
- case SPECCASE_QUESTION: /* ? */
- {
- unsigned int d, t;
-
- *op++ = '?'; /* Normal non-trigraph case */
- if (ip[0] != '?')
- break;
-
- d = ip[1];
- t = chartab[d];
- if (NONTRI (t))
- break;
-
- if (CPP_OPTION (pfile, warn_trigraphs))
- {
- unsigned long col;
- line_base = find_position (line_base, op, &line);
- col = op - line_base + 1;
- if (CPP_OPTION (pfile, trigraphs))
- cpp_warning_with_line (pfile, line, col,
- "trigraph ??%c converted to %c", d, t);
- else
- cpp_warning_with_line (pfile, line, col,
- "trigraph ??%c ignored", d);
- }
-
- ip += 2;
- if (CPP_OPTION (pfile, trigraphs))
- {
- op[-1] = t; /* Overwrite '?' */
- if (t == '\\')
- {
- op--;
- *--ip = '\\';
- goto do_speccase; /* May need buffer refill */
- }
- }
- else
- {
- *op++ = '?';
- *op++ = d;
- }
- }
- break;
- }
+/* Returns a temporary token from the temporary token store of PFILE. */
+static cpp_token *
+get_temp_token (pfile)
+ cpp_reader *pfile;
+{
+ if (pfile->temp_used == pfile->temp_alloced)
+ {
+ if (pfile->temp_used == pfile->temp_cap)
+ {
+ pfile->temp_cap += pfile->temp_cap + 20;
+ pfile->temp_tokens = (cpp_token **) xrealloc
+ (pfile->temp_tokens, pfile->temp_cap * sizeof (cpp_token *));
}
- /* Copy previous char plus unprocessed (at most 2) chars
- to beginning of buffer, refill it with another
- read(), and continue processing */
- memmove (ip - count - 1, ip - 1, 4 - (ip - near_buff_end));
- ip -= count;
+ pfile->temp_tokens[pfile->temp_alloced++] = (cpp_token *) xmalloc
+ (sizeof (cpp_token));
}
- if (offset == 0)
- return 0;
+ return pfile->temp_tokens[pfile->temp_used++];
+}
- if (op[-1] != '\n')
+/* Release (not free) for re-use the temporary tokens of PFILE. */
+static void
+release_temp_tokens (pfile)
+ cpp_reader *pfile;
+{
+ while (pfile->temp_used)
{
- unsigned long col;
- line_base = find_position (line_base, op, &line);
- col = op - line_base + 1;
- cpp_warning_with_line (pfile, line, col, "no newline at end of file");
- if (offset + 1 > len)
- {
- len += 1;
- if (offset + 1 > len)
- goto too_big;
- buf = (U_CHAR *) xrealloc (buf, len);
- op = buf + offset;
- }
- *op++ = '\n';
- }
-
- fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
- return op - buf;
+ cpp_token *token = pfile->temp_tokens[--pfile->temp_used];
- too_big:
- cpp_notice (pfile, "%s is too large (>%lu bytes)", fp->ihash->name,
- (unsigned long)offset);
- free (buf);
- return -1;
-
- error:
- cpp_error_from_errno (pfile, fp->ihash->name);
- free (buf);
- return -1;
+ if (TOKEN_SPELL (token) == SPELL_STRING)
+ {
+ free ((char *) token->val.str.text);
+ token->val.str.text = 0;
+ }
+ }
}
-/* Allocate pfile->input_buffer, and initialize chartab[]
- if it hasn't happened already. */
-
+/* Free all of PFILE's dynamically-allocated temporary tokens. */
void
-_cpp_init_input_buffer (pfile)
+_cpp_free_temp_tokens (pfile)
cpp_reader *pfile;
{
- U_CHAR *tmp;
+ if (pfile->temp_tokens)
+ {
+ /* It is possible, though unlikely (looking for '(' of a funlike
+ macro into EOF), that we haven't released the tokens yet. */
+ release_temp_tokens (pfile);
+ while (pfile->temp_alloced)
+ free (pfile->temp_tokens[--pfile->temp_alloced]);
+ free (pfile->temp_tokens);
+ }
- init_chartab ();
- init_token_list (pfile, &pfile->directbuf, 0);
+ if (pfile->date)
+ {
+ free ((char *) pfile->date->val.str.text);
+ free (pfile->date);
+ free ((char *) pfile->time->val.str.text);
+ free (pfile->time);
+ }
+}
- /* Determine the appropriate size for the input buffer. Normal C
- source files are smaller than eight K. */
- /* 8Kbytes of buffer proper, 1 to detect running off the end without
- address arithmetic all the time, and 3 for pushback during buffer
- refill, in case there's a potential trigraph or end-of-line
- digraph at the end of a block. */
+/* Copy TOKEN into a temporary token from PFILE's store. */
+static cpp_token *
+duplicate_token (pfile, token)
+ cpp_reader *pfile;
+ const cpp_token *token;
+{
+ cpp_token *result = get_temp_token (pfile);
- tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
- pfile->input_buffer = tmp;
- pfile->input_buffer_len = 8192;
+ *result = *token;
+ if (TOKEN_SPELL (token) == SPELL_STRING)
+ {
+ U_CHAR *buff = (U_CHAR *) xmalloc (token->val.str.len);
+ memcpy (buff, token->val.str.text, token->val.str.len);
+ result->val.str.text = buff;
+ }
+ return result;
}
-#if 0
-
-/* Lexing algorithm.
-
- The original lexer in cpplib was made up of two passes: a first pass
- that replaced trigraphs and deleted esacped newlines, and a second
- pass that tokenized the result of the first pass. Tokenisation was
- performed by peeking at the next character in the input stream. For
- example, if the input stream contained "~=", the handler for the ~
- character would peek at the next character, and if it were a '='
- would skip over it, and return a "~=" token, otherwise it would
- return just the "~" token.
-
- To implement a single-pass lexer, this peeking ahead is unworkable.
- An arbitrary number of escaped newlines, and trigraphs (in particular
- ??/ which translates to the escape \), could separate the '~' and '='
- in the input stream, yet the next token is still a "~=".
-
- Suppose instead that we lex by one logical line at a time, producing
- a token list or stack for each logical line, and when seeing the '~'
- push a CPP_COMPLEMENT token on the list. Then if the '~' is part of
- a longer token ("~=") we know we must see the remainder of the token
- by the time we reach the end of the logical line. Thus we can have
- the '=' handler look at the previous token (at the end of the list /
- top of the stack) and see if it is a "~" token, and if so, instead of
- pushing a "=" token revise the existing token to be a "~=" token.
-
- This works in the presence of escaped newlines, because the '\' would
- have been pushed on the top of the stack as a CPP_BACKSLASH. The
- newline ('\n' or '\r') handler looks at the token at the top of the
- stack to see if it is a CPP_BACKSLASH, and if so discards both.
- Otherwise it pushes the newline (CPP_VSPACE) token as normal. Hence
- the '=' handler would never see any intervening escaped newlines.
-
- To make trigraphs work in this context, as in precedence trigraphs
- are highest and converted before anything else, the '?' handler does
- lookahead to see if it is a trigraph, and if so skips the trigraph
- and pushes the token it represents onto the top of the stack. This
- also works in the particular case of a CPP_BACKSLASH trigraph.
-
- To the preprocessor, whitespace is only significant to the point of
- knowing whether whitespace precedes a particular token. For example,
- the '=' handler needs to know whether there was whitespace between it
- and a "~" token on the top of the stack, to make the token conversion
- decision correctly. So each token has a PREV_WHITESPACE flag to
- indicate this - the standard permits consecutive whitespace to be
- regarded as a single space. The compiler front ends are not
- interested in whitespace at all; they just require a token stream.
- Another place where whitespace is significant to the preprocessor is
- a #define statment - if there is whitespace between the macro name
- and an initial "(" token the macro is "object-like", otherwise it is
- a function-like macro that takes arguments.
-
- However, all is not rosy. Parsing of identifiers, numbers, comments
- and strings becomes trickier because of the possibility of raw
- trigraphs and escaped newlines in the input stream.
-
- The trigraphs are three consecutive characters beginning with two
- question marks. A question mark is not a valid as part of a number
- or identifier, so parsing of a number or identifier terminates
- normally upon reaching it, returning to the mainloop which handles
- the trigraph just like it would in any other position. Similarly for
- the backslash of a backslash-newline combination. So we just need
- the escaped-newline dropper in the mainloop to check if the token on
- the top of the stack is a number or identifier, and to continue the
- processing of the token as if nothing had happened.
-
- For strings, we replace trigraphs whenever we reach a quote or
- newline, because there might be a backslash trigraph escaping them.
- We need to be careful that we start trigraph replacing from where we
- left off previously, because it is possible for a first scan to leave
- "fake" trigraphs that a second scan would pick up as real (e.g. the
- sequence "????\\n=" would find a fake ??= trigraph after removing the
- escaped newline.)
-
- For line comments, on reaching a newline we scan the previous
- character(s) to see if it escaped, and continue if it is. Block
- comments ignore everything and just focus on finding the comment
- termination mark. The only difficult thing, and it is surprisingly
- tricky, is checking if an asterisk precedes the final slash since
- they could be separated by escaped newlines. If the preprocessor is
- invoked with the output comments option, we don't bother removing
- escaped newlines and replacing trigraphs for output.
-
- Finally, numbers can begin with a period, which is pushed initially
- as a CPP_DOT token in its own right. The digit handler checks if the
- previous token was a CPP_DOT not separated by whitespace, and if so
- pops it off the stack and pushes a period into the number's buffer
- before calling the number parser.
+/* Determine whether two tokens can be pasted together, and if so,
+ what the resulting token is. Returns CPP_EOF if the tokens cannot
+ be pasted, or the appropriate type for the merged token if they
+ can. */
+enum cpp_ttype
+_cpp_can_paste (pfile, token1, token2, digraph)
+ cpp_reader * pfile;
+ const cpp_token *token1, *token2;
+ int* digraph;
+{
+ enum cpp_ttype a = token1->type, b = token2->type;
+ int cxx = CPP_OPTION (pfile, cplusplus);
-*/
+ /* Treat named operators as if they were ordinary NAMEs. */
+ if (token1->flags & NAMED_OP)
+ a = CPP_NAME;
+ if (token2->flags & NAMED_OP)
+ b = CPP_NAME;
-static void expand_comment_space PARAMS ((cpp_toklist *));
-void init_trigraph_map PARAMS ((void));
-static unsigned char* trigraph_replace PARAMS ((cpp_reader *, unsigned char *,
- unsigned char *));
-static const unsigned char *backslash_start PARAMS ((cpp_reader *,
- const unsigned char *));
-static int skip_block_comment PARAMS ((cpp_reader *));
-static int skip_line_comment PARAMS ((cpp_reader *));
-static void skip_whitespace PARAMS ((cpp_reader *, int));
-static void parse_name PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *));
-static void parse_number PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *));
-static void parse_string PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *,
- unsigned int));
-static int trigraph_ok PARAMS ((cpp_reader *, const unsigned char *));
-static void copy_comment PARAMS ((cpp_toklist *, const unsigned char *,
- unsigned int, unsigned int, unsigned int));
-void _cpp_lex_line PARAMS ((cpp_reader *, cpp_toklist *));
-
-static void _cpp_output_list PARAMS ((cpp_reader *, cpp_toklist *));
-
-unsigned int spell_char PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *token));
-unsigned int spell_string PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *token));
-unsigned int spell_comment PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *token));
-unsigned int spell_name PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *token));
-unsigned int spell_other PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *token));
+ if (a <= CPP_LAST_EQ && b == CPP_EQ)
+ return a + (CPP_EQ_EQ - CPP_EQ);
-typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
- cpp_token *));
+ switch (a)
+ {
+ case CPP_GREATER:
+ if (b == a) return CPP_RSHIFT;
+ if (b == CPP_QUERY && cxx) return CPP_MAX;
+ if (b == CPP_GREATER_EQ) return CPP_RSHIFT_EQ;
+ break;
+ case CPP_LESS:
+ if (b == a) return CPP_LSHIFT;
+ if (b == CPP_QUERY && cxx) return CPP_MIN;
+ if (b == CPP_LESS_EQ) return CPP_LSHIFT_EQ;
+ if (CPP_OPTION (pfile, digraphs))
+ {
+ if (b == CPP_COLON)
+ {*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
+ if (b == CPP_MOD)
+ {*digraph = 1; return CPP_OPEN_BRACE;} /* <% digraph */
+ }
+ break;
+
+ case CPP_PLUS: if (b == a) return CPP_PLUS_PLUS; break;
+ case CPP_AND: if (b == a) return CPP_AND_AND; break;
+ case CPP_OR: if (b == a) return CPP_OR_OR; break;
+
+ case CPP_MINUS:
+ if (b == a) return CPP_MINUS_MINUS;
+ if (b == CPP_GREATER) return CPP_DEREF;
+ break;
+ case CPP_COLON:
+ if (b == a && cxx) return CPP_SCOPE;
+ if (b == CPP_GREATER && CPP_OPTION (pfile, digraphs))
+ {*digraph = 1; return CPP_CLOSE_SQUARE;} /* :> digraph */
+ break;
+
+ case CPP_MOD:
+ if (CPP_OPTION (pfile, digraphs))
+ {
+ if (b == CPP_GREATER)
+ {*digraph = 1; return CPP_CLOSE_BRACE;} /* %> digraph */
+ if (b == CPP_COLON)
+ {*digraph = 1; return CPP_HASH;} /* %: digraph */
+ }
+ break;
+ case CPP_DEREF:
+ if (b == CPP_MULT && cxx) return CPP_DEREF_STAR;
+ break;
+ case CPP_DOT:
+ if (b == CPP_MULT && cxx) return CPP_DOT_STAR;
+ if (b == CPP_NUMBER) return CPP_NUMBER;
+ break;
+
+ case CPP_HASH:
+ if (b == a && (token1->flags & DIGRAPH) == (token2->flags & DIGRAPH))
+ /* %:%: digraph */
+ {*digraph = (token1->flags & DIGRAPH); return CPP_PASTE;}
+ break;
+
+ case CPP_NAME:
+ if (b == CPP_NAME) return CPP_NAME;
+ if (b == CPP_NUMBER
+ && is_numstart(token2->val.str.text[0])) return CPP_NAME;
+ if (b == CPP_CHAR
+ && token1->val.node == pfile->spec_nodes->n_L) return CPP_WCHAR;
+ if (b == CPP_STRING
+ && token1->val.node == pfile->spec_nodes->n_L) return CPP_WSTRING;
+ break;
+
+ case CPP_NUMBER:
+ if (b == CPP_NUMBER) return CPP_NUMBER;
+ if (b == CPP_NAME) return CPP_NUMBER;
+ if (b == CPP_DOT) return CPP_NUMBER;
+ /* Numbers cannot have length zero, so this is safe. */
+ if ((b == CPP_PLUS || b == CPP_MINUS)
+ && VALID_SIGN ('+', token1->val.str.text[token1->val.str.len - 1]))
+ return CPP_NUMBER;
+ break;
+
+ case CPP_OTHER:
+ if (CPP_OPTION (pfile, objc) && token1->val.aux == '@')
+ {
+ if (b == CPP_NAME) return CPP_NAME;
+ if (b == CPP_STRING) return CPP_OSTRING;
+ }
-/* Macros on a cpp_name. */
-#define INIT_NAME(list, name) \
- do {(name).len = 0; (name).offset = (list)->name_used;} while (0)
+ default:
+ break;
+ }
-#define IS_DIRECTIVE(list) (list->tokens[0].type == CPP_HASH)
-#define COLUMN(cur) ((cur) - buffer->line_base)
+ return CPP_EOF;
+}
-/* Maybe put these in the ISTABLE eventually. */
-#define IS_HSPACE(c) ((c) == ' ' || (c) == '\t')
-#define IS_NEWLINE(c) ((c) == '\n' || (c) == '\r')
+/* Check if TOKEN is to be ##-pasted with the token after it. */
+static const cpp_token *
+maybe_paste_with_next (pfile, token)
+ cpp_reader *pfile;
+ const cpp_token *token;
+{
+ cpp_token *pasted;
+ const cpp_token *second;
+ cpp_context *context = CURRENT_CONTEXT (pfile);
-/* Handle LF, CR, CR-LF and LF-CR style newlines. Assumes next
- character, if any, is in buffer. */
-#define handle_newline(cur, limit, c) \
- do {\
- if ((cur) < (limit) && *(cur) == '\r' + '\n' - c) \
- (cur)++; \
- CPP_BUMP_LINE_CUR (pfile, (cur)); \
- } while (0)
+ /* Is this token on the LHS of ## ? */
-#define IMMED_TOKEN() (!(cur_token->flags & PREV_WHITESPACE))
-#define PREV_TOKEN_TYPE (cur_token[-1].type)
+ while ((token->flags & PASTE_LEFT)
+ || ((context->flags & CONTEXT_PASTEL)
+ && context->posn == context->count))
+ {
+ /* Suppress macro expansion for next token, but don't conflict
+ with the other method of suppression. If it is an argument,
+ macro expansion within the argument will still occur. */
+ pfile->paste_level = pfile->cur_context;
+ second = _cpp_get_token (pfile);
+ pfile->paste_level = 0;
+ context = CURRENT_CONTEXT (pfile);
+
+ /* Ignore placemarker argument tokens (cannot be from an empty
+ macro since macros are not expanded). */
+ if (token->type == CPP_PLACEMARKER)
+ pasted = duplicate_token (pfile, second);
+ else if (second->type == CPP_PLACEMARKER)
+ {
+ /* GCC has special extended semantics for , ## b where b is
+ a varargs parameter: the comma disappears if b was given
+ no actual arguments (not merely if b is an empty
+ argument). */
+ if (token->type == CPP_COMMA && (context->flags & CONTEXT_VARARGS))
+ pasted = duplicate_token (pfile, second);
+ else
+ pasted = duplicate_token (pfile, token);
+ }
+ else
+ {
+ int digraph = 0;
+ enum cpp_ttype type = _cpp_can_paste (pfile, token, second, &digraph);
-#define SPELL_TEXT 0
-#define SPELL_HANDLER 1
-#define SPELL_NONE 2
-#define SPELL_EOL 3
+ if (type == CPP_EOF)
+ {
+ if (CPP_OPTION (pfile, warn_paste))
+ {
+ /* Do not complain about , ## <whatever> if
+ <whatever> came from a variable argument, because
+ the author probably intended the ## to trigger
+ the special extended semantics (see above). */
+ if (token->type == CPP_COMMA
+ && (context->flags & CONTEXT_VARARGS))
+ /* no warning */;
+ else
+ cpp_warning (pfile,
+ "pasting would not give a valid preprocessing token");
+ }
+ _cpp_push_token (pfile, second);
+ /* A short term hack to safely clear the PASTE_LEFT flag. */
+ pasted = duplicate_token (pfile, token);
+ pasted->flags &= ~PASTE_LEFT;
+ return pasted;
+ }
-#define T(e, s) {SPELL_TEXT, s},
-#define H(e, s) {SPELL_HANDLER, s},
-#define N(e, s) {SPELL_NONE, s},
-#define E(e, s) {SPELL_EOL, s},
+ if (type == CPP_NAME || type == CPP_NUMBER)
+ {
+ /* Join spellings. */
+ U_CHAR *buf, *end;
-static const struct token_spelling
-{
- char type;
- PTR speller;
-} token_spellings [N_TTYPES + 1] = {TTYPE_TABLE {0, 0} };
+ pasted = get_temp_token (pfile);
+ buf = (U_CHAR *) alloca (TOKEN_LEN (token) + TOKEN_LEN (second));
+ end = spell_token (pfile, token, buf);
+ end = spell_token (pfile, second, end);
+ *end = '\0';
-#undef T
-#undef H
-#undef N
-#undef E
+ if (type == CPP_NAME)
+ pasted->val.node = cpp_lookup (pfile, buf, end - buf);
+ else
+ {
+ pasted->val.str.text = uxstrdup (buf);
+ pasted->val.str.len = end - buf;
+ }
+ }
+ else if (type == CPP_WCHAR || type == CPP_WSTRING
+ || type == CPP_OSTRING)
+ pasted = duplicate_token (pfile, second);
+ else
+ {
+ pasted = get_temp_token (pfile);
+ pasted->val.integer = 0;
+ }
-static const unsigned char *digraph_spellings [] = {"%:", "%:%:", "<:",
- ":>", "<%", "%>"};
+ pasted->type = type;
+ pasted->flags = digraph ? DIGRAPH : 0;
-static void
-expand_comment_space (list)
- cpp_toklist *list;
-{
- if (list->comments_cap == 0)
- {
- list->comments_cap = 10;
- list->comments = (cpp_token *)
- xmalloc (list->comments_cap * sizeof (cpp_token));
- }
- else
- {
- list->comments_cap *= 2;
- list->comments = (cpp_token *)
- xrealloc (list->comments, list->comments_cap);
+ if (type == CPP_NAME && pasted->val.node->type == T_OPERATOR)
+ {
+ pasted->type = pasted->val.node->value.code;
+ pasted->flags |= NAMED_OP;
+ }
+ }
+
+ /* The pasted token gets the whitespace flags and position of the
+ first token, the PASTE_LEFT flag of the second token, plus the
+ PASTED flag to indicate it is the result of a paste. However, we
+ want to preserve the DIGRAPH flag. */
+ pasted->flags &= ~(PREV_WHITE | BOL | PASTE_LEFT);
+ pasted->flags |= ((token->flags & (PREV_WHITE | BOL))
+ | (second->flags & PASTE_LEFT) | PASTED);
+ pasted->col = token->col;
+ pasted->line = token->line;
+
+ /* See if there is another token to be pasted onto the one we just
+ constructed. */
+ token = pasted;
+ /* and loop */
}
+ return token;
}
-void
-cpp_free_token_list (list)
- cpp_toklist *list;
+/* Convert a token sequence to a single string token according to the
+ rules of the ISO C #-operator. */
+#define INIT_SIZE 200
+static cpp_token *
+stringify_arg (pfile, token)
+ cpp_reader *pfile;
+ const cpp_token *token;
{
- if (list->comments)
- free (list->comments);
- free (list->tokens - 1);
- free (list->namebuf);
- free (list);
-}
+ cpp_token *result;
+ unsigned char *main_buf;
+ unsigned int prev_value, backslash_count = 0;
+ unsigned int buf_used = 0, whitespace = 0, buf_cap = INIT_SIZE;
-static char trigraph_map[256];
+ push_arg_context (pfile, token);
+ prev_value = prevent_macro_expansion (pfile);
+ main_buf = (unsigned char *) xmalloc (buf_cap);
-void
-init_trigraph_map ()
-{
- trigraph_map['='] = '#';
- trigraph_map['('] = '[';
- trigraph_map[')'] = ']';
- trigraph_map['/'] = '\\';
- trigraph_map['\''] = '^';
- trigraph_map['<'] = '{';
- trigraph_map['>'] = '}';
- trigraph_map['!'] = '|';
- trigraph_map['-'] = '~';
-}
+ result = get_temp_token (pfile);
+ ASSIGN_FLAGS_AND_POS (result, token);
-/* Call when a trigraph is encountered. It warns if necessary, and
- returns true if the trigraph should be honoured. END is the third
- character of a trigraph in the input stream. */
-static int
-trigraph_ok (pfile, end)
- cpp_reader *pfile;
- const unsigned char *end;
-{
- int accept = CPP_OPTION (pfile, trigraphs);
-
- if (CPP_OPTION (pfile, warn_trigraphs))
+ for (; (token = _cpp_get_token (pfile))->type != CPP_EOF; )
{
- unsigned int col = end - 1 - pfile->buffer->line_base;
- if (accept)
- cpp_warning_with_line (pfile, pfile->buffer->lineno, col,
- "trigraph ??%c converted to %c",
- (int) *end, (int) trigraph_map[*end]);
- else
- cpp_warning_with_line (pfile, pfile->buffer->lineno, col,
- "trigraph ??%c ignored", (int) *end);
- }
- return accept;
-}
+ int escape;
+ unsigned char *buf;
+ unsigned int len = TOKEN_LEN (token);
-/* Scan a string for trigraphs, warning or replacing them inline as
- appropriate. When parsing a string, we must call this routine
- before processing a newline character (if trigraphs are enabled),
- since the newline might be escaped by a preceding backslash
- trigraph sequence. Returns a pointer to the end of the name after
- replacement. */
+ if (token->type == CPP_PLACEMARKER)
+ continue;
-static unsigned char*
-trigraph_replace (pfile, src, limit)
- cpp_reader *pfile;
- unsigned char *src;
- unsigned char* limit;
-{
- unsigned char *dest;
+ escape = (token->type == CPP_STRING || token->type == CPP_WSTRING
+ || token->type == CPP_CHAR || token->type == CPP_WCHAR);
+ if (escape)
+ len *= 4 + 1;
- /* Starting with src[1], find two consecutive '?'. The case of no
- trigraphs is streamlined. */
-
- for (; src + 1 < limit; src += 2)
- {
- if (src[0] != '?')
- continue;
+ if (buf_used + len > buf_cap)
+ {
+ buf_cap = buf_used + len + INIT_SIZE;
+ main_buf = xrealloc (main_buf, buf_cap);
+ }
- /* Make src point to the 1st (NOT 2nd) of two consecutive '?'s. */
- if (src[-1] == '?')
- src--;
- else if (src + 2 == limit || src[1] != '?')
- continue;
+ if (whitespace && (token->flags & PREV_WHITE))
+ main_buf[buf_used++] = ' ';
- /* Check if it really is a trigraph. */
- if (trigraph_map[src[2]] == 0)
- continue;
+ if (escape)
+ buf = (unsigned char *) xmalloc (len);
+ else
+ buf = main_buf + buf_used;
+
+ len = spell_token (pfile, token, buf) - buf;
+ if (escape)
+ {
+ buf_used = quote_string (&main_buf[buf_used], buf, len) - main_buf;
+ free (buf);
+ }
+ else
+ buf_used += len;
- dest = src;
- goto trigraph_found;
+ whitespace = 1;
+ if (token->type == CPP_BACKSLASH)
+ backslash_count++;
+ else
+ backslash_count = 0;
}
- return limit;
- /* Now we have a trigraph, we need to scan the remaining buffer, and
- copy-shifting its contents left if replacement is enabled. */
- for (; src + 2 < limit; dest++, src++)
- if ((*dest = *src) == '?' && src[1] == '?' && trigraph_map[src[2]])
- {
- trigraph_found:
- src += 2;
- if (trigraph_ok (pfile, pfile->buffer->cur - (limit - src)))
- *dest = trigraph_map[*src];
- }
-
- /* Copy remaining (at most 2) characters. */
- while (src < limit)
- *dest++ = *src++;
- return dest;
+ /* Ignore the final \ of invalid string literals. */
+ if (backslash_count & 1)
+ {
+ cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
+ buf_used--;
+ }
+
+ result->type = CPP_STRING;
+ result->val.str.text = main_buf;
+ result->val.str.len = buf_used;
+ restore_macro_expansion (pfile, prev_value);
+ return result;
}
-/* If CUR is a backslash or the end of a trigraphed backslash, return
- a pointer to its beginning, otherwise NULL. We don't read beyond
- the buffer start, because there is the start of the comment in the
- buffer. */
-static const unsigned char *
-backslash_start (pfile, cur)
+/* Allocate more room on the context stack of PFILE. */
+static void
+expand_context_stack (pfile)
cpp_reader *pfile;
- const unsigned char *cur;
{
- if (cur[0] == '\\')
- return cur;
- if (cur[0] == '/' && cur[-1] == '?' && cur[-2] == '?'
- && trigraph_ok (pfile, cur))
- return cur - 2;
- return 0;
+ pfile->context_cap += pfile->context_cap + 20;
+ pfile->contexts = (cpp_context *)
+ xrealloc (pfile->contexts, pfile->context_cap * sizeof (cpp_context));
}
-/* Skip a C-style block comment. This is probably the trickiest
- handler. We find the end of the comment by seeing if an asterisk
- is before every '/' we encounter. The nasty complication is that a
- previous asterisk may be separated by one or more escaped newlines.
- Returns non-zero if comment terminated by EOF, zero otherwise. */
+/* Push the context of macro NODE onto the context stack. TOKEN is
+ the CPP_NAME token invoking the macro. */
static int
-skip_block_comment (pfile)
+push_macro_context (pfile, token)
cpp_reader *pfile;
+ const cpp_token *token;
{
- cpp_buffer *buffer = pfile->buffer;
- const unsigned char *char_after_star = 0;
- register const unsigned char *cur = buffer->cur;
- int seen_eof = 0;
-
- /* Inner loop would think the comment has ended if the first comment
- character is a '/'. Avoid this and keep the inner loop clean by
- skipping such a character. */
- if (cur < buffer->rlimit && cur[0] == '/')
- cur++;
-
- for (; cur < buffer->rlimit; )
+ unsigned char orig_flags;
+ macro_args *args;
+ cpp_context *context;
+ cpp_hashnode *node = token->val.node;
+
+ /* Token's flags may change when parsing args containing a nested
+ invocation of this macro. */
+ orig_flags = token->flags & (PREV_WHITE | BOL);
+ args = 0;
+ if (node->value.expansion->paramc >= 0)
{
- unsigned char c = *cur++;
-
- /* People like decorating comments with '*', so check for
- '/' instead for efficiency. */
- if (c == '/')
- {
- if (cur[-2] == '*' || cur - 1 == char_after_star)
- goto out;
-
- /* Warn about potential nested comments, but not when
- the final character inside the comment is a '/'.
- Don't bother to get it right across escaped newlines. */
- if (CPP_OPTION (pfile, warn_comments) && cur + 1 < buffer->rlimit
- && cur[0] == '*' && cur[1] != '/')
- {
- buffer->cur = cur;
- cpp_warning (pfile, "'/*' within comment");
- }
- }
- else if (IS_NEWLINE(c))
+ unsigned int error, prev_nme;
+
+ /* Allocate room for the argument contexts, and parse them. */
+ args = (macro_args *) xmalloc (sizeof (macro_args));
+ args->ends = (unsigned int *)
+ xmalloc (node->value.expansion->paramc * sizeof (unsigned int));
+ args->tokens = 0;
+ args->capacity = 0;
+ args->used = 0;
+
+ prev_nme = prevent_macro_expansion (pfile);
+ pfile->args = args;
+ error = parse_args (pfile, node, args);
+ pfile->args = 0;
+ restore_macro_expansion (pfile, prev_nme);
+ if (error)
{
- const unsigned char* bslash = backslash_start (pfile, cur - 2);
-
- handle_newline (cur, buffer->rlimit, c);
- /* Work correctly if there is an asterisk before an
- arbirtrarily long sequence of escaped newlines. */
- if (bslash && (bslash[-1] == '*' || bslash == char_after_star))
- char_after_star = cur;
- else
- char_after_star = 0;
+ free_macro_args (args);
+ return 1;
}
+ /* Set the level after the call to parse_args. */
+ args->level = pfile->cur_context;
}
- seen_eof = 1;
-
- out:
- buffer->cur = cur;
- return seen_eof;
-}
-/* Skip a C++ or Chill line comment. Handles escaped newlines.
- Returns non-zero if a multiline comment. */
-static int
-skip_line_comment (pfile)
- cpp_reader *pfile;
-{
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
- int multiline = 0;
+ /* Now push its context. */
+ pfile->cur_context++;
+ if (pfile->cur_context == pfile->context_cap)
+ expand_context_stack (pfile);
- for (; cur < buffer->rlimit; )
- {
- unsigned char c = *cur++;
+ context = CURRENT_CONTEXT (pfile);
+ context->u.list = node->value.expansion;
+ context->args = args;
+ context->posn = 0;
+ context->count = context->u.list->tokens_used;
+ context->level = pfile->cur_context;
+ context->flags = 0;
+ context->pushed_token = 0;
- if (IS_NEWLINE (c))
- {
- /* Check for a (trigaph?) backslash escaping the newline. */
- if (!backslash_start (pfile, cur - 2))
- goto out;
- multiline = 1;
- handle_newline (cur, buffer->rlimit, c);
- }
- }
- cur++;
+ /* Set the flags of the first token. We know there must
+ be one, empty macros are a single placemarker token. */
+ MODIFY_FLAGS_AND_POS (&context->u.list->tokens[0], token, orig_flags);
- out:
- buffer->cur = cur - 1; /* Leave newline for caller. */
- return multiline;
+ return 0;
}
-/* Skips whitespace, stopping at next non-whitespace character. */
+/* Push an argument to the current macro onto the context stack.
+ TOKEN is the MACRO_ARG token representing the argument expansion. */
static void
-skip_whitespace (pfile, in_directive)
+push_arg_context (pfile, token)
cpp_reader *pfile;
- int in_directive;
+ const cpp_token *token;
{
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
- unsigned short null_count = 0;
-
- for (; cur < buffer->rlimit; )
- {
- unsigned char c = *cur++;
+ cpp_context *context;
+ macro_args *args;
+
+ pfile->cur_context++;
+ if (pfile->cur_context == pfile->context_cap)
+ expand_context_stack (pfile);
+
+ context = CURRENT_CONTEXT (pfile);
+ args = context[-1].args;
+
+ context->count = token->val.aux ? args->ends[token->val.aux - 1]: 0;
+ context->u.arg = args->tokens + context->count;
+ context->count = args->ends[token->val.aux] - context->count;
+ context->args = 0;
+ context->posn = 0;
+ context->level = args->level;
+ context->flags = CONTEXT_ARG | CONTEXT_RAW;
+ if ((context[-1].u.list->flags & VAR_ARGS)
+ && token->val.aux + 1 == (unsigned) context[-1].u.list->paramc)
+ context->flags |= CONTEXT_VARARGS;
+ context->pushed_token = 0;
+
+ /* Set the flags of the first token. There is one. */
+ {
+ const cpp_token *first = context->u.arg[0];
+ if (!first)
+ first = context->u.arg[1];
- if (IS_HSPACE(c)) /* FIXME: Fix ISTABLE. */
- continue;
- if (!is_space(c) || IS_NEWLINE (c)) /* Main loop handles newlines. */
- goto out;
- if (c == '\0')
- null_count++;
- /* Mut be '\f' or '\v' */
- else if (in_directive && CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "%s in preprocessing directive",
- c == '\f' ? "formfeed" : "vertical tab");
- }
- cur++;
+ MODIFY_FLAGS_AND_POS ((cpp_token *) first, token,
+ token->flags & (PREV_WHITE | BOL));
+ }
- out:
- buffer->cur = cur - 1;
- if (null_count)
- cpp_warning (pfile, null_count > 1 ? "embedded null characters ignored"
- : "embedded null character ignored");
+ if (token->flags & PASTE_LEFT)
+ context->flags |= CONTEXT_PASTEL;
+ if (pfile->paste_level)
+ context->flags |= CONTEXT_PASTER;
}
-/* Parse (append) an identifier. */
-static void
-parse_name (pfile, list, name)
+/* "Unget" a token. It is effectively inserted in the token queue and
+ will be returned by the next call to get_raw_token. */
+void
+_cpp_push_token (pfile, token)
cpp_reader *pfile;
- cpp_toklist *list;
- cpp_name *name;
+ const cpp_token *token;
{
- const unsigned char *name_limit;
- unsigned char *namebuf;
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
-
- expanded:
- name_limit = list->namebuf + list->name_cap;
- namebuf = list->namebuf + list->name_used;
+ cpp_context *context = CURRENT_CONTEXT (pfile);
- for (; cur < buffer->rlimit && namebuf < name_limit; )
+ if (context->posn > 0)
{
- unsigned char c = *namebuf = *cur; /* Copy a single char. */
+ const cpp_token *prev;
+ if (IS_ARG_CONTEXT (context))
+ prev = context->u.arg[context->posn - 1];
+ else
+ prev = &context->u.list->tokens[context->posn - 1];
- if (! is_idchar(c))
- goto out;
- namebuf++;
- cur++;
- if (c == '$' && CPP_PEDANTIC (pfile))
+ if (prev == token)
{
- buffer->cur = cur;
- cpp_pedwarn (pfile, "'$' character in identifier");
+ context->posn--;
+ return;
}
}
- /* Run out of name space? */
- if (cur < buffer->rlimit)
- {
- list->name_used = namebuf - list->namebuf;
- auto_expand_name_space (list);
- goto expanded;
- }
-
- out:
- buffer->cur = cur;
- name->len = namebuf - (list->namebuf + name->offset);
- list->name_used = namebuf - list->namebuf;
+ if (context->pushed_token)
+ cpp_ice (pfile, "two tokens pushed in a row");
+ if (token->type != CPP_EOF)
+ context->pushed_token = token;
+ /* Don't push back a directive's CPP_EOF, step back instead. */
+ else if (pfile->cur_context == 0)
+ pfile->contexts[0].posn--;
}
-/* Parse (append) a number. */
-
-#define VALID_SIGN(c, prevc) \
- (((c) == '+' || (c) == '-') && \
- ((prevc) == 'e' || (prevc) == 'E' \
- || (((prevc) == 'p' || (prevc) == 'P') && !CPP_OPTION (pfile, c89))))
-
-static void
-parse_number (pfile, list, name)
+/* Handle a preprocessing directive. TOKEN is the CPP_HASH token
+ introducing the directive. */
+void
+_cpp_process_directive (pfile, token)
cpp_reader *pfile;
- cpp_toklist *list;
- cpp_name *name;
+ const cpp_token *token;
{
- const unsigned char *name_limit;
- unsigned char *namebuf;
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
-
- expanded:
- name_limit = list->namebuf + list->name_cap;
- namebuf = list->namebuf + list->name_used;
+ const struct directive *d = pfile->token_list.directive;
+ int prev_nme = 0;
+
+ /* Skip over the directive name. */
+ if (token[1].type == CPP_NAME)
+ _cpp_get_raw_token (pfile);
+ else if (token[1].type != CPP_NUMBER)
+ cpp_ice (pfile, "directive begins with %s?!", TOKEN_NAME (token));
+
+ if (! (d->flags & EXPAND))
+ prev_nme = prevent_macro_expansion (pfile);
+ (void) (*d->handler) (pfile);
+ if (! (d->flags & EXPAND))
+ restore_macro_expansion (pfile, prev_nme);
+ _cpp_skip_rest_of_line (pfile);
+}
- for (; cur < buffer->rlimit && namebuf < name_limit; )
+/* The external interface to return the next token. All macro
+ expansion and directive processing is handled internally, the
+ caller only ever sees the output after preprocessing. */
+const cpp_token *
+cpp_get_token (pfile)
+ cpp_reader *pfile;
+{
+ const cpp_token *token;
+ /* Loop till we hit a non-directive, non-placemarker token. */
+ for (;;)
{
- unsigned char c = *namebuf = *cur; /* Copy a single char. */
+ token = _cpp_get_token (pfile);
- /* Perhaps we should accept '$' here if we accept it for
- identifiers. We know namebuf[-1] is safe, because for c to
- be a sign we must have pushed at least one character. */
- if (!is_numchar (c) && c != '.' && ! VALID_SIGN (c, namebuf[-1]))
- goto out;
+ if (token->type == CPP_PLACEMARKER)
+ continue;
- namebuf++;
- cur++;
- }
+ if (token->type == CPP_HASH && token->flags & BOL
+ && pfile->token_list.directive)
+ {
+ _cpp_process_directive (pfile, token);
+ continue;
+ }
- /* Run out of name space? */
- if (cur < buffer->rlimit)
- {
- list->name_used = namebuf - list->namebuf;
- auto_expand_name_space (list);
- goto expanded;
+ return token;
}
-
- out:
- buffer->cur = cur;
- name->len = namebuf - (list->namebuf + name->offset);
- list->name_used = namebuf - list->namebuf;
}
-/* Places a string terminated by an unescaped TERMINATOR into a
- cpp_name, which should be expandable and thus at the top of the
- list's stack. Handles embedded trigraphs, if necessary, and
- escaped newlines.
-
- Can be used for character constants (terminator = '\''), string
- constants ('"'), angled headers ('>') and assertions (')'). */
-
-static void
-parse_string (pfile, list, name, terminator)
+/* The internal interface to return the next token. There are two
+ differences between the internal and external interfaces: the
+ internal interface may return a PLACEMARKER token, and it does not
+ process directives. */
+const cpp_token *
+_cpp_get_token (pfile)
cpp_reader *pfile;
- cpp_toklist *list;
- cpp_name *name;
- unsigned int terminator;
{
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
- const unsigned char *name_limit;
- unsigned char *namebuf;
- unsigned int null_count = 0;
- int trigraphed_len = 0;
-
- expanded:
- name_limit = list->namebuf + list->name_cap;
- namebuf = list->namebuf + list->name_used;
+ const cpp_token *token, *old_token;
+ cpp_hashnode *node;
- for (; cur < buffer->rlimit && namebuf < name_limit; )
+ /* Loop until we hit a non-macro token. */
+ for (;;)
{
- unsigned int c = *namebuf++ = *cur++; /* Copy a single char. */
-
- if (c == '\0')
- null_count++;
- else if (c == terminator || IS_NEWLINE (c))
- {
- unsigned char* name_start = list->namebuf + name->offset;
+ token = get_raw_token (pfile);
- /* Needed for trigraph_replace and multiline string warning. */
- buffer->cur = cur;
-
- /* Scan for trigraphs before checking if backslash-escaped. */
- if (CPP_OPTION (pfile, trigraphs)
- || CPP_OPTION (pfile, warn_trigraphs))
- {
- namebuf = trigraph_replace (pfile, name_start + trigraphed_len,
- namebuf);
- trigraphed_len = namebuf - 2 - (name_start + trigraphed_len);
- if (trigraphed_len < 0)
- trigraphed_len = 0;
- }
-
- namebuf--; /* Drop the newline / terminator from the name. */
- if (IS_NEWLINE (c))
- {
- /* Drop a backslash newline, and continue. */
- if (namebuf[-1] == '\\')
- {
- handle_newline (cur, buffer->rlimit, c);
- namebuf--;
- continue;
- }
-
- cur--;
-
- /* In Fortran and assembly language, silently terminate
- strings of either variety at end of line. This is a
- kludge around not knowing where comments are in these
- languages. */
- if (CPP_OPTION (pfile, lang_fortran)
- || CPP_OPTION (pfile, lang_asm))
- goto out;
-
- /* Character constants, headers and asserts may not
- extend over multiple lines. In Standard C, neither
- may strings. We accept multiline strings as an
- extension, but not in directives. */
- if (terminator != '"' || IS_DIRECTIVE (list))
- goto unterminated;
-
- cur++; /* Move forwards again. */
-
- if (pfile->multiline_string_line == 0)
- {
- pfile->multiline_string_line = list->line;
- if (CPP_PEDANTIC (pfile))
- cpp_pedwarn (pfile, "multi-line string constant");
- }
-
- *namebuf++ = '\n';
- handle_newline (cur, buffer->rlimit, c);
- }
- else
- {
- unsigned char *temp;
+ /* Short circuit EOF. */
+ if (token->type == CPP_EOF)
+ return token;
- /* An odd number of consecutive backslashes represents
- an escaped terminator. */
- temp = namebuf - 1;
- while (temp >= name_start && *temp == '\\')
- temp--;
+ /* If we are skipping... */
+ if (pfile->skipping)
+ {
+ /* we still have to process directives, */
+ if (pfile->token_list.directive)
+ return token;
- if ((namebuf - temp) & 1)
- goto out;
- namebuf++;
- }
+ /* but everything else is ignored. */
+ _cpp_skip_rest_of_line (pfile);
+ continue;
}
- }
-
- /* Run out of name space? */
- if (cur < buffer->rlimit)
- {
- list->name_used = namebuf - list->namebuf;
- auto_expand_name_space (list);
- goto expanded;
- }
- /* We may not have trigraph-replaced the input for this code path,
- but as the input is in error by being unterminated we don't
- bother. Prevent warnings about no newlines at EOF. */
- if (IS_NEWLINE(cur[-1]))
- cur--;
+ /* If there's a potential control macro and we get here, then that
+ #ifndef didn't cover the entire file and its argument shouldn't
+ be taken as a control macro. */
+ pfile->potential_control_macro = 0;
- unterminated:
- cpp_error (pfile, "missing terminating %c character", (int) terminator);
+ /* If we are rescanning preprocessed input, no macro expansion or
+ token pasting may occur. */
+ if (CPP_OPTION (pfile, preprocessed))
+ return token;
- if (terminator == '\"' && pfile->multiline_string_line != list->line
- && pfile->multiline_string_line != 0)
- {
- cpp_error_with_line (pfile, pfile->multiline_string_line, -1,
- "possible start of unterminated string literal");
- pfile->multiline_string_line = 0;
- }
-
- out:
- buffer->cur = cur;
- name->len = namebuf - (list->namebuf + name->offset);
- list->name_used = namebuf - list->namebuf;
+ old_token = token;
- if (null_count > 0)
- cpp_warning (pfile, (null_count > 1 ? "null characters preserved"
- : "null character preserved"));
-}
+ /* See if there's a token to paste with this one. */
+ if (!pfile->paste_level)
+ token = maybe_paste_with_next (pfile, token);
-/* The character C helps us distinguish comment types: '*' = C style,
- '-' = Chill-style and '/' = C++ style. For code simplicity, the
- stored comment includes any C-style comment terminator. */
-static void
-copy_comment (list, from, len, tok_no, type)
- cpp_toklist *list;
- const unsigned char *from;
- unsigned int len;
- unsigned int tok_no;
- unsigned int type;
-{
- cpp_token *comment;
+ /* If it isn't a macro, return it now. */
+ if (token->type != CPP_NAME || token->val.node->type == T_VOID)
+ return token;
- if (list->comments_used == list->comments_cap)
- expand_comment_space (list);
+ /* Is macro expansion disabled in general, or are we in the
+ middle of a token paste, or was this token just pasted?
+ (Note we don't check token->flags & PASTED, because that
+ counts tokens that were pasted at some point in the past,
+ we're only interested in tokens that were pasted by this call
+ to maybe_paste_with_next.) */
+ if (pfile->no_expand_level == pfile->cur_context
+ || pfile->paste_level
+ || (token != old_token
+ && pfile->no_expand_level + 1 == pfile->cur_context))
+ return token;
- if (list->name_used + len > list->name_cap)
- expand_name_space (list, len);
+ node = token->val.node;
+ if (node->type != T_MACRO)
+ return special_symbol (pfile, node, token);
- comment = &list->comments[list->comments_used++];
- comment->type = type;
- comment->aux = tok_no;
- comment->val.name.len = len;
- comment->val.name.offset = list->name_used;
+ if (is_macro_disabled (pfile, node->value.expansion, token))
+ return token;
- memcpy (list->namebuf + list->name_used, from, len);
- list->name_used += len;
+ if (push_macro_context (pfile, token))
+ return token;
+ /* else loop */
+ }
}
-/*
- * The tokenizer's main loop. Returns a token list, representing a
- * logical line in the input file, terminated with a CPP_VSPACE
- * token. On EOF, a token list containing the single CPP_EOF token
- * is returned.
- *
- * Implementation relies almost entirely on lookback, rather than
- * looking forwards. This means that tokenization requires just
- * a single pass of the file, even in the presence of trigraphs and
- * escaped newlines, providing significant performance benefits.
- * Trigraph overhead is negligible if they are disabled, and low
- * even when enabled.
- */
-
-#define PUSH_TOKEN(ttype) cur_token++->type = ttype
-#define REVISE_TOKEN(ttype) cur_token[-1].type = ttype
-#define BACKUP_TOKEN(ttype) (--cur_token)->type = ttype
-#define BACKUP_DIGRAPH(ttype) do { \
- BACKUP_TOKEN(ttype); cur_token->flags |= DIGRAPH;} while (0)
-
-void
-_cpp_lex_line (pfile, list)
+/* Returns the next raw token, i.e. without performing macro
+ expansion. Argument contexts are automatically entered. */
+static const cpp_token *
+get_raw_token (pfile)
cpp_reader *pfile;
- cpp_toklist *list;
{
- cpp_token *cur_token, *token_limit;
- cpp_buffer *buffer = pfile->buffer;
- register const unsigned char *cur = buffer->cur;
- unsigned char flags = 0;
+ const cpp_token *result;
+ cpp_context *context;
- expanded:
- token_limit = list->tokens + list->tokens_cap;
- cur_token = list->tokens + list->tokens_used;
-
- for (; cur < buffer->rlimit && cur_token < token_limit;)
+ for (;;)
{
- unsigned char c = *cur++;
-
- /* Optimize whitespace skipping, in particular the case of a
- single whitespace character, as every other token is probably
- whitespace. (' ' '\t' '\v' '\f' '\0'). */
- if (is_hspace ((unsigned int) c))
+ context = CURRENT_CONTEXT (pfile);
+ if (context->pushed_token)
{
- if (c == '\0' || (cur < buffer->rlimit && is_hspace (*cur)))
- {
- buffer->cur = cur - (c == '\0'); /* Get the null warning. */
- skip_whitespace (pfile, IS_DIRECTIVE (list));
- cur = buffer->cur;
- }
- flags = PREV_WHITESPACE;
- if (cur == buffer->rlimit)
- break;
- c = *cur++;
+ result = context->pushed_token;
+ context->pushed_token = 0;
+ return result; /* Cannot be a CPP_MACRO_ARG */
}
-
- /* Initialize current token. Its type is set in the switch. */
- cur_token->col = COLUMN (cur);
- cur_token->flags = flags;
- flags = 0;
-
- switch (c)
+ else if (context->posn == context->count)
{
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- /* Prepend an immediately previous CPP_DOT token. */
- if (PREV_TOKEN_TYPE == CPP_DOT && IMMED_TOKEN ())
- {
- cur_token--;
- if (list->name_cap == list->name_used)
- auto_expand_name_space (list);
-
- cur_token->val.name.len = 1;
- cur_token->val.name.offset = list->name_used;
- list->namebuf[list->name_used++] = '.';
- }
- else
- INIT_NAME (list, cur_token->val.name);
- cur--; /* Backup character. */
-
- continue_number:
- buffer->cur = cur;
- parse_number (pfile, list, &cur_token->val.name);
- cur = buffer->cur;
-
- PUSH_TOKEN (CPP_NUMBER); /* Number not yet interpreted. */
- break;
-
- letter:
- case '_':
- case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
- case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
- case 's': case 't': case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
- case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
- case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- INIT_NAME (list, cur_token->val.name);
- cur--; /* Backup character. */
- cur_token->type = CPP_NAME; /* Identifier, macro etc. */
-
- continue_name:
- buffer->cur = cur;
- parse_name (pfile, list, &cur_token->val.name);
- cur = buffer->cur;
-
- /* Find handler for newly created / extended directive. */
- if (IS_DIRECTIVE (list) && cur_token == &list->tokens[1])
- _cpp_check_directive (list, cur_token);
- cur_token++;
- break;
-
- case '\'':
- /* Fall through. */
- case '\"':
- cur_token->type = c == '\'' ? CPP_CHAR : CPP_STRING;
- /* Do we have a wide string? */
- if (cur_token[-1].type == CPP_NAME && IMMED_TOKEN ()
- && cur_token[-1].val.name.len == 1
- && TOK_NAME (list, cur_token - 1)[0] == 'L'
- && !CPP_TRADITIONAL (pfile))
- {
- /* No need for 'L' any more. */
- list->name_used--;
- (--cur_token)->type = (c == '\'' ? CPP_WCHAR : CPP_WSTRING);
- }
-
- do_parse_string:
- /* Here c is one of ' " > or ). */
- INIT_NAME (list, cur_token->val.name);
- buffer->cur = cur;
- parse_string (pfile, list, &cur_token->val.name, c);
- cur = buffer->cur;
- cur_token++;
- break;
-
- case '/':
- cur_token->type = CPP_DIV;
- if (IMMED_TOKEN ())
- {
- if (PREV_TOKEN_TYPE == CPP_DIV)
- {
- /* We silently allow C++ comments in system headers,
- irrespective of conformance mode, because lots of
- broken systems do that and trying to clean it up
- in fixincludes is a nightmare. */
- if (buffer->system_header_p)
- goto do_line_comment;
- else if (CPP_OPTION (pfile, cplusplus_comments))
- {
- if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)
- && ! buffer->warned_cplusplus_comments)
- {
- buffer->cur = cur;
- cpp_pedwarn (pfile,
- "C++ style comments are not allowed in ISO C89");
- cpp_pedwarn (pfile,
- "(this will be reported only once per input file)");
- buffer->warned_cplusplus_comments = 1;
- }
- do_line_comment:
- buffer->cur = cur;
- if (cur[-2] != c)
- cpp_warning (pfile,
- "comment start split across lines");
- if (skip_line_comment (pfile))
- cpp_error_with_line (pfile, list->line,
- cur_token[-1].col,
- "multi-line comment");
- if (!CPP_OPTION (pfile, discard_comments))
- copy_comment (list, cur, buffer->cur - cur,
- cur_token - 1 - list->tokens, c == '/'
- ? CPP_CPP_COMMENT: CPP_CHILL_COMMENT);
- cur = buffer->cur;
-
- /* Back-up to first '-' or '/'. */
- cur_token -= 2;
- if (!CPP_OPTION (pfile, traditional))
- flags = PREV_WHITESPACE;
- }
- }
- }
- cur_token++;
- break;
-
- case '*':
- cur_token->type = CPP_MULT;
- if (IMMED_TOKEN ())
- {
- if (PREV_TOKEN_TYPE == CPP_DIV)
- {
- buffer->cur = cur;
- if (cur[-2] != '/')
- cpp_warning (pfile,
- "comment start '/*' split across lines");
- if (skip_block_comment (pfile))
- cpp_error_with_line (pfile, list->line, cur_token[-1].col,
- "unterminated comment");
- else if (buffer->cur[-2] != '*')
- cpp_warning (pfile,
- "comment end '*/' split across lines");
- if (!CPP_OPTION (pfile, discard_comments))
- copy_comment (list, cur, buffer->cur - cur,
- cur_token - 1 - list->tokens, CPP_C_COMMENT);
- cur = buffer->cur;
-
- cur_token -= 2;
- if (!CPP_OPTION (pfile, traditional))
- flags = PREV_WHITESPACE;
- }
- else if (CPP_OPTION (pfile, cplusplus))
- {
- /* In C++, there are .* and ->* operators. */
- if (PREV_TOKEN_TYPE == CPP_DEREF)
- BACKUP_TOKEN (CPP_DEREF_STAR);
- else if (PREV_TOKEN_TYPE == CPP_DOT)
- BACKUP_TOKEN (CPP_DOT_STAR);
- }
- }
- cur_token++;
- break;
-
- case '\n':
- case '\r':
- handle_newline (cur, buffer->rlimit, c);
- if (PREV_TOKEN_TYPE != CPP_BACKSLASH || !IMMED_TOKEN ())
- {
- if (PREV_TOKEN_TYPE == CPP_BACKSLASH)
- {
- buffer->cur = cur;
- cpp_warning (pfile,
- "backslash and newline separated by space");
- }
- PUSH_TOKEN (CPP_VSPACE);
- goto out;
- }
- /* Remove the escaped newline. Then continue to process
- any interrupted name or number. */
- cur_token--;
- if (IMMED_TOKEN ())
- {
- cur_token--;
- if (cur_token->type == CPP_NAME)
- goto continue_name;
- else if (cur_token->type == CPP_NUMBER)
- goto continue_number;
- cur_token++;
- }
- break;
-
- case '-':
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_MINUS)
+ if (pop_context (pfile))
+ return &eof_token;
+ continue;
+ }
+ else if (IS_ARG_CONTEXT (context))
+ {
+ result = context->u.arg[context->posn++];
+ if (result == 0)
{
- if (CPP_OPTION (pfile, chill))
- goto do_line_comment;
- REVISE_TOKEN (CPP_MINUS_MINUS);
+ context->flags ^= CONTEXT_RAW;
+ result = context->u.arg[context->posn++];
}
- else
- PUSH_TOKEN (CPP_MINUS);
- break;
-
- /* The digraph flag checking ensures that ## and %:%:
- are interpreted as CPP_PASTE, but #%: and %:# are not. */
- make_hash:
- case '#':
- if (PREV_TOKEN_TYPE == CPP_HASH && IMMED_TOKEN ()
- && ((cur_token->flags ^ cur_token[-1].flags) & DIGRAPH) == 0)
- REVISE_TOKEN (CPP_PASTE);
- else
- PUSH_TOKEN (CPP_HASH);
- break;
+ return result; /* Cannot be a CPP_MACRO_ARG */
+ }
- case ':':
- cur_token->type = CPP_COLON;
- if (IMMED_TOKEN ())
- {
- if (PREV_TOKEN_TYPE == CPP_COLON
- && CPP_OPTION (pfile, cplusplus))
- BACKUP_TOKEN (CPP_SCOPE);
- /* Digraph: "<:" is a '[' */
- else if (PREV_TOKEN_TYPE == CPP_LESS)
- BACKUP_DIGRAPH (CPP_OPEN_SQUARE);
- /* Digraph: "%:" is a '#' */
- else if (PREV_TOKEN_TYPE == CPP_MOD)
- {
- (--cur_token)->flags |= DIGRAPH;
- goto make_hash;
- }
- }
- cur_token++;
- break;
+ result = &context->u.list->tokens[context->posn++];
- case '&':
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_AND)
- REVISE_TOKEN (CPP_AND_AND);
- else
- PUSH_TOKEN (CPP_AND);
- break;
+ if (result->type != CPP_MACRO_ARG)
+ return result;
- make_or:
- case '|':
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_OR)
- REVISE_TOKEN (CPP_OR_OR);
- else
- PUSH_TOKEN (CPP_OR);
- break;
+ if (result->flags & STRINGIFY_ARG)
+ return stringify_arg (pfile, result);
- case '+':
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_PLUS)
- REVISE_TOKEN (CPP_PLUS_PLUS);
- else
- PUSH_TOKEN (CPP_PLUS);
- break;
+ push_arg_context (pfile, result);
+ }
+}
- case '=':
- /* This relies on equidistance of "?=" and "?" tokens. */
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE <= CPP_LAST_EQ)
- REVISE_TOKEN (PREV_TOKEN_TYPE + (CPP_EQ_EQ - CPP_EQ));
- else
- PUSH_TOKEN (CPP_EQ);
- break;
+/* Internal interface to get the token without macro expanding. */
+const cpp_token *
+_cpp_get_raw_token (pfile)
+ cpp_reader *pfile;
+{
+ int prev_nme = prevent_macro_expansion (pfile);
+ const cpp_token *result = _cpp_get_token (pfile);
+ restore_macro_expansion (pfile, prev_nme);
+ return result;
+}
- case '>':
- cur_token->type = CPP_GREATER;
- if (IMMED_TOKEN ())
- {
- if (PREV_TOKEN_TYPE == CPP_GREATER)
- BACKUP_TOKEN (CPP_RSHIFT);
- else if (PREV_TOKEN_TYPE == CPP_MINUS)
- BACKUP_TOKEN (CPP_DEREF);
- /* Digraph: ":>" is a ']' */
- else if (PREV_TOKEN_TYPE == CPP_COLON)
- BACKUP_DIGRAPH (CPP_CLOSE_SQUARE);
- /* Digraph: "%>" is a '}' */
- else if (PREV_TOKEN_TYPE == CPP_MOD)
- BACKUP_DIGRAPH (CPP_CLOSE_BRACE);
- }
- cur_token++;
- break;
-
- case '<':
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS)
- {
- REVISE_TOKEN (CPP_LSHIFT);
- break;
- }
- /* Is this the beginning of a header name? */
- if (list->dir_flags & SYNTAX_INCLUDE)
- {
- c = '>'; /* Terminator. */
- cur_token->type = CPP_HEADER_NAME;
- goto do_parse_string;
- }
- PUSH_TOKEN (CPP_LESS);
- break;
+/* A thin wrapper to lex_line. CLEAR is non-zero if the current token
+ list should be overwritten, or zero if we need to append
+ (typically, if we are within the arguments to a macro, or looking
+ for the '(' to start a function-like macro invocation). */
+static int
+lex_next (pfile, clear)
+ cpp_reader *pfile;
+ int clear;
+{
+ cpp_toklist *list = &pfile->token_list;
+ const cpp_token *old_list = list->tokens;
+ unsigned int old_used = list->tokens_used;
- case '%':
- /* Digraph: "<%" is a '{' */
- cur_token->type = CPP_MOD;
- if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS)
- BACKUP_DIGRAPH (CPP_OPEN_BRACE);
- cur_token++;
- break;
+ if (clear)
+ {
+ /* Release all temporary tokens. */
+ _cpp_clear_toklist (list);
+ pfile->contexts[0].posn = 0;
+ if (pfile->temp_used)
+ release_temp_tokens (pfile);
+ }
+ lex_line (pfile, list);
+ pfile->contexts[0].count = list->tokens_used;
- case ')':
- PUSH_TOKEN (CPP_CLOSE_PAREN);
- break;
+ if (!clear && pfile->args)
+ {
+ /* Fix up argument token pointers. */
+ if (old_list != list->tokens)
+ {
+ unsigned int i;
- case '(':
- /* Is this the beginning of an assertion string? */
- if (list->dir_flags & SYNTAX_ASSERT)
+ for (i = 0; i < pfile->args->used; i++)
{
- c = ')'; /* Terminator. */
- cur_token->type = CPP_ASSERTION;
- goto do_parse_string;
+ const cpp_token *token = pfile->args->tokens[i];
+ if (token >= old_list && token < old_list + old_used)
+ pfile->args->tokens[i] = (const cpp_token *)
+ ((char *) token + ((char *) list->tokens - (char *) old_list));
}
- PUSH_TOKEN (CPP_OPEN_PAREN);
- break;
-
- make_complement:
- case '~':
- PUSH_TOKEN (CPP_COMPL);
- break;
+ }
- case '?':
- if (cur + 1 < buffer->rlimit && *cur == '?'
- && trigraph_map[cur[1]] && trigraph_ok (pfile, cur + 1))
- {
- /* Handle trigraph. */
- cur++;
- switch (*cur++)
- {
- case '(': goto make_open_square;
- case ')': goto make_close_square;
- case '<': goto make_open_brace;
- case '>': goto make_close_brace;
- case '=': goto make_hash;
- case '!': goto make_or;
- case '-': goto make_complement;
- case '/': goto make_backslash;
- case '\'': goto make_xor;
- }
- }
- if (IMMED_TOKEN () && CPP_OPTION (pfile, cplusplus))
- {
- /* GNU C++ defines <? and >? operators. */
- if (PREV_TOKEN_TYPE == CPP_LESS)
- {
- REVISE_TOKEN (CPP_MIN);
- break;
- }
- else if (PREV_TOKEN_TYPE == CPP_GREATER)
- {
- REVISE_TOKEN (CPP_MAX);
- break;
- }
- }
- PUSH_TOKEN (CPP_QUERY);
- break;
+ /* 6.10.3 paragraph 11: If there are sequences of preprocessing
+ tokens within the list of arguments that would otherwise act as
+ preprocessing directives, the behavior is undefined.
- case '.':
- if (PREV_TOKEN_TYPE == CPP_DOT && cur_token[-2].type == CPP_DOT
- && IMMED_TOKEN ()
- && !(cur_token[-1].flags & PREV_WHITESPACE))
- {
- cur_token -= 2;
- PUSH_TOKEN (CPP_ELLIPSIS);
- }
- else
- PUSH_TOKEN (CPP_DOT);
- break;
+ This implementation will report a hard error and treat the
+ 'sequence of preprocessing tokens' as part of the macro argument,
+ not a directive.
- make_xor:
- case '^': PUSH_TOKEN (CPP_XOR); break;
- make_open_brace:
- case '{': PUSH_TOKEN (CPP_OPEN_BRACE); break;
- make_close_brace:
- case '}': PUSH_TOKEN (CPP_CLOSE_BRACE); break;
- make_open_square:
- case '[': PUSH_TOKEN (CPP_OPEN_SQUARE); break;
- make_close_square:
- case ']': PUSH_TOKEN (CPP_CLOSE_SQUARE); break;
- make_backslash:
- case '\\': PUSH_TOKEN (CPP_BACKSLASH); break;
- case '!': PUSH_TOKEN (CPP_NOT); break;
- case ',': PUSH_TOKEN (CPP_COMMA); break;
- case ';': PUSH_TOKEN (CPP_SEMICOLON); break;
-
- case '$':
- if (CPP_OPTION (pfile, dollars_in_ident))
- goto letter;
- /* Fall through */
- default:
- cur_token->aux = c;
- PUSH_TOKEN (CPP_OTHER);
- break;
+ Note if pfile->args == 0, we're OK since we're only inside a
+ macro argument after a '('. */
+ if (list->directive)
+ {
+ cpp_error_with_line (pfile, list->tokens[old_used].line,
+ list->tokens[old_used].col,
+ "#%s may not be used inside a macro argument",
+ list->directive->name);
+ return 1;
}
}
- /* Run out of token space? */
- if (cur_token == token_limit)
- {
- list->tokens_used = cur_token - list->tokens;
- expand_token_space (list);
- goto expanded;
- }
+ return 0;
+}
- cur_token->type = CPP_EOF;
- cur_token->flags = flags;
+/* Pops a context off the context stack. If we're at the bottom, lexes
+ the next logical line. Returns EOF if we're at the end of the
+ argument list to the # operator, or we should not "overflow"
+ into the rest of the file (e.g. 6.10.3.1.1). */
+static int
+pop_context (pfile)
+ cpp_reader *pfile;
+{
+ cpp_context *context;
- if (cur_token != &list->tokens[0])
+ if (pfile->cur_context == 0)
{
- /* Next call back will get just a CPP_EOF. */
- buffer->cur = cur;
- cpp_warning (pfile, "no newline at end of file");
- PUSH_TOKEN (CPP_VSPACE);
+ /* If we are currently processing a directive, do not advance. 6.10
+ paragraph 2: A new-line character ends the directive even if it
+ occurs within what would otherwise be an invocation of a
+ function-like macro. */
+ if (pfile->token_list.directive)
+ return 1;
+
+ return lex_next (pfile, pfile->no_expand_level == UINT_MAX);
}
- out:
- buffer->cur = cur;
+ /* Argument contexts, when parsing args or handling # operator
+ return CPP_EOF at the end. */
+ context = CURRENT_CONTEXT (pfile);
+ if (IS_ARG_CONTEXT (context) && pfile->cur_context == pfile->no_expand_level)
+ return 1;
- list->tokens_used = cur_token - list->tokens;
+ /* Free resources when leaving macro contexts. */
+ if (context->args)
+ free_macro_args (context->args);
- /* FIXME: take this check out and put it in the caller.
- list->directive == 0 indicates an unknown directive (but null
- directive is OK). This is the first time we can be sure the
- directive is invalid, and thus warn about it, because it might
- have been split by escaped newlines. Also, don't complain about
- invalid directives in assembly source, we don't know where the
- comments are, and # may introduce assembler pseudo-ops. */
+ if (pfile->cur_context == pfile->no_expand_level)
+ pfile->no_expand_level--;
+ pfile->cur_context--;
- if (IS_DIRECTIVE (list) && list->dir_handler == 0
- && list->tokens[1].type != CPP_VSPACE
- && !CPP_OPTION (pfile, lang_asm))
- cpp_error_with_line (pfile, list->line, list->tokens[1].col,
- "invalid preprocessing directive");
+ return 0;
}
-/* Token spelling functions. Used for output of a preprocessed file,
- stringizing and token pasting. They all assume sufficient buffer
- is allocated, and return exactly how much they used. */
-
-/* Needs buffer of 3 + len. */
-unsigned int
-spell_char (buffer, list, token)
- unsigned char *buffer;
- cpp_toklist *list;
- cpp_token *token;
+/* Turn off macro expansion at the current context level. */
+static unsigned int
+prevent_macro_expansion (pfile)
+ cpp_reader *pfile;
{
- unsigned char* orig_buff = buffer;
- size_t len;
-
- if (token->type == CPP_WCHAR)
- *buffer++ = 'L';
- *buffer++ = '\'';
-
- len = token->val.name.len;
- memcpy (buffer, TOK_NAME (list, token), len);
- buffer += len;
- *buffer++ = '\'';
- return buffer - orig_buff;
+ unsigned int prev_value = pfile->no_expand_level;
+ pfile->no_expand_level = pfile->cur_context;
+ return prev_value;
}
-/* Needs buffer of 3 + len. */
-unsigned int
-spell_string (buffer, list, token)
- unsigned char *buffer;
- cpp_toklist *list;
- cpp_token *token;
+/* Restore macro expansion to its previous state. */
+static void
+restore_macro_expansion (pfile, prev_value)
+ cpp_reader *pfile;
+ unsigned int prev_value;
{
- unsigned char* orig_buff = buffer;
- size_t len;
-
- if (token->type == CPP_WSTRING)
- *buffer++ = 'L';
- *buffer++ = '"';
-
- len = token->val.name.len;
- memcpy (buffer, TOK_NAME (list, token), len);
- buffer += len;
- *buffer++ = '"';
- return buffer - orig_buff;
+ pfile->no_expand_level = prev_value;
}
-/* Needs buffer of len + 2. */
+/* Used by cpperror.c to obtain the correct line and column to report
+ in a diagnostic. */
unsigned int
-spell_comment (buffer, list, token)
- unsigned char *buffer;
- cpp_toklist *list;
- cpp_token *token;
+_cpp_get_line (pfile, pcol)
+ cpp_reader *pfile;
+ unsigned int *pcol;
{
- size_t len;
+ unsigned int index;
+ const cpp_token *cur_token;
- if (token->type == CPP_C_COMMENT)
- {
- *buffer++ = '/';
- *buffer++ = '*';
- }
- else if (token->type == CPP_CPP_COMMENT)
- {
- *buffer++ = '/';
- *buffer++ = '/';
- }
- else
+ if (pfile->state.in_lex_line)
+ index = pfile->token_list.tokens_used;
+ else
{
- *buffer++ = '-';
- *buffer++ = '-';
- }
+ index = pfile->contexts[0].posn;
- len = token->val.name.len;
- memcpy (buffer, TOK_NAME (list, token), len);
+ if (index == 0)
+ {
+ if (pcol)
+ *pcol = 0;
+ return 0;
+ }
+ index--;
+ }
- return len + 2;
+ cur_token = &pfile->token_list.tokens[index];
+ if (pcol)
+ *pcol = cur_token->col;
+ return cur_token->line;
}
-/* Needs buffer of len. */
-unsigned int
-spell_name (buffer, list, token)
- unsigned char *buffer;
- cpp_toklist *list;
- cpp_token *token;
+#define DSC(str) (const U_CHAR *)str, sizeof str - 1
+static const char * const monthnames[] =
{
- size_t len;
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+};
- len = token->val.name.len;
- memcpy (buffer, TOK_NAME (list, token), len);
- buffer += len;
-
- return len;
-}
-
-/* Needs buffer of 1. */
-unsigned int
-spell_other (buffer, list, token)
- unsigned char *buffer;
- cpp_toklist *list ATTRIBUTE_UNUSED;
- cpp_token *token;
+/* Handle builtin macros like __FILE__. */
+static const cpp_token *
+special_symbol (pfile, node, token)
+ cpp_reader *pfile;
+ cpp_hashnode *node;
+ const cpp_token *token;
{
- *buffer++ = token->aux;
- return 1;
-}
+ cpp_token *result;
+ cpp_buffer *ip;
-void
-_cpp_lex_file (pfile)
- cpp_reader* pfile;
-{
- int recycle;
- cpp_toklist* list;
+ switch (node->type)
+ {
+ case T_FILE:
+ case T_BASE_FILE:
+ {
+ const char *file;
- init_trigraph_map ();
- list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
+ ip = CPP_BUFFER (pfile);
+ if (ip == 0)
+ file = "";
+ else
+ {
+ if (node->type == T_BASE_FILE)
+ while (CPP_PREV_BUFFER (ip) != NULL)
+ ip = CPP_PREV_BUFFER (ip);
- for (recycle = 0; ;)
- {
- init_token_list (pfile, list, recycle);
- recycle = 1;
+ file = ip->nominal_fname;
+ }
+ result = make_string_token (get_temp_token (pfile), (U_CHAR *) file,
+ strlen (file));
+ }
+ break;
+
+ case T_INCLUDE_LEVEL:
+ /* pfile->include_depth counts the primary source as level 1,
+ but historically __INCLUDE_DEPTH__ has called the primary
+ source level 0. */
+ result = alloc_number_token (pfile, pfile->include_depth - 1);
+ break;
+
+ case T_SPECLINE:
+ /* If __LINE__ is embedded in a macro, it must expand to the
+ line of the macro's invocation, not its definition.
+ Otherwise things like assert() will not work properly. */
+ result = alloc_number_token (pfile, _cpp_get_line (pfile, NULL));
+ break;
+
+ case T_STDC:
+ {
+ int stdc = 1;
- _cpp_lex_line (pfile, list);
- if (list->tokens[0].type == CPP_EOF)
- break;
+#ifdef STDC_0_IN_SYSTEM_HEADERS
+ if (CPP_IN_SYSTEM_HEADER (pfile)
+ && pfile->spec_nodes->n__STRICT_ANSI__->type == T_VOID)
+ stdc = 0;
+#endif
+ result = alloc_number_token (pfile, stdc);
+ }
+ break;
- if (list->dir_handler)
+ case T_DATE:
+ case T_TIME:
+ if (pfile->date == 0)
{
- if (list->dir_handler (pfile))
- {
- list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
- recycle = 0;
- }
+ /* Allocate __DATE__ and __TIME__ from permanent storage,
+ and save them in pfile so we don't have to do this again.
+ We don't generate these strings at init time because
+ time() and localtime() are very slow on some systems. */
+ time_t tt = time (NULL);
+ struct tm *tb = localtime (&tt);
+
+ pfile->date = make_string_token
+ ((cpp_token *) xmalloc (sizeof (cpp_token)), DSC("Oct 11 1347"));
+ pfile->time = make_string_token
+ ((cpp_token *) xmalloc (sizeof (cpp_token)), DSC("12:34:56"));
+
+ sprintf ((char *) pfile->date->val.str.text, "%s %2d %4d",
+ monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
+ sprintf ((char *) pfile->time->val.str.text, "%02d:%02d:%02d",
+ tb->tm_hour, tb->tm_min, tb->tm_sec);
}
- else
- _cpp_output_list (pfile, list);
+ result = node->type == T_DATE ? pfile->date: pfile->time;
+ break;
+
+ case T_POISON:
+ cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name);
+ return token;
+
+ default:
+ cpp_ice (pfile, "invalid special hash type");
+ return token;
}
+
+ ASSIGN_FLAGS_AND_POS (result, token);
+ return result;
}
+#undef DSC
-static void
-_cpp_output_list (pfile, list)
+/* Allocate pfile->input_buffer, and initialize _cpp_trigraph_map[]
+ if it hasn't happened already. */
+
+void
+_cpp_init_input_buffer (pfile)
cpp_reader *pfile;
- cpp_toklist *list;
{
- unsigned int comment_no = 0;
- cpp_token *token, *comment_token = 0;
+ cpp_context *base;
+
+ _cpp_init_toklist (&pfile->token_list, 0);
+ pfile->no_expand_level = UINT_MAX;
+ pfile->context_cap = 20;
+ pfile->cur_context = 0;
+
+ pfile->contexts = (cpp_context *)
+ xmalloc (pfile->context_cap * sizeof (cpp_context));
+
+ /* Clear the base context. */
+ base = &pfile->contexts[0];
+ base->u.list = &pfile->token_list;
+ base->posn = 0;
+ base->count = 0;
+ base->args = 0;
+ base->level = 0;
+ base->flags = 0;
+ base->pushed_token = 0;
+}
- if (list->comments_used > 0)
- comment_token = list->tokens + list->comments[0].aux;
+/* Moves to the end of the directive line, popping contexts as
+ necessary. */
+void
+_cpp_skip_rest_of_line (pfile)
+ cpp_reader *pfile;
+{
+ /* Discard all stacked contexts. */
+ int i;
+ for (i = pfile->cur_context; i > 0; i--)
+ if (pfile->contexts[i].args)
+ free_macro_args (pfile->contexts[i].args);
+
+ if (pfile->no_expand_level <= pfile->cur_context)
+ pfile->no_expand_level = 0;
+ pfile->cur_context = 0;
+
+ /* Clear the base context, and clear the directive pointer so that
+ get_raw_token will advance to the next line. */
+ pfile->contexts[0].count = 0;
+ pfile->contexts[0].posn = 0;
+ pfile->token_list.directive = 0;
+}
- CPP_RESERVE (pfile, 2); /* Always have room for " \n". */
- for (token = &list->tokens[0];; token++)
+/* Directive handler wrapper used by the command line option
+ processor. */
+void
+_cpp_run_directive (pfile, dir, buf, count, name)
+ cpp_reader *pfile;
+ const struct directive *dir;
+ const char *buf;
+ size_t count;
+ const char *name;
+{
+ if (cpp_push_buffer (pfile, (const U_CHAR *)buf, count) != NULL)
{
- if (token->flags & PREV_WHITESPACE)
- {
- /* Output comments if -C. Otherwise a space will do. */
- if (token == comment_token)
- {
- cpp_token *comment = &list->comments[comment_no];
- do
- {
- /* Longest wrapper is 4. */
- CPP_RESERVE (pfile, 4 + 2 + comment->val.name.len);
- pfile->limit += spell_comment (pfile->limit, list, comment);
- comment_no++, comment++;
- if (comment_no == list->comments_used)
- break;
- comment_token = comment->aux + list->tokens;
- }
- while (comment_token == token);
- }
- else
- CPP_PUTC_Q (pfile, ' ');
- }
-
- switch (token_spellings[token->type].type)
- {
- case SPELL_TEXT:
- {
- const unsigned char *spelling;
- unsigned char c;
+ unsigned int prev_lvl = 0;
- CPP_RESERVE (pfile, 4 + 2); /* Longest is 4. */
- if (token->flags & DIGRAPH)
- spelling = digraph_spellings [token->type - CPP_FIRST_DIGRAPH];
- else
- spelling = token_spellings[token->type].speller;
-
- while ((c = *spelling++) != '\0')
- CPP_PUTC_Q (pfile, c);
- }
- break;
-
- case SPELL_HANDLER:
- {
- speller s;
+ if (name)
+ CPP_BUFFER (pfile)->nominal_fname = name;
+ else
+ CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
+ CPP_BUFFER (pfile)->lineno = (unsigned int)-1;
- s = (speller) token_spellings[token->type].speller;
- /* Longest wrapper is 4. */
- CPP_RESERVE (pfile, 4 + 2 + token->val.name.len);
- pfile->limit += s (pfile->limit, list, token);
- }
- break;
+ /* Scan the line now, else prevent_macro_expansion won't work. */
+ lex_next (pfile, 1);
+ if (! (dir->flags & EXPAND))
+ prev_lvl = prevent_macro_expansion (pfile);
- case SPELL_EOL:
- CPP_PUTC_Q (pfile, '\n');
- return;
+ (void) (*dir->handler) (pfile);
- case SPELL_NONE:
- cpp_error (pfile, "Unwriteable token");
- break;
- }
+ if (! (dir->flags & EXPAND))
+ restore_macro_expansion (pfile, prev_lvl);
+
+ _cpp_skip_rest_of_line (pfile);
+ cpp_pop_buffer (pfile);
}
}
-
-#endif