1 /* CPP Library - lexical analysis.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Broken out to separate file, Zack Weinberg, Mar 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \
29 ? CPP_BUFFER (pfile)->cur[N] : EOF)
30 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
31 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
32 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 static void skip_block_comment PARAMS ((cpp_reader *));
35 static void skip_line_comment PARAMS ((cpp_reader *));
36 static int maybe_macroexpand PARAMS ((cpp_reader *, long));
37 static int skip_comment PARAMS ((cpp_reader *, int));
38 static int copy_comment PARAMS ((cpp_reader *, int));
39 static void skip_string PARAMS ((cpp_reader *, int));
40 static void parse_string PARAMS ((cpp_reader *, int));
41 static U_CHAR *find_position PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
42 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
44 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
47 _cpp_grow_token_buffer (pfile, n)
51 long old_written = CPP_WRITTEN (pfile);
52 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
53 pfile->token_buffer = (U_CHAR *)
54 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
55 CPP_SET_WRITTEN (pfile, old_written);
59 null_cleanup (pbuf, pfile)
60 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
61 cpp_reader *pfile ATTRIBUTE_UNUSED;
66 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
67 If BUFFER != NULL, then use the LENGTH characters in BUFFER
68 as the new input buffer.
69 Return the new buffer, or NULL on failure. */
72 cpp_push_buffer (pfile, buffer, length)
77 cpp_buffer *buf = CPP_BUFFER (pfile);
79 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
81 cpp_fatal (pfile, "macro or `#include' recursion too deep");
85 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
87 new->if_stack = pfile->if_stack;
88 new->cleanup = null_cleanup;
89 new->buf = new->cur = buffer;
90 new->alimit = new->rlimit = buffer + length;
93 new->line_base = NULL;
95 CPP_BUFFER (pfile) = new;
100 cpp_pop_buffer (pfile)
103 cpp_buffer *buf = CPP_BUFFER (pfile);
104 if (ACTIVE_MARK_P (pfile))
105 cpp_ice (pfile, "mark active in cpp_pop_buffer");
106 (*buf->cleanup) (buf, pfile);
107 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
109 pfile->buffer_stack_depth--;
110 return CPP_BUFFER (pfile);
113 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
114 Pop the buffer when done. */
117 cpp_scan_buffer (pfile)
120 cpp_buffer *buffer = CPP_BUFFER (pfile);
121 enum cpp_token token;
122 if (CPP_OPTION (pfile, no_output))
124 long old_written = CPP_WRITTEN (pfile);
125 /* In no-output mode, we can ignore everything but directives. */
128 if (! pfile->only_seen_white)
129 _cpp_skip_rest_of_line (pfile);
130 token = cpp_get_token (pfile);
131 if (token == CPP_EOF) /* Should not happen ... */
133 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
135 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
136 cpp_pop_buffer (pfile);
140 CPP_SET_WRITTEN (pfile, old_written);
146 token = cpp_get_token (pfile);
147 if (token == CPP_EOF) /* Should not happen ... */
149 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
151 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
152 cpp_pop_buffer (pfile);
160 * Rescan a string (which may have escape marks) into pfile's buffer.
161 * Place the result in pfile->token_buffer.
163 * The input is copied before it is scanned, so it is safe to pass
164 * it something from the token_buffer that will get overwritten
165 * (because it follows CPP_WRITTEN). This is used by do_include.
169 cpp_expand_to_buffer (pfile, buf, length)
174 register cpp_buffer *ip;
180 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
184 /* Set up the input on the input stack. */
186 buf1 = (U_CHAR *) alloca (length + 1);
187 memcpy (buf1, buf, length);
190 ip = cpp_push_buffer (pfile, buf1, length);
195 /* Scan the input, create the output. */
196 save_no_output = CPP_OPTION (pfile, no_output);
197 CPP_OPTION (pfile, no_output) = 0;
198 CPP_OPTION (pfile, no_line_commands)++;
199 cpp_scan_buffer (pfile);
200 CPP_OPTION (pfile, no_line_commands)--;
201 CPP_OPTION (pfile, no_output) = save_no_output;
203 CPP_NUL_TERMINATE (pfile);
207 cpp_buf_line_and_col (pbuf, linep, colp)
208 register cpp_buffer *pbuf;
213 *linep = pbuf->lineno;
215 *colp = pbuf->cur - pbuf->line_base;
225 /* Return the topmost cpp_buffer that corresponds to a file (not a macro). */
228 cpp_file_buffer (pfile)
233 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
234 if (ip->ihash != NULL)
239 /* Skip a C-style block comment. We know it's a comment, and point is
240 at the second character of the starter. */
242 skip_block_comment (pfile)
249 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
255 cpp_error_with_line (pfile, line, col, "unterminated comment");
258 else if (c == '\n' || c == '\r')
260 /* \r cannot be a macro escape marker here. */
261 if (!ACTIVE_MARK_P (pfile))
262 CPP_BUMP_LINE (pfile);
264 else if (c == '/' && prev_c == '*')
266 else if (c == '*' && prev_c == '/'
267 && CPP_OPTION (pfile, warn_comments))
268 cpp_warning (pfile, "`/*' within comment");
274 /* Skip a C++/Chill line comment. We know it's a comment, and point
275 is at the second character of the initiator. */
277 skip_line_comment (pfile)
285 /* We don't have to worry about EOF in here. */
288 /* Don't consider final '\n' to be part of comment. */
294 /* \r cannot be a macro escape marker here. */
295 if (!ACTIVE_MARK_P (pfile))
296 CPP_BUMP_LINE (pfile);
297 if (CPP_OPTION (pfile, warn_comments))
298 cpp_warning (pfile, "backslash-newline within line comment");
303 /* Skip a comment - C, C++, or Chill style. M is the first character
304 of the comment marker. If this really is a comment, skip to its
305 end and return ' '. If this is not a comment, return M (which will
309 skip_comment (pfile, m)
313 if (m == '/' && PEEKC() == '*')
315 skip_block_comment (pfile);
318 else if (m == '/' && PEEKC() == '/')
320 if (CPP_BUFFER (pfile)->system_header_p)
322 /* We silently allow C++ comments in system headers, irrespective
323 of conformance mode, because lots of busted systems do that
324 and trying to clean it up in fixincludes is a nightmare. */
325 skip_line_comment (pfile);
328 else if (CPP_OPTION (pfile, cplusplus_comments))
330 if (CPP_OPTION (pfile, c89)
331 && CPP_PEDANTIC (pfile)
332 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
335 "C++ style comments are not allowed in ISO C89");
337 "(this will be reported only once per input file)");
338 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
340 skip_line_comment (pfile);
346 else if (m == '-' && PEEKC() == '-'
347 && CPP_OPTION (pfile, chill))
349 skip_line_comment (pfile);
356 /* Identical to skip_comment except that it copies the comment into the
357 token_buffer. This is used if !discard_comments. */
359 copy_comment (pfile, m)
363 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
366 if (skip_comment (pfile, m) == m)
369 limit = CPP_BUFFER (pfile)->cur;
370 CPP_RESERVE (pfile, limit - start + 2);
371 CPP_PUTC_Q (pfile, m);
372 for (; start <= limit; start++)
374 CPP_PUTC_Q (pfile, *start);
379 /* Skip whitespace \-newline and comments. Does not macro-expand. */
382 _cpp_skip_hspace (pfile)
391 else if (is_hspace(c))
393 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
394 cpp_pedwarn (pfile, "%s in preprocessing directive",
395 c == '\f' ? "formfeed" : "vertical tab");
399 /* \r is a backslash-newline marker if !has_escapes, and
400 a deletable-whitespace or no-reexpansion marker otherwise. */
401 if (CPP_BUFFER (pfile)->has_escapes)
409 CPP_BUMP_LINE (pfile);
411 else if (c == '/' || c == '-')
413 c = skip_comment (pfile, c);
423 /* Read and discard the rest of the current line. */
426 _cpp_skip_rest_of_line (pfile)
440 if (! CPP_BUFFER (pfile)->has_escapes)
441 CPP_BUMP_LINE (pfile);
446 skip_string (pfile, c);
451 skip_comment (pfile, c);
456 if (CPP_PEDANTIC (pfile))
457 cpp_pedwarn (pfile, "%s in preprocessing directive",
458 c == '\f' ? "formfeed" : "vertical tab");
465 /* Parse an identifier starting with C. */
468 _cpp_parse_name (pfile, c)
480 if (c == '$' && CPP_PEDANTIC (pfile))
481 cpp_pedwarn (pfile, "`$' in identifier");
483 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
484 CPP_PUTC_Q (pfile, c);
489 CPP_NUL_TERMINATE_Q (pfile);
493 /* Parse and skip over a string starting with C. A single quoted
494 string is treated like a double -- some programs (e.g., troff) are
495 perverse this way. (However, a single quoted string is not allowed
496 to extend over multiple lines.) */
498 skip_string (pfile, c)
502 long start_line, start_column;
503 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
511 cpp_error_with_line (pfile, start_line, start_column,
512 "unterminated string or character constant");
513 if (pfile->multiline_string_line != start_line
514 && pfile->multiline_string_line != 0)
515 cpp_error_with_line (pfile,
516 pfile->multiline_string_line, -1,
517 "possible real start of unterminated constant");
518 pfile->multiline_string_line = 0;
522 CPP_BUMP_LINE (pfile);
523 /* In Fortran and assembly language, silently terminate
524 strings of either variety at end of line. This is a
525 kludge around not knowing where comments are in these
527 if (CPP_OPTION (pfile, lang_fortran)
528 || CPP_OPTION (pfile, lang_asm))
533 /* Character constants may not extend over multiple lines.
534 In Standard C, neither may strings. We accept multiline
535 strings as an extension. */
538 cpp_error_with_line (pfile, start_line, start_column,
539 "unterminated character constant");
543 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
544 cpp_pedwarn_with_line (pfile, start_line, start_column,
545 "string constant runs past end of line");
546 if (pfile->multiline_string_line == 0)
547 pfile->multiline_string_line = start_line;
551 if (CPP_BUFFER (pfile)->has_escapes)
553 cpp_ice (pfile, "\\r escape inside string constant");
557 /* Backslash newline is replaced by nothing at all. */
558 CPP_BUMP_LINE (pfile);
574 /* Parse a string and copy it to the output. */
577 parse_string (pfile, c)
581 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
584 skip_string (pfile, c);
586 limit = CPP_BUFFER (pfile)->cur;
587 CPP_RESERVE (pfile, limit - start + 2);
588 CPP_PUTC_Q (pfile, c);
589 for (; start < limit; start++)
591 CPP_PUTC_Q (pfile, *start);
594 /* Read an assertion into the token buffer, converting to
595 canonical form: `#predicate(a n swe r)' The next non-whitespace
596 character to read should be the first letter of the predicate.
597 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
598 with answer (see callers for why). In case of 0, an error has been
601 _cpp_parse_assertion (pfile)
605 _cpp_skip_hspace (pfile);
609 cpp_error (pfile, "assertion predicate is not an identifier");
612 CPP_PUTC(pfile, '#');
614 _cpp_parse_name (pfile, c);
619 if (is_hspace(c) || c == '\r')
620 _cpp_skip_hspace (pfile);
626 CPP_PUTC(pfile, '(');
629 while ((c = GETC()) != ')')
635 CPP_PUTC(pfile, ' ');
639 else if (c == '\n' || c == EOF)
641 if (c == '\n') FORWARD(-1);
642 cpp_error (pfile, "un-terminated assertion answer");
646 /* \r cannot be a macro escape here. */
647 CPP_BUMP_LINE (pfile);
655 if (pfile->limit[-1] == ' ')
656 pfile->limit[-1] = ')';
657 else if (pfile->limit[-1] == '(')
659 cpp_error (pfile, "empty token sequence in assertion");
663 CPP_PUTC (pfile, ')');
665 CPP_NUL_TERMINATE (pfile);
669 /* Get the next token, and add it to the text in pfile->token_buffer.
670 Return the kind of token we got. */
673 _cpp_lex_token (pfile)
676 register int c, c2, c3;
677 enum cpp_token token;
691 if (CPP_OPTION (pfile, discard_comments))
692 c = skip_comment (pfile, c);
694 c = copy_comment (pfile, c);
698 /* Comments are equivalent to spaces.
699 For -traditional, a comment is equivalent to nothing. */
700 if (CPP_TRADITIONAL (pfile) || !CPP_OPTION (pfile, discard_comments))
709 if (pfile->parsing_if_directive)
711 _cpp_skip_hspace (pfile);
712 _cpp_parse_assertion (pfile);
713 return CPP_ASSERTION;
716 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
718 CPP_RESERVE (pfile, 3);
719 CPP_PUTC_Q (pfile, '#');
720 CPP_NUL_TERMINATE_Q (pfile);
722 return CPP_STRINGIZE;
725 CPP_PUTC_Q (pfile, '#');
726 CPP_NUL_TERMINATE_Q (pfile);
730 if (!pfile->only_seen_white)
732 return CPP_DIRECTIVE;
736 parse_string (pfile, c);
737 pfile->only_seen_white = 0;
738 return c == '\'' ? CPP_CHAR : CPP_STRING;
741 if (!CPP_OPTION (pfile, dollars_in_ident))
746 if (CPP_OPTION (pfile, cplusplus) && PEEKC () == ':')
754 if (c2 == c || c2 == '=')
771 if (CPP_OPTION (pfile, chill))
772 goto comment; /* Chill style comment */
780 if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
782 /* In C++, there's a ->* operator. */
784 pfile->only_seen_white = 0;
785 CPP_RESERVE (pfile, 4);
786 CPP_PUTC_Q (pfile, c);
787 CPP_PUTC_Q (pfile, GETC ());
788 CPP_PUTC_Q (pfile, GETC ());
789 CPP_NUL_TERMINATE_Q (pfile);
797 if (pfile->parsing_include_directive)
805 if (c == '\n' || c == EOF)
808 "missing '>' in `#include <FILENAME>'");
813 if (!CPP_BUFFER (pfile)->has_escapes)
815 /* Backslash newline is replaced by nothing. */
816 CPP_ADJUST_WRITTEN (pfile, -1);
817 CPP_BUMP_LINE (pfile);
821 /* We might conceivably get \r- or \r<space> in
822 here. Just delete 'em. */
824 if (d != '-' && d != ' ')
825 cpp_ice (pfile, "unrecognized escape \\r%c", d);
826 CPP_ADJUST_WRITTEN (pfile, -1);
832 /* else fall through */
837 /* GNU C++ supports MIN and MAX operators <? and >?. */
838 if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
841 CPP_RESERVE (pfile, 4);
843 CPP_PUTC (pfile, c2);
846 CPP_PUTC_Q (pfile, GETC ());
847 CPP_NUL_TERMINATE_Q (pfile);
848 pfile->only_seen_white = 0;
855 CPP_RESERVE(pfile, 2);
856 CPP_PUTC_Q (pfile, '.');
861 /* In C++ there's a .* operator. */
862 if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
865 if (c2 == '.' && PEEKN(1) == '.')
867 CPP_RESERVE(pfile, 4);
868 CPP_PUTC_Q (pfile, '.');
869 CPP_PUTC_Q (pfile, '.');
870 CPP_PUTC_Q (pfile, '.');
872 CPP_NUL_TERMINATE_Q (pfile);
873 pfile->only_seen_white = 0;
880 pfile->only_seen_white = 0;
881 CPP_RESERVE(pfile, 3);
882 CPP_PUTC_Q (pfile, c);
883 CPP_PUTC_Q (pfile, GETC ());
884 CPP_NUL_TERMINATE_Q (pfile);
889 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
893 parse_string (pfile, c);
894 pfile->only_seen_white = 0;
895 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
899 case '0': case '1': case '2': case '3': case '4':
900 case '5': case '6': case '7': case '8': case '9':
905 CPP_RESERVE (pfile, 2);
906 CPP_PUTC_Q (pfile, c);
910 if (!is_numchar(c) && c != '.'
911 && ((c2 != 'e' && c2 != 'E'
912 && ((c2 != 'p' && c2 != 'P')
913 || CPP_OPTION (pfile, c89)))
914 || (c != '+' && c != '-')))
919 CPP_NUL_TERMINATE_Q (pfile);
920 pfile->only_seen_white = 0;
922 case 'b': case 'c': case 'd': case 'h': case 'o':
923 case 'B': case 'C': case 'D': case 'H': case 'O':
924 if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
926 pfile->only_seen_white = 0;
927 CPP_RESERVE (pfile, 2);
928 CPP_PUTC_Q (pfile, c);
929 CPP_PUTC_Q (pfile, '\'');
935 goto chill_number_eof;
942 CPP_RESERVE (pfile, 2);
943 CPP_PUTC_Q (pfile, c);
944 CPP_NUL_TERMINATE_Q (pfile);
951 CPP_NUL_TERMINATE (pfile);
958 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
959 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
960 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
961 case 'x': case 'y': case 'z':
962 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
963 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
964 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
967 pfile->only_seen_white = 0;
968 _cpp_parse_name (pfile, c);
971 case ' ': case '\t': case '\v':
976 if (c == EOF || !is_hspace(c))
983 if (CPP_BUFFER (pfile)->has_escapes)
988 if (pfile->output_escapes)
989 CPP_PUTS (pfile, "\r-", 2);
990 _cpp_parse_name (pfile, GETC ());
995 CPP_RESERVE (pfile, 2);
996 if (pfile->output_escapes)
997 CPP_PUTC_Q (pfile, '\r');
998 CPP_PUTC_Q (pfile, c);
1003 cpp_ice (pfile, "unrecognized escape \\r%c", c);
1009 /* Backslash newline is ignored. */
1010 CPP_BUMP_LINE (pfile);
1015 CPP_PUTC (pfile, c);
1016 if (pfile->only_seen_white == 0)
1017 pfile->only_seen_white = 1;
1018 CPP_BUMP_LINE (pfile);
1019 if (! CPP_OPTION (pfile, no_line_commands))
1022 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
1023 _cpp_output_line_command (pfile, same_file);
1027 case '(': token = CPP_LPAREN; goto char1;
1028 case ')': token = CPP_RPAREN; goto char1;
1029 case '{': token = CPP_LBRACE; goto char1;
1030 case '}': token = CPP_RBRACE; goto char1;
1031 case ',': token = CPP_COMMA; goto char1;
1032 case ';': token = CPP_SEMICOLON; goto char1;
1038 pfile->only_seen_white = 0;
1039 CPP_PUTC (pfile, c);
1044 /* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
1045 Caller is expected to have checked no_macro_expand. */
1047 maybe_macroexpand (pfile, written)
1051 U_CHAR *macro = pfile->token_buffer + written;
1052 size_t len = CPP_WRITTEN (pfile) - written;
1053 HASHNODE *hp = _cpp_lookup (pfile, macro, len);
1057 if (hp->type == T_DISABLED)
1059 if (pfile->output_escapes)
1061 /* Insert a no-reexpand marker before IDENT. */
1062 CPP_RESERVE (pfile, 2);
1063 CPP_ADJUST_WRITTEN (pfile, 2);
1064 macro = pfile->token_buffer + written;
1066 memmove (macro + 2, macro, len);
1073 /* If macro wants an arglist, verify that a '(' follows. */
1074 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1076 int macbuf_whitespace = 0;
1079 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1081 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1084 _cpp_skip_hspace (pfile);
1091 if (point != CPP_BUFFER (pfile)->cur)
1092 macbuf_whitespace = 1;
1096 goto not_macro_call;
1097 cpp_pop_buffer (pfile);
1100 CPP_SET_MARK (pfile);
1103 _cpp_skip_hspace (pfile);
1110 CPP_GOTO_MARK (pfile);
1115 if (macbuf_whitespace)
1116 CPP_PUTC (pfile, ' ');
1122 /* This is now known to be a macro call.
1123 Expand the macro, reading arguments as needed,
1124 and push the expansion on the input stack. */
1125 _cpp_macroexpand (pfile, hp);
1126 CPP_SET_WRITTEN (pfile, written);
1131 cpp_get_token (pfile)
1134 enum cpp_token token;
1135 long written = CPP_WRITTEN (pfile);
1138 token = _cpp_lex_token (pfile);
1146 if (_cpp_handle_directive (pfile))
1147 return CPP_DIRECTIVE;
1148 pfile->only_seen_white = 0;
1149 CPP_PUTC (pfile, '#');
1153 if (! pfile->no_macro_expand
1154 && maybe_macroexpand (pfile, written))
1159 if (CPP_BUFFER (pfile)->manual_pop)
1160 /* If we've been reading from redirected input, the
1161 frontend will pop the buffer. */
1163 else if (CPP_BUFFER (pfile)->seen_eof)
1165 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
1168 cpp_pop_buffer (pfile);
1173 _cpp_handle_eof (pfile);
1179 /* Like cpp_get_token, but skip spaces and comments. */
1182 cpp_get_non_space_token (pfile)
1185 int old_written = CPP_WRITTEN (pfile);
1188 enum cpp_token token = cpp_get_token (pfile);
1189 if (token != CPP_COMMENT && token != CPP_POP
1190 && token != CPP_HSPACE && token != CPP_VSPACE)
1192 CPP_SET_WRITTEN (pfile, old_written);
1196 /* Like cpp_get_token, except that it does not read past end-of-line.
1197 Also, horizontal space is skipped, and macros are popped. */
1200 _cpp_get_directive_token (pfile)
1203 long old_written = CPP_WRITTEN (pfile);
1204 enum cpp_token token;
1208 _cpp_skip_hspace (pfile);
1209 if (PEEKC () == '\n')
1212 token = cpp_get_token (pfile);
1213 /* token could be hspace at the beginning of a macro. */
1214 if (token == CPP_HSPACE || token == CPP_COMMENT)
1216 CPP_SET_WRITTEN (pfile, old_written);
1220 /* token cannot be vspace, it would have been caught above. */
1221 if (token == CPP_VSPACE)
1223 cpp_ice (pfile, "VSPACE in get_directive_token");
1227 /* token cannot be POP unless the buffer is a macro buffer. */
1228 if (token != CPP_POP)
1231 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1233 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1237 /* We must pop the buffer by hand, or else cpp_get_token might
1238 hand us white space or newline on the next invocation. */
1239 cpp_pop_buffer (pfile);
1243 /* Determine the current line and column. Used only by read_and_prescan. */
1245 find_position (start, limit, linep)
1248 unsigned long *linep;
1250 unsigned long line = *linep;
1251 U_CHAR *lbase = start;
1252 while (start < limit)
1254 U_CHAR ch = *start++;
1255 if (ch == '\n' || ch == '\r')
1265 /* These are tables used by _cpp_read_and_prescan. If we have
1266 designated initializers, they can be constant data; otherwise, they
1267 are set up at runtime by _cpp_init_input_buffer. */
1270 #define UCHAR_MAX 255 /* assume 8-bit bytes */
1273 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
1274 #define CHARTAB(name) static const unsigned char name[UCHAR_MAX + 1]
1275 #define init_speccase() /* nothing */
1276 #define init_trigraph_map() /* nothing */
1277 #define SPECCASE CHARTAB(speccase) = {
1278 #define TRIGRAPH_MAP CHARTAB(trigraph_map) = {
1280 #define s(p, v) [p] = v,
1282 #define CHARTAB(name) static unsigned char name[UCHAR_MAX + 1]
1283 #define SPECCASE CHARTAB(speccase) = { 0 }; \
1284 static void init_speccase PARAMS ((void)) { \
1285 unsigned char *x = speccase;
1286 #define TRIGRAPH_MAP CHARTAB(trigraph_map) = { 0 }; \
1287 static void init_trigraph_map PARAMS ((void)) { \
1288 unsigned char *x = trigraph_map;
1290 #define s(p, v) x[p] = v;
1293 /* Table of characters that can't be handled in the inner loop.
1294 Keep these contiguous to optimize the performance of the code generated
1295 for the switch that uses them. */
1296 #define SPECCASE_EMPTY 0
1297 #define SPECCASE_CR 1
1298 #define SPECCASE_BACKSLASH 2
1299 #define SPECCASE_QUESTION 3
1302 s('\r', SPECCASE_CR)
1303 s('\\', SPECCASE_BACKSLASH)
1304 s('?', SPECCASE_QUESTION)
1307 /* Map of trigraph third characters to their replacements. */
1310 s('=', '#') s(')', ']') s('!', '|')
1311 s('(', '[') s('\'', '^') s('>', '}')
1312 s('/', '\\') s('<', '{') s('-', '~')
1321 /* Read the entire contents of file DESC into buffer BUF. LEN is how
1322 much memory to allocate initially; more will be allocated if
1323 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1324 canonical form (\n). If enabled, convert and/or warn about
1325 trigraphs. Convert backslash-newline to a one-character escape
1326 (\r) and remove it from "embarrassing" places (i.e. the middle of a
1327 token). If there is no newline at the end of the file, add one and
1328 warn. Returns -1 on failure, or the actual length of the data to
1331 This function does a lot of work, and can be a serious performance
1332 bottleneck. It has been tuned heavily; make sure you understand it
1333 before hacking. The common case - no trigraphs, Unix style line
1334 breaks, backslash-newline set off by whitespace, newline at EOF -
1335 has been optimized at the expense of the others. The performance
1336 penalty for DOS style line breaks (\r\n) is about 15%.
1338 Warnings lose particularly heavily since we have to determine the
1339 line number, which involves scanning from the beginning of the file
1340 or from the last warning. The penalty for the absence of a newline
1341 at the end of reload1.c is about 60%. (reload1.c is 329k.)
1343 If your file has more than one kind of end-of-line marker, you
1344 will get messed-up line numbering.
1346 So that the cases of the switch statement do not have to concern
1347 themselves with the complications of reading beyond the end of the
1348 buffer, the buffer is guaranteed to have at least 3 characters in
1349 it (or however many are left in the file, if less) on entry to the
1350 switch. This is enough to handle trigraphs and the "\\\n\r" and
1353 The end of the buffer is marked by a '\\', which, being a special
1354 character, guarantees we will exit the fast-scan loops and perform
1358 _cpp_read_and_prescan (pfile, fp, desc, len)
1364 U_CHAR *buf = (U_CHAR *) xmalloc (len);
1365 U_CHAR *ip, *op, *line_base;
1368 unsigned int deferred_newlines;
1373 deferred_newlines = 0;
1377 ibase = pfile->input_buffer + 3;
1379 ip[-1] = '\0'; /* Guarantee no match with \n for SPECCASE_CR */
1383 U_CHAR *near_buff_end;
1385 /* Copy previous char plus unprocessed (at most 2) chars
1386 to beginning of buffer, refill it with another
1387 read(), and continue processing */
1388 memcpy(ip - count - 1, ip - 1, 3);
1391 count = read (desc, ibase, pfile->input_buffer_len);
1395 ibase[count] = '\\'; /* Marks end of buffer */
1398 near_buff_end = pfile->input_buffer + count;
1403 size_t delta_line_base;
1407 This could happen if the file is larger than half the
1408 maximum address space of the machine. */
1411 delta_op = op - buf;
1412 delta_line_base = line_base - buf;
1413 buf = (U_CHAR *) xrealloc (buf, len);
1414 op = buf + delta_op;
1415 line_base = buf + delta_line_base;
1422 /* Allow normal processing of the (at most 2) remaining
1423 characters. The end-of-buffer marker is still present
1424 and prevents false matches within the switch. */
1425 near_buff_end = ibase - 1;
1432 /* Deal with \-newline, potentially in the middle of a token. */
1433 if (deferred_newlines)
1435 if (op != buf && op[-1] != ' ' && op[-1] != '\n' && op[-1] != '\t' && op[-1] != '\r')
1437 /* Previous was not white space. Skip to white
1438 space, if we can, before outputting the \r's */
1440 while (ip[span] != ' '
1443 && speccase[ip[span]] == SPECCASE_EMPTY)
1445 memcpy (op, ip, span);
1448 if (speccase[ip[0]] != SPECCASE_EMPTY)
1451 while (deferred_newlines)
1452 deferred_newlines--, *op++ = '\r';
1455 /* Copy as much as we can without special treatment. */
1457 while (speccase[ip[span]] == SPECCASE_EMPTY) span++;
1458 memcpy (op, ip, span);
1463 if (ip > near_buff_end) /* Do we have enough chars? */
1465 switch (speccase[*ip++])
1467 case SPECCASE_CR: /* \r */
1476 case SPECCASE_BACKSLASH: /* \ */
1479 deferred_newlines++;
1481 if (*ip == '\r') ip++;
1483 else if (*ip == '\r')
1485 deferred_newlines++;
1487 if (*ip == '\n') ip++;
1493 case SPECCASE_QUESTION: /* ? */
1497 *op++ = '?'; /* Normal non-trigraph case */
1502 t = trigraph_map[d];
1506 if (CPP_OPTION (pfile, warn_trigraphs))
1509 line_base = find_position (line_base, op, &line);
1510 col = op - line_base + 1;
1511 if (CPP_OPTION (pfile, trigraphs))
1512 cpp_warning_with_line (pfile, line, col,
1513 "trigraph ??%c converted to %c", d, t);
1515 cpp_warning_with_line (pfile, line, col,
1516 "trigraph ??%c ignored", d);
1520 if (CPP_OPTION (pfile, trigraphs))
1522 op[-1] = t; /* Overwrite '?' */
1527 goto do_speccase; /* May need buffer refill */
1547 line_base = find_position (line_base, op, &line);
1548 col = op - line_base + 1;
1549 cpp_warning_with_line (pfile, line, col, "no newline at end of file\n");
1550 if (offset + 1 > len)
1553 if (offset + 1 > len)
1555 buf = (U_CHAR *) xrealloc (buf, len);
1561 fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1565 cpp_error (pfile, "file is too large (>%lu bytes)\n", (unsigned long)offset);
1570 cpp_error_from_errno (pfile, fp->ihash->name);
1575 /* Allocate pfile->input_buffer, and initialize speccase[] and
1576 trigraph_map[] if it hasn't happened already. */
1579 _cpp_init_input_buffer (pfile)
1585 init_trigraph_map ();
1587 /* Determine the appropriate size for the input buffer. Normal C
1588 source files are smaller than eight K. */
1589 /* 8Kbytes of buffer proper, 1 to detect running off the end without
1590 address arithmetic all the time, and 3 for pushback during buffer
1591 refill, in case there's a potential trigraph or end-of-line
1592 digraph at the end of a block. */
1594 tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
1595 pfile->input_buffer = tmp;
1596 pfile->input_buffer_len = 8192;