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 PEEKBUF(BUFFER, N) \
29 ((BUFFER)->rlimit - (BUFFER)->cur > (N) ? (BUFFER)->cur[N] : EOF)
30 #define GETBUF(BUFFER) \
31 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
32 #define FORWARDBUF(BUFFER, N) ((BUFFER)->cur += (N))
34 #define PEEKN(N) PEEKBUF (CPP_BUFFER (pfile), N)
35 #define FORWARD(N) FORWARDBUF (CPP_BUFFER (pfile), (N))
36 #define GETC() GETBUF (CPP_BUFFER (pfile))
37 #define PEEKC() PEEKBUF (CPP_BUFFER (pfile), 0)
39 static void skip_block_comment PARAMS ((cpp_reader *));
40 static void skip_line_comment PARAMS ((cpp_reader *));
41 static int maybe_macroexpand PARAMS ((cpp_reader *, long));
42 static int skip_comment PARAMS ((cpp_reader *, int));
43 static int copy_comment PARAMS ((cpp_reader *, int));
44 static void skip_string PARAMS ((cpp_reader *, int));
45 static void parse_string PARAMS ((cpp_reader *, int));
46 static U_CHAR *find_position PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
47 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
49 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
52 _cpp_grow_token_buffer (pfile, n)
56 long old_written = CPP_WRITTEN (pfile);
57 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
58 pfile->token_buffer = (U_CHAR *)
59 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
60 CPP_SET_WRITTEN (pfile, old_written);
64 null_cleanup (pbuf, pfile)
65 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
66 cpp_reader *pfile ATTRIBUTE_UNUSED;
71 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
72 If BUFFER != NULL, then use the LENGTH characters in BUFFER
73 as the new input buffer.
74 Return the new buffer, or NULL on failure. */
77 cpp_push_buffer (pfile, buffer, length)
82 cpp_buffer *buf = CPP_BUFFER (pfile);
84 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
86 cpp_fatal (pfile, "macro or `#include' recursion too deep");
90 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
92 new->if_stack = pfile->if_stack;
93 new->cleanup = null_cleanup;
94 new->buf = new->cur = buffer;
95 new->rlimit = buffer + length;
98 new->line_base = NULL;
100 CPP_BUFFER (pfile) = new;
105 cpp_pop_buffer (pfile)
108 cpp_buffer *buf = CPP_BUFFER (pfile);
109 if (ACTIVE_MARK_P (pfile))
110 cpp_ice (pfile, "mark active in cpp_pop_buffer");
111 (*buf->cleanup) (buf, pfile);
112 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
114 pfile->buffer_stack_depth--;
115 return CPP_BUFFER (pfile);
118 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
119 Pop the buffer when done. */
122 cpp_scan_buffer (pfile)
125 cpp_buffer *buffer = CPP_BUFFER (pfile);
126 enum cpp_token token;
127 if (CPP_OPTION (pfile, no_output))
129 long old_written = CPP_WRITTEN (pfile);
130 /* In no-output mode, we can ignore everything but directives. */
133 if (! pfile->only_seen_white)
134 _cpp_skip_rest_of_line (pfile);
135 token = cpp_get_token (pfile);
136 if (token == CPP_EOF) /* Should not happen ... */
138 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
140 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
141 cpp_pop_buffer (pfile);
145 CPP_SET_WRITTEN (pfile, old_written);
151 token = cpp_get_token (pfile);
152 if (token == CPP_EOF) /* Should not happen ... */
154 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
156 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
157 cpp_pop_buffer (pfile);
165 * Rescan a string (which may have escape marks) into pfile's buffer.
166 * Place the result in pfile->token_buffer.
168 * The input is copied before it is scanned, so it is safe to pass
169 * it something from the token_buffer that will get overwritten
170 * (because it follows CPP_WRITTEN). This is used by do_include.
174 cpp_expand_to_buffer (pfile, buf, length)
179 register cpp_buffer *ip;
185 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
189 /* Set up the input on the input stack. */
191 buf1 = (U_CHAR *) alloca (length + 1);
192 memcpy (buf1, buf, length);
195 ip = cpp_push_buffer (pfile, buf1, length);
200 /* Scan the input, create the output. */
201 save_no_output = CPP_OPTION (pfile, no_output);
202 CPP_OPTION (pfile, no_output) = 0;
203 CPP_OPTION (pfile, no_line_commands)++;
204 cpp_scan_buffer (pfile);
205 CPP_OPTION (pfile, no_line_commands)--;
206 CPP_OPTION (pfile, no_output) = save_no_output;
208 CPP_NUL_TERMINATE (pfile);
212 cpp_buf_line_and_col (pbuf, linep, colp)
213 register cpp_buffer *pbuf;
218 *linep = pbuf->lineno;
220 *colp = pbuf->cur - pbuf->line_base;
230 /* Return the topmost cpp_buffer that corresponds to a file (not a macro). */
233 cpp_file_buffer (pfile)
238 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
239 if (ip->ihash != NULL)
244 /* Skip a C-style block comment. We know it's a comment, and point is
245 at the second character of the starter. */
247 skip_block_comment (pfile)
254 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
260 cpp_error_with_line (pfile, line, col, "unterminated comment");
263 else if (c == '\n' || c == '\r')
265 /* \r cannot be a macro escape marker here. */
266 if (!ACTIVE_MARK_P (pfile))
267 CPP_BUMP_LINE (pfile);
269 else if (c == '/' && prev_c == '*')
271 else if (c == '*' && prev_c == '/'
272 && CPP_OPTION (pfile, warn_comments))
273 cpp_warning (pfile, "`/*' within comment");
279 /* Skip a C++/Chill line comment. We know it's a comment, and point
280 is at the second character of the initiator. */
282 skip_line_comment (pfile)
290 /* We don't have to worry about EOF in here. */
293 /* Don't consider final '\n' to be part of comment. */
299 /* \r cannot be a macro escape marker here. */
300 if (!ACTIVE_MARK_P (pfile))
301 CPP_BUMP_LINE (pfile);
302 if (CPP_OPTION (pfile, warn_comments))
303 cpp_warning (pfile, "backslash-newline within line comment");
308 /* Skip a comment - C, C++, or Chill style. M is the first character
309 of the comment marker. If this really is a comment, skip to its
310 end and return ' '. If this is not a comment, return M (which will
314 skip_comment (pfile, m)
318 if (m == '/' && PEEKC() == '*')
320 skip_block_comment (pfile);
323 else if (m == '/' && PEEKC() == '/')
325 if (CPP_BUFFER (pfile)->system_header_p)
327 /* We silently allow C++ comments in system headers, irrespective
328 of conformance mode, because lots of busted systems do that
329 and trying to clean it up in fixincludes is a nightmare. */
330 skip_line_comment (pfile);
333 else if (CPP_OPTION (pfile, cplusplus_comments))
335 if (CPP_OPTION (pfile, c89)
336 && CPP_PEDANTIC (pfile)
337 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
340 "C++ style comments are not allowed in ISO C89");
342 "(this will be reported only once per input file)");
343 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
345 skip_line_comment (pfile);
351 else if (m == '-' && PEEKC() == '-'
352 && CPP_OPTION (pfile, chill))
354 skip_line_comment (pfile);
361 /* Identical to skip_comment except that it copies the comment into the
362 token_buffer. This is used if !discard_comments. */
364 copy_comment (pfile, m)
368 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
371 if (skip_comment (pfile, m) == m)
374 limit = CPP_BUFFER (pfile)->cur;
375 CPP_RESERVE (pfile, limit - start + 2);
376 CPP_PUTC_Q (pfile, m);
377 for (; start <= limit; start++)
379 CPP_PUTC_Q (pfile, *start);
384 /* Skip whitespace \-newline and comments. Does not macro-expand. */
387 _cpp_skip_hspace (pfile)
396 else if (is_hspace(c))
398 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
399 cpp_pedwarn (pfile, "%s in preprocessing directive",
400 c == '\f' ? "formfeed" : "vertical tab");
404 /* \r is a backslash-newline marker if !has_escapes, and
405 a deletable-whitespace or no-reexpansion marker otherwise. */
406 if (CPP_BUFFER (pfile)->has_escapes)
414 CPP_BUMP_LINE (pfile);
416 else if (c == '/' || c == '-')
418 c = skip_comment (pfile, c);
428 /* Read and discard the rest of the current line. */
431 _cpp_skip_rest_of_line (pfile)
445 if (! CPP_BUFFER (pfile)->has_escapes)
446 CPP_BUMP_LINE (pfile);
451 skip_string (pfile, c);
456 skip_comment (pfile, c);
461 if (CPP_PEDANTIC (pfile))
462 cpp_pedwarn (pfile, "%s in preprocessing directive",
463 c == '\f' ? "formfeed" : "vertical tab");
470 /* Parse an identifier starting with C. */
473 _cpp_parse_name (pfile, c)
485 if (c == '$' && CPP_PEDANTIC (pfile))
486 cpp_pedwarn (pfile, "`$' in identifier");
488 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
489 CPP_PUTC_Q (pfile, c);
494 CPP_NUL_TERMINATE_Q (pfile);
498 /* Parse and skip over a string starting with C. A single quoted
499 string is treated like a double -- some programs (e.g., troff) are
500 perverse this way. (However, a single quoted string is not allowed
501 to extend over multiple lines.) */
503 skip_string (pfile, c)
507 long start_line, start_column;
508 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
516 cpp_error_with_line (pfile, start_line, start_column,
517 "unterminated string or character constant");
518 if (pfile->multiline_string_line != start_line
519 && pfile->multiline_string_line != 0)
520 cpp_error_with_line (pfile,
521 pfile->multiline_string_line, -1,
522 "possible real start of unterminated constant");
523 pfile->multiline_string_line = 0;
527 CPP_BUMP_LINE (pfile);
528 /* In Fortran and assembly language, silently terminate
529 strings of either variety at end of line. This is a
530 kludge around not knowing where comments are in these
532 if (CPP_OPTION (pfile, lang_fortran)
533 || CPP_OPTION (pfile, lang_asm))
538 /* Character constants may not extend over multiple lines.
539 In Standard C, neither may strings. We accept multiline
540 strings as an extension. */
543 cpp_error_with_line (pfile, start_line, start_column,
544 "unterminated character constant");
548 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
549 cpp_pedwarn_with_line (pfile, start_line, start_column,
550 "string constant runs past end of line");
551 if (pfile->multiline_string_line == 0)
552 pfile->multiline_string_line = start_line;
556 if (CPP_BUFFER (pfile)->has_escapes)
558 cpp_ice (pfile, "\\r escape inside string constant");
562 /* Backslash newline is replaced by nothing at all. */
563 CPP_BUMP_LINE (pfile);
579 /* Parse a string and copy it to the output. */
582 parse_string (pfile, c)
586 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
589 skip_string (pfile, c);
591 limit = CPP_BUFFER (pfile)->cur;
592 CPP_RESERVE (pfile, limit - start + 2);
593 CPP_PUTC_Q (pfile, c);
594 for (; start < limit; start++)
596 CPP_PUTC_Q (pfile, *start);
599 /* Read an assertion into the token buffer, converting to
600 canonical form: `#predicate(a n swe r)' The next non-whitespace
601 character to read should be the first letter of the predicate.
602 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
603 with answer (see callers for why). In case of 0, an error has been
606 _cpp_parse_assertion (pfile)
610 _cpp_skip_hspace (pfile);
614 cpp_error (pfile, "assertion without predicate");
617 else if (! is_idstart(c))
619 cpp_error (pfile, "assertion predicate is not an identifier");
622 CPP_PUTC(pfile, '#');
624 _cpp_parse_name (pfile, c);
629 if (is_hspace(c) || c == '\r')
630 _cpp_skip_hspace (pfile);
636 CPP_PUTC(pfile, '(');
639 while ((c = GETC()) != ')')
645 CPP_PUTC(pfile, ' ');
649 else if (c == '\n' || c == EOF)
651 if (c == '\n') FORWARD(-1);
652 cpp_error (pfile, "un-terminated assertion answer");
656 /* \r cannot be a macro escape here. */
657 CPP_BUMP_LINE (pfile);
665 if (pfile->limit[-1] == ' ')
666 pfile->limit[-1] = ')';
667 else if (pfile->limit[-1] == '(')
669 cpp_error (pfile, "empty token sequence in assertion");
673 CPP_PUTC (pfile, ')');
678 /* Get the next token, and add it to the text in pfile->token_buffer.
679 Return the kind of token we got. */
682 _cpp_lex_token (pfile)
685 register int c, c2, c3;
686 enum cpp_token token;
700 if (CPP_OPTION (pfile, discard_comments))
701 c = skip_comment (pfile, c);
703 c = copy_comment (pfile, c);
707 /* Comments are equivalent to spaces.
708 For -traditional, a comment is equivalent to nothing. */
709 if (!CPP_OPTION (pfile, discard_comments))
711 else if (CPP_TRADITIONAL (pfile)
712 && ! is_space (PEEKC ()))
714 if (pfile->parsing_define_directive)
726 if (pfile->parsing_if_directive)
728 if (_cpp_parse_assertion (pfile))
729 return CPP_ASSERTION;
733 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
735 CPP_RESERVE (pfile, 3);
736 CPP_PUTC_Q (pfile, '#');
737 CPP_NUL_TERMINATE_Q (pfile);
739 return CPP_STRINGIZE;
742 CPP_PUTC_Q (pfile, '#');
743 CPP_NUL_TERMINATE_Q (pfile);
747 if (!pfile->only_seen_white)
749 return CPP_DIRECTIVE;
753 parse_string (pfile, c);
754 return c == '\'' ? CPP_CHAR : CPP_STRING;
757 if (!CPP_OPTION (pfile, dollars_in_ident))
762 if (CPP_OPTION (pfile, cplusplus) && PEEKC () == ':')
770 if (c2 == c || c2 == '=')
787 if (CPP_OPTION (pfile, chill))
788 goto comment; /* Chill style comment */
796 if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
798 /* In C++, there's a ->* operator. */
800 CPP_RESERVE (pfile, 4);
801 CPP_PUTC_Q (pfile, c);
802 CPP_PUTC_Q (pfile, GETC ());
803 CPP_PUTC_Q (pfile, GETC ());
804 CPP_NUL_TERMINATE_Q (pfile);
812 if (pfile->parsing_include_directive)
820 if (c == '\n' || c == EOF)
823 "missing '>' in `#include <FILENAME>'");
828 if (!CPP_BUFFER (pfile)->has_escapes)
830 /* Backslash newline is replaced by nothing. */
831 CPP_ADJUST_WRITTEN (pfile, -1);
832 CPP_BUMP_LINE (pfile);
836 /* We might conceivably get \r- or \r<space> in
837 here. Just delete 'em. */
839 if (d != '-' && d != ' ')
840 cpp_ice (pfile, "unrecognized escape \\r%c", d);
841 CPP_ADJUST_WRITTEN (pfile, -1);
847 /* else fall through */
852 /* GNU C++ supports MIN and MAX operators <? and >?. */
853 if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
856 CPP_RESERVE (pfile, 4);
858 CPP_PUTC (pfile, c2);
861 CPP_PUTC_Q (pfile, GETC ());
862 CPP_NUL_TERMINATE_Q (pfile);
869 CPP_RESERVE(pfile, 2);
870 CPP_PUTC_Q (pfile, '.');
875 /* In C++ there's a .* operator. */
876 if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
879 if (c2 == '.' && PEEKN(1) == '.')
881 CPP_RESERVE(pfile, 4);
882 CPP_PUTC_Q (pfile, '.');
883 CPP_PUTC_Q (pfile, '.');
884 CPP_PUTC_Q (pfile, '.');
886 CPP_NUL_TERMINATE_Q (pfile);
893 CPP_RESERVE(pfile, 3);
894 CPP_PUTC_Q (pfile, c);
895 CPP_PUTC_Q (pfile, GETC ());
896 CPP_NUL_TERMINATE_Q (pfile);
901 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
905 parse_string (pfile, c);
906 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
910 case '0': case '1': case '2': case '3': case '4':
911 case '5': case '6': case '7': case '8': case '9':
916 CPP_RESERVE (pfile, 2);
917 CPP_PUTC_Q (pfile, c);
921 if (!is_numchar(c) && c != '.'
922 && ((c2 != 'e' && c2 != 'E'
923 && ((c2 != 'p' && c2 != 'P')
924 || CPP_OPTION (pfile, c89)))
925 || (c != '+' && c != '-')))
930 CPP_NUL_TERMINATE_Q (pfile);
932 case 'b': case 'c': case 'd': case 'h': case 'o':
933 case 'B': case 'C': case 'D': case 'H': case 'O':
934 if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
936 CPP_RESERVE (pfile, 2);
937 CPP_PUTC_Q (pfile, c);
938 CPP_PUTC_Q (pfile, '\'');
944 goto chill_number_eof;
951 CPP_RESERVE (pfile, 2);
952 CPP_PUTC_Q (pfile, c);
953 CPP_NUL_TERMINATE_Q (pfile);
960 CPP_NUL_TERMINATE (pfile);
967 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
968 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
969 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
970 case 'x': case 'y': case 'z':
971 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
972 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
973 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
976 _cpp_parse_name (pfile, c);
979 case ' ': case '\t': case '\v': case '\f':
984 if (c == EOF || !is_hspace(c))
991 if (CPP_BUFFER (pfile)->has_escapes)
996 if (pfile->output_escapes)
997 CPP_PUTS (pfile, "\r-", 2);
998 _cpp_parse_name (pfile, GETC ());
1003 /* "\r " means a space, but only if necessary to prevent
1004 accidental token concatenation. */
1005 CPP_RESERVE (pfile, 2);
1006 if (pfile->output_escapes)
1007 CPP_PUTC_Q (pfile, '\r');
1008 CPP_PUTC_Q (pfile, c);
1013 cpp_ice (pfile, "unrecognized escape \\r%c", c);
1019 /* Backslash newline is ignored. */
1020 CPP_BUMP_LINE (pfile);
1025 CPP_PUTC (pfile, c);
1028 case '(': token = CPP_LPAREN; goto char1;
1029 case ')': token = CPP_RPAREN; goto char1;
1030 case '{': token = CPP_LBRACE; goto char1;
1031 case '}': token = CPP_RBRACE; goto char1;
1032 case ',': token = CPP_COMMA; goto char1;
1033 case ';': token = CPP_SEMICOLON; goto char1;
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);
1072 if (hp->type == T_EMPTY)
1074 /* Special case optimization: macro expands to nothing. */
1075 CPP_SET_WRITTEN (pfile, written);
1076 CPP_PUTC_Q (pfile, ' ');
1080 /* If macro wants an arglist, verify that a '(' follows. */
1081 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1083 int macbuf_whitespace = 0;
1086 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1088 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1091 _cpp_skip_hspace (pfile);
1098 if (point != CPP_BUFFER (pfile)->cur)
1099 macbuf_whitespace = 1;
1103 goto not_macro_call;
1104 cpp_pop_buffer (pfile);
1107 CPP_SET_MARK (pfile);
1110 _cpp_skip_hspace (pfile);
1117 CPP_GOTO_MARK (pfile);
1122 if (macbuf_whitespace)
1123 CPP_PUTC (pfile, ' ');
1129 /* This is now known to be a macro call.
1130 Expand the macro, reading arguments as needed,
1131 and push the expansion on the input stack. */
1132 _cpp_macroexpand (pfile, hp);
1133 CPP_SET_WRITTEN (pfile, written);
1138 cpp_get_token (pfile)
1141 enum cpp_token token;
1142 long written = CPP_WRITTEN (pfile);
1145 token = _cpp_lex_token (pfile);
1150 pfile->potential_control_macro = 0;
1151 pfile->only_seen_white = 0;
1155 if (pfile->only_seen_white == 0)
1156 pfile->only_seen_white = 1;
1157 CPP_BUMP_LINE (pfile);
1158 if (! CPP_OPTION (pfile, no_line_commands))
1161 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
1162 _cpp_output_line_command (pfile, same_file);
1171 pfile->potential_control_macro = 0;
1172 if (_cpp_handle_directive (pfile))
1173 return CPP_DIRECTIVE;
1174 pfile->only_seen_white = 0;
1175 CPP_PUTC (pfile, '#');
1179 pfile->potential_control_macro = 0;
1180 pfile->only_seen_white = 0;
1181 if (! pfile->no_macro_expand
1182 && maybe_macroexpand (pfile, written))
1187 if (CPP_BUFFER (pfile)->manual_pop)
1188 /* If we've been reading from redirected input, the
1189 frontend will pop the buffer. */
1191 else if (CPP_BUFFER (pfile)->seen_eof)
1193 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
1196 cpp_pop_buffer (pfile);
1201 _cpp_handle_eof (pfile);
1207 /* Like cpp_get_token, but skip spaces and comments. */
1210 cpp_get_non_space_token (pfile)
1213 int old_written = CPP_WRITTEN (pfile);
1216 enum cpp_token token = cpp_get_token (pfile);
1217 if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
1219 CPP_SET_WRITTEN (pfile, old_written);
1223 /* Like cpp_get_token, except that it does not execute directives,
1224 does not consume vertical space, and automatically pops off macro
1227 XXX This function will exist only till collect_expansion doesn't
1228 need to see whitespace anymore, then it'll be merged with
1229 _cpp_get_directive_token (below). */
1231 _cpp_get_define_token (pfile)
1235 enum cpp_token token;
1238 old_written = CPP_WRITTEN (pfile);
1239 token = _cpp_lex_token (pfile);
1246 /* Put it back and return VSPACE. */
1248 CPP_ADJUST_WRITTEN (pfile, -1);
1252 if (CPP_PEDANTIC (pfile))
1255 p = pfile->token_buffer + old_written;
1256 limit = CPP_PWRITTEN (pfile);
1259 if (*p == '\v' || *p == '\f')
1260 cpp_pedwarn (pfile, "%s in preprocessing directive",
1261 *p == '\f' ? "formfeed" : "vertical tab");
1268 /* Don't execute the directive, but don't smash it to OTHER either. */
1269 CPP_PUTC (pfile, '#');
1270 return CPP_DIRECTIVE;
1273 if (! pfile->no_macro_expand
1274 && maybe_macroexpand (pfile, old_written))
1279 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1281 cpp_pop_buffer (pfile);
1285 /* This can happen for files that don't end with a newline,
1286 and for cpp_define and friends. Pretend they do, so
1287 callers don't have to deal. A warning will be issued by
1288 someone else, if necessary. */
1293 /* Just like _cpp_get_define_token except that it discards horizontal
1297 _cpp_get_directive_token (pfile)
1300 int old_written = CPP_WRITTEN (pfile);
1303 enum cpp_token token = _cpp_get_define_token (pfile);
1304 if (token != CPP_COMMENT && token != CPP_HSPACE)
1306 CPP_SET_WRITTEN (pfile, old_written);
1310 /* Determine the current line and column. Used only by read_and_prescan. */
1312 find_position (start, limit, linep)
1315 unsigned long *linep;
1317 unsigned long line = *linep;
1318 U_CHAR *lbase = start;
1319 while (start < limit)
1321 U_CHAR ch = *start++;
1322 if (ch == '\n' || ch == '\r')
1332 /* The following table is used by _cpp_read_and_prescan. If we have
1333 designated initializers, it can be constant data; otherwise, it is
1334 set up at runtime by _cpp_init_input_buffer. */
1337 #define UCHAR_MAX 255 /* assume 8-bit bytes */
1340 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
1341 #define init_chartab() /* nothing */
1342 #define CHARTAB static const unsigned char chartab[UCHAR_MAX + 1] = {
1344 #define s(p, v) [p] = v,
1346 #define CHARTAB static unsigned char chartab[UCHAR_MAX + 1] = { 0 }; \
1347 static void init_chartab PARAMS ((void)) { \
1348 unsigned char *x = chartab;
1350 #define s(p, v) x[p] = v;
1353 /* Table of characters that can't be handled in the inner loop.
1354 Also contains the mapping between trigraph third characters and their
1356 #define SPECCASE_CR 1
1357 #define SPECCASE_BACKSLASH 2
1358 #define SPECCASE_QUESTION 3
1361 s('\r', SPECCASE_CR)
1362 s('\\', SPECCASE_BACKSLASH)
1363 s('?', SPECCASE_QUESTION)
1365 s('=', '#') s(')', ']') s('!', '|')
1366 s('(', '[') s('\'', '^') s('>', '}')
1367 s('/', '\\') s('<', '{') s('-', '~')
1374 #define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
1375 #define NONTRI(c) ((c) <= SPECCASE_QUESTION)
1377 /* Read the entire contents of file DESC into buffer BUF. LEN is how
1378 much memory to allocate initially; more will be allocated if
1379 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1380 canonical form (\n). If enabled, convert and/or warn about
1381 trigraphs. Convert backslash-newline to a one-character escape
1382 (\r) and remove it from "embarrassing" places (i.e. the middle of a
1383 token). If there is no newline at the end of the file, add one and
1384 warn. Returns -1 on failure, or the actual length of the data to
1387 This function does a lot of work, and can be a serious performance
1388 bottleneck. It has been tuned heavily; make sure you understand it
1389 before hacking. The common case - no trigraphs, Unix style line
1390 breaks, backslash-newline set off by whitespace, newline at EOF -
1391 has been optimized at the expense of the others. The performance
1392 penalty for DOS style line breaks (\r\n) is about 15%.
1394 Warnings lose particularly heavily since we have to determine the
1395 line number, which involves scanning from the beginning of the file
1396 or from the last warning. The penalty for the absence of a newline
1397 at the end of reload1.c is about 60%. (reload1.c is 329k.)
1399 If your file has more than one kind of end-of-line marker, you
1400 will get messed-up line numbering.
1402 So that the cases of the switch statement do not have to concern
1403 themselves with the complications of reading beyond the end of the
1404 buffer, the buffer is guaranteed to have at least 3 characters in
1405 it (or however many are left in the file, if less) on entry to the
1406 switch. This is enough to handle trigraphs and the "\\\n\r" and
1409 The end of the buffer is marked by a '\\', which, being a special
1410 character, guarantees we will exit the fast-scan loops and perform
1414 _cpp_read_and_prescan (pfile, fp, desc, len)
1420 U_CHAR *buf = (U_CHAR *) xmalloc (len);
1421 U_CHAR *ip, *op, *line_base;
1424 unsigned int deferred_newlines;
1429 deferred_newlines = 0;
1433 ibase = pfile->input_buffer + 3;
1435 ip[-1] = '\0'; /* Guarantee no match with \n for SPECCASE_CR */
1439 U_CHAR *near_buff_end;
1441 /* Copy previous char plus unprocessed (at most 2) chars
1442 to beginning of buffer, refill it with another
1443 read(), and continue processing */
1444 memcpy(ip - count - 1, ip - 1, 3);
1447 count = read (desc, ibase, pfile->input_buffer_len);
1451 ibase[count] = '\\'; /* Marks end of buffer */
1454 near_buff_end = pfile->input_buffer + count;
1459 size_t delta_line_base;
1463 This could happen if the file is larger than half the
1464 maximum address space of the machine. */
1467 delta_op = op - buf;
1468 delta_line_base = line_base - buf;
1469 buf = (U_CHAR *) xrealloc (buf, len);
1470 op = buf + delta_op;
1471 line_base = buf + delta_line_base;
1478 /* Allow normal processing of the (at most 2) remaining
1479 characters. The end-of-buffer marker is still present
1480 and prevents false matches within the switch. */
1481 near_buff_end = ibase - 1;
1488 /* Deal with \-newline, potentially in the middle of a token. */
1489 if (deferred_newlines)
1491 if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
1493 /* Previous was not white space. Skip to white
1494 space, if we can, before outputting the \r's */
1496 while (ip[span] != ' '
1499 && NORMAL(ip[span]))
1501 memcpy (op, ip, span);
1504 if (! NORMAL(ip[0]))
1507 while (deferred_newlines)
1508 deferred_newlines--, *op++ = '\r';
1511 /* Copy as much as we can without special treatment. */
1513 while (NORMAL (ip[span])) span++;
1514 memcpy (op, ip, span);
1519 if (ip > near_buff_end) /* Do we have enough chars? */
1521 switch (chartab[*ip++])
1523 case SPECCASE_CR: /* \r */
1532 case SPECCASE_BACKSLASH: /* \ */
1535 deferred_newlines++;
1537 if (*ip == '\r') ip++;
1539 else if (*ip == '\r')
1541 deferred_newlines++;
1543 if (*ip == '\n') ip++;
1549 case SPECCASE_QUESTION: /* ? */
1553 *op++ = '?'; /* Normal non-trigraph case */
1562 if (CPP_OPTION (pfile, warn_trigraphs))
1565 line_base = find_position (line_base, op, &line);
1566 col = op - line_base + 1;
1567 if (CPP_OPTION (pfile, trigraphs))
1568 cpp_warning_with_line (pfile, line, col,
1569 "trigraph ??%c converted to %c", d, t);
1571 cpp_warning_with_line (pfile, line, col,
1572 "trigraph ??%c ignored", d);
1576 if (CPP_OPTION (pfile, trigraphs))
1578 op[-1] = t; /* Overwrite '?' */
1583 goto do_speccase; /* May need buffer refill */
1603 line_base = find_position (line_base, op, &line);
1604 col = op - line_base + 1;
1605 cpp_warning_with_line (pfile, line, col, "no newline at end of file\n");
1606 if (offset + 1 > len)
1609 if (offset + 1 > len)
1611 buf = (U_CHAR *) xrealloc (buf, len);
1617 fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1621 cpp_error (pfile, "file is too large (>%lu bytes)\n", (unsigned long)offset);
1626 cpp_error_from_errno (pfile, fp->ihash->name);
1631 /* Allocate pfile->input_buffer, and initialize chartab[]
1632 if it hasn't happened already. */
1635 _cpp_init_input_buffer (pfile)
1642 /* Determine the appropriate size for the input buffer. Normal C
1643 source files are smaller than eight K. */
1644 /* 8Kbytes of buffer proper, 1 to detect running off the end without
1645 address arithmetic all the time, and 3 for pushback during buffer
1646 refill, in case there's a potential trigraph or end-of-line
1647 digraph at the end of a block. */
1649 tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
1650 pfile->input_buffer = tmp;
1651 pfile->input_buffer_len = 8192;