+
+/* Utility routine:
+ Compares, in the manner of strcmp(3), the token beginning at TOKEN
+ and extending for LEN characters to the NUL-terminated string
+ STRING. Typical usage:
+
+ if (! cpp_idcmp (pfile->token_buffer + here, CPP_WRITTEN (pfile) - here,
+ "inline"))
+ { ... }
+ */
+
+int
+cpp_idcmp (token, len, string)
+ const U_CHAR *token;
+ size_t len;
+ const char *string;
+{
+ size_t len2 = strlen (string);
+ int r;
+
+ if ((r = memcmp (token, string, MIN (len, len2))))
+ return r;
+
+ /* The longer of the two strings sorts after the shorter. */
+ if (len == len2)
+ return 0;
+ else if (len < len2)
+ return -1;
+ else
+ return 1;
+}
+
+#ifdef NEW_LEXER
+
+/* 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_NOT 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 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 after dropping the escaped newline is a number or
+ identifier, and if so to continue the processing it 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.
+
+*/
+
+static const unsigned char *digraph_spellings [] = {U"%:", U"%:%:", U"<:",
+ U":>", U"<%", U"%>"};
+static unsigned char trigraph_map[256];
+
+void
+init_trigraph_map ()
+{
+ trigraph_map['='] = '#';
+ trigraph_map['('] = '[';
+ trigraph_map[')'] = ']';
+ trigraph_map['/'] = '\\';
+ trigraph_map['\''] = '^';
+ trigraph_map['<'] = '{';
+ trigraph_map['>'] = '}';
+ trigraph_map['!'] = '|';
+ trigraph_map['-'] = '~';
+}
+
+/* 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))
+ {
+ 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;
+}
+
+/* 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. */
+
+static unsigned char*
+trigraph_replace (pfile, src, limit)
+ cpp_reader *pfile;
+ unsigned char *src;
+ unsigned char* limit;
+{
+ unsigned char *dest;
+
+ /* Starting with src[1], find two consecutive '?'. The case of no
+ trigraphs is streamlined. */
+
+ for (; src + 1 < limit; src += 2)
+ {
+ if (src[0] != '?')
+ continue;
+
+ /* Make src point to the 1st (NOT 2nd) of two consecutive '?'s. */
+ if (src[-1] == '?')
+ src--;
+ else if (src + 2 == limit || src[1] != '?')
+ continue;
+
+ /* Check if it really is a trigraph. */
+ if (trigraph_map[src[2]] == 0)
+ continue;
+
+ dest = src;
+ goto trigraph_found;
+ }
+ 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;
+}
+
+/* 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)
+ 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;
+}
+
+/* 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. */
+static int
+skip_block_comment2 (pfile)
+ cpp_reader *pfile;
+{
+ 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 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))
+ {
+ 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;
+ }
+ }
+ 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_comment2 (pfile)
+ cpp_reader *pfile;
+{
+ cpp_buffer *buffer = pfile->buffer;
+ register const unsigned char *cur = buffer->cur;
+ int multiline = 0;
+
+ for (; cur < buffer->rlimit; )
+ {
+ unsigned char c = *cur++;
+
+ 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++;
+
+ out:
+ buffer->cur = cur - 1; /* Leave newline for caller. */
+ return multiline;
+}
+
+/* Skips whitespace, stopping at next non-whitespace character.
+ Adjusts pfile->col_adjust to account for tabs. This enables tokens
+ to be assigned the correct column. */
+static void
+skip_whitespace (pfile, in_directive)
+ cpp_reader *pfile;
+ int in_directive;
+{
+ 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++;
+
+ if (c == '\t')
+ {
+ unsigned int col = CPP_BUF_COLUMN (buffer, cur - 1);
+ pfile->col_adjust += (CPP_OPTION (pfile, tabstop) - 1
+ - col % CPP_OPTION(pfile, tabstop));
+ }
+ 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++;
+
+ out:
+ buffer->cur = cur - 1;
+ if (null_count)
+ cpp_warning (pfile, null_count > 1 ? "embedded null characters ignored"
+ : "embedded null character ignored");
+}
+
+/* Parse (append) an identifier. */
+static void
+parse_name (pfile, list, name)
+ cpp_reader *pfile;
+ cpp_toklist *list;
+ cpp_name *name;
+{
+ 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;
+
+ for (; cur < buffer->rlimit && namebuf < name_limit; )
+ {
+ unsigned char c = *namebuf = *cur; /* Copy a single char. */
+
+ if (! is_idchar(c))
+ goto out;
+ namebuf++;
+ cur++;
+ if (c == '$' && CPP_PEDANTIC (pfile))
+ {
+ buffer->cur = cur;
+ cpp_pedwarn (pfile, "'$' character in identifier");
+ }
+ }
+
+ /* 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 - name->text;
+ list->name_used = namebuf - list->namebuf;
+}
+
+/* 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)
+ cpp_reader *pfile;
+ cpp_toklist *list;
+ cpp_name *name;
+{
+ 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;
+
+ for (; cur < buffer->rlimit && namebuf < name_limit; )
+ {
+ unsigned char c = *namebuf = *cur; /* Copy a single char. */
+
+ /* 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;
+
+ namebuf++;
+ cur++;
+ }
+
+ /* 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 - name->text;
+ 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 ('"') and angled headers ('>'). Multi-line strings are
+ allowed, except for within directives. */
+
+static void
+parse_string2 (pfile, list, name, terminator, multiline_ok)
+ cpp_reader *pfile;
+ cpp_toklist *list;
+ cpp_name *name;
+ unsigned int terminator;
+ int multiline_ok;
+{
+ 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;
+
+ for (; cur < buffer->rlimit && namebuf < name_limit; )
+ {
+ unsigned int c = *namebuf++ = *cur++; /* Copy a single char. */
+
+ if (c == '\0')
+ null_count++;
+ else if (c == terminator || IS_NEWLINE (c))
+ {
+ /* 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->text + trigraphed_len,
+ namebuf);
+ trigraphed_len = namebuf - 2 - (name->text + 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 (!multiline_ok)
+ 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;
+
+ /* An odd number of consecutive backslashes represents
+ an escaped terminator. */
+ temp = namebuf - 1;
+ while (temp >= name->text && *temp == '\\')
+ temp--;
+
+ if ((namebuf - temp) & 1)
+ goto out;
+ namebuf++;
+ }
+ }
+ }
+
+ /* 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--;
+
+ unterminated:
+ cpp_error (pfile, "missing terminating %c character", (int) terminator);
+
+ 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 - name->text;
+ list->name_used = namebuf - list->namebuf;
+
+ if (null_count > 0)
+ cpp_warning (pfile, (null_count > 1 ? "null characters preserved"
+ : "null character preserved"));
+}
+
+/* The character TYPE helps us distinguish comment types: '*' = C
+ style, '-' = Chill-style and '/' = C++ style. For code simplicity,
+ the stored comment includes the comment start and any terminator. */
+
+#define COMMENT_START_LEN 2
+static void
+save_comment (list, token, from, len, type)
+ cpp_toklist *list;
+ cpp_token *token;
+ const unsigned char *from;
+ unsigned int len;
+ unsigned int type;
+{
+ unsigned char *buffer;
+
+ len += COMMENT_START_LEN;
+
+ if (list->name_used + len > list->name_cap)
+ expand_name_space (list, len);
+
+ INIT_TOKEN_NAME (list, token);
+ token->type = CPP_COMMENT;
+ token->val.name.len = len;
+
+ buffer = list->namebuf + list->name_used;
+ list->name_used += len;
+
+ /* Copy the comment. */
+ if (type == '*')
+ {
+ *buffer++ = '/';
+ *buffer++ = '*';
+ }
+ else
+ {
+ *buffer++ = type;
+ *buffer++ = type;
+ }
+ memcpy (buffer, from, len - COMMENT_START_LEN);
+}
+
+/*
+ * 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.
+ *
+ * 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 IS_DIRECTIVE() (list->tokens[first_token].type == CPP_HASH)
+
+void
+_cpp_lex_line (pfile, list)
+ 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;
+ unsigned int first_token = list->tokens_used;
+
+ list->line = CPP_BUF_LINE (buffer);
+ pfile->col_adjust = 0;
+ expanded:
+ token_limit = list->tokens + list->tokens_cap;
+ cur_token = list->tokens + list->tokens_used;
+
+ for (; cur < buffer->rlimit && cur_token < token_limit;)
+ {
+ unsigned char c = *cur++;
+
+ /* Optimize whitespace skipping, as most tokens are probably
+ separated by whitespace. (' ' '\t' '\v' '\f' '\0'). */
+
+ if (is_hspace ((unsigned int) c))
+ {
+ /* Step back to get the null warning and tab correction. */
+ buffer->cur = cur - 1;
+ skip_whitespace (pfile, IS_DIRECTIVE ());
+ cur = buffer->cur;
+
+ flags = PREV_WHITESPACE;
+ if (cur == buffer->rlimit)
+ break;
+ c = *cur++;
+ }
+
+ /* Initialize current token. Its type is set in the switch. */
+ cur_token->col = CPP_BUF_COLUMN (buffer, cur);
+ cur_token->flags = flags;
+ flags = 0;
+
+ switch (c)
+ {
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ {
+ int prev_dot;
+
+ cur--; /* Backup character. */
+ prev_dot = PREV_TOKEN_TYPE == CPP_DOT && IMMED_TOKEN ();
+ if (prev_dot)
+ cur_token--;
+ INIT_TOKEN_NAME (list, cur_token);
+ /* Prepend an immediately previous CPP_DOT token. */
+ if (prev_dot)
+ {
+ if (list->name_cap == list->name_used)
+ auto_expand_name_space (list);
+
+ cur_token->val.name.len = 1;
+ list->namebuf[list->name_used++] = '.';
+ }
+
+ continue_number:
+ cur_token->type = CPP_NUMBER; /* Before parse_number. */
+ buffer->cur = cur;
+ parse_number (pfile, list, &cur_token->val.name);
+ cur = buffer->cur;
+ cur_token++;
+ }
+ 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':
+ cur--; /* Backup character. */
+ INIT_TOKEN_NAME (list, cur_token);
+ 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 () && cur_token == &list->tokens[first_token + 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
+ && cur_token[-1].val.name.text[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_TOKEN_NAME (list, cur_token);
+ buffer->cur = cur;
+ parse_string2 (pfile, list, &cur_token->val.name, c,
+ c == '"' && !IS_DIRECTIVE());
+ 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 (CPP_IN_SYSTEM_HEADER (pfile))
+ 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_comment2 (pfile))
+ cpp_error_with_line (pfile, list->line,
+ cur_token[-1].col,
+ "multi-line comment");
+
+ /* Back-up to first '-' or '/'. */
+ cur_token--;
+ if (!CPP_OPTION (pfile, discard_comments)
+ && (!IS_DIRECTIVE() || list->dirno == 0))
+ save_comment (list, cur_token++, cur,
+ buffer->cur - cur, c);
+ cur = buffer->cur;
+
+ if (!CPP_OPTION (pfile, traditional))
+ flags = PREV_WHITESPACE;
+ break;
+ }
+ }
+ }
+ 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_comment2 (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");
+
+ /* Back up to opening '/'. */
+ cur_token--;
+ if (!CPP_OPTION (pfile, discard_comments)
+ && (!IS_DIRECTIVE() || list->dirno == 0))
+ save_comment (list, cur_token++, cur,
+ buffer->cur - cur, c);
+ cur = buffer->cur;
+
+ if (!CPP_OPTION (pfile, traditional))
+ flags = PREV_WHITESPACE;
+ break;
+ }
+ 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 ())
+ {
+ /* 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++;
+ }
+ /* Remember whitespace setting. */
+ flags = cur_token->flags;
+ break;
+ }
+ if (PREV_TOKEN_TYPE == CPP_BACKSLASH)
+ {
+ buffer->cur = cur;
+ cpp_warning (pfile, "backslash and newline separated by space");
+ }
+ /* Skip vertical space until we have at least one token to
+ return. */
+ if (cur_token != &list->tokens[first_token])
+ goto out;
+ list->line = CPP_BUF_LINE (buffer);
+ break;
+
+ case '-':
+ if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_MINUS)
+ {
+ if (CPP_OPTION (pfile, chill))
+ goto do_line_comment;
+ REVISE_TOKEN (CPP_MINUS_MINUS);
+ }
+ 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;
+
+ 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;
+
+ case '&':
+ if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_AND)
+ REVISE_TOKEN (CPP_AND_AND);
+ else
+ PUSH_TOKEN (CPP_AND);
+ break;
+
+ make_or:
+ case '|':
+ if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_OR)
+ REVISE_TOKEN (CPP_OR_OR);
+ else
+ PUSH_TOKEN (CPP_OR);
+ break;
+
+ case '+':
+ if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_PLUS)
+ REVISE_TOKEN (CPP_PLUS_PLUS);
+ else
+ PUSH_TOKEN (CPP_PLUS);
+ break;
+
+ 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;
+
+ 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->flags & SYNTAX_INCLUDE)
+ {
+ c = '>'; /* Terminator. */
+ cur_token->type = CPP_HEADER_NAME;
+ goto do_parse_string;
+ }
+ PUSH_TOKEN (CPP_LESS);
+ break;
+
+ 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;
+
+ 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;
+
+ 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;
+
+ make_complement:
+ case '~': PUSH_TOKEN (CPP_COMPL); break;
+ 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 '(': PUSH_TOKEN (CPP_OPEN_PAREN); break;
+ case ')': PUSH_TOKEN (CPP_CLOSE_PAREN); break;
+
+ case '$':
+ if (CPP_OPTION (pfile, dollars_in_ident))
+ goto letter;
+ /* Fall through */
+ default:
+ cur_token->aux = c;
+ cur_token->val.name.len = 0; /* FIXME: needed for transition only */
+ PUSH_TOKEN (CPP_OTHER);
+ break;
+ }
+ }
+
+ /* Run out of token space? */
+ if (cur_token == token_limit)
+ {
+ list->tokens_used = cur_token - list->tokens;
+ _cpp_expand_token_space (list, 256);
+ goto expanded;
+ }
+
+ cur_token->flags = flags;
+ if (cur_token == &list->tokens[first_token])
+ {
+ /* FIXME: move this warning to callers who care. */
+ if (cur > buffer->buf && !IS_NEWLINE (cur[-1]))
+ cpp_warning (pfile, "no newline at end of file");
+ cur_token++->type = CPP_EOF;
+ }
+
+ out:
+ list->tokens[first_token].flags |= BOL;
+ buffer->cur = cur;
+ list->tokens_used = cur_token - list->tokens;
+}
+
+/* Write the spelling of a token TOKEN to BUFFER. The buffer must
+ already contain the enough space to hold the token's spelling. If
+ WHITESPACE is true, and the token was preceded by whitespace,
+ output a single space before the token proper. Returns a pointer
+ to the character after the last character written. */
+
+static unsigned char *
+spell_token (pfile, token, buffer, whitespace)
+ cpp_reader *pfile; /* Would be nice to be rid of this... */
+ const cpp_token *token;
+ unsigned char *buffer;
+ int whitespace;
+{
+ /* Whitespace will not be wanted by handlers of the # and ##
+ operators calling this function, but will be wanted by the
+ function that writes out the preprocessed file. */
+ if (whitespace && token->flags & PREV_WHITESPACE)
+ *buffer++ = ' ';
+
+ switch (token_spellings[token->type].type)
+ {
+ case SPELL_OPERATOR:
+ {
+ const unsigned char *spelling;
+ unsigned char c;
+
+ if (token->flags & DIGRAPH)
+ spelling = digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
+ else
+ spelling = token_spellings[token->type].spelling;
+
+ while ((c = *spelling++) != '\0')
+ *buffer++ = c;
+ }
+ break;
+
+ case SPELL_IDENT:
+ memcpy (buffer, token->val.name.text, token->val.name.len);
+ buffer += token->val.name.len;
+ break;
+
+ case SPELL_STRING:
+ {
+ unsigned char c;
+
+ if (token->type == CPP_WSTRING || token->type == CPP_WCHAR)
+ *buffer++ = 'L';
+ c = '\'';
+ if (token->type == CPP_STRING || token->type == CPP_WSTRING)
+ c = '"';
+ *buffer++ = c;
+ memcpy (buffer, token->val.name.text, token->val.name.len);
+ buffer += token->val.name.len;
+ *buffer++ = c;
+ }
+ break;
+
+ case SPELL_CHAR:
+ *buffer++ = token->aux;
+ break;
+
+ case SPELL_NONE:
+ cpp_ice (pfile, "Unspellable token");
+ break;
+ }
+
+ return buffer;
+}
+
+/* Temporary function for illustrative purposes. */
+void
+_cpp_lex_file (pfile)
+ cpp_reader* pfile;
+{
+ cpp_toklist* list;
+
+ init_trigraph_map ();
+ list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
+ _cpp_init_toklist (list, DUMMY_TOKEN);
+
+ for (;;)
+ {
+ _cpp_lex_line (pfile, list);
+ if (list->tokens[0].type == CPP_EOF)
+ break;
+
+#if 0
+ if (list->dirno)
+ _cpp_handle_directive (pfile, list);
+ else
+#endif
+ _cpp_output_list (pfile, list);
+ _cpp_clear_toklist (list);
+ }
+}
+
+/* Temporary function for illustrative purposes. */
+static void
+_cpp_output_list (pfile, list)
+ cpp_reader *pfile;
+ cpp_toklist *list;
+{
+ unsigned int i;
+
+ for (i = 0; i < list->tokens_used; i++)
+ {
+ CPP_RESERVE (pfile, TOKEN_LEN (&list->tokens[i]));
+ pfile->limit = spell_token (pfile, &list->tokens[i], pfile->limit, 1);
+ }
+}
+
+#endif