Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
Broken out to separate file, Zack Weinberg, Mar 2000
- Single-pass line tokenization by Neil Booth, April 2000
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
#include "config.h"
#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
#include "cpplib.h"
#include "cpphash.h"
-#ifdef MULTIBYTE_CHARS
-#include "mbchar.h"
-#include <locale.h>
-#endif
-
-/* Tokens with SPELL_STRING store their spelling in the token list,
- and it's length in the token->val.name.len. */
enum spell_type
{
SPELL_OPERATOR = 0,
- SPELL_CHAR,
SPELL_IDENT,
- SPELL_NUMBER,
- SPELL_STRING,
+ SPELL_LITERAL,
SPELL_NONE
};
#define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
#define TOKEN_NAME(token) (token_spellings[(token)->type].name)
-#define BACKUP() do {buffer->cur = buffer->backup_to;} while (0)
-static void handle_newline PARAMS ((cpp_reader *));
-static cppchar_t skip_escaped_newlines PARAMS ((cpp_reader *));
-static cppchar_t get_effective_char PARAMS ((cpp_reader *));
-
-static int skip_block_comment PARAMS ((cpp_reader *));
+static void add_line_note PARAMS ((cpp_buffer *, const uchar *, unsigned int));
static int skip_line_comment PARAMS ((cpp_reader *));
-static void adjust_column PARAMS ((cpp_reader *));
-static int skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
-static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *));
-static uchar *parse_slow PARAMS ((cpp_reader *, const uchar *, int,
- unsigned int *));
-static void parse_number PARAMS ((cpp_reader *, cpp_string *, int));
-static int unescaped_terminator_p PARAMS ((cpp_reader *, const uchar *));
-static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
-static bool trigraph_p PARAMS ((cpp_reader *));
+static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
+static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
+static void lex_number PARAMS ((cpp_reader *, cpp_string *));
+static bool forms_identifier_p PARAMS ((cpp_reader *, int));
+static void lex_string PARAMS ((cpp_reader *, cpp_token *, const uchar *));
static void save_comment PARAMS ((cpp_reader *, cpp_token *, const uchar *,
cppchar_t));
+static void create_literal PARAMS ((cpp_reader *, cpp_token *, const uchar *,
+ unsigned int, enum cpp_ttype));
+static bool warn_in_comment PARAMS ((cpp_reader *, _cpp_line_note *));
static int name_p PARAMS ((cpp_reader *, const cpp_string *));
-static int maybe_read_ucs PARAMS ((cpp_reader *, const unsigned char **,
- const unsigned char *, cppchar_t *));
+static cppchar_t maybe_read_ucn PARAMS ((cpp_reader *, const uchar **));
static tokenrun *next_tokenrun PARAMS ((tokenrun *));
static unsigned int hex_digit_value PARAMS ((unsigned int));
static _cpp_buff *new_buff PARAMS ((size_t));
+
/* Utility routine:
Compares, the token TOKEN to the NUL-terminated string STRING.
return !ustrcmp (NODE_NAME (token->val.node), (const uchar *) string);
}
-/* Call when meeting a newline, assumed to be in buffer->cur[-1].
- Returns with buffer->cur pointing to the character immediately
- following the newline (combination). */
+/* Record a note TYPE at byte POS into the current cleaned logical
+ line. */
static void
-handle_newline (pfile)
+add_line_note (buffer, pos, type)
+ cpp_buffer *buffer;
+ const uchar *pos;
+ unsigned int type;
+{
+ if (buffer->notes_used == buffer->notes_cap)
+ {
+ buffer->notes_cap = buffer->notes_cap * 2 + 200;
+ buffer->notes = (_cpp_line_note *)
+ xrealloc (buffer->notes, buffer->notes_cap * sizeof (_cpp_line_note));
+ }
+
+ buffer->notes[buffer->notes_used].pos = pos;
+ buffer->notes[buffer->notes_used].type = type;
+ buffer->notes_used++;
+}
+
+/* Returns with a logical line that contains no escaped newlines or
+ trigraphs. This is a time-critical inner loop. */
+void
+_cpp_clean_line (pfile)
cpp_reader *pfile;
{
- cpp_buffer *buffer = pfile->buffer;
+ cpp_buffer *buffer;
+ const uchar *s;
+ uchar c, *d, *p;
- /* Handle CR-LF and LF-CR. Most other implementations (e.g. java)
- only accept CR-LF; maybe we should fall back to that behaviour? */
- if (buffer->cur[-1] + buffer->cur[0] == '\r' + '\n')
- buffer->cur++;
+ buffer = pfile->buffer;
+ buffer->cur_note = buffer->notes_used = 0;
+ buffer->cur = buffer->line_base = buffer->next_line;
+ buffer->need_line = false;
+ s = buffer->next_line - 1;
- buffer->line_base = buffer->cur;
- buffer->col_adjust = 0;
- pfile->line++;
-}
+ if (!buffer->from_stage3)
+ {
+ d = (uchar *) s;
+
+ for (;;)
+ {
+ c = *++s;
+ *++d = c;
+
+ if (c == '\n' || c == '\r')
+ {
+ /* Handle DOS line endings. */
+ if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
+ s++;
+ if (s == buffer->rlimit)
+ break;
+
+ /* Escaped? */
+ p = d;
+ while (p != buffer->next_line && is_nvspace (p[-1]))
+ p--;
+ if (p == buffer->next_line || p[-1] != '\\')
+ break;
+
+ add_line_note (buffer, p - 1, p != d ? ' ': '\\');
+ d = p - 2;
+ buffer->next_line = p - 1;
+ }
+ else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
+ {
+ /* Add a note regardless, for the benefit of -Wtrigraphs. */
+ add_line_note (buffer, d, s[2]);
+ if (CPP_OPTION (pfile, trigraphs))
+ {
+ *d = _cpp_trigraph_map[s[2]];
+ s += 2;
+ }
+ }
+ }
+ }
+ else
+ {
+ do
+ s++;
+ while (*s != '\n' && *s != '\r');
+ d = (uchar *) s;
+
+ /* Handle DOS line endings. */
+ if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
+ s++;
+ }
-/* Subroutine of skip_escaped_newlines; called when a 3-character
- sequence beginning with "??" is encountered. buffer->cur points to
- the second '?'.
+ *d = '\n';
+ /* A sentinel note that should never be processed. */
+ add_line_note (buffer, d + 1, '\n');
+ buffer->next_line = s + 1;
+}
- Warn if necessary, and returns true if the sequence forms a
- trigraph and the trigraph should be honoured. */
+/* Return true if the trigraph indicated by NOTE should be warned
+ about in a comment. */
static bool
-trigraph_p (pfile)
+warn_in_comment (pfile, note)
cpp_reader *pfile;
+ _cpp_line_note *note;
{
- cpp_buffer *buffer = pfile->buffer;
- cppchar_t from_char = buffer->cur[1];
- bool accept;
+ const uchar *p;
- if (!_cpp_trigraph_map[from_char])
+ /* Within comments we don't warn about trigraphs, unless the
+ trigraph forms an escaped newline, as that may change
+ behaviour. */
+ if (note->type != '/')
return false;
- accept = CPP_OPTION (pfile, trigraphs);
+ /* If -trigraphs, then this was an escaped newline iff the next note
+ is coincident. */
+ if (CPP_OPTION (pfile, trigraphs))
+ return note[1].pos == note->pos;
- /* Don't warn about trigraphs in comments. */
- if (CPP_OPTION (pfile, warn_trigraphs) && !pfile->state.lexing_comment)
- {
- if (accept)
- cpp_error_with_line (pfile, DL_WARNING,
- pfile->line, CPP_BUF_COL (buffer) - 1,
- "trigraph ??%c converted to %c",
- (int) from_char,
- (int) _cpp_trigraph_map[from_char]);
- else if (buffer->cur != buffer->last_Wtrigraphs)
- {
- buffer->last_Wtrigraphs = buffer->cur;
- cpp_error_with_line (pfile, DL_WARNING,
- pfile->line, CPP_BUF_COL (buffer) - 1,
- "trigraph ??%c ignored", (int) from_char);
- }
- }
+ /* Otherwise, see if this forms an escaped newline. */
+ p = note->pos + 3;
+ while (is_nvspace (*p))
+ p++;
- return accept;
+ /* There might have been escaped newlines between the trigraph and the
+ newline we found. Hence the position test. */
+ return (*p == '\n' && p < note[1].pos);
}
-/* Skips any escaped newlines introduced by '?' or a '\\', assumed to
- lie in buffer->cur[-1]. Returns the next byte, which will be in
- buffer->cur[-1]. This routine performs preprocessing stages 1 and
- 2 of the ISO C standard. */
-static cppchar_t
-skip_escaped_newlines (pfile)
+/* Process the notes created by add_line_note as far as the current
+ location. */
+void
+_cpp_process_line_notes (pfile, in_comment)
cpp_reader *pfile;
+ int in_comment;
{
cpp_buffer *buffer = pfile->buffer;
- cppchar_t next = buffer->cur[-1];
- /* Only do this if we apply stages 1 and 2. */
- if (!buffer->from_stage3)
+ for (;;)
{
- const unsigned char *saved_cur;
- cppchar_t next1;
+ _cpp_line_note *note = &buffer->notes[buffer->cur_note];
+ unsigned int col;
- do
- {
- if (next == '?')
- {
- if (buffer->cur[0] != '?' || !trigraph_p (pfile))
- break;
-
- /* Translate the trigraph. */
- next = _cpp_trigraph_map[buffer->cur[1]];
- buffer->cur += 2;
- if (next != '\\')
- break;
- }
+ if (note->pos > buffer->cur)
+ break;
- if (buffer->cur == buffer->rlimit)
- break;
+ buffer->cur_note++;
+ col = CPP_BUF_COLUMN (buffer, note->pos + 1);
- /* We have a backslash, and room for at least one more
- character. Skip horizontal whitespace. */
- saved_cur = buffer->cur;
- do
- next1 = *buffer->cur++;
- while (is_nvspace (next1) && buffer->cur < buffer->rlimit);
+ if (note->type == '\\' || note->type == ' ')
+ {
+ if (note->type == ' ' && !in_comment)
+ cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+ "backslash and newline separated by space");
- if (!is_vspace (next1))
+ if (buffer->next_line > buffer->rlimit)
{
- buffer->cur = saved_cur;
- break;
+ cpp_error_with_line (pfile, DL_PEDWARN, pfile->line, col,
+ "backslash-newline at end of file");
+ /* Prevent "no newline at end of file" warning. */
+ buffer->next_line = buffer->rlimit;
}
- if (saved_cur != buffer->cur - 1
- && !pfile->state.lexing_comment)
- cpp_error (pfile, DL_WARNING,
- "backslash and newline separated by space");
-
- handle_newline (pfile);
- buffer->backup_to = buffer->cur;
- if (buffer->cur == buffer->rlimit)
+ buffer->line_base = note->pos;
+ pfile->line++;
+ }
+ else if (_cpp_trigraph_map[note->type])
+ {
+ if (CPP_OPTION (pfile, warn_trigraphs)
+ && (!in_comment || warn_in_comment (pfile, note)))
{
- cpp_error (pfile, DL_PEDWARN,
- "backslash-newline at end of file");
- next = EOF;
+ if (CPP_OPTION (pfile, trigraphs))
+ cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+ "trigraph ??%c converted to %c",
+ note->type,
+ (int) _cpp_trigraph_map[note->type]);
+ else
+ cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
+ "trigraph ??%c ignored",
+ note->type);
}
- else
- next = *buffer->cur++;
}
- while (next == '\\' || next == '?');
+ else
+ abort ();
}
-
- return next;
-}
-
-/* Obtain the next character, after trigraph conversion and skipping
- an arbitrarily long string of escaped newlines. The common case of
- no trigraphs or escaped newlines falls through quickly. On return,
- buffer->backup_to points to where to return to if the character is
- not to be processed. */
-static cppchar_t
-get_effective_char (pfile)
- cpp_reader *pfile;
-{
- cppchar_t next;
- cpp_buffer *buffer = pfile->buffer;
-
- buffer->backup_to = buffer->cur;
- next = *buffer->cur++;
- if (__builtin_expect (next == '?' || next == '\\', 0))
- next = skip_escaped_newlines (pfile);
-
- 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)
+ nonzero if comment terminated by EOF, zero otherwise.
+
+ Buffer->cur points to the initial asterisk of the comment. */
+bool
+_cpp_skip_block_comment (pfile)
cpp_reader *pfile;
{
cpp_buffer *buffer = pfile->buffer;
- cppchar_t c = EOF, prevc = EOF;
+ cppchar_t c;
- pfile->state.lexing_comment = 1;
- while (buffer->cur != buffer->rlimit)
- {
- prevc = c, c = *buffer->cur++;
+ buffer->cur++;
+ if (*buffer->cur == '/')
+ buffer->cur++;
- /* FIXME: For speed, create a new character class of characters
- of interest inside block comments. */
- if (c == '?' || c == '\\')
- c = skip_escaped_newlines (pfile);
+ for (;;)
+ {
+ c = *buffer->cur++;
/* People like decorating comments with '*', so check for '/'
instead for efficiency. */
if (c == '/')
{
- if (prevc == '*')
+ if (buffer->cur[-2] == '*')
break;
/* Warn about potential nested comments, but not if the '/'
pfile->line, CPP_BUF_COL (buffer),
"\"/*\" within comment");
}
- else if (is_vspace (c))
- handle_newline (pfile);
- else if (c == '\t')
- adjust_column (pfile);
+ else if (c == '\n')
+ {
+ buffer->cur--;
+ _cpp_process_line_notes (pfile, true);
+ if (buffer->next_line >= buffer->rlimit)
+ return true;
+ _cpp_clean_line (pfile);
+ pfile->line++;
+ }
}
- pfile->state.lexing_comment = 0;
- return c != '/' || prevc != '*';
+ _cpp_process_line_notes (pfile, true);
+ return false;
}
/* Skip a C++ line comment, leaving buffer->cur pointing to the
- terminating newline. Handles escaped newlines. Returns non-zero
+ terminating newline. Handles escaped newlines. Returns nonzero
if a multiline comment. */
static int
skip_line_comment (pfile)
{
cpp_buffer *buffer = pfile->buffer;
unsigned int orig_line = pfile->line;
- cppchar_t c;
-#ifdef MULTIBYTE_CHARS
- wchar_t wc;
- int char_len;
-#endif
- pfile->state.lexing_comment = 1;
-#ifdef MULTIBYTE_CHARS
- /* Reset multibyte conversion state. */
- (void) local_mbtowc (NULL, NULL, 0);
-#endif
- do
- {
- if (buffer->cur == buffer->rlimit)
- goto at_eof;
-
-#ifdef MULTIBYTE_CHARS
- char_len = local_mbtowc (&wc, (const char *) buffer->cur,
- buffer->rlimit - buffer->cur);
- if (char_len == -1)
- {
- cpp_error (pfile, DL_WARNING,
- "ignoring invalid multibyte character");
- char_len = 1;
- c = *buffer->cur++;
- }
- else
- {
- buffer->cur += char_len;
- c = wc;
- }
-#else
- c = *buffer->cur++;
-#endif
- if (c == '?' || c == '\\')
- c = skip_escaped_newlines (pfile);
- }
- while (!is_vspace (c));
-
- /* Step back over the newline, except at EOF. */
- buffer->cur--;
- at_eof:
+ while (*buffer->cur != '\n')
+ buffer->cur++;
- pfile->state.lexing_comment = 0;
+ _cpp_process_line_notes (pfile, true);
return orig_line != pfile->line;
}
-/* pfile->buffer->cur is one beyond the \t character. Update
- col_adjust so we track the column correctly. */
+/* Skips whitespace, saving the next non-whitespace character. */
static void
-adjust_column (pfile)
- cpp_reader *pfile;
-{
- 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;
-}
-
-/* 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 int
skip_whitespace (pfile, c)
cpp_reader *pfile;
cppchar_t c;
{
cpp_buffer *buffer = pfile->buffer;
- unsigned int warned = 0;
+ bool saw_NUL = false;
do
{
/* Horizontal space always OK. */
- if (c == ' ')
+ if (c == ' ' || c == '\t')
;
- else if (c == '\t')
- adjust_column (pfile);
/* Just \f \v or \0 left. */
else if (c == '\0')
- {
- if (buffer->cur - 1 == buffer->rlimit)
- return 0;
- if (!warned)
- {
- cpp_error (pfile, DL_WARNING, "null character(s) ignored");
- warned = 1;
- }
- }
+ saw_NUL = true;
else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
cpp_error_with_line (pfile, DL_PEDWARN, pfile->line,
CPP_BUF_COL (buffer),
/* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
while (is_nvspace (c));
+ if (saw_NUL)
+ cpp_error (pfile, DL_WARNING, "null character(s) ignored");
+
buffer->cur--;
- return 1;
}
/* See if the characters of a number token are valid in a name (no
if (!is_idchar (string->text[i]))
return 0;
- return 1;
+ return 1;
}
-/* Parse an identifier, skipping embedded backslash-newlines. This is
- a critical inner loop. The common case is an identifier which has
- not been split by backslash-newline, does not contain a dollar
- sign, and has already been scanned (roughly 10:1 ratio of
- seen:unseen identifiers in normal code; the distribution is
- Poisson-like). Second most common case is a new identifier, not
- split and no dollar sign. The other possibilities are rare and
- have been relegated to parse_slow. */
-static cpp_hashnode *
-parse_identifier (pfile)
+/* Returns TRUE if the sequence starting at buffer->cur is invalid in
+ an identifier. FIRST is TRUE if this starts an identifier. */
+static bool
+forms_identifier_p (pfile, first)
cpp_reader *pfile;
+ int first;
{
- cpp_hashnode *result;
- const uchar *cur, *base;
-
- /* Fast-path loop. Skim over a normal identifier.
- N.B. ISIDNUM does not include $. */
- cur = pfile->buffer->cur;
- while (ISIDNUM (*cur))
- cur++;
+ cpp_buffer *buffer = pfile->buffer;
- /* Check for slow-path cases. */
- if (*cur == '?' || *cur == '\\' || *cur == '$')
+ if (*buffer->cur == '$')
{
- unsigned int len;
+ if (!CPP_OPTION (pfile, dollars_in_ident))
+ return false;
+
+ buffer->cur++;
+ if (pfile->warn_dollars && !pfile->state.skipping)
+ {
+ pfile->warn_dollars = false;
+ cpp_error (pfile, DL_PEDWARN, "'$' in identifier or number");
+ }
- base = parse_slow (pfile, cur, 0, &len);
- result = (cpp_hashnode *)
- ht_lookup (pfile->hash_table, base, len, HT_ALLOCED);
+ return true;
}
- else
+
+ /* Is this a syntactically valid UCN? */
+ if (0 && *buffer->cur == '\\'
+ && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
+ {
+ buffer->cur += 2;
+ if (_cpp_valid_ucn (pfile, &buffer->cur, 1 + !first))
+ return true;
+ buffer->cur -= 2;
+ }
+
+ return false;
+}
+
+/* Lex an identifier starting at BUFFER->CUR - 1. */
+static cpp_hashnode *
+lex_identifier (pfile, base)
+ cpp_reader *pfile;
+ const uchar *base;
+{
+ cpp_hashnode *result;
+ const uchar *cur;
+
+ do
{
- base = pfile->buffer->cur - 1;
+ cur = pfile->buffer->cur;
+
+ /* N.B. ISIDNUM does not include $. */
+ while (ISIDNUM (*cur))
+ cur++;
+
pfile->buffer->cur = cur;
- result = (cpp_hashnode *)
- ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
}
+ while (forms_identifier_p (pfile, false));
- /* Rarely, identifiers require diagnostics when lexed.
- XXX Has to be forced out of the fast path. */
+ result = (cpp_hashnode *)
+ ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
+
+ /* Rarely, identifiers require diagnostics when lexed. */
if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
&& !pfile->state.skipping, 0))
{
return result;
}
-/* Slow path. This handles numbers and identifiers which have been
- split, or contain dollar signs. The part of the token from
- PFILE->buffer->cur-1 to CUR has already been scanned. NUMBER_P is
- 1 if it's a number, and 2 if it has a leading period. Returns a
- pointer to the token's NUL-terminated spelling in permanent
- storage, and sets PLEN to its length. */
-static uchar *
-parse_slow (pfile, cur, number_p, plen)
- cpp_reader *pfile;
- const uchar *cur;
- int number_p;
- unsigned int *plen;
-{
- cpp_buffer *buffer = pfile->buffer;
- const uchar *base = buffer->cur - 1;
- struct obstack *stack = &pfile->hash_table->stack;
- unsigned int c, prevc, saw_dollar = 0;
-
- /* Place any leading period. */
- if (number_p == 2)
- obstack_1grow (stack, '.');
-
- /* Copy the part of the token which is known to be okay. */
- obstack_grow (stack, base, cur - base);
-
- /* Now process the part which isn't. We are looking at one of
- '$', '\\', or '?' on entry to this loop. */
- prevc = cur[-1];
- c = *cur++;
- buffer->cur = cur;
- for (;;)
- {
- /* Potential escaped newline? */
- buffer->backup_to = buffer->cur - 1;
- if (c == '?' || c == '\\')
- c = skip_escaped_newlines (pfile);
-
- if (!is_idchar (c))
- {
- if (!number_p)
- break;
- if (c != '.' && !VALID_SIGN (c, prevc))
- break;
- }
-
- /* Handle normal identifier characters in this loop. */
- do
- {
- prevc = c;
- obstack_1grow (stack, c);
-
- if (c == '$')
- saw_dollar++;
-
- c = *buffer->cur++;
- }
- while (is_idchar (c));
- }
-
- /* Step back over the unwanted char. */
- BACKUP ();
-
- /* $ is not an 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->state.skipping)
- cpp_error (pfile, DL_PEDWARN, "'$' character(s) in identifier or number");
-
- /* Identifiers and numbers are null-terminated. */
- *plen = obstack_object_size (stack);
- obstack_1grow (stack, '\0');
- return obstack_finish (stack);
-}
-
-/* Parse a number, beginning with character C, skipping embedded
- backslash-newlines. LEADING_PERIOD is non-zero if there was a "."
- before C. Place the result in NUMBER. */
+/* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
static void
-parse_number (pfile, number, leading_period)
+lex_number (pfile, number)
cpp_reader *pfile;
cpp_string *number;
- int leading_period;
{
const uchar *cur;
+ const uchar *base;
+ uchar *dest;
- /* Fast-path loop. Skim over a normal number.
- N.B. ISIDNUM does not include $. */
- cur = pfile->buffer->cur;
- while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
- cur++;
-
- /* Check for slow-path cases. */
- if (*cur == '?' || *cur == '\\' || *cur == '$')
- number->text = parse_slow (pfile, cur, 1 + leading_period, &number->len);
- else
+ base = pfile->buffer->cur - 1;
+ do
{
- const uchar *base = pfile->buffer->cur - 1;
- uchar *dest;
+ cur = pfile->buffer->cur;
- number->len = cur - base + leading_period;
- dest = _cpp_unaligned_alloc (pfile, number->len + 1);
- dest[number->len] = '\0';
- number->text = dest;
+ /* N.B. ISIDNUM does not include $. */
+ while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
+ cur++;
- if (leading_period)
- *dest++ = '.';
- memcpy (dest, base, cur - base);
pfile->buffer->cur = cur;
}
+ while (forms_identifier_p (pfile, false));
+
+ number->len = cur - base;
+ dest = _cpp_unaligned_alloc (pfile, number->len + 1);
+ memcpy (dest, base, number->len);
+ dest[number->len] = '\0';
+ number->text = dest;
}
-/* Subroutine of parse_string. */
-static int
-unescaped_terminator_p (pfile, dest)
+/* Create a token of type TYPE with a literal spelling. */
+static void
+create_literal (pfile, token, base, len, type)
cpp_reader *pfile;
- const unsigned char *dest;
+ cpp_token *token;
+ const uchar *base;
+ unsigned int len;
+ enum cpp_ttype type;
{
- const unsigned char *start, *temp;
+ uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
- /* In #include-style directives, terminators are not escapeable. */
- if (pfile->state.angled_headers)
- return 1;
-
- start = BUFF_FRONT (pfile->u_buff);
-
- /* An odd number of consecutive backslashes represents an escaped
- terminator. */
- for (temp = dest; temp > start && temp[-1] == '\\'; temp--)
- ;
-
- return ((dest - temp) & 1) == 0;
+ memcpy (dest, base, len);
+ dest[len] = '\0';
+ token->type = type;
+ token->val.str.len = len;
+ token->val.str.text = dest;
}
-/* Parses a string, character constant, or angle-bracketed header file
- name. Handles embedded trigraphs and escaped newlines. The stored
- string is guaranteed NUL-terminated, but it is not guaranteed that
- this is the first NUL since embedded NULs are preserved.
+/* Lexes a string, character constant, or angle-bracketed header file
+ name. The stored string contains the spelling, including opening
+ quote and leading any leading 'L'. It returns the type of the
+ literal, or CPP_OTHER if it was not properly terminated.
- When this function returns, buffer->cur points to the next
- character to be processed. */
+ The spelling is NUL-terminated, but it is not guaranteed that this
+ is the first NUL since embedded NULs are preserved. */
static void
-parse_string (pfile, token, terminator)
+lex_string (pfile, token, base)
cpp_reader *pfile;
cpp_token *token;
- cppchar_t terminator;
+ const uchar *base;
{
- cpp_buffer *buffer = pfile->buffer;
- unsigned char *dest, *limit;
- cppchar_t c;
- bool warned_nulls = false;
-#ifdef MULTIBYTE_CHARS
- wchar_t wc;
- int char_len;
-#endif
-
- dest = BUFF_FRONT (pfile->u_buff);
- limit = BUFF_LIMIT (pfile->u_buff);
+ bool saw_NUL = false;
+ const uchar *cur;
+ cppchar_t terminator;
+ enum cpp_ttype type;
+
+ cur = base;
+ terminator = *cur++;
+ if (terminator == 'L')
+ terminator = *cur++;
+ if (terminator == '\"')
+ type = *base == 'L' ? CPP_WSTRING: CPP_STRING;
+ else if (terminator == '\'')
+ type = *base == 'L' ? CPP_WCHAR: CPP_CHAR;
+ else
+ terminator = '>', type = CPP_HEADER_NAME;
-#ifdef MULTIBYTE_CHARS
- /* Reset multibyte conversion state. */
- (void) local_mbtowc (NULL, NULL, 0);
-#endif
for (;;)
{
- /* We need room for another char, possibly the terminating NUL. */
- if ((size_t) (limit - dest) < 1)
- {
- size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
- _cpp_extend_buff (pfile, &pfile->u_buff, 2);
- dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
- limit = BUFF_LIMIT (pfile->u_buff);
- }
+ cppchar_t c = *cur++;
-#ifdef MULTIBYTE_CHARS
- char_len = local_mbtowc (&wc, (const char *) buffer->cur,
- buffer->rlimit - buffer->cur);
- if (char_len == -1)
- {
- cpp_error (pfile, DL_WARNING,
- "ignoring invalid multibyte character");
- char_len = 1;
- c = *buffer->cur++;
- }
- else
- {
- buffer->cur += char_len;
- c = wc;
- }
-#else
- c = *buffer->cur++;
-#endif
-
- /* Handle trigraphs, escaped newlines etc. */
- if (c == '?' || c == '\\')
- c = skip_escaped_newlines (pfile);
-
- if (c == terminator)
- {
- if (unescaped_terminator_p (pfile, dest))
- break;
- }
- else if (is_vspace (c))
+ /* In #include-style directives, terminators are not escapable. */
+ if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
+ cur++;
+ else if (c == terminator)
+ break;
+ else if (c == '\n')
{
- /* No string literal may extend over multiple lines. In
- assembly language, suppress the error except for <>
- includes. This is a kludge around not knowing where
- comments are. */
- unterminated:
- if (CPP_OPTION (pfile, lang) != CLK_ASM || terminator == '>')
- cpp_error (pfile, DL_ERROR, "missing terminating %c character",
- (int) terminator);
- buffer->cur--;
+ cur--;
+ type = CPP_OTHER;
break;
}
else if (c == '\0')
- {
- if (buffer->cur - 1 == buffer->rlimit)
- goto unterminated;
- if (!warned_nulls)
- {
- warned_nulls = true;
- cpp_error (pfile, DL_WARNING,
- "null character(s) preserved in literal");
- }
- }
-#ifdef MULTIBYTE_CHARS
- if (char_len > 1)
- {
- for ( ; char_len > 0; --char_len)
- *dest++ = (*buffer->cur - char_len);
- }
- else
-#endif
- *dest++ = c;
+ saw_NUL = true;
}
- *dest = '\0';
+ if (saw_NUL && !pfile->state.skipping)
+ cpp_error (pfile, DL_WARNING, "null character(s) preserved in literal");
- token->val.str.text = BUFF_FRONT (pfile->u_buff);
- token->val.str.len = dest - BUFF_FRONT (pfile->u_buff);
- BUFF_FRONT (pfile->u_buff) = dest + 1;
+ pfile->buffer->cur = cur;
+ create_literal (pfile, token, base, cur - base, type);
}
/* The stored comment includes the comment start and any terminator. */
{
unsigned char *buffer;
unsigned int len, clen;
-
+
len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
/* C++ comments probably (not definitely) have moved past a new
clen = (pfile->state.in_directive && type == '/') ? len + 2 : len;
buffer = _cpp_unaligned_alloc (pfile, clen);
-
+
token->type = CPP_COMMENT;
token->val.str.len = clen;
token->val.str.text = buffer;
return result;
}
-#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
- do { \
- if (get_effective_char (pfile) == CHAR) \
- result->type = THEN_TYPE; \
- else \
- { \
- BACKUP (); \
- result->type = ELSE_TYPE; \
- } \
- } while (0)
+/* Returns true if a fresh line has been loaded. */
+bool
+_cpp_get_fresh_line (pfile)
+ cpp_reader *pfile;
+{
+ /* We can't get a new line until we leave the current directive. */
+ if (pfile->state.in_directive)
+ return false;
+
+ for (;;)
+ {
+ cpp_buffer *buffer = pfile->buffer;
+
+ if (!buffer->need_line)
+ return true;
+
+ if (buffer->next_line < buffer->rlimit)
+ {
+ _cpp_clean_line (pfile);
+ return true;
+ }
+
+ /* First, get out of parsing arguments state. */
+ if (pfile->state.parsing_args)
+ return false;
+
+ /* End of buffer. Non-empty files should end in a newline. */
+ if (buffer->buf != buffer->rlimit
+ && buffer->next_line > buffer->rlimit
+ && !buffer->from_stage3)
+ {
+ /* Only warn once. */
+ buffer->next_line = buffer->rlimit;
+ cpp_error_with_line (pfile, DL_PEDWARN, pfile->line - 1,
+ CPP_BUF_COLUMN (buffer, buffer->cur),
+ "no newline at end of file");
+ }
+
+ if (!buffer->prev)
+ return false;
+
+ if (buffer->return_at_eof)
+ {
+ _cpp_pop_buffer (pfile);
+ return false;
+ }
+
+ _cpp_pop_buffer (pfile);
+ }
+}
+
+#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
+ do \
+ { \
+ result->type = ELSE_TYPE; \
+ if (*buffer->cur == CHAR) \
+ buffer->cur++, result->type = THEN_TYPE; \
+ } \
+ while (0)
/* Lex a token into pfile->cur_token, which is also incremented, to
get diagnostics pointing to the correct location.
cpp_token *result = pfile->cur_token++;
fresh_line:
+ result->flags = 0;
+ if (pfile->buffer->need_line)
+ {
+ if (!_cpp_get_fresh_line (pfile))
+ {
+ result->type = CPP_EOF;
+ if (!pfile->state.in_directive)
+ {
+ /* Tell the compiler the line number of the EOF token. */
+ result->line = pfile->line;
+ result->flags = BOL;
+ }
+ return result;
+ }
+ if (!pfile->keep_tokens)
+ {
+ pfile->cur_run = &pfile->base_run;
+ result = pfile->base_run.base;
+ pfile->cur_token = result + 1;
+ }
+ result->flags = BOL;
+ if (pfile->state.parsing_args == 2)
+ result->flags |= PREV_WHITE;
+ }
buffer = pfile->buffer;
- result->flags = buffer->saved_flags;
- buffer->saved_flags = 0;
update_tokens_line:
result->line = pfile->line;
skipped_white:
+ if (buffer->cur >= buffer->notes[buffer->cur_note].pos
+ && !pfile->overlaid_buffer)
+ {
+ _cpp_process_line_notes (pfile, false);
+ result->line = pfile->line;
+ }
c = *buffer->cur++;
result->col = CPP_BUF_COLUMN (buffer, buffer->cur);
- trigraph:
switch (c)
{
case ' ': case '\t': case '\f': case '\v': case '\0':
result->flags |= PREV_WHITE;
- if (skip_whitespace (pfile, c))
- goto skipped_white;
-
- /* EOF. */
- buffer->cur--;
- buffer->saved_flags = BOL;
- if (!pfile->state.parsing_args && !pfile->state.in_directive)
- {
- if (buffer->cur != buffer->line_base)
- {
- /* Non-empty files should end in a newline. Don't warn
- for command line and _Pragma buffers. */
- if (!buffer->from_stage3)
- cpp_error (pfile, DL_PEDWARN, "no newline at end of file");
- handle_newline (pfile);
- }
-
- /* Don't pop the last buffer. */
- if (buffer->prev)
- {
- unsigned char stop = buffer->return_at_eof;
-
- _cpp_pop_buffer (pfile);
- if (!stop)
- goto fresh_line;
- }
- }
- result->type = CPP_EOF;
- break;
+ skip_whitespace (pfile, c);
+ goto skipped_white;
- case '\n': case '\r':
- handle_newline (pfile);
- buffer->saved_flags = BOL;
- if (! pfile->state.in_directive)
- {
- if (pfile->state.parsing_args == 2)
- buffer->saved_flags |= PREV_WHITE;
- if (!pfile->keep_tokens)
- {
- pfile->cur_run = &pfile->base_run;
- result = pfile->base_run.base;
- pfile->cur_token = result + 1;
- }
- goto fresh_line;
- }
- result->type = CPP_EOF;
- break;
-
- case '?':
- case '\\':
- /* These could start an escaped newline, or '?' a trigraph. Let
- skip_escaped_newlines do all the work. */
- {
- unsigned int line = pfile->line;
-
- c = skip_escaped_newlines (pfile);
- if (line != pfile->line)
- {
- buffer->cur--;
- /* We had at least one escaped newline of some sort.
- Update the token's line and column. */
- goto update_tokens_line;
- }
- }
-
- /* We are either the original '?' or '\\', or a trigraph. */
- if (c == '?')
- result->type = CPP_QUERY;
- else if (c == '\\')
- goto random_char;
- else
- goto trigraph;
- break;
+ case '\n':
+ pfile->line++;
+ buffer->need_line = true;
+ goto fresh_line;
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, 0);
+ lex_number (pfile, &result->val.str);
break;
case 'L':
/* 'L' may introduce wide characters or strings. */
+ if (*buffer->cur == '\'' || *buffer->cur == '"')
{
- const unsigned char *pos = buffer->cur;
-
- c = get_effective_char (pfile);
- if (c == '\'' || c == '"')
- {
- result->type = (c == '"' ? CPP_WSTRING: CPP_WCHAR);
- parse_string (pfile, result, c);
- break;
- }
- buffer->cur = pos;
+ lex_string (pfile, result, buffer->cur - 1);
+ break;
}
- /* Fall through. */
+ /* Fall through. */
- start_ident:
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 '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);
+ result->val.node = lex_identifier (pfile, buffer->cur - 1);
/* Convert named operators to their proper types. */
if (result->val.node->flags & NODE_OPERATOR)
{
result->flags |= NAMED_OP;
- result->type = result->val.node->value.operator;
+ result->type = result->val.node->directive_index;
}
break;
case '\'':
case '"':
- result->type = c == '"' ? CPP_STRING: CPP_CHAR;
- parse_string (pfile, result, c);
+ lex_string (pfile, result, buffer->cur - 1);
break;
case '/':
/* A potential block or line comment. */
comment_start = buffer->cur;
- c = get_effective_char (pfile);
-
+ c = *buffer->cur;
+
if (c == '*')
{
- if (skip_block_comment (pfile))
+ if (_cpp_skip_block_comment (pfile))
cpp_error (pfile, DL_ERROR, "unterminated comment");
}
else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
&& ! buffer->warned_cplusplus_comments)
{
cpp_error (pfile, DL_PEDWARN,
- "C++ style comments are not allowed in ISO C89");
+ "C++ style comments are not allowed in ISO C90");
cpp_error (pfile, DL_PEDWARN,
"(this will be reported only once per input file)");
buffer->warned_cplusplus_comments = 1;
}
else if (c == '=')
{
+ buffer->cur++;
result->type = CPP_DIV_EQ;
break;
}
else
{
- BACKUP ();
result->type = CPP_DIV;
break;
}
case '<':
if (pfile->state.angled_headers)
{
- result->type = CPP_HEADER_NAME;
- parse_string (pfile, result, '>');
+ lex_string (pfile, result, buffer->cur - 1);
break;
}
- c = get_effective_char (pfile);
- if (c == '=')
- result->type = CPP_LESS_EQ;
- else if (c == '<')
- IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
- else if (c == '?' && CPP_OPTION (pfile, cplusplus))
- IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
- else if (c == ':' && CPP_OPTION (pfile, digraphs))
+ result->type = CPP_LESS;
+ if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_LESS_EQ;
+ else if (*buffer->cur == '<')
{
- result->type = CPP_OPEN_SQUARE;
- result->flags |= DIGRAPH;
+ buffer->cur++;
+ IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
}
- else if (c == '%' && CPP_OPTION (pfile, digraphs))
+ else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
{
- result->type = CPP_OPEN_BRACE;
- result->flags |= DIGRAPH;
+ buffer->cur++;
+ IF_NEXT_IS ('=', CPP_MIN_EQ, CPP_MIN);
}
- else
+ else if (CPP_OPTION (pfile, digraphs))
{
- BACKUP ();
- result->type = CPP_LESS;
+ if (*buffer->cur == ':')
+ {
+ buffer->cur++;
+ result->flags |= DIGRAPH;
+ result->type = CPP_OPEN_SQUARE;
+ }
+ else if (*buffer->cur == '%')
+ {
+ buffer->cur++;
+ result->flags |= DIGRAPH;
+ result->type = CPP_OPEN_BRACE;
+ }
}
break;
case '>':
- c = get_effective_char (pfile);
- if (c == '=')
- result->type = CPP_GREATER_EQ;
- else if (c == '>')
- IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
- else if (c == '?' && CPP_OPTION (pfile, cplusplus))
- IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
- else
+ result->type = CPP_GREATER;
+ if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_GREATER_EQ;
+ else if (*buffer->cur == '>')
{
- BACKUP ();
- result->type = CPP_GREATER;
+ buffer->cur++;
+ IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
+ }
+ else if (*buffer->cur == '?' && CPP_OPTION (pfile, cplusplus))
+ {
+ buffer->cur++;
+ IF_NEXT_IS ('=', CPP_MAX_EQ, CPP_MAX);
}
break;
case '%':
- c = get_effective_char (pfile);
- if (c == '=')
- result->type = CPP_MOD_EQ;
- else if (CPP_OPTION (pfile, digraphs) && c == ':')
+ result->type = CPP_MOD;
+ if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_MOD_EQ;
+ else if (CPP_OPTION (pfile, digraphs))
{
- result->flags |= DIGRAPH;
- result->type = CPP_HASH;
- if (get_effective_char (pfile) == '%')
+ if (*buffer->cur == ':')
{
- const unsigned char *pos = buffer->cur;
-
- if (get_effective_char (pfile) == ':')
- result->type = CPP_PASTE;
- else
- buffer->cur = pos - 1;
+ buffer->cur++;
+ result->flags |= DIGRAPH;
+ result->type = CPP_HASH;
+ if (*buffer->cur == '%' && buffer->cur[1] == ':')
+ buffer->cur += 2, result->type = CPP_PASTE;
+ }
+ else if (*buffer->cur == '>')
+ {
+ buffer->cur++;
+ result->flags |= DIGRAPH;
+ result->type = CPP_CLOSE_BRACE;
}
- else
- BACKUP ();
- }
- else if (CPP_OPTION (pfile, digraphs) && c == '>')
- {
- result->flags |= DIGRAPH;
- result->type = CPP_CLOSE_BRACE;
- }
- else
- {
- BACKUP ();
- result->type = CPP_MOD;
}
break;
case '.':
result->type = CPP_DOT;
- c = get_effective_char (pfile);
- if (c == '.')
- {
- const unsigned char *pos = buffer->cur;
-
- if (get_effective_char (pfile) == '.')
- result->type = CPP_ELLIPSIS;
- else
- buffer->cur = pos - 1;
- }
- /* All known character sets have 0...9 contiguous. */
- else if (ISDIGIT (c))
+ if (ISDIGIT (*buffer->cur))
{
result->type = CPP_NUMBER;
- parse_number (pfile, &result->val.str, 1);
+ lex_number (pfile, &result->val.str);
}
- else if (c == '*' && CPP_OPTION (pfile, cplusplus))
- result->type = CPP_DOT_STAR;
- else
- BACKUP ();
+ else if (*buffer->cur == '.' && buffer->cur[1] == '.')
+ buffer->cur += 2, result->type = CPP_ELLIPSIS;
+ else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
+ buffer->cur++, result->type = CPP_DOT_STAR;
break;
case '+':
- c = get_effective_char (pfile);
- if (c == '+')
- result->type = CPP_PLUS_PLUS;
- else if (c == '=')
- result->type = CPP_PLUS_EQ;
- else
- {
- BACKUP ();
- result->type = CPP_PLUS;
- }
+ result->type = CPP_PLUS;
+ if (*buffer->cur == '+')
+ buffer->cur++, result->type = CPP_PLUS_PLUS;
+ else if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_PLUS_EQ;
break;
case '-':
- c = get_effective_char (pfile);
- if (c == '>')
+ result->type = CPP_MINUS;
+ if (*buffer->cur == '>')
{
+ buffer->cur++;
result->type = CPP_DEREF;
- if (CPP_OPTION (pfile, cplusplus))
- {
- if (get_effective_char (pfile) == '*')
- result->type = CPP_DEREF_STAR;
- else
- BACKUP ();
- }
- }
- else if (c == '-')
- result->type = CPP_MINUS_MINUS;
- else if (c == '=')
- result->type = CPP_MINUS_EQ;
- else
- {
- BACKUP ();
- result->type = CPP_MINUS;
+ if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
+ buffer->cur++, result->type = CPP_DEREF_STAR;
}
+ else if (*buffer->cur == '-')
+ buffer->cur++, result->type = CPP_MINUS_MINUS;
+ else if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_MINUS_EQ;
break;
case '&':
- c = get_effective_char (pfile);
- if (c == '&')
- result->type = CPP_AND_AND;
- else if (c == '=')
- result->type = CPP_AND_EQ;
- else
- {
- BACKUP ();
- result->type = CPP_AND;
- }
+ result->type = CPP_AND;
+ if (*buffer->cur == '&')
+ buffer->cur++, result->type = CPP_AND_AND;
+ else if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_AND_EQ;
break;
-
+
case '|':
- c = get_effective_char (pfile);
- if (c == '|')
- result->type = CPP_OR_OR;
- else if (c == '=')
- result->type = CPP_OR_EQ;
- else
- {
- BACKUP ();
- result->type = CPP_OR;
- }
+ result->type = CPP_OR;
+ if (*buffer->cur == '|')
+ buffer->cur++, result->type = CPP_OR_OR;
+ else if (*buffer->cur == '=')
+ buffer->cur++, result->type = CPP_OR_EQ;
break;
case ':':
- c = get_effective_char (pfile);
- if (c == ':' && CPP_OPTION (pfile, cplusplus))
- result->type = CPP_SCOPE;
- else if (c == '>' && CPP_OPTION (pfile, digraphs))
+ result->type = CPP_COLON;
+ if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
+ buffer->cur++, result->type = CPP_SCOPE;
+ else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
{
+ buffer->cur++;
result->flags |= DIGRAPH;
result->type = CPP_CLOSE_SQUARE;
}
- else
- {
- BACKUP ();
- result->type = CPP_COLON;
- }
break;
case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); break;
+ case '?': result->type = CPP_QUERY; 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_BRACE; break;
case ';': result->type = CPP_SEMICOLON; break;
- /* @ is a punctuator in Objective C. */
+ /* @ is a punctuator in Objective-C. */
case '@': result->type = CPP_ATSIGN; break;
case '$':
- if (CPP_OPTION (pfile, dollars_in_ident))
- goto start_ident;
- /* Fall through... */
+ case '\\':
+ {
+ const uchar *base = --buffer->cur;
+
+ if (forms_identifier_p (pfile, true))
+ {
+ result->type = CPP_NAME;
+ result->val.node = lex_identifier (pfile, base);
+ break;
+ }
+ buffer->cur++;
+ }
- random_char:
default:
- result->type = CPP_OTHER;
- result->val.c = c;
+ create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
break;
}
return result;
}
-/* An upper bound on the number of bytes needed to spell TOKEN,
- including preceding whitespace. */
+/* An upper bound on the number of bytes needed to spell TOKEN.
+ Does not include preceding whitespace. */
unsigned int
cpp_token_len (token)
const cpp_token *token;
switch (TOKEN_SPELL (token))
{
- default: len = 0; break;
- case SPELL_NUMBER:
- case SPELL_STRING: len = token->val.str.len; break;
+ default: len = 4; break;
+ case SPELL_LITERAL: len = token->val.str.len; break;
case SPELL_IDENT: len = NODE_LEN (token->val.node); break;
}
- /* 1 for whitespace, 4 for comment delimiters. */
- return len + 5;
+
+ return len;
}
/* Write the spelling of a token TOKEN to BUFFER. The buffer must
goto spell_ident;
else
spelling = TOKEN_NAME (token);
-
+
while ((c = *spelling++) != '\0')
*buffer++ = c;
}
break;
- case SPELL_CHAR:
- *buffer++ = token->val.c;
- break;
-
spell_ident:
case SPELL_IDENT:
memcpy (buffer, NODE_NAME (token->val.node), NODE_LEN (token->val.node));
buffer += NODE_LEN (token->val.node);
break;
- case SPELL_NUMBER:
+ case SPELL_LITERAL:
memcpy (buffer, token->val.str.text, token->val.str.len);
buffer += token->val.str.len;
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_CHAR: left = '\''; right = '\''; tag = '\0'; break;
- case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
- case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
- default:
- cpp_error (pfile, DL_ICE, "unknown string token %s\n",
- TOKEN_NAME (token));
- return buffer;
- }
- if (tag) *buffer++ = tag;
- *buffer++ = left;
- memcpy (buffer, token->val.str.text, token->val.str.len);
- buffer += token->val.str.len;
- *buffer++ = right;
- }
- break;
-
case SPELL_NONE:
cpp_error (pfile, DL_ICE, "unspellable token %s", TOKEN_NAME (token));
break;
cpp_token_as_text (pfile, token)
cpp_reader *pfile;
const cpp_token *token;
-{
- unsigned int len = cpp_token_len (token);
+{
+ unsigned int len = cpp_token_len (token) + 1;
unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
end = cpp_spell_token (pfile, token, start);
}
break;
- case SPELL_CHAR:
- putc (token->val.c, fp);
- break;
-
spell_ident:
case SPELL_IDENT:
fwrite (NODE_NAME (token->val.node), 1, NODE_LEN (token->val.node), fp);
break;
- case SPELL_NUMBER:
+ case SPELL_LITERAL:
fwrite (token->val.str.text, 1, token->val.str.len, fp);
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_CHAR: left = '\''; right = '\''; tag = '\0'; break;
- case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
- case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
- default:
- fprintf (stderr, "impossible STRING token %s\n", TOKEN_NAME (token));
- return;
- }
- if (tag) putc (tag, fp);
- putc (left, fp);
- fwrite (token->val.str.text, 1, token->val.str.len, fp);
- putc (right, fp);
- }
- break;
-
case SPELL_NONE:
/* An error, most probably. */
break;
default: /* Keep compiler happy. */
case SPELL_OPERATOR:
return 1;
- case SPELL_CHAR:
- return a->val.c == b->val.c; /* Character. */
case SPELL_NONE:
return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
case SPELL_IDENT:
return a->val.node == b->val.node;
- case SPELL_NUMBER:
- case SPELL_STRING:
+ case SPELL_LITERAL:
return (a->val.str.len == b->val.str.len
&& !memcmp (a->val.str.text, b->val.str.text,
a->val.str.len));
|| b == CPP_CHAR || b == CPP_STRING); /* L */
case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
|| c == '.' || c == '+' || c == '-');
- case CPP_OTHER: return (CPP_OPTION (pfile, objc)
- && token1->val.c == '@'
- && (b == CPP_NAME || b == CPP_STRING));
+ /* UCNs */
+ case CPP_OTHER: return ((token1->val.str.text[0] == '\\'
+ && b == CPP_NAME)
+ || (CPP_OPTION (pfile, objc)
+ && token1->val.str.text[0] == '@'
+ && (b == CPP_NAME || b == CPP_STRING)));
default: break;
}
abort ();
}
-/* Parse a '\uNNNN' or '\UNNNNNNNN' sequence. Returns 1 to indicate
- failure if cpplib is not parsing C++ or C99. Such failure is
- silent, and no variables are updated. Otherwise returns 0, and
- warns if -Wtraditional.
-
- [lex.charset]: The character designated by the universal character
- name \UNNNNNNNN is that character whose character short name in
- ISO/IEC 10646 is NNNNNNNN; the character designated by the
- universal character name \uNNNN is that character whose character
- short name in ISO/IEC 10646 is 0000NNNN. If the hexadecimal value
- for a universal character name is less than 0x20 or in the range
- 0x7F-0x9F (inclusive), or if the universal character name
- designates a character in the basic source character set, then the
- program is ill-formed.
-
- We assume that wchar_t is Unicode, so we don't need to do any
- mapping. Is this ever wrong?
-
- PC points to the 'u' or 'U', PSTR is points to the byte after PC,
- LIMIT is the end of the string or charconst. PSTR is updated to
- point after the UCS on return, and the UCS is written into PC. */
-
-static int
-maybe_read_ucs (pfile, pstr, limit, pc)
+/* Read a possible universal character name starting at *PSTR. */
+static cppchar_t
+maybe_read_ucn (pfile, pstr)
cpp_reader *pfile;
- const unsigned char **pstr;
- const unsigned char *limit;
- cppchar_t *pc;
+ const uchar **pstr;
{
- const unsigned char *p = *pstr;
- unsigned int code = 0;
- unsigned int c = *pc, length;
-
- /* Only attempt to interpret a UCS for C++ and C99. */
- if (! (CPP_OPTION (pfile, cplusplus) || CPP_OPTION (pfile, c99)))
- return 1;
-
- if (CPP_WTRADITIONAL (pfile))
- cpp_error (pfile, DL_WARNING,
- "the meaning of '\\%c' is different in traditional C", c);
-
- length = (c == 'u' ? 4: 8);
+ cppchar_t result, c = (*pstr)[-1];
- if ((size_t) (limit - p) < length)
+ result = _cpp_valid_ucn (pfile, pstr, false);
+ if (result)
{
- cpp_error (pfile, DL_ERROR, "incomplete universal-character-name");
- /* Skip to the end to avoid more diagnostics. */
- p = limit;
- }
- else
- {
- for (; length; length--, p++)
+ if (CPP_WTRADITIONAL (pfile))
+ cpp_error (pfile, DL_WARNING,
+ "the meaning of '\\%c' is different in traditional C",
+ (int) c);
+
+ if (CPP_OPTION (pfile, EBCDIC))
{
- c = *p;
- if (ISXDIGIT (c))
- code = (code << 4) + hex_digit_value (c);
- else
- {
- cpp_error (pfile, DL_ERROR,
- "non-hex digit '%c' in universal-character-name", c);
- /* We shouldn't skip in case there are multibyte chars. */
- break;
- }
+ cpp_error (pfile, DL_ERROR,
+ "universal character with an EBCDIC target");
+ result = 0x3f; /* EBCDIC invalid character */
}
}
-#ifdef TARGET_EBCDIC
- cpp_error (pfile, DL_ERROR, "universal-character-name on EBCDIC target");
- code = 0x3f; /* EBCDIC invalid character */
-#else
- /* True extended characters are OK. */
- if (code >= 0xa0
- && !(code & 0x80000000)
- && !(code >= 0xD800 && code <= 0xDFFF))
- ;
- /* The standard permits $, @ and ` to be specified as UCNs. We use
- hex escapes so that this also works with EBCDIC hosts. */
- else if (code == 0x24 || code == 0x40 || code == 0x60)
- ;
- /* Don't give another error if one occurred above. */
- else if (length == 0)
- cpp_error (pfile, DL_ERROR, "universal-character-name out of range");
-#endif
-
- *pstr = p;
- *pc = code;
- return 0;
+ return result;
}
/* Returns the value of an escape sequence, truncated to the correct
const unsigned char *limit;
int wide;
{
+ /* Values of \a \b \e \f \n \r \t \v respectively. */
+ static const uchar ascii[] = { 7, 8, 27, 12, 10, 13, 9, 11 };
+ static const uchar ebcdic[] = { 47, 22, 39, 12, 21, 13, 5, 11 };
+
int unknown = 0;
- const unsigned char *str = *pstr;
- cppchar_t c, mask;
+ const unsigned char *str = *pstr, *charconsts;
+ cppchar_t c, ucn, mask;
unsigned int width;
+ if (CPP_OPTION (pfile, EBCDIC))
+ charconsts = ebcdic;
+ else
+ charconsts = ascii;
+
if (wide)
width = CPP_OPTION (pfile, wchar_precision);
else
switch (c)
{
case '\\': case '\'': case '"': case '?': break;
- case 'b': c = TARGET_BS; break;
- case 'f': c = TARGET_FF; break;
- case 'n': c = TARGET_NEWLINE; break;
- case 'r': c = TARGET_CR; break;
- case 't': c = TARGET_TAB; break;
- case 'v': c = TARGET_VT; break;
+ case 'b': c = charconsts[1]; break;
+ case 'f': c = charconsts[3]; break;
+ case 'n': c = charconsts[4]; break;
+ case 'r': c = charconsts[5]; break;
+ case 't': c = charconsts[6]; break;
+ case 'v': c = charconsts[7]; break;
case '(': case '{': case '[': case '%':
/* '\(', etc, are used at beginning of line to avoid confusing Emacs.
if (CPP_WTRADITIONAL (pfile))
cpp_error (pfile, DL_WARNING,
"the meaning of '\\a' is different in traditional C");
- c = TARGET_BELL;
+ c = charconsts[0];
break;
case 'e': case 'E':
if (CPP_PEDANTIC (pfile))
cpp_error (pfile, DL_PEDWARN,
"non-ISO-standard escape sequence, '\\%c'", (int) c);
- c = TARGET_ESC;
+ c = charconsts[2];
break;
-
+
case 'u': case 'U':
- unknown = maybe_read_ucs (pfile, &str, limit, &c);
+ ucn = maybe_read_ucn (pfile, &str);
+ if (ucn)
+ c = ucn;
+ else
+ unknown = true;
break;
case 'x':
cpp_error (pfile, DL_WARNING,
"the meaning of '\\x' is different in traditional C");
- {
- cppchar_t i = 0, overflow = 0;
- int digits_found = 0;
+ {
+ cppchar_t i = 0, overflow = 0;
+ int digits_found = 0;
- while (str < limit)
- {
- c = *str;
- if (! ISXDIGIT (c))
- break;
- str++;
- overflow |= i ^ (i << 4 >> 4);
- i = (i << 4) + hex_digit_value (c);
- digits_found = 1;
- }
+ while (str < limit)
+ {
+ c = *str;
+ if (! ISXDIGIT (c))
+ break;
+ str++;
+ overflow |= i ^ (i << 4 >> 4);
+ i = (i << 4) + hex_digit_value (c);
+ digits_found = 1;
+ }
- if (!digits_found)
- cpp_error (pfile, DL_ERROR,
+ if (!digits_found)
+ cpp_error (pfile, DL_ERROR,
"\\x used with no following hex digits");
- if (overflow | (i != (i & mask)))
- {
- cpp_error (pfile, DL_PEDWARN,
- "hex escape sequence out of range");
- i &= mask;
- }
- c = i;
- }
+ if (overflow | (i != (i & mask)))
+ {
+ cpp_error (pfile, DL_PEDWARN,
+ "hex escape sequence out of range");
+ i &= mask;
+ }
+ c = i;
+ }
break;
case '0': case '1': case '2': case '3':
unsigned int *pchars_seen;
int *unsignedp;
{
- const unsigned char *str = token->val.str.text;
- const unsigned char *limit = str + token->val.str.len;
+ const unsigned char *str, *limit;
unsigned int chars_seen = 0;
size_t width, max_chars;
cppchar_t c, mask, result = 0;
bool unsigned_p;
-#ifdef MULTIBYTE_CHARS
- (void) local_mbtowc (NULL, NULL, 0);
-#endif
+ str = token->val.str.text + 1 + (token->type == CPP_WCHAR);
+ limit = token->val.str.text + token->val.str.len - 1;
- /* Width in bits. */
if (token->type == CPP_CHAR)
{
width = CPP_OPTION (pfile, char_precision);
max_chars = CPP_OPTION (pfile, int_precision) / width;
- unsigned_p = CPP_OPTION (pfile, signed_char) == 0;
+ unsigned_p = CPP_OPTION (pfile, unsigned_char);
}
else
{
width = CPP_OPTION (pfile, wchar_precision);
max_chars = 1;
- unsigned_p = WCHAR_UNSIGNED;
+ unsigned_p = CPP_OPTION (pfile, unsigned_wchar);
}
if (width < BITS_PER_CPPCHAR_T)
while (str < limit)
{
-#ifdef MULTIBYTE_CHARS
- wchar_t wc;
- int char_len;
-
- char_len = local_mbtowc (&wc, str, limit - str);
- if (char_len == -1)
- {
- cpp_error (pfile, DL_WARNING,
- "ignoring invalid multibyte character");
- c = *str++;
- }
- else
- {
- str += char_len;
- c = wc;
- }
-#else
c = *str++;
-#endif
if (c == '\\')
c = cpp_parse_escape (pfile, &str, limit, token->type == CPP_WCHAR);
if (ISPRINT (c))
c = MAP_CHARACTER (c);
#endif
-
+
chars_seen++;
/* Truncate the character, scale the result and merge the two. */
cpp_error (pfile, DL_WARNING, "multi-character character constant");
}
- /* Sign-extend the constant. */
- if (!unsigned_p)
+ /* Sign-extend or truncate the constant to cppchar_t. The value is
+ in WIDTH bits, but for multi-char charconsts it's value is the
+ full target type's width. */
+ if (chars_seen > 1)
+ width *= max_chars;
+ if (width < BITS_PER_CPPCHAR_T)
{
- size_t precision = width;
-
- if (chars_seen > 1)
- precision *= max_chars;
- if (precision < BITS_PER_CPPCHAR_T
- && (result & ((cppchar_t) 1 << (precision - 1))))
- result |= ~(((cppchar_t) 1 << precision) - 1);
+ mask = ((cppchar_t) 1 << width) - 1;
+ if (unsigned_p || !(result & (1 << (width - 1))))
+ result &= mask;
+ else
+ result |= ~mask;
}
*pchars_seen = chars_seen;
#error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
#endif
-struct dummy
-{
- char c;
- union
- {
- double d;
- int *p;
- } u;
-};
-
-#define DEFAULT_ALIGNMENT (offsetof (struct dummy, u))
-#define CPP_ALIGN(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
-
/* Create a new allocation buffer. Place the control block at the end
of the buffer, so that buffer overflows will cause immediate chaos. */
static _cpp_buff *
if (len < MIN_BUFF_SIZE)
len = MIN_BUFF_SIZE;
- len = CPP_ALIGN (len, DEFAULT_ALIGNMENT);
+ len = CPP_ALIGN (len);
base = xmalloc (len + sizeof (_cpp_buff));
result = (_cpp_buff *) (base + len);