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 *));
48 static void null_warning PARAMS ((cpp_reader *, unsigned int));
50 static void safe_fwrite PARAMS ((cpp_reader *, const U_CHAR *,
52 static void output_line_command PARAMS ((cpp_reader *, cpp_printer *));
54 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
57 _cpp_grow_token_buffer (pfile, n)
61 long old_written = CPP_WRITTEN (pfile);
62 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
63 pfile->token_buffer = (U_CHAR *)
64 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
65 CPP_SET_WRITTEN (pfile, old_written);
69 null_cleanup (pbuf, pfile)
70 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
71 cpp_reader *pfile ATTRIBUTE_UNUSED;
76 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
77 If BUFFER != NULL, then use the LENGTH characters in BUFFER
78 as the new input buffer.
79 Return the new buffer, or NULL on failure. */
82 cpp_push_buffer (pfile, buffer, length)
87 cpp_buffer *buf = CPP_BUFFER (pfile);
89 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
91 cpp_fatal (pfile, "macro or `#include' recursion too deep");
95 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
97 new->if_stack = pfile->if_stack;
98 new->cleanup = null_cleanup;
99 new->buf = new->cur = buffer;
100 new->rlimit = buffer + length;
103 new->line_base = NULL;
105 CPP_BUFFER (pfile) = new;
110 cpp_pop_buffer (pfile)
113 cpp_buffer *buf = CPP_BUFFER (pfile);
114 if (ACTIVE_MARK_P (pfile))
115 cpp_ice (pfile, "mark active in cpp_pop_buffer");
116 (*buf->cleanup) (buf, pfile);
117 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
119 pfile->buffer_stack_depth--;
120 return CPP_BUFFER (pfile);
123 /* Deal with the annoying semantics of fwrite. */
125 safe_fwrite (pfile, buf, len, fp)
135 count = fwrite (buf, 1, len, fp);
144 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
147 /* Notify the compiler proper that the current line number has jumped,
148 or the current file name has changed. */
151 output_line_command (pfile, print)
157 enum { same = 0, enter, leave, rname } change;
158 static const char * const codes[] = { "", " 1", " 2", "" };
160 if (CPP_OPTION (pfile, no_line_commands))
163 ip = cpp_file_buffer (pfile);
166 line = CPP_BUF_LINE (ip);
168 /* Determine whether the current filename has changed, and if so,
169 how. 'nominal_fname' values are unique, so they can be compared
170 by comparing pointers. */
171 if (ip->nominal_fname == print->last_fname)
175 if (pfile->buffer_stack_depth == print->last_bsd)
179 if (pfile->buffer_stack_depth > print->last_bsd)
183 print->last_bsd = pfile->buffer_stack_depth;
185 print->last_fname = ip->nominal_fname;
187 /* If the current file has not changed, we can output a few newlines
188 instead if we want to increase the line number by a small amount.
189 We cannot do this if print->lineno is zero, because that means we
190 haven't output any line commands yet. (The very first line
191 command output is a `same_file' command.) */
192 if (change == same && print->lineno != 0
193 && line >= print->lineno && line < print->lineno + 8)
195 while (line > print->lineno)
197 putc ('\n', print->outf);
203 #ifndef NO_IMPLICIT_EXTERN_C
204 if (CPP_OPTION (pfile, cplusplus))
205 fprintf (print->outf, "# %u \"%s\"%s%s%s\n", line, ip->nominal_fname,
207 ip->system_header_p ? " 3" : "",
208 (ip->system_header_p == 2) ? " 4" : "");
211 fprintf (print->outf, "# %u \"%s\"%s%s\n", line, ip->nominal_fname,
213 ip->system_header_p ? " 3" : "");
214 print->lineno = line;
217 /* Write the contents of the token_buffer to the output stream, and
218 clear the token_buffer. Also handles generating line commands and
219 keeping track of file transitions. */
222 cpp_output_tokens (pfile, print)
226 if (CPP_WRITTEN (pfile) - print->written)
228 if (CPP_PWRITTEN (pfile)[-1] == '\n' && print->lineno)
230 safe_fwrite (pfile, pfile->token_buffer,
231 CPP_WRITTEN (pfile) - print->written, print->outf);
233 output_line_command (pfile, print);
234 CPP_SET_WRITTEN (pfile, print->written);
237 /* Scan a string (which may have escape marks), perform macro expansion,
238 and write the result to the token_buffer. */
241 _cpp_expand_to_buffer (pfile, buf, length)
247 enum cpp_ttype token;
252 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
256 /* Copy the buffer, because it might be in an unsafe place - for
257 example, a sequence on the token_buffer, where the pointers will
258 be invalidated if we enlarge the token_buffer. */
259 buf1 = alloca (length);
260 memcpy (buf1, buf, length);
262 /* Set up the input on the input stack. */
263 ip = cpp_push_buffer (pfile, buf1, length);
268 /* Scan the input, create the output. */
271 token = cpp_get_token (pfile);
272 if (token == CPP_EOF)
274 if (token == CPP_POP && CPP_BUFFER (pfile) == ip)
276 cpp_pop_buffer (pfile);
280 CPP_NUL_TERMINATE (pfile);
283 /* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output.
284 Then pop the buffer. */
287 cpp_scan_buffer_nooutput (pfile)
290 cpp_buffer *buffer = CPP_BUFFER (pfile);
291 enum cpp_ttype token;
292 unsigned int old_written = CPP_WRITTEN (pfile);
293 /* In no-output mode, we can ignore everything but directives. */
296 if (! pfile->only_seen_white)
297 _cpp_skip_rest_of_line (pfile);
298 token = cpp_get_token (pfile);
299 if (token == CPP_EOF)
301 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
303 cpp_pop_buffer (pfile);
307 CPP_SET_WRITTEN (pfile, old_written);
310 /* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT.
311 Then pop the buffer. */
314 cpp_scan_buffer (pfile, print)
318 cpp_buffer *buffer = CPP_BUFFER (pfile);
319 enum cpp_ttype token;
323 token = cpp_get_token (pfile);
324 if ((token == CPP_POP && !CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
325 || token == CPP_EOF || token == CPP_VSPACE
326 /* XXX Temporary kluge - force flush after #include only */
327 || (token == CPP_DIRECTIVE
328 && CPP_BUFFER (pfile)->nominal_fname != print->last_fname))
330 cpp_output_tokens (pfile, print);
331 if (token == CPP_EOF)
333 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
335 cpp_pop_buffer (pfile);
342 /* Return the topmost cpp_buffer that corresponds to a file (not a macro). */
345 cpp_file_buffer (pfile)
350 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
351 if (ip->ihash != NULL)
356 /* Skip a C-style block comment. We know it's a comment, and point is
357 at the second character of the starter. */
359 skip_block_comment (pfile)
362 unsigned int line, col;
363 const U_CHAR *limit, *cur;
366 line = CPP_BUF_LINE (CPP_BUFFER (pfile));
367 col = CPP_BUF_COL (CPP_BUFFER (pfile));
368 limit = CPP_BUFFER (pfile)->rlimit;
369 cur = CPP_BUFFER (pfile)->cur;
374 if (c == '\n' || c == '\r')
376 /* \r cannot be a macro escape marker here. */
377 if (!ACTIVE_MARK_P (pfile))
378 CPP_BUMP_LINE_CUR (pfile, cur);
382 /* Check for teminator. */
383 if (cur < limit && *cur == '/')
386 /* Warn about comment starter embedded in comment. */
387 if (cur[-2] == '/' && CPP_OPTION (pfile, warn_comments))
388 cpp_warning_with_line (pfile, CPP_BUFFER (pfile)->lineno,
389 cur - CPP_BUFFER (pfile)->line_base,
390 "'/*' within comment");
394 cpp_error_with_line (pfile, line, col, "unterminated comment");
397 CPP_BUFFER (pfile)->cur = cur + 1;
400 /* Skip a C++/Chill line comment. We know it's a comment, and point
401 is at the second character of the initiator. */
403 skip_line_comment (pfile)
411 /* We don't have to worry about EOF in here. */
414 /* Don't consider final '\n' to be part of comment. */
420 /* \r cannot be a macro escape marker here. */
421 if (!ACTIVE_MARK_P (pfile))
422 CPP_BUMP_LINE (pfile);
423 if (CPP_OPTION (pfile, warn_comments))
424 cpp_warning (pfile, "backslash-newline within line comment");
429 /* Skip a comment - C, C++, or Chill style. M is the first character
430 of the comment marker. If this really is a comment, skip to its
431 end and return ' '. If this is not a comment, return M (which will
435 skip_comment (pfile, m)
439 if (m == '/' && PEEKC() == '*')
441 skip_block_comment (pfile);
444 else if (m == '/' && PEEKC() == '/')
446 if (CPP_BUFFER (pfile)->system_header_p)
448 /* We silently allow C++ comments in system headers, irrespective
449 of conformance mode, because lots of busted systems do that
450 and trying to clean it up in fixincludes is a nightmare. */
451 skip_line_comment (pfile);
454 else if (CPP_OPTION (pfile, cplusplus_comments))
456 if (CPP_OPTION (pfile, c89)
457 && CPP_PEDANTIC (pfile)
458 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
461 "C++ style comments are not allowed in ISO C89");
463 "(this will be reported only once per input file)");
464 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
466 skip_line_comment (pfile);
472 else if (m == '-' && PEEKC() == '-'
473 && CPP_OPTION (pfile, chill))
475 skip_line_comment (pfile);
482 /* Identical to skip_comment except that it copies the comment into the
483 token_buffer. This is used if !discard_comments. */
485 copy_comment (pfile, m)
489 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
492 if (skip_comment (pfile, m) == m)
495 limit = CPP_BUFFER (pfile)->cur;
496 CPP_RESERVE (pfile, limit - start + 2);
497 CPP_PUTC_Q (pfile, m);
498 for (; start <= limit; start++)
500 CPP_PUTC_Q (pfile, *start);
506 null_warning (pfile, count)
511 cpp_warning (pfile, "embedded null character ignored");
513 cpp_warning (pfile, "embedded null characters ignored");
516 /* Skip whitespace \-newline and comments. Does not macro-expand. */
519 _cpp_skip_hspace (pfile)
522 unsigned int null_count = 0;
530 else if (is_hspace(c))
532 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
533 cpp_pedwarn (pfile, "%s in preprocessing directive",
534 c == '\f' ? "formfeed" : "vertical tab");
540 /* \r is a backslash-newline marker if !has_escapes, and
541 a deletable-whitespace or no-reexpansion marker otherwise. */
542 if (CPP_BUFFER (pfile)->has_escapes)
550 CPP_BUMP_LINE (pfile);
552 else if (c == '/' || c == '-')
554 c = skip_comment (pfile, c);
564 null_warning (pfile, null_count);
567 /* Read and discard the rest of the current line. */
570 _cpp_skip_rest_of_line (pfile)
584 if (! CPP_BUFFER (pfile)->has_escapes)
585 CPP_BUMP_LINE (pfile);
590 skip_string (pfile, c);
595 skip_comment (pfile, c);
600 if (CPP_PEDANTIC (pfile))
601 cpp_pedwarn (pfile, "%s in preprocessing directive",
602 c == '\f' ? "formfeed" : "vertical tab");
609 /* Parse an identifier starting with C. */
612 _cpp_parse_name (pfile, c)
624 if (c == '$' && CPP_PEDANTIC (pfile))
625 cpp_pedwarn (pfile, "`$' in identifier");
627 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
628 CPP_PUTC_Q (pfile, c);
633 CPP_NUL_TERMINATE_Q (pfile);
637 /* Parse and skip over a string starting with C. A single quoted
638 string is treated like a double -- some programs (e.g., troff) are
639 perverse this way. (However, a single quoted string is not allowed
640 to extend over multiple lines.) */
642 skip_string (pfile, c)
646 unsigned int start_line, start_column;
647 unsigned int null_count = 0;
649 start_line = CPP_BUF_LINE (CPP_BUFFER (pfile));
650 start_column = CPP_BUF_COL (CPP_BUFFER (pfile));
657 cpp_error_with_line (pfile, start_line, start_column,
658 "unterminated string or character constant");
659 if (pfile->multiline_string_line != start_line
660 && pfile->multiline_string_line != 0)
661 cpp_error_with_line (pfile,
662 pfile->multiline_string_line, -1,
663 "possible real start of unterminated constant");
664 pfile->multiline_string_line = 0;
672 CPP_BUMP_LINE (pfile);
673 /* In Fortran and assembly language, silently terminate
674 strings of either variety at end of line. This is a
675 kludge around not knowing where comments are in these
677 if (CPP_OPTION (pfile, lang_fortran)
678 || CPP_OPTION (pfile, lang_asm))
683 /* Character constants may not extend over multiple lines.
684 In Standard C, neither may strings. We accept multiline
685 strings as an extension. */
688 cpp_error_with_line (pfile, start_line, start_column,
689 "unterminated character constant");
693 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
694 cpp_pedwarn_with_line (pfile, start_line, start_column,
695 "string constant runs past end of line");
696 if (pfile->multiline_string_line == 0)
697 pfile->multiline_string_line = start_line;
701 if (CPP_BUFFER (pfile)->has_escapes)
703 cpp_ice (pfile, "\\r escape inside string constant");
707 /* Backslash newline is replaced by nothing at all. */
708 CPP_BUMP_LINE (pfile);
725 cpp_warning (pfile, "null character in string or character constant");
726 else if (null_count > 1)
727 cpp_warning (pfile, "null characters in string or character constant");
730 /* Parse a string and copy it to the output. */
733 parse_string (pfile, c)
737 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
740 skip_string (pfile, c);
742 limit = CPP_BUFFER (pfile)->cur;
743 CPP_RESERVE (pfile, limit - start + 2);
744 CPP_PUTC_Q (pfile, c);
745 for (; start < limit; start++)
747 CPP_PUTC_Q (pfile, *start);
750 /* Read an assertion into the token buffer, converting to
751 canonical form: `#predicate(a n swe r)' The next non-whitespace
752 character to read should be the first letter of the predicate.
753 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
754 with answer (see callers for why). In case of 0, an error has been
757 _cpp_parse_assertion (pfile)
761 _cpp_skip_hspace (pfile);
765 cpp_error (pfile, "assertion without predicate");
768 else if (! is_idstart(c))
770 cpp_error (pfile, "assertion predicate is not an identifier");
773 CPP_PUTC(pfile, '#');
775 _cpp_parse_name (pfile, c);
780 if (is_hspace(c) || c == '\r')
781 _cpp_skip_hspace (pfile);
787 CPP_PUTC(pfile, '(');
790 while ((c = GETC()) != ')')
796 CPP_PUTC(pfile, ' ');
800 else if (c == '\n' || c == EOF)
802 if (c == '\n') FORWARD(-1);
803 cpp_error (pfile, "un-terminated assertion answer");
807 /* \r cannot be a macro escape here. */
808 CPP_BUMP_LINE (pfile);
816 if (pfile->limit[-1] == ' ')
817 pfile->limit[-1] = ')';
818 else if (pfile->limit[-1] == '(')
820 cpp_error (pfile, "empty token sequence in assertion");
824 CPP_PUTC (pfile, ')');
829 /* Get the next token, and add it to the text in pfile->token_buffer.
830 Return the kind of token we got. */
833 _cpp_lex_token (pfile)
837 enum cpp_ttype token;
839 if (CPP_BUFFER (pfile) == NULL)
854 if (CPP_OPTION (pfile, discard_comments))
855 c = skip_comment (pfile, c);
857 c = copy_comment (pfile, c);
861 /* Comments are equivalent to spaces.
862 For -traditional, a comment is equivalent to nothing. */
863 if (!CPP_OPTION (pfile, discard_comments))
865 else if (CPP_TRADITIONAL (pfile)
866 && ! is_space (PEEKC ()))
868 if (pfile->parsing_define_directive)
883 if (pfile->parsing_if_directive)
885 CPP_ADJUST_WRITTEN (pfile, -1);
886 if (_cpp_parse_assertion (pfile))
887 return CPP_ASSERTION;
891 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
897 CPP_PUTC (pfile, c2);
899 else if (c2 == '%' && PEEKN (1) == ':')
901 /* Digraph: "%:" == "#". */
903 CPP_RESERVE (pfile, 2);
904 CPP_PUTC_Q (pfile, c2);
905 CPP_PUTC_Q (pfile, GETC ());
908 return CPP_STRINGIZE;
913 if (!pfile->only_seen_white)
916 /* Remove the "#" or "%:" from the token buffer. */
917 CPP_ADJUST_WRITTEN (pfile, (c == '#' ? -1 : -2));
918 return CPP_DIRECTIVE;
922 parse_string (pfile, c);
923 return c == '\'' ? CPP_CHAR : CPP_STRING;
926 if (!CPP_OPTION (pfile, dollars_in_ident))
932 /* Digraph: ":>" == "]". */
934 || (c2 == ':' && CPP_OPTION (pfile, cplusplus)))
942 if (c2 == c || c2 == '=')
947 /* Digraphs: "%:" == "#", "%>" == "}". */
952 CPP_RESERVE (pfile, 2);
953 CPP_PUTC_Q (pfile, c);
954 CPP_PUTC_Q (pfile, c2);
960 CPP_RESERVE (pfile, 2);
961 CPP_PUTC_Q (pfile, c);
962 CPP_PUTC_Q (pfile, c2);
965 /* else fall through */
979 if (CPP_OPTION (pfile, chill))
980 goto comment; /* Chill style comment */
988 if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
990 /* In C++, there's a ->* operator. */
992 CPP_RESERVE (pfile, 4);
993 CPP_PUTC_Q (pfile, c);
994 CPP_PUTC_Q (pfile, GETC ());
995 CPP_PUTC_Q (pfile, GETC ());
1003 if (pfile->parsing_include_directive)
1007 CPP_PUTC (pfile, c);
1011 if (c == '\n' || c == EOF)
1014 "missing '>' in `#include <FILENAME>'");
1019 if (!CPP_BUFFER (pfile)->has_escapes)
1021 /* Backslash newline is replaced by nothing. */
1022 CPP_ADJUST_WRITTEN (pfile, -1);
1023 CPP_BUMP_LINE (pfile);
1027 /* We might conceivably get \r- or \r<space> in
1028 here. Just delete 'em. */
1030 if (d != '-' && d != ' ')
1031 cpp_ice (pfile, "unrecognized escape \\r%c", d);
1032 CPP_ADJUST_WRITTEN (pfile, -1);
1038 /* Digraphs: "<%" == "{", "<:" == "[". */
1043 CPP_RESERVE (pfile, 2);
1044 CPP_PUTC_Q (pfile, c);
1045 CPP_PUTC_Q (pfile, c2);
1050 /* else fall through */
1055 /* GNU C++ supports MIN and MAX operators <? and >?. */
1056 if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
1059 CPP_RESERVE (pfile, 3);
1060 CPP_PUTC_Q (pfile, c);
1061 CPP_PUTC_Q (pfile, c2);
1062 if (PEEKC () == '=')
1063 CPP_PUTC_Q (pfile, GETC ());
1070 CPP_PUTC (pfile, c);
1075 /* In C++ there's a .* operator. */
1076 if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
1079 if (c2 == '.' && PEEKN(1) == '.')
1081 CPP_RESERVE (pfile, 3);
1082 CPP_PUTC_Q (pfile, '.');
1083 CPP_PUTC_Q (pfile, '.');
1084 CPP_PUTC_Q (pfile, '.');
1091 CPP_RESERVE (pfile, 2);
1092 CPP_PUTC_Q (pfile, c);
1093 CPP_PUTC_Q (pfile, GETC ());
1098 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
1100 CPP_PUTC (pfile, c);
1102 parse_string (pfile, c);
1103 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
1107 case '0': case '1': case '2': case '3': case '4':
1108 case '5': case '6': case '7': case '8': case '9':
1113 CPP_RESERVE (pfile, 2);
1114 CPP_PUTC_Q (pfile, c);
1118 if (!is_numchar(c) && c != '.'
1119 && ((c2 != 'e' && c2 != 'E'
1120 && ((c2 != 'p' && c2 != 'P')
1121 || CPP_OPTION (pfile, c89)))
1122 || (c != '+' && c != '-')))
1127 CPP_NUL_TERMINATE_Q (pfile);
1129 case 'b': case 'c': case 'd': case 'h': case 'o':
1130 case 'B': case 'C': case 'D': case 'H': case 'O':
1131 if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
1133 CPP_RESERVE (pfile, 2);
1134 CPP_PUTC_Q (pfile, c);
1135 CPP_PUTC_Q (pfile, '\'');
1141 goto chill_number_eof;
1144 CPP_PUTC (pfile, c);
1148 CPP_RESERVE (pfile, 2);
1149 CPP_PUTC_Q (pfile, c);
1150 CPP_NUL_TERMINATE_Q (pfile);
1157 CPP_NUL_TERMINATE (pfile);
1164 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
1165 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
1166 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
1167 case 'x': case 'y': case 'z':
1168 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
1169 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
1170 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1173 _cpp_parse_name (pfile, c);
1176 case ' ': case '\t': case '\v': case '\f': case '\0':
1185 CPP_PUTC (pfile, c);
1187 if (c == EOF || !is_hspace(c))
1192 null_warning (pfile, null_count);
1197 if (CPP_BUFFER (pfile)->has_escapes)
1202 if (pfile->output_escapes)
1203 CPP_PUTS (pfile, "\r-", 2);
1204 _cpp_parse_name (pfile, GETC ());
1209 /* "\r " means a space, but only if necessary to prevent
1210 accidental token concatenation. */
1211 CPP_RESERVE (pfile, 2);
1212 if (pfile->output_escapes)
1213 CPP_PUTC_Q (pfile, '\r');
1214 CPP_PUTC_Q (pfile, c);
1219 cpp_ice (pfile, "unrecognized escape \\r%c", c);
1225 /* Backslash newline is ignored. */
1226 CPP_BUMP_LINE (pfile);
1231 CPP_PUTC (pfile, c);
1234 case '(': token = CPP_LPAREN; goto char1;
1235 case ')': token = CPP_RPAREN; goto char1;
1236 case '{': token = CPP_LBRACE; goto char1;
1237 case '}': token = CPP_RBRACE; goto char1;
1238 case ',': token = CPP_COMMA; goto char1;
1239 case ';': token = CPP_SEMICOLON; goto char1;
1245 CPP_PUTC (pfile, c);
1250 /* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
1251 Caller is expected to have checked no_macro_expand. */
1253 maybe_macroexpand (pfile, written)
1257 U_CHAR *macro = pfile->token_buffer + written;
1258 size_t len = CPP_WRITTEN (pfile) - written;
1259 HASHNODE *hp = _cpp_lookup (pfile, macro, len);
1263 if (hp->type == T_DISABLED)
1265 if (pfile->output_escapes)
1267 /* Insert a no-reexpand marker before IDENT. */
1268 CPP_RESERVE (pfile, 2);
1269 CPP_ADJUST_WRITTEN (pfile, 2);
1270 macro = pfile->token_buffer + written;
1272 memmove (macro + 2, macro, len);
1278 if (hp->type == T_EMPTY)
1280 /* Special case optimization: macro expands to nothing. */
1281 CPP_SET_WRITTEN (pfile, written);
1282 CPP_PUTC_Q (pfile, ' ');
1286 /* If macro wants an arglist, verify that a '(' follows. */
1287 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1289 int macbuf_whitespace = 0;
1292 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1294 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1297 _cpp_skip_hspace (pfile);
1304 if (point != CPP_BUFFER (pfile)->cur)
1305 macbuf_whitespace = 1;
1309 goto not_macro_call;
1310 cpp_pop_buffer (pfile);
1313 CPP_SET_MARK (pfile);
1316 _cpp_skip_hspace (pfile);
1323 CPP_GOTO_MARK (pfile);
1328 if (macbuf_whitespace)
1329 CPP_PUTC (pfile, ' ');
1335 /* This is now known to be a macro call.
1336 Expand the macro, reading arguments as needed,
1337 and push the expansion on the input stack. */
1338 _cpp_macroexpand (pfile, hp);
1339 CPP_SET_WRITTEN (pfile, written);
1344 cpp_get_token (pfile)
1347 enum cpp_ttype token;
1348 long written = CPP_WRITTEN (pfile);
1351 token = _cpp_lex_token (pfile);
1356 pfile->potential_control_macro = 0;
1357 pfile->only_seen_white = 0;
1361 if (pfile->only_seen_white == 0)
1362 pfile->only_seen_white = 1;
1363 CPP_BUMP_LINE (pfile);
1371 pfile->potential_control_macro = 0;
1372 if (_cpp_handle_directive (pfile))
1373 return CPP_DIRECTIVE;
1374 pfile->only_seen_white = 0;
1375 CPP_PUTC (pfile, '#');
1379 pfile->potential_control_macro = 0;
1380 pfile->only_seen_white = 0;
1381 if (! pfile->no_macro_expand
1382 && maybe_macroexpand (pfile, written))
1387 if (CPP_BUFFER (pfile) == NULL)
1389 if (CPP_BUFFER (pfile)->manual_pop)
1390 /* If we've been reading from redirected input, the
1391 frontend will pop the buffer. */
1394 if (CPP_BUFFER (pfile)->seen_eof)
1396 cpp_pop_buffer (pfile);
1401 _cpp_handle_eof (pfile);
1407 /* Like cpp_get_token, but skip spaces and comments. */
1410 cpp_get_non_space_token (pfile)
1413 int old_written = CPP_WRITTEN (pfile);
1416 enum cpp_ttype token = cpp_get_token (pfile);
1417 if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
1419 CPP_SET_WRITTEN (pfile, old_written);
1423 /* Like cpp_get_token, except that it does not execute directives,
1424 does not consume vertical space, and automatically pops off macro
1427 XXX This function will exist only till collect_expansion doesn't
1428 need to see whitespace anymore, then it'll be merged with
1429 _cpp_get_directive_token (below). */
1431 _cpp_get_define_token (pfile)
1435 enum cpp_ttype token;
1438 old_written = CPP_WRITTEN (pfile);
1439 token = _cpp_lex_token (pfile);
1446 /* Put it back and return VSPACE. */
1448 CPP_ADJUST_WRITTEN (pfile, -1);
1452 if (CPP_PEDANTIC (pfile))
1455 p = pfile->token_buffer + old_written;
1456 limit = CPP_PWRITTEN (pfile);
1459 if (*p == '\v' || *p == '\f')
1460 cpp_pedwarn (pfile, "%s in preprocessing directive",
1461 *p == '\f' ? "formfeed" : "vertical tab");
1468 /* Don't execute the directive, but don't smash it to OTHER either. */
1469 CPP_PUTC (pfile, '#');
1470 return CPP_DIRECTIVE;
1473 if (! pfile->no_macro_expand
1474 && maybe_macroexpand (pfile, old_written))
1479 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1481 cpp_pop_buffer (pfile);
1485 /* This can happen for files that don't end with a newline,
1486 and for cpp_define and friends. Pretend they do, so
1487 callers don't have to deal. A warning will be issued by
1488 someone else, if necessary. */
1493 /* Just like _cpp_get_define_token except that it discards horizontal
1497 _cpp_get_directive_token (pfile)
1500 int old_written = CPP_WRITTEN (pfile);
1503 enum cpp_ttype token = _cpp_get_define_token (pfile);
1504 if (token != CPP_COMMENT && token != CPP_HSPACE)
1506 CPP_SET_WRITTEN (pfile, old_written);
1510 /* Determine the current line and column. Used only by read_and_prescan. */
1512 find_position (start, limit, linep)
1515 unsigned long *linep;
1517 unsigned long line = *linep;
1518 U_CHAR *lbase = start;
1519 while (start < limit)
1521 U_CHAR ch = *start++;
1522 if (ch == '\n' || ch == '\r')
1532 /* The following table is used by _cpp_read_and_prescan. If we have
1533 designated initializers, it can be constant data; otherwise, it is
1534 set up at runtime by _cpp_init_input_buffer. */
1537 #define UCHAR_MAX 255 /* assume 8-bit bytes */
1540 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
1541 #define init_chartab() /* nothing */
1542 #define CHARTAB static const unsigned char chartab[UCHAR_MAX + 1] = {
1544 #define s(p, v) [p] = v,
1546 #define CHARTAB static unsigned char chartab[UCHAR_MAX + 1] = { 0 }; \
1547 static void init_chartab PARAMS ((void)) { \
1548 unsigned char *x = chartab;
1550 #define s(p, v) x[p] = v;
1553 /* Table of characters that can't be handled in the inner loop.
1554 Also contains the mapping between trigraph third characters and their
1556 #define SPECCASE_CR 1
1557 #define SPECCASE_BACKSLASH 2
1558 #define SPECCASE_QUESTION 3
1561 s('\r', SPECCASE_CR)
1562 s('\\', SPECCASE_BACKSLASH)
1563 s('?', SPECCASE_QUESTION)
1565 s('=', '#') s(')', ']') s('!', '|')
1566 s('(', '[') s('\'', '^') s('>', '}')
1567 s('/', '\\') s('<', '{') s('-', '~')
1574 #define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
1575 #define NONTRI(c) ((c) <= SPECCASE_QUESTION)
1577 /* Read the entire contents of file DESC into buffer BUF. LEN is how
1578 much memory to allocate initially; more will be allocated if
1579 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1580 canonical form (\n). If enabled, convert and/or warn about
1581 trigraphs. Convert backslash-newline to a one-character escape
1582 (\r) and remove it from "embarrassing" places (i.e. the middle of a
1583 token). If there is no newline at the end of the file, add one and
1584 warn. Returns -1 on failure, or the actual length of the data to
1587 This function does a lot of work, and can be a serious performance
1588 bottleneck. It has been tuned heavily; make sure you understand it
1589 before hacking. The common case - no trigraphs, Unix style line
1590 breaks, backslash-newline set off by whitespace, newline at EOF -
1591 has been optimized at the expense of the others. The performance
1592 penalty for DOS style line breaks (\r\n) is about 15%.
1594 Warnings lose particularly heavily since we have to determine the
1595 line number, which involves scanning from the beginning of the file
1596 or from the last warning. The penalty for the absence of a newline
1597 at the end of reload1.c is about 60%. (reload1.c is 329k.)
1599 If your file has more than one kind of end-of-line marker, you
1600 will get messed-up line numbering.
1602 So that the cases of the switch statement do not have to concern
1603 themselves with the complications of reading beyond the end of the
1604 buffer, the buffer is guaranteed to have at least 3 characters in
1605 it (or however many are left in the file, if less) on entry to the
1606 switch. This is enough to handle trigraphs and the "\\\n\r" and
1609 The end of the buffer is marked by a '\\', which, being a special
1610 character, guarantees we will exit the fast-scan loops and perform
1614 _cpp_read_and_prescan (pfile, fp, desc, len)
1620 U_CHAR *buf = (U_CHAR *) xmalloc (len);
1621 U_CHAR *ip, *op, *line_base;
1624 unsigned int deferred_newlines;
1629 deferred_newlines = 0;
1633 ibase = pfile->input_buffer + 3;
1635 ip[-1] = '\0'; /* Guarantee no match with \n for SPECCASE_CR */
1639 U_CHAR *near_buff_end;
1641 count = read (desc, ibase, pfile->input_buffer_len);
1645 ibase[count] = '\\'; /* Marks end of buffer */
1648 near_buff_end = pfile->input_buffer + count;
1653 size_t delta_line_base;
1657 This could happen if the file is larger than half the
1658 maximum address space of the machine. */
1661 delta_op = op - buf;
1662 delta_line_base = line_base - buf;
1663 buf = (U_CHAR *) xrealloc (buf, len);
1664 op = buf + delta_op;
1665 line_base = buf + delta_line_base;
1672 /* Allow normal processing of the (at most 2) remaining
1673 characters. The end-of-buffer marker is still present
1674 and prevents false matches within the switch. */
1675 near_buff_end = ibase - 1;
1682 /* Deal with \-newline, potentially in the middle of a token. */
1683 if (deferred_newlines)
1685 if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
1687 /* Previous was not white space. Skip to white
1688 space, if we can, before outputting the \r's */
1690 while (ip[span] != ' '
1693 && NORMAL(ip[span]))
1695 memcpy (op, ip, span);
1698 if (! NORMAL(ip[0]))
1701 while (deferred_newlines)
1702 deferred_newlines--, *op++ = '\r';
1705 /* Copy as much as we can without special treatment. */
1707 while (NORMAL (ip[span])) span++;
1708 memcpy (op, ip, span);
1713 if (ip > near_buff_end) /* Do we have enough chars? */
1715 switch (chartab[*ip++])
1717 case SPECCASE_CR: /* \r */
1726 case SPECCASE_BACKSLASH: /* \ */
1729 deferred_newlines++;
1731 if (*ip == '\r') ip++;
1733 else if (*ip == '\r')
1735 deferred_newlines++;
1737 if (*ip == '\n') ip++;
1743 case SPECCASE_QUESTION: /* ? */
1747 *op++ = '?'; /* Normal non-trigraph case */
1756 if (CPP_OPTION (pfile, warn_trigraphs))
1759 line_base = find_position (line_base, op, &line);
1760 col = op - line_base + 1;
1761 if (CPP_OPTION (pfile, trigraphs))
1762 cpp_warning_with_line (pfile, line, col,
1763 "trigraph ??%c converted to %c", d, t);
1765 cpp_warning_with_line (pfile, line, col,
1766 "trigraph ??%c ignored", d);
1770 if (CPP_OPTION (pfile, trigraphs))
1772 op[-1] = t; /* Overwrite '?' */
1777 goto do_speccase; /* May need buffer refill */
1789 /* Copy previous char plus unprocessed (at most 2) chars
1790 to beginning of buffer, refill it with another
1791 read(), and continue processing */
1792 memmove (ip - count - 1, ip - 1, 4 - (ip - near_buff_end));
1802 line_base = find_position (line_base, op, &line);
1803 col = op - line_base + 1;
1804 cpp_warning_with_line (pfile, line, col, "no newline at end of file");
1805 if (offset + 1 > len)
1808 if (offset + 1 > len)
1810 buf = (U_CHAR *) xrealloc (buf, len);
1816 fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1820 cpp_notice (pfile, "%s is too large (>%lu bytes)", fp->ihash->name,
1821 (unsigned long)offset);
1826 cpp_error_from_errno (pfile, fp->ihash->name);
1831 /* Allocate pfile->input_buffer, and initialize chartab[]
1832 if it hasn't happened already. */
1835 _cpp_init_input_buffer (pfile)
1842 /* Determine the appropriate size for the input buffer. Normal C
1843 source files are smaller than eight K. */
1844 /* 8Kbytes of buffer proper, 1 to detect running off the end without
1845 address arithmetic all the time, and 3 for pushback during buffer
1846 refill, in case there's a potential trigraph or end-of-line
1847 digraph at the end of a block. */
1849 tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
1850 pfile->input_buffer = tmp;
1851 pfile->input_buffer_len = 8192;