3 http://src.openresources.com/debian/src/devel/HTML/S/altgcc_2.7.2.2.orig%20altgcc-2.7.2.2.orig%20protoize.c.html
5 abspath (cwd, rel_filename)
10 *** cpplib.c.old Tue Nov 28 2000 09:04:09 AM
11 --- cpplib.c Tue Nov 28 2000 08:55:18 AM
14 c2 = cppReader_peekC (pfile)
21 c2 = cppReader_peekC (pfile)
24 ! cppReader_forward (pfile, 1)
30 cppReader_putChar (pfile, c)
33 Carl J. Appellof ( mailto:cappello@legato.com <mailto:cappello@legato.com> )
37 ** LCLint - annotation-assisted static program checker
38 ** Copyright (C) 1994-2001 University of Virginia,
39 ** Massachusetts Institute of Technology
41 ** This program is free software; you can redistribute it and/or modify it
42 ** under the terms of the GNU General Public License as published by the
43 ** Free Software Foundation; either version 2 of the License, or (at your
44 ** option) any later version.
46 ** This program is distributed in the hope that it will be useful, but
47 ** WITHOUT ANY WARRANTY; without even the implied warranty of
48 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 ** General Public License for more details.
51 ** The GNU General Public License is available from http://www.gnu.org/ or
52 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
53 ** MA 02111-1307, USA.
55 ** For information on lclint: lclint-request@cs.virginia.edu
56 ** To report a bug: lclint-bug@cs.virginia.edu
57 ** For more information: http://lclint.cs.virginia.edu
63 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
64 Contributed by Per Bothner, 1994-95.
65 Based on CCCP program by Paul Rubin, June 1986
66 Adapted to ANSI C, Richard Stallman, Jan 1987
68 This program is free software; you can redistribute it and/or modify it
69 under the terms of the GNU General Public License as published by the
70 Free Software Foundation; either version 2, or (at your option) any
73 This program is distributed in the hope that it will be useful,
74 but WITHOUT ANY WARRANTY; without even the implied warranty of
75 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76 GNU General Public License for more details.
78 You should have received a copy of the GNU General Public License
79 along with this program; if not, write to the Free Software
80 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
82 In other words, you are welcome to use, share and improve this program.
83 You are forbidden to forbid anyone else to use, share and improve
84 what you give them. Help stamp out software-hoarding! */
88 * - OS2 drive specs like WIN32
89 * - Includes for IBMs OS/2 compiler
101 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
105 # include <sys/types.h>
106 # include <sys/stat.h>
109 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
111 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
116 # include <time.h> /* Reported by Paul Smith */
117 # include <sys/time.h>
118 # include <sys/resource.h>
120 # include <sys/times.h>
124 # endif /* not VMS */
125 # endif /* not WIN32 */
127 /* This defines "errno" properly for VMS, and gives us EACCES. */
130 # include "lclintMacros.nf"
131 # include "llbasic.h"
134 # include "cpperror.h"
135 # include "cpphash.h"
137 # include "version.h"
142 ** This is really kludgey code...
148 #define NO_SHORTNAMES
156 /*@constant int IMPORT_FOUND@*/
157 # define IMPORT_FOUND -2
159 /*@constant int SKIP_INCLUDE@*/
160 # define SKIP_INCLUDE IMPORT_FOUND
162 /*@constant unused int IMPORT_NOT_FOUND@*/
163 # define IMPORT_NOT_FOUND -1
166 /*@constant unused int STDC_VALUE@*/
170 /* By default, colon separates directories in a path. */
171 #ifndef PATH_SEPARATOR
172 /*@constant char PATH_SEPARATOR@*/
173 #define PATH_SEPARATOR ':'
176 static void parse_name (cppReader *, int);
178 static int cpp_openIncludeFile (char *p_filename)
179 /*@modifies fileSystem @*/ ;
181 static void cpp_setLocation (cppReader *p_pfile)
182 /*@modifies g_currentloc@*/ ;
184 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
185 struct parse_marker *p_smark)
186 /*@modifies p_pfile, p_smark@*/;
188 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@*/ ;
190 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
196 /* Symbols to predefine. */
198 #ifdef CPP_PREDEFINES
199 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
201 static /*@observer@*/ char *predefs = "";
204 /* We let tm.h override the types used here, to handle trivial differences
205 such as the choice of unsigned int or long unsigned int for size_t.
206 When machines start needing nontrivial differences in the size type,
207 it would be best to do something here to figure out automatically
208 from other information what type to use. */
210 /* The string value for __SIZE_TYPE__. */
213 /*@constant observer char *SIZE_TYPE@*/
214 #define SIZE_TYPE "long unsigned int"
217 /* The string value for __PTRDIFF_TYPE__. */
220 /*@constant observer char *PTRDIFF_TYPE@*/
221 #define PTRDIFF_TYPE "long int"
224 /* The string value for __WCHAR_TYPE__. */
227 /*@constant observer char *WCHAR_TYPE@*/
228 #define WCHAR_TYPE "int"
231 /* The string value for __USER_LABEL_PREFIX__ */
233 #ifndef USER_LABEL_PREFIX
234 /*@constant observer char *USER_LABEL_PREFIX@*/
235 #define USER_LABEL_PREFIX ""
238 /* The string value for __REGISTER_PREFIX__ */
240 #ifndef REGISTER_PREFIX
241 /*@constant observer char *REGISTER_PREFIX@*/
242 #define REGISTER_PREFIX ""
245 /* table to tell if char can be part of a C identifier. */
246 static bool is_idchar[256];
247 /* table to tell if char can be first char of a c identifier. */
248 static bool is_idstart[256];
249 /* table to tell if c is horizontal space. */
250 static bool is_hor_space[256];
251 /* table to tell if c is horizontal or vertical space. */
252 static bool is_space[256];
254 static /*@exposed@*/ /*@null@*/ cppBuffer *
255 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
256 /*@uses p_pfile->buffer@*/
257 /*@modifies nothing@*/ ;
260 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
263 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
265 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
267 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
268 # define cppBuffer_get(BUFFER) \
269 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
271 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
272 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
273 modifies *p_file; @*/
274 # define cppReader_puts(PFILE, STR, N) \
275 cppReader_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
277 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
279 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
280 modifies *p_file; @*/
281 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
283 /* Append character CH to PFILE's output buffer. Make space if need be. */
285 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
286 modifies *p_file; @*/
287 #define cppReader_putChar(PFILE, CH) (cppReader_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
289 /* Make sure PFILE->limit is followed by '\0'. */
290 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
291 modifies *p_file; @*/
293 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
295 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
296 modifies *p_file; @*/
297 # define cppReader_nullTerminate(PFILE) \
298 (cppReader_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
300 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
301 modifies *p_file; @*/
302 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
304 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
305 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
307 /*@function static observer char *cppReader_wcharType (cppReader *)
308 modifies nothing; @*/
310 # define cppReader_wcharType(PFILE) \
311 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
313 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
315 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
316 # define cppReader_forward(pfile, N) \
317 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
319 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
320 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
322 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
323 # define cppReader_peekC(pfile) (cppBufPeek (cppReader_getBufferSafe (pfile)))
325 /* Move all backslash-newline pairs out of embarrassing places.
326 Exchange all such pairs following BP
327 with any potentially-embarrassing characters that follow them.
328 Potentially-embarrassing characters are / and *
329 (because a backslash-newline inside a comment delimiter
330 would cause it not to be recognized). */
333 # define NEWLINE_FIX \
334 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
336 /* Same, but assume we've already read the potential '\\' into C. */
338 # define NEWLINE_FIX1(C) do { \
339 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
342 static void parseSetMark (/*@out@*/ struct parse_marker *,
344 static void parseClearMark (struct parse_marker *);
345 static void parseGotoMark (struct parse_marker *, cppReader *);
346 static void parseMoveMark (struct parse_marker *, cppReader *);
348 /* If we have a huge buffer, may need to cache more recent counts */
349 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
351 static /*@exposed@*/ /*@null@*/ cppBuffer *
352 cppReader_pushBuffer (cppReader *p_pfile,
353 /*@owned@*/ /*@null@*/ char *, size_t)
354 /*@modifies p_pfile@*/ ;
356 static void cppReader_appendIncludeChain
358 /*@keep@*/ struct file_name_list *p_first,
359 /*@dependent@*/ struct file_name_list *p_last);
361 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
362 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
364 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
365 /*@unused@*/ cppReader *p_pfile);
367 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
368 /*@unused@*/ cppReader *p_pfile);
370 static int cppReader_handleDirective (cppReader *p_pfile);
372 static void cppReader_scanBuffer (cppReader *p_pfile);
374 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
377 ** WIN32 (at least the VC++ include files) does not define mode_t.
380 /*@-incondefs@*/ /*@-czechtypes@*/
381 typedef unsigned int mode_t;
382 /*@=incondefs@*/ /*@=czechtypes@*/
386 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
387 /*@out@*/ size_t *p_size_pointer);
388 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
392 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
393 ** (Note that it is false while we're expanding marco *arguments*.)
396 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
398 static void path_include (cppReader *p_pfile, char *p_path)
399 /*@modifies p_pfile@*/ ;
401 static void initialize_builtins (cppReader *p_pfile)
402 /*@modifies p_pfile@*/ ;
404 static void initialize_char_syntax (struct cppOptions *p_opts) ;
406 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
408 bool p_system_header_p,
409 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
411 static void validate_else (cppReader *p_pfile, cstring p_directive);
413 static void conditional_skip (cppReader *p_pfile, int p_skip,
414 enum node_type p_type,
415 /*@dependent@*/ /*@null@*/ char *p_control_macro);
417 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
421 static void skip_if_group (cppReader *p_pfile, int p_any);
423 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
424 char *p_beg2, int p_len2, bool p_last);
427 extern void fancy_abort ();
430 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
431 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
433 static /*@observer@*/ /*@null@*/ struct file_name_map *
434 read_name_map (cppReader *p_pfile, cstring p_dirname);
436 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
438 static int open_include_file (cppReader *p_pfile,
439 /*@owned@*/ cstring p_fname,
440 /*@null@*/ struct file_name_list *p_searchptr);
442 static void push_macro_expansion (cppReader *,
443 /*@owned@*/ char *, size_t,
444 /*@dependent@*/ hashNode);
446 /* Last arg to output_line_command. */
447 enum file_change_code {
448 same_file, enter_file, leave_file
451 /* `struct directive' defines one #-directive, including how to handle it. */
454 int length; /* Length of name */
455 /*@null@*/ int (*func)(); /* Function to handle directive */
456 /*@observer@*/ cstring name; /* Name of directive */
457 enum node_type type; /* Code which describes which directive. */
458 bool command_reads_line; /* One if rest of line is read by func. */
459 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
460 bool pass_thru; /* Copy preprocessed directive to output file.*/
463 /* These functions are declared to return int instead of void since they
464 are going to be placed in a table and some old compilers have trouble with
465 pointers to functions returning void. */
467 static int do_define (cppReader *, /*@null@*/ struct directive *,
469 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
470 char *, char *, bool);
472 static int do_line (cppReader *, /*@null@*/ struct directive *);
473 static int do_include (cppReader *, struct directive *, char *, char *);
474 static int do_undef (cppReader *, struct directive *, char *, char *);
475 static int do_error (cppReader *, struct directive *, char *, char *);
476 static int do_pragma (cppReader *, struct directive *, char *, char *);
477 static int do_ident (cppReader *, struct directive *, char *, char *);
478 static int do_if (cppReader *, struct directive *, char *, char *);
479 static int do_xifdef (cppReader *, struct directive *, char *, char *);
480 static int do_else (cppReader *, struct directive *, char *, char *);
481 static int do_elif (cppReader *, struct directive *, char *, char *);
482 static int do_endif (cppReader *, struct directive *, char *, char *);
483 static int do_warning (cppReader *, struct directive *, char *, char *);
485 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
486 via the same directory as the file that #included it. */
488 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
489 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
491 /* #include "file" looks in source file dir, then stack. */
492 /* #include <file> just looks in the stack. */
493 /* -I directories are added to the end, then the defaults are added. */
497 static struct default_include {
498 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
499 int cplusplus; /* Only look here if we're compiling C++. */
500 int cxx_aware; /* Includes in this directory don't need to
501 be wrapped in extern "C" when compiling
503 } include_defaults_array[]
505 /* This is the dir for fixincludes. Put it just before
506 the files that we fix. */
507 { GCC_INCLUDE_DIR, 0, 0 },
508 { GCC_INCLUDE_DIR2, 0, 0 },
509 { cstring_undefined, 0, 0 }
512 /*@noaccess cstring@*/
514 /* Here is the actual list of #-directives, most-often-used first.
515 The initialize_builtins function assumes #define is the very first. */
519 static struct directive directive_table[] = {
520 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
521 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
522 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
523 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
524 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
525 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
526 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
527 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
528 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
529 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
530 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
531 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
532 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
533 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
534 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
535 { -1, 0, "", T_UNUSED, FALSE, FALSE, FALSE },
537 /*@noaccess cstring@*/
539 static cstring searchPath_unparse (struct file_name_list *search_start)
541 cstring res = cstring_newEmpty ();
542 struct file_name_list *searchptr = NULL;
544 for (searchptr = search_start; searchptr != NULL;
545 searchptr = searchptr->next)
547 if (!cstring_isEmpty (searchptr->fname)) {
548 res = cstring_concatFree1 (res, searchptr->fname);
549 if (searchptr->next != NULL) {
550 res = cstring_appendChar (res, ';');
560 initialize_char_syntax (struct cppOptions *opts)
565 * Set up is_idchar and is_idstart tables. These should be
566 * faster than saying (is_alpha (c) || c == '_'), etc.
567 * Set up these things before calling any routines tthat
571 for (i = 'a'; i <= 'z'; i++) {
572 is_idchar[i - 'a' + 'A'] = TRUE;
573 is_idchar[(int) i] = TRUE;
574 is_idstart[i - 'a' + 'A'] = TRUE;
575 is_idstart[(int) i] = TRUE;
578 for (i = '0'; i <= '9'; i++)
580 is_idchar[(int) i] = TRUE;
583 is_idchar['_'] = TRUE;
584 is_idstart['_'] = TRUE;
585 is_idchar['$'] = opts->dollars_in_ident;
586 is_idstart['$'] = opts->dollars_in_ident;
588 /* horizontal space table */
589 is_hor_space[' '] = TRUE;
590 is_hor_space['\t'] = TRUE;
591 is_hor_space['\v'] = TRUE;
592 is_hor_space['\f'] = TRUE;
593 is_hor_space['\r'] = TRUE;
595 is_space[' '] = TRUE;
596 is_space['\t'] = TRUE;
597 is_space['\v'] = TRUE;
598 is_space['\f'] = TRUE;
599 is_space['\n'] = TRUE;
600 is_space['\r'] = TRUE;
603 bool isIdentifierChar (char c)
605 return is_idchar[(int) c];
608 /* Place into P_PFILE a quoted string representing the string SRC.
609 Caller must reserve enough space in pfile->token_buffer. */
612 quote_string (cppReader *pfile, char *src)
616 cppReader_putCharQ (pfile, '\"');
619 switch ((c = *src++))
623 cppReader_putCharQ (pfile, c);
626 sprintf (cppReader_getPWritten (pfile), "\\%03o",
628 cppReader_adjustWritten (pfile, (size_t) 4);
630 /*@switchbreak@*/ break;
634 cppReader_putCharQ (pfile, '\\');
635 cppReader_putCharQ (pfile, c);
636 /*@switchbreak@*/ break;
639 cppReader_putCharQ (pfile, '\"');
640 cppReader_nullTerminateQ (pfile);
646 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
649 cppReader_growBuffer (cppReader *pfile, size_t n)
651 size_t old_written = cppReader_getWritten (pfile);
652 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
653 pfile->token_buffer = (char *)
654 drealloc (pfile->token_buffer, pfile->token_buffer_size);
655 cppReader_setWritten (pfile, old_written);
659 * process a given definition string, for initialization
660 * If STR is just an identifier, define it with value 1.
661 * If STR has anything after the identifier, then it should
662 * be identifier=definition.
666 cppReader_define (cppReader *pfile, char *str)
671 DPRINTF (("Cpp reader define: %s", str));
673 if (!is_idstart[(int) *p])
675 DPRINTF (("ERROR 1"));
676 cppReader_error (pfile,
677 message ("Malformed option `-D%s'",
678 cstring_fromChars (str)));
685 DPRINTF (("Here 2"));
687 while (is_idchar[(int) *p])
694 while (*p != ')' && *p != '\0') {
703 message ("Malformed option: -D%s (no closing parenthesis)",
704 cstring_fromChars (str)));
708 DPRINTF (("Here 2"));
712 buf = (char *) dmalloc (size_fromInt (p - str + 4));
713 strcpy ((char *) buf, str);
714 strcat ((char *) buf, " 1");
718 DPRINTF (("ERROR 2"));
719 cppReader_error (pfile,
720 message ("Malformed option: -D%s (expected '=', found '%c')",
721 cstring_fromChars (str),
728 /* Copy the entire option so we can modify it. */
729 DPRINTF (("Copying..."));
730 buf = (char *) dmalloc (2 * strlen (str) + 1);
731 strncpy (buf, str, size_fromInt (p - str));
733 /* Change the = to a space. */
735 /* Scan for any backslash-newline and remove it. */
741 if (*p == '\\' && p[1] == '\n')
747 DPRINTF (("Here we are..."));
751 llassert (buf != NULL);
752 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
753 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
757 /* Append a chain of `struct file_name_list's
758 to the end of the main include chain.
759 FIRST is gthe beginning of the chain to append, and LAST is the end. */
762 cppReader_appendIncludeChain (cppReader *pfile,
763 struct file_name_list *first,
764 struct file_name_list *last)
766 struct cppOptions *opts = CPPOPTIONS (pfile);
767 struct file_name_list *dir;
769 if (first == NULL || last == NULL)
774 if (opts->include == 0)
776 opts->include = first;
780 llassert (opts->last_include->next == NULL);
781 opts->last_include->next = first;
784 if (opts->first_bracket_include == 0)
786 opts->first_bracket_include = first;
788 for (dir = first; ; dir = dir->next) {
789 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
790 if (len > pfile->max_include_len)
791 pfile->max_include_len = len;
797 llassert (last->next == NULL);
798 /* last->next = NULL; */
799 opts->last_include = last;
803 static /*@unused@*/ void
804 cppReader_showIncludeChain (cppReader *pfile)
806 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
812 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
816 fprintf (stderr, "\n");
820 fprintf (stderr, "No includes\n");
826 cppReader_getIncludePath ()
828 cppReader *pfile = &g_cppState;
829 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
830 cstring res = cstring_undefined;
836 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
842 res = cstring_makeLiteral ("<no include path>");
849 cppReader_addIncludeChain (cppReader *pfile, struct file_name_list *dir)
851 struct cppOptions *opts = CPPOPTIONS (pfile);
858 if (opts->include == 0)
864 llassert (opts->last_include->next == NULL);
865 opts->last_include->next = dir;
868 if (opts->first_bracket_include == 0)
870 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
871 opts->first_bracket_include = dir;
872 if (len > pfile->max_include_len)
874 pfile->max_include_len = len;
879 opts->last_include = dir;
880 /* cppReader_showIncludeChain (pfile); */
883 /* Given a colon-separated list of file names PATH,
884 add all the names to the search path for include files. */
887 path_include (cppReader *pfile, char *path)
894 /* if we have a posix path list, convert to win32 path list */
895 win32temp = (char *) dmalloc /*@i4@*/
896 (cygwin32_posix_to_win32_path_list_buf_size (path));
897 cygwin32_posix_to_win32_path_list (path, win32temp);
907 struct file_name_list *dirtmp;
909 /* Find the end of this name. */
910 while (*q != '\0' && *q != PATH_SEPARATOR)
917 /* An empty name in the path stands for the current directory. */
918 name = (char *) dmalloc ((size_t) 2);
924 /* Otherwise use the directory that is named. */
925 name = (char *) dmalloc (size_fromInt (q - p + 1));
926 memcpy (name, p, size_fromInt (q - p));
930 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
931 dirtmp->next = 0; /* New one goes on the end */
932 dirtmp->control_macro = 0;
933 dirtmp->c_system_include_path = 0;
934 dirtmp->fname = cstring_fromChars (name);
935 dirtmp->got_name_map = 0;
936 cppReader_addIncludeChain (pfile, dirtmp);
938 /* Advance past this name. */
942 /* Skip the colon. */
948 cppOptions_init (cppOptions *opts)
950 memset ((char *) opts, 0, sizeof *opts);
953 opts->in_fname = NULL;
954 opts->out_fname = NULL;
956 /* Initialize is_idchar to allow $. */
957 opts->dollars_in_ident = TRUE;
959 opts->no_line_commands = 0;
960 opts->no_trigraphs = TRUE;
961 opts->put_out_comments = 1;
962 opts->print_include_names = 0;
963 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
967 opts->cplusplus_comments = 1;
972 opts->pedantic_errors = 0;
973 opts->warn_comments = 0;
974 opts->warnings_are_errors = 0;
976 initialize_char_syntax (opts);
980 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
986 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
987 /*@unused@*/ cppReader *pfile)
993 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
995 hashNode macro = pbuf->hnode;
997 if (macro->type == T_DISABLED)
999 macro->type = T_MACRO;
1002 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
1010 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
1012 if (pbuf->buf != NULL)
1019 /* Assuming we have read '/'.
1020 If this is the start of a comment (followed by '*' or '/'),
1021 skip to the end of the comment, and return ' '.
1022 Return EOF if we reached the end of file before the end of the comment.
1023 If not the start of a comment, return '/'. */
1026 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1030 llassert (pfile->buffer != NULL);
1031 llassert (pfile->buffer->cur != NULL);
1033 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1040 cppReader_forward (pfile, 2);
1043 if (cppReader_peekC (pfile) == '*')
1045 cppReader_forward (pfile, 1);
1050 c = cppReader_getC (pfile);
1057 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1064 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1067 if (prev_c == (int) '*' && c == (int) '/')
1072 if (c == (int) '\n' && (linep != NULL))
1078 else if (cppReader_peekC (pfile) == '/'
1079 && CPPOPTIONS (pfile)->cplusplus_comments)
1081 cppReader_forward (pfile, 1);
1085 c = cppReader_getC (pfile);
1089 /* Allow hash comment to be terminated by EOF. */
1093 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1095 cppReader_forward (pfile, 1);
1096 c = cppReader_getC (pfile);
1104 if (c == (int) '\n')
1106 /* Don't consider final '\n' to be part of comment. */
1107 cppReader_forward (pfile, -1);
1118 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1120 cppSkipHspace (cppReader *pfile)
1128 llassert (pfile->buffer != NULL);
1130 c = cppReader_peekC (pfile);
1134 return 0; /* FIXME */
1137 if (is_hor_space[c])
1139 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1140 cppReader_pedwarn (pfile,
1141 message ("%s in preprocessing directive",
1143 ? cstring_makeLiteralTemp ("formfeed")
1144 : cstring_makeLiteralTemp ("vertical tab")));
1147 cppReader_forward (pfile, 1);
1151 cppReader_forward (pfile, 1);
1152 c = skip_comment (pfile, NULL);
1156 cppReader_forward (pfile, -1);
1159 if (c == EOF || c == '/')
1164 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1166 cppReader_forward (pfile, 2);
1168 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1169 && is_hor_space [cpp_peekN (pfile, 1)])
1171 cppReader_forward (pfile, 2);
1180 /* Read the rest of the current line.
1181 The line is appended to PFILE's output buffer. */
1184 copy_rest_of_line (cppReader *pfile)
1186 struct cppOptions *opts = CPPOPTIONS (pfile);
1193 llassert (pfile->buffer != NULL);
1195 c = cppReader_getC (pfile);
1201 if (cppReader_peekC (pfile) == '\n')
1203 cppReader_forward (pfile, 1);
1207 /*@fallthrough@*/ case '\'': case '\"':
1208 goto scan_directive_token;
1211 nextc = cppReader_peekC (pfile);
1214 ** was (opts->cplusplus_comments && nextc == '*')
1219 || (opts->cplusplus_comments && nextc == '/'))
1221 goto scan_directive_token;
1223 /*@switchbreak@*/ break;
1226 if (cppReader_isPedantic (pfile))
1227 cppReader_pedwarn (pfile,
1228 message ("%s in preprocessing directive",
1230 ? cstring_makeLiteralTemp ("formfeed")
1231 : cstring_makeLiteralTemp ("vertical tab")));
1232 /*@switchbreak@*/ break;
1235 cppReader_forward (pfile, -1);
1237 scan_directive_token:
1238 cppReader_forward (pfile, -1);
1239 (void) cppGetToken (pfile);
1242 cppReader_putChar (pfile, c);
1245 cppReader_nullTerminate (pfile);
1249 cppReader_skipRestOfLine (cppReader *pfile)
1251 size_t old = cppReader_getWritten (pfile);
1252 copy_rest_of_line (pfile);
1253 cppReader_setWritten (pfile, old);
1256 /* Handle a possible # directive.
1257 '#' has already been read. */
1260 cppReader_handleDirective (cppReader *pfile)
1263 struct directive *kt = NULL;
1265 size_t after_ident = 0;
1266 char *ident, *line_end = NULL;
1267 size_t old_written = cppReader_getWritten (pfile);
1268 int nspaces = cppSkipHspace (pfile);
1270 c = cppReader_peekC (pfile);
1272 if (c >= '0' && c <= '9')
1274 /* Handle # followed by a line number. */
1275 if (cppReader_isPedantic (pfile))
1277 cppReader_pedwarnLit
1279 cstring_makeLiteralTemp ("`#' followed by integer"));
1282 (void) do_line (pfile, NULL);
1283 goto done_a_directive;
1287 /* Now find the directive name. */
1289 cppReader_putChar (pfile, '#');
1291 parse_name (pfile, cppReader_getC (pfile));
1293 llassert (pfile->token_buffer != NULL);
1294 ident = pfile->token_buffer + old_written + 1;
1296 ident_length = cppReader_getPWritten (pfile) - ident;
1298 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1300 /* A line of just `#' becomes blank. */
1304 for (kt = directive_table; ; kt++)
1306 if (kt->length <= 0)
1308 return 0; /* goto not_a_directive; */
1311 if (kt->length == ident_length
1312 && (cstring_equalPrefix (kt->name, ident)))
1318 if (kt->command_reads_line)
1324 /* Nonzero means do not delete comments within the directive.
1325 #define needs this when -traditional. */
1326 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1327 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1328 CPPOPTIONS (pfile)->put_out_comments = comments;
1329 after_ident = cppReader_getWritten (pfile);
1330 copy_rest_of_line (pfile);
1331 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1335 /* For #pragma and #define, we may want to pass through the directive.
1336 Other directives may create output, but we don't want the directive
1337 itself out, so we pop it now. For example #include may write a #line
1338 command (see comment in do_include), and conditionals may emit
1339 #failed ... #endfailed stuff. But note that popping the buffer
1340 means the parameters to kt->func may point after pfile->limit
1341 so these parameters are invalid as soon as something gets appended
1342 to the token_buffer. */
1344 line_end = cppReader_getPWritten (pfile);
1347 if (!kt->pass_thru && kt->type != T_DEFINE)
1349 cppReader_setWritten (pfile, old_written);
1352 llassert (pfile->token_buffer != NULL);
1354 /* was kt->pass_thru || */
1356 if (kt->type == T_DEFINE
1357 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1359 char *p = pfile->token_buffer + old_written;
1362 ** Still need to record value for preprocessing, so
1363 ** #ifdef's, etc. using the value behave correctly.
1366 (void) do_defineAux (pfile, kt,
1367 pfile->token_buffer + after_ident,
1376 SKIP_WHITE_SPACE (p);
1378 llassert (*p == 'd');
1381 llassert (*p == 'e');
1384 llassert (*p == 'f');
1387 llassert (*p == 'i');
1390 llassert (*p == 'n');
1393 llassert (*p == 'e');
1396 ** This is way-bogus. We use the last char to record the number of
1397 ** spaces. Its too hard to get them back into the input stream.
1400 if (nspaces > 9) nspaces = 9;
1402 *p++ = '0' + nspaces;
1404 return 0; /* not_a_directive */
1406 else if (kt->pass_thru)
1408 /* Just leave the entire #define in the output stack. */
1409 return 0; /* not_a_directive */
1412 else if (kt->type == T_DEFINE
1413 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1415 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1416 SKIP_WHITE_SPACE (p);
1418 while (is_idchar[(int) *p])
1424 cppReader_putChar (pfile, '\n');
1426 else if (kt->type == T_DEFINE)
1428 cppReader_setWritten (pfile, old_written);
1439 llassert (kt->func != NULL);
1440 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1445 /* Pass a directive through to the output file.
1446 BUF points to the contents of the directive, as a contiguous string.
1447 LIMIT points to the first character past the end of the directive.
1448 KEYWORD is the keyword-table entry for the directive. */
1451 pass_thru_directive (char *buf, char *limit,
1453 struct directive *keyword)
1455 int keyword_length = keyword->length;
1457 cppReader_reserve (pfile,
1458 size_fromInt (2 + keyword_length + (limit - buf)));
1459 cppReader_putCharQ (pfile, '#');
1460 /*@-observertrans@*/
1461 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1462 size_fromInt (keyword_length));
1463 /*:=observertrans@*/
1465 if (limit != buf && buf[0] != ' ')
1467 /* Was a bug, since reserve only used 1 + ... */
1468 cppReader_putCharQ (pfile, ' ');
1471 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1474 /* Read a replacement list for a macro with parameters.
1475 Build the DEFINITION structure.
1476 Reads characters of text starting at BUF until END.
1477 ARGLIST specifies the formal parameters to look for
1478 in the text of the definition; NARGS is the number of args
1479 in that list, or -1 for a macro name that wants no argument list.
1480 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1481 and NAMELEN is its length in characters.
1483 Note that comments, backslash-newlines, and leading white space
1484 have already been deleted from the argument. */
1487 collect_expansion (cppReader *pfile, char *buf, char *limit,
1488 int nargs, /*@null@*/ struct arglist *arglist)
1491 char *p, *lastp, *exp_p;
1492 struct reflist *endpat = NULL;
1493 /* Pointer to first nonspace after last ## seen. */
1495 /* Pointer to first nonspace after last single-# seen. */
1496 char *stringify = 0;
1498 char expected_delimiter = '\0';
1501 /* Scan thru the replacement list, ignoring comments and quoted
1502 strings, picking up on the macro calls. It does a linear search
1503 thru the arg list on every potential symbol. Profiling might say
1504 that something smarter should happen. */
1509 /* Find the beginning of the trailing whitespace. */
1512 while (p < limit && is_space[(int) limit[-1]])
1517 /* Allocate space for the text in the macro definition.
1518 Leading and trailing whitespace chars need 2 bytes each.
1519 Each other input char may or may not need 1 byte,
1520 so this is an upper bound. The extra 5 are for invented
1521 leading and trailing newline-marker and final null. */
1522 maxsize = (sizeof (*defn) + (limit - p) + 5);
1524 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1533 defn = (DEFINITION *) dmalloc (maxsize);
1534 defn->noExpand = FALSE;
1536 defn->pattern = NULL;
1537 defn->nargs = nargs;
1538 defn->predefined = NULL;
1540 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1543 defn->rest_args = NULL;
1544 defn->args.argnames = NULL;
1550 /* Add one initial space escape-marker to prevent accidental
1551 token-pasting (often removed by macroexpand). */
1555 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1556 cppReader_errorLit (pfile,
1557 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1561 /* Process the main body of the definition. */
1563 int skipped_arg = 0;
1564 register char c = *p++;
1568 if (!cppReader_isTraditional (pfile)) {
1572 if (expected_delimiter != '\0')
1574 if (c == expected_delimiter)
1575 expected_delimiter = '\0';
1579 expected_delimiter = c;
1581 /*@switchbreak@*/ break;
1584 if (p < limit && (expected_delimiter != '\0'))
1586 /* In a string, backslash goes through
1587 and makes next char ordinary. */
1590 /*@switchbreak@*/ break;
1593 /* An '@' in a string or character constant stands for itself,
1594 and does not need to be escaped. */
1595 if (expected_delimiter == '\0')
1600 /*@switchbreak@*/ break;
1603 /* # is ordinary inside a string. */
1604 if (expected_delimiter != '\0')
1606 /*@switchbreak@*/ break;
1609 if (p < limit && *p == '#') {
1610 /* ##: concatenate preceding and following tokens. */
1611 /* Take out the first #, discard preceding whitespace. */
1615 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1621 /* Skip the second #. */
1623 /* Discard following whitespace. */
1624 SKIP_WHITE_SPACE (p);
1628 cppReader_errorLit (pfile,
1629 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1631 } else if (nargs >= 0) {
1632 /* Single #: stringify following argument ref.
1633 Don't leave the # in the expansion. */
1635 SKIP_WHITE_SPACE (p);
1636 if (p == limit || ! is_idstart[(int) *p]
1637 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1638 cppReader_errorLit (pfile,
1639 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1646 /*@switchbreak@*/ break;
1649 /* In -traditional mode, recognize arguments inside strings and
1650 and character constants, and ignore special properties of #.
1651 Arguments inside strings are considered "stringified", but no
1652 extra quote marks are supplied. */
1656 if (expected_delimiter != '\0') {
1657 if (c == expected_delimiter)
1658 expected_delimiter = '\0';
1660 expected_delimiter = c;
1661 /*@switchbreak@*/ break;
1664 /* Backslash quotes delimiters and itself, but not macro args. */
1665 if (expected_delimiter != '\0' && p < limit
1666 && (*p == expected_delimiter || *p == '\\')) {
1670 /*@switchbreak@*/ break;
1673 if (expected_delimiter != '\0') /* No comments inside strings. */
1674 /*@switchbreak@*/ break;
1676 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1677 this must be -traditional. So replace the comment with
1681 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1686 /*@switchbreak@*/ break;
1690 /* Handle the start of a symbol. */
1691 if (is_idchar[(int) c] && nargs > 0) {
1692 char *id_beg = p - 1;
1696 while (p != limit && is_idchar[(int) *p])
1701 id_len = p - id_beg;
1703 if (is_idstart[(int) c]
1704 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1705 register struct arglist *arg;
1707 for (arg = arglist; arg != NULL; arg = arg->next) {
1708 struct reflist *tpat;
1710 if (arg->name[0] == c
1711 && arg->length == id_len
1712 && strncmp (arg->name, id_beg, size_fromInt (id_len)) == 0) {
1715 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1716 if (cppReader_isTraditional (pfile)) {
1717 cppReader_warning (pfile,
1718 message ("macro argument `%x' is stringified.",
1719 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1721 cppReader_warning (pfile,
1722 message ("macro arg `%x' would be stringified with -traditional.",
1723 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1726 /* If ANSI, don't actually substitute inside a string. */
1727 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1728 /*@innerbreak@*/ break;
1729 /* make a pat node for this arg and append it to the end of
1731 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1733 tpat->raw_before = (concat == id_beg);
1734 tpat->raw_after = 0;
1735 tpat->rest_args = arg->rest_args;
1736 tpat->stringify = (cppReader_isTraditional (pfile)
1737 ? expected_delimiter != '\0'
1738 : stringify == id_beg);
1742 defn->pattern = tpat;
1746 endpat->next = tpat;
1748 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1752 tpat->argno = arg->argno;
1753 tpat->nchars = exp_p - lastp;
1757 SKIP_WHITE_SPACE (p1);
1759 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1761 tpat->raw_after = 1;
1764 lastp = exp_p; /* place to start copying from next time */
1767 /*@innerbreak@*/ break;
1772 /* If this was not a macro arg, copy it into the expansion. */
1773 if (skipped_arg == 0) {
1774 register char *lim1 = p;
1782 if (stringify == id_beg)
1783 cppReader_errorLit (pfile,
1784 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1789 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1791 /* If ANSI, put in a "@ " marker to prevent token pasting.
1792 But not if "inside a string" (which in ANSI mode
1793 happens only for -D option). */
1800 defn->length = size_fromInt (exp_p - defn->expansion);
1802 /* Crash now if we overrun the allocated size. */
1803 if (defn->length + 1 > maxsize)
1805 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1812 * special extension string that can be added to the last macro argument to
1813 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1814 * #define wow(a, b...) process (b, a, b)
1815 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1816 * { wow (one, two); } -> { process (two, one, two); }
1817 * if this "rest_arg" is used with the concat token '##' and if it is not
1818 * supplied then the token attached to with ## will not be outputted. Ex:
1819 * #define wow (a, b...) process (b ## , a, ## b)
1820 * { wow (1, 2); } -> { process (2, 1, 2); }
1821 * { wow (one); } -> { process (one); {
1824 /*@-readonlytrans@*/
1825 static char rest_extension[] = "...";
1826 /*:=readonlytrans@*/
1829 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1831 /* Create a DEFINITION node from a #define directive. Arguments are
1832 as for do_define. */
1834 static /*@null@*/ MACRODEF
1835 create_definition (char *buf, char *limit,
1836 cppReader *pfile, bool predefinition,
1839 char *bp; /* temp ptr into input buffer */
1840 char *symname; /* remember where symbol name starts */
1841 int sym_length; /* and how long it is */
1842 int rest_args = 0; /* really int! */
1845 cstring file = (CPPBUFFER (pfile) != NULL)
1846 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
1848 int arglengths = 0; /* Accumulate lengths of arg names
1849 plus number of args. */
1852 cppBuffer_lineAndColumn (CPPBUFFER (pfile), &line, &col);
1856 while (is_hor_space[(int) *bp])
1861 symname = bp; /* remember where it starts */
1863 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
1867 /* Lossage will occur if identifiers or control keywords are broken
1868 across lines using backslash. This is not the right place to take
1872 struct arglist *arg_ptrs = NULL;
1875 bp++; /* skip '(' */
1876 SKIP_WHITE_SPACE (bp);
1878 /* Loop over macro argument names. */
1881 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
1883 temp->next = arg_ptrs;
1884 temp->argno = argno++;
1885 temp->rest_args = 0;
1891 cppReader_pedwarn (pfile,
1892 message ("another parameter follows `%s'",
1893 cstring_fromChars (rest_extension)));
1896 if (!is_idstart[(int) *bp])
1898 cppReader_pedwarnLit (pfile,
1899 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
1902 /* Find the end of the arg name. */
1903 while (is_idchar[(int) *bp])
1906 /* do we have a "special" rest-args extension here? */
1907 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
1908 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
1911 temp->rest_args = 1;
1912 /*@innerbreak@*/ break;
1916 temp->length = bp - temp->name;
1920 bp += REST_EXTENSION_LENGTH;
1923 arglengths += temp->length + 2;
1924 SKIP_WHITE_SPACE (bp);
1926 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1927 cppReader_errorLit (pfile,
1928 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
1934 SKIP_WHITE_SPACE (bp);
1937 cppReader_errorLit (pfile,
1938 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
1942 struct arglist *otemp;
1944 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1946 if (temp->length == otemp->length &&
1947 strncmp (temp->name, otemp->name, size_fromInt (temp->length)) == 0) {
1948 cstring name = cstring_copyLength (temp->name, temp->length);
1949 cppReader_error (pfile,
1950 message ("duplicate argument name `%x' in `#define'", name));
1957 ++bp; /* skip paren */
1958 SKIP_WHITE_SPACE (bp);
1959 /* now everything from bp before limit is the definition. */
1960 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1961 defn->rest_args = rest_args;
1963 /* Now set defn->args.argnames to the result of concatenating
1964 the argument names in reverse order
1965 with comma-space between them. */
1966 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
1969 struct arglist *temp;
1971 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
1972 memcpy (&defn->args.argnames[i], temp->name, size_fromInt (temp->length));
1974 if (temp->next != 0) {
1975 defn->args.argnames[i++] = ',';
1976 defn->args.argnames[i++] = ' ';
1980 defn->args.argnames[i] = '\0';
1985 /* Simple expansion or empty definition. */
1989 if (is_hor_space[(int) *bp]) {
1991 SKIP_WHITE_SPACE (bp);
1994 case '!': case '"': case '#': case '%': case '&': case '\'':
1995 case ')': case '*': case '+': case ',': case '-': case '.':
1996 case '/': case ':': case ';': case '<': case '=': case '>':
1997 case '?': case '[': case '\\': case ']': case '^': case '{':
1998 case '|': case '}': case '~':
1999 cppReader_warning (pfile,
2000 message ("Missing white space after #define %x",
2001 cstring_prefix (cstring_fromChars (symname),
2006 cppReader_pedwarn (pfile,
2007 message ("Missing white space after #define %x",
2008 cstring_prefix (cstring_fromChars (symname),
2014 /* now everything from bp before limit is the definition. */
2015 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2016 defn->args.argnames = mstring_createEmpty ();
2019 defn->noExpand = noExpand;
2020 DPRINTF (("No expand: %d", noExpand));
2024 /* not: llassert (cstring_isUndefined (defn->file)); */
2027 /* OP is null if this is a predefinition */
2028 defn->predefined = predefinition;
2030 mdef.symnam = symname;
2031 mdef.symlen = sym_length;
2041 /* Check a purported macro name SYMNAME, and yield its length.
2042 USAGE is the kind of name this is intended for. */
2044 int cppReader_checkMacroName (cppReader *pfile,
2051 for (p = symname; is_idchar[(int) *p]; p++)
2056 sym_length = size_fromInt (p - symname);
2059 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
2060 cppReader_error (pfile, message ("invalid %s name", usage));
2061 else if (!is_idstart[(int) *symname])
2063 char *msg = (char *) dmalloc (sym_length + 1);
2064 memcpy (msg, symname, sym_length);
2065 msg[sym_length] = '\0';
2066 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2067 cstring_fromChars (msg)));
2072 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2074 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2078 return size_toInt (sym_length);
2081 /* Return zero if two DEFINITIONs are isomorphic. */
2084 compare_defs (DEFINITION *d1, DEFINITION *d2)
2086 register struct reflist *a1, *a2;
2087 register char *p1 = d1->expansion;
2088 register char *p2 = d2->expansion;
2091 if (d1->nargs != d2->nargs)
2096 llassert (d1->args.argnames != NULL);
2097 llassert (d2->args.argnames != NULL);
2099 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2104 for (a1 = d1->pattern, a2 = d2->pattern;
2105 (a1 != NULL) && (a2 != NULL);
2106 a1 = a1->next, a2 = a2->next) {
2107 if (!((a1->nchars == a2->nchars
2108 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2109 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2110 || a1->argno != a2->argno
2111 || a1->stringify != a2->stringify
2112 || a1->raw_before != a2->raw_before
2113 || a1->raw_after != a2->raw_after)
2122 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2123 p2, d2->length - (p2 - d2->expansion), 1))
2129 /* Return TRUE if two parts of two macro definitions are effectively different.
2130 One of the parts starts at BEG1 and has LEN1 chars;
2131 the other has LEN2 chars at BEG2.
2132 Any sequence of whitespace matches any other sequence of whitespace.
2133 FIRST means these parts are the first of a macro definition;
2134 so ignore leading whitespace entirely.
2135 LAST means these parts are the last of a macro definition;
2136 so ignore trailing whitespace entirely. */
2139 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2141 char *end1 = beg1 + len1;
2142 char *end2 = beg2 + len2;
2145 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2146 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2149 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2150 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2152 while (beg1 != end1 && beg2 != end2) {
2153 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2154 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2155 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2156 } else if (*beg1 == *beg2) {
2160 return (beg1 != end1) || (beg2 != end2);
2163 /* Process a #define command.
2164 BUF points to the contents of the #define command, as a contiguous string.
2165 LIMIT points to the first character past the end of the definition.
2166 KEYWORD is the keyword-table entry for #define,
2167 or NULL for a "predefined" macro. */
2170 do_defineAux (cppReader *pfile, struct directive *keyword,
2171 char *buf, char *limit, bool noExpand)
2177 DPRINTF (("Define aux: %d", noExpand));
2179 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2184 hashcode = hashf (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2186 DPRINTF (("Macro: %s / %s",
2187 cstring_copyLength (mdef.symnam, mdef.symlen),
2188 bool_unparse (noExpand)));
2190 if ((hp = cppReader_lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL)
2194 /* Redefining a precompiled key is ok. */
2195 if (hp->type == T_PCSTRING)
2197 /* Redefining a macro is ok if the definitions are the same. */
2198 else if (hp->type == T_MACRO)
2199 ok = !compare_defs (mdef.defn, hp->value.defn);
2200 /* Redefining a constant is ok with -D. */
2201 else if (hp->type == T_CONST)
2202 ok = !CPPOPTIONS (pfile)->done_initializing;
2207 /* Print the warning if it's not ok. */
2211 ** If we are passing through #define and #undef directives, do
2212 ** that for this re-definition now.
2215 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2217 /* llassert (keyword != NULL); */
2218 pass_thru_directive (buf, limit, pfile, keyword);
2221 cpp_setLocation (pfile);
2223 if (hp->type == T_MACRO)
2225 if (hp->value.defn->noExpand)
2227 ; /* error will be reported checking macros */
2233 message ("Macro %q already defined",
2234 cstring_copyLength (mdef.symnam, mdef.symlen)),
2235 message ("%q: Previous definition of %q",
2236 fileloc_unparseRaw (hp->value.defn->file,
2237 (int) hp->value.defn->line),
2238 cstring_copyLength (mdef.symnam, mdef.symlen)));
2243 genppllerror (FLG_MACROREDEF,
2244 message ("Macro %q already defined",
2245 cstring_copyLength (mdef.symnam,
2251 /* Replace the old definition. */
2253 hp->value.defn = mdef.defn;
2258 ** If we are passing through #define and #undef directives, do
2259 ** that for this new definition now.
2264 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2266 pass_thru_directive (buf, limit, pfile, keyword);
2269 DPRINTF (("Define macro: %s / %d",
2270 mdef.symnam, mdef.defn->noExpand));
2272 hn = cppReader_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2274 } /*@=branchstate@*/
2284 do_define (cppReader *pfile, struct directive *keyword,
2285 char *buf, char *limit)
2287 DPRINTF (("Regular do define"));
2288 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2291 /* This structure represents one parsed argument in a macro call.
2292 `raw' points to the argument text as written (`raw_length' is its length).
2293 `expanded' points to the argument's macro-expansion
2294 (its length is `expand_length').
2295 `stringified_length' is the length the argument would have
2297 `use_count' is the number of times this macro arg is substituted
2298 into the macro. If the actual use count exceeds 10,
2299 the value stored is 10. */
2301 /* raw and expanded are relative to ARG_BASE */
2303 #define ARG_BASE ((pfile)->token_buffer)
2306 /* Strings relative to pfile->token_buffer */
2312 int stringified_length;
2317 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2318 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2319 as the new input buffer.
2320 Return the new buffer, or NULL on failure. */
2322 /*@null@*/ /*@exposed@*/ cppBuffer *
2323 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2325 cppBuffer *buf = cppReader_getBuffer (pfile);
2327 if (buf == pfile->buffer_stack)
2329 cppReader_fatalError
2331 message ("%s: macro or `#include' recursion too deep",
2332 (buf->fname != NULL)
2334 : cstring_makeLiteral ("<no name>")));
2335 sfreeEventually (buffer);
2339 llassert (buf != NULL);
2342 memset ((char *) buf, 0, sizeof (*buf));
2343 CPPBUFFER (pfile) = buf;
2345 buf->if_stack = pfile->if_stack;
2346 buf->cleanup = cppReader_nullCleanup;
2347 buf->underflow = cppReader_nullUnderflow;
2349 buf->cur = buf->buf;
2353 buf->alimit = buf->rlimit = buffer + length;
2357 buf->alimit = buf->rlimit = NULL;
2364 cppReader_popBuffer (cppReader *pfile)
2366 cppBuffer *buf = CPPBUFFER (pfile);
2368 llassert (buf != NULL);
2370 (void) (*buf->cleanup) (buf, pfile);
2371 return ++CPPBUFFER (pfile);
2374 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2375 Pop the buffer when done. */
2378 cppReader_scanBuffer (cppReader *pfile)
2380 cppBuffer *buffer = CPPBUFFER (pfile);
2383 enum cpp_token token;
2385 token = cppGetToken (pfile);
2387 if (token == CPP_EOF) /* Should not happen ... */
2392 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2394 (void) cppReader_popBuffer (pfile);
2401 * Rescan a string (which may have escape marks) into pfile's buffer.
2402 * Place the result in pfile->token_buffer.
2404 * The input is copied before it is scanned, so it is safe to pass
2405 * it something from the token_buffer that will get overwritten
2406 * (because it follows cppReader_getWritten). This is used by do_include.
2410 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
2412 register cppBuffer *ip;
2413 char *limit = buf + length;
2414 char *buf1, *p1, *p2;
2419 /* Set up the input on the input stack. */
2421 buf1 = (char *) dmalloc (length + 1);
2431 buf1[length] = '\0';
2433 ip = cppReader_pushBuffer (pfile, buf1, length);
2438 ip->has_escapes = TRUE;
2440 /* Scan the input, create the output. */
2441 cppReader_scanBuffer (pfile);
2443 cppReader_nullTerminate (pfile);
2447 adjust_position (char *buf, char *limit, int *linep, int *colp)
2453 (*linep)++, (*colp) = 1;
2459 /* Move line_base forward, updating lineno and colno. */
2462 update_position (cppBuffer *pbuf)
2465 char *new_pos = pbuf->cur;
2466 register struct parse_marker *mark;
2468 llassert (pbuf->buf != NULL);
2469 old_pos = pbuf->buf + pbuf->line_base;
2471 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2473 if (pbuf->buf + mark->position < new_pos)
2474 new_pos = pbuf->buf + mark->position;
2476 pbuf->line_base += new_pos - old_pos;
2478 llassert (old_pos != NULL);
2479 llassert (new_pos != NULL);
2481 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2485 cppBuffer_lineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
2486 /*@null@*/ /*@out@*/ int *colp)
2494 } /*@=branchstate@*/
2498 *linep = pbuf->lineno;
2499 *colp = pbuf->colno;
2501 llassert (pbuf->buf != NULL);
2502 llassert (pbuf->cur != NULL);
2504 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2513 /* Return the cppBuffer that corresponds to a file (not a macro). */
2515 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
2517 cppBuffer *ip = cppReader_getBuffer (pfile);
2520 ip != NULL && ip != cppReader_nullBuffer (pfile);
2521 ip = cppBuffer_prevBuffer (ip))
2523 if (ip->fname != NULL)
2533 count_newlines (char *buf, char *limit)
2535 register long count = 0;
2547 * write out a #line command, for instance, after an #include file.
2548 * If CONDITIONAL is nonzero, we can omit the #line if it would
2549 * appear to be a no-op, and we can output a few newlines instead
2550 * if we want to increase the line number by a small amount.
2551 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2555 output_line_command (cppReader *pfile, bool conditional,
2556 enum file_change_code file_change)
2559 cppBuffer *ip = CPPBUFFER (pfile);
2562 llassert (ip != NULL);
2564 if (ip->fname == NULL)
2567 update_position (ip);
2569 if (CPPOPTIONS (pfile)->no_line_commands
2570 || CPPOPTIONS (pfile)->no_output)
2573 buf = CPPBUFFER (pfile);
2575 llassert (buf != NULL);
2580 llassert (ip->cur != NULL);
2582 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2584 if (CPPOPTIONS (pfile)->no_line_commands)
2588 if (line == pfile->lineno)
2591 /* If the inherited line number is a little too small,
2592 output some newlines instead of a #line command. */
2594 if (line > pfile->lineno && line < pfile->lineno + 8)
2596 cppReader_reserve (pfile, 20);
2597 while (line > pfile->lineno)
2599 cppReader_putCharQ (pfile, '\n');
2607 cppReader_reserve (pfile,
2608 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
2611 #ifdef OUTPUT_LINE_COMMANDS
2612 static char sharp_line[] = "#line ";
2614 static char sharp_line[] = "# ";
2616 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
2619 sprintf (cppReader_getPWritten (pfile), "%d ", line);
2620 cppReader_adjustWritten (pfile, strlen (cppReader_getPWritten (pfile)));
2622 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
2624 if (file_change != same_file) {
2625 cppReader_putCharQ (pfile, ' ');
2626 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
2628 /* Tell cc1 if following text comes from a system header file. */
2629 if (ip->system_header_p != '\0') {
2630 cppReader_putCharQ (pfile, ' ');
2631 cppReader_putCharQ (pfile, '3');
2633 #ifndef NO_IMPLICIT_EXTERN_C
2634 /* Tell cc1plus if following text should be treated as C. */
2635 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
2636 cppReader_putCharQ (pfile, ' ');
2637 cppReader_putCharQ (pfile, '4');
2640 cppReader_putCharQ (pfile, '\n');
2641 pfile->lineno = line;
2646 * Parse a macro argument and append the info on PFILE's token_buffer.
2647 * REST_ARGS means to absorb the rest of the args.
2648 * Return nonzero to indicate a syntax error.
2651 static enum cpp_token
2652 macarg (cppReader *pfile, int rest_args)
2655 enum cpp_token token;
2656 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
2657 bool oldexpand = pfile->no_macro_expand;
2658 CPPOPTIONS (pfile)->put_out_comments = 1;
2660 /* Try to parse as much of the argument as exists at this
2661 input stack level. */
2663 pfile->no_macro_expand = TRUE;
2667 token = cppGetToken (pfile);
2674 /* If we've hit end of file, it's an error (reported by caller).
2675 Ditto if it's the end of cpp_expand_to_buffer text.
2676 If we've hit end of macro, just continue. */
2677 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
2679 /*@switchbreak@*/ break;
2682 /*@switchbreak@*/ break;
2686 /*@switchbreak@*/ break;
2688 /* if we've returned to lowest level and
2689 we aren't absorbing all args */
2690 if (paren == 0 && rest_args == 0)
2692 /*@switchbreak@*/ break;
2694 /* Remove ',' or ')' from argument buffer. */
2695 cppReader_adjustWritten (pfile, -1);
2703 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
2704 pfile->no_macro_expand = oldexpand;
2710 /* Turn newlines to spaces in the string of length LENGTH at START,
2711 except inside of string constants.
2712 The string is copied into itself with its beginning staying fixed. */
2715 change_newlines (char *start, int length)
2719 register char *limit;
2723 limit = start + length;
2726 while (ibp < limit) {
2727 *obp++ = c = *ibp++;
2732 /* Notice and skip strings, so that we don't delete newlines in them. */
2735 while (ibp < limit) {
2736 *obp++ = c = *ibp++;
2738 /*@innerbreak@*/ break;
2739 if (c == '\n' && quotec == '\'')
2740 /*@innerbreak@*/ break;
2743 /*@switchbreak@*/ break;
2750 static /*@observer@*/ struct tm *
2751 timestamp (/*@returned@*/ cppReader *pfile)
2753 if (pfile->timebuf == NULL)
2755 time_t t = time ((time_t *) 0);
2756 pfile->timebuf = localtime (&t);
2759 llassert (pfile->timebuf != NULL);
2761 return pfile->timebuf;
2764 static ob_mstring monthnames[] = {
2765 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2766 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2770 * expand things like __FILE__. Place the expansion into the output
2771 * buffer *without* rescanning.
2775 special_symbol (hashNode hp, cppReader *pfile)
2777 cstring buf = cstring_undefined;
2783 int paren = 0; /* For special `defined' keyword */
2785 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
2787 if (ip == cppReader_nullBuffer (pfile))
2789 cppReader_errorLit (pfile,
2790 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
2791 return; /* the show must go on */
2794 if (ip != NULL && ip->fname != NULL)
2806 if (hp->type == T_BASE_FILE)
2808 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
2810 ip = cppBuffer_prevBuffer (ip);
2814 llassert (ip != NULL);
2815 string = cstring_toCharsSafe (ip->nominal_fname);
2822 cppReader_reserve (pfile, 3 + 4 * strlen (string));
2823 quote_string (pfile, string);
2827 case T_INCLUDE_LEVEL:
2829 ip = cppReader_getBuffer (pfile);
2831 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
2832 ip = cppBuffer_prevBuffer (ip))
2834 if (ip != NULL && ip->fname != NULL)
2840 buf = message ("%d", true_indepth - 1);
2844 buf = message ("\"%s\"", cstring_makeLiteralTemp (CPP_VERSION));
2847 #ifndef NO_BUILTIN_SIZE_TYPE
2849 buf = cstring_makeLiteral (SIZE_TYPE);
2853 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2854 case T_PTRDIFF_TYPE:
2855 buf = cstring_makeLiteral (PTRDIFF_TYPE);
2860 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
2863 case T_USER_LABEL_PREFIX_TYPE:
2864 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
2867 case T_REGISTER_PREFIX_TYPE:
2868 buf = cstring_makeLiteral (REGISTER_PREFIX);
2872 buf = message ("%d", hp->value.ival);
2879 int line = ip->lineno;
2880 int col = ip->colno;
2882 llassert (ip->cur != NULL);
2883 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2885 buf = message ("%d", (int) line);
2897 char *sbuf = (char *) dmalloc (20);
2898 timebuf = timestamp (pfile);
2899 if (hp->type == T_DATE)
2901 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2902 timebuf->tm_mday, timebuf->tm_year + 1900);
2906 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2910 buf = cstring_fromCharsNew (sbuf);
2915 case T_SPEC_DEFINED:
2916 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
2917 ip = cppReader_getBuffer (pfile);
2919 llassert (ip->cur != NULL);
2920 SKIP_WHITE_SPACE (ip->cur);
2922 if (*ip->cur == '(')
2925 ip->cur++; /* Skip over the paren */
2926 SKIP_WHITE_SPACE (ip->cur);
2929 if (!is_idstart[(int) *ip->cur])
2931 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2934 if ((hp = cppReader_lookup (ip->cur, -1, -1)) != 0)
2937 buf = cstring_makeLiteral (" 1 ");
2940 while (is_idchar[(int) *ip->cur])
2945 SKIP_WHITE_SPACE (ip->cur);
2949 if (*ip->cur != ')')
2957 cppReader_errorLit (pfile,
2958 cstring_makeLiteralTemp ("`defined' without an identifier"));
2962 cpp_setLocation (pfile);
2963 llfatalerror (message ("Pre-processing error: invalid special hash type"));
2966 len = size_fromInt (cstring_length (buf));
2968 cppReader_reserve (pfile, len + 1);
2969 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
2970 cppReader_nullTerminateQ (pfile);
2976 /* Write out a #define command for the special named MACRO_NAME
2977 to PFILE's token_buffer. */
2980 dump_special_to_buffer (cppReader *pfile, char *macro_name)
2982 static char define_directive[] = "#define ";
2983 size_t macro_name_length = strlen (macro_name);
2984 output_line_command (pfile, 0, same_file);
2985 cppReader_reserve (pfile, sizeof(define_directive) + macro_name_length);
2986 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
2987 cppReader_putStrN (pfile, macro_name, macro_name_length);
2988 cppReader_putCharQ (pfile, ' ');
2989 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2990 cppReader_putChar (pfile, '\n');
2993 /* Initialize the built-in macros. */
2996 cppReader_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
2997 int len, enum node_type type,
2998 int ivalue, /*@null@*/ /*@only@*/ char *value,
3001 cstring sname = cstring_fromCharsNew (name);
3003 llassert (usymtab_inGlobalScope ());
3006 ** Be careful here: this is done before the ctype table has
3007 ** been initialized.
3010 if (!usymtab_exists (sname))
3012 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3014 if (ctype_equal (ctyp, ctype_string))
3016 qualList ql = qualList_new ();
3017 ql = qualList_add (ql, qual_createObserver ());
3018 uentry_reflectQualifiers (ue, ql);
3022 usymtab_addGlobalEntry (ue);
3029 (void) cppReader_install (name, len, type, ivalue, value, hash);
3030 cstring_free (sname);
3034 cppReader_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3035 int len, enum node_type type,
3037 /*@only@*/ /*@null@*/ char *value, int hash)
3039 cstring sname = cstring_fromChars (name);
3040 /* evs 2000 07 10 - removed a memory leak, detected by lclint */
3042 llassert (usymtab_inGlobalScope ());
3044 if (!usymtab_existsTypeEither (sname))
3046 uentry ue = uentry_makeDatatype (sname, ctyp,
3048 fileloc_createBuiltin ());
3049 llassert (!usymtab_existsEither (sname));
3050 usymtab_addGlobalEntry (ue);
3053 (void) cppReader_install (name, len, type, ivalue, value, hash);
3057 initialize_builtins (cppReader *pfile)
3059 cppReader_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3060 cppReader_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3061 cppReader_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3062 cppReader_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3063 cppReader_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3064 cppReader_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3065 #ifndef NO_BUILTIN_SIZE_TYPE
3066 cppReader_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3068 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3069 cppReader_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3071 cppReader_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3072 cppReader_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3073 cppReader_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3074 cppReader_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3077 ** No, don't define __STDC__
3080 if (!cppReader_isTraditional (pfile))
3082 cppReader_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3089 cppReader_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3093 ** This is supplied using a -D by the compiler driver
3094 ** so that it is present only when truly compiling with GNU C.
3097 /* cppReader_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3099 cppReader_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3101 if (CPPOPTIONS (pfile)->debug_output)
3103 dump_special_to_buffer (pfile, "__BASE_FILE__");
3104 dump_special_to_buffer (pfile, "__VERSION__");
3105 #ifndef NO_BUILTIN_SIZE_TYPE
3106 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3108 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3109 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3111 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3112 dump_special_to_buffer (pfile, "__DATE__");
3113 dump_special_to_buffer (pfile, "__TIME__");
3114 if (!cppReader_isTraditional (pfile))
3115 dump_special_to_buffer (pfile, "__STDC__");
3120 /* Return 1 iff a token ending in C1 followed directly by a token C2
3121 could cause mis-tokenization. */
3124 unsafe_chars (char c1, char c2)
3129 if (c2 == c1 || c2 == '=')
3133 case '0': case '1': case '2': case '3': case '4':
3134 case '5': case '6': case '7': case '8': case '9':
3135 case 'e': case 'E': case 'p': case 'P':
3136 if (c2 == '-' || c2 == '+')
3137 return 1; /* could extend a pre-processing number */
3140 if (c2 == '\'' || c2 == '\"')
3141 return 1; /* Could turn into L"xxx" or L'xxx'. */
3145 case 'a': case 'b': case 'c': case 'd': case 'f':
3146 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3147 case 'm': case 'n': case 'o': case 'q': case 'r':
3148 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3150 case 'A': case 'B': case 'C': case 'D': case 'F':
3151 case 'G': case 'H': case 'I': case 'J': case 'K':
3152 case 'M': case 'N': case 'O': case 'Q': case 'R':
3153 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3155 /* We're in the middle of either a name or a pre-processing number. */
3156 return (is_idchar[(int) c2] || c2 == '.');
3157 case '<': case '>': case '!': case '%': case '#': case ':':
3158 case '^': case '&': case '|': case '*': case '/': case '=':
3159 return (c2 == c1 || c2 == '=');
3164 /* Expand a macro call.
3165 HP points to the symbol that is the macro being called.
3166 Put the result of expansion onto the input stack
3167 so that subsequent input by our caller will use it.
3169 If macro wants arguments, caller has already verified that
3170 an argument list follows; arguments come from the input stack. */
3173 macroexpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3176 DEFINITION *defn = hp->value.defn;
3182 size_t old_written = cppReader_getWritten (pfile);
3186 struct argdata *args = NULL;
3188 pfile->output_escapes++;
3190 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3192 nargs = defn->nargs;
3196 enum cpp_token token = CPP_EOF;
3198 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3200 for (i = 0; i < nargs; i++)
3202 args[i].expanded = 0;
3204 args[i].raw_length = 0;
3205 args[i].expand_length = args[i].stringified_length = -1;
3206 args[i].use_count = 0;
3210 ** Parse all the macro args that are supplied. I counts them.
3211 ** The first NARGS args are stored in ARGS.
3212 ** The rest are discarded. If rest_args is set then we assume
3213 ** macarg absorbed the rest of the args.
3219 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3227 if (i < nargs || (nargs == 0 && i == 0))
3229 /* if we are working on last arg which absorbs rest of args... */
3230 if (i == nargs - 1 && defn->rest_args)
3235 args[i].raw = size_toLong (cppReader_getWritten (pfile));
3236 token = macarg (pfile, rest_args);
3237 args[i].raw_length = cppReader_getWritten (pfile) - args[i].raw;
3238 args[i].newlines = FALSE; /* FIXME */
3242 token = macarg (pfile, 0);
3245 if (token == CPP_EOF || token == CPP_POP)
3247 cppReader_errorWithLine (pfile, start_line, start_column,
3248 cstring_fromCharsNew ("unterminated macro call"));
3253 } while (token == CPP_COMMA);
3255 /* If we got one arg but it was just whitespace, call that 0 args. */
3263 bp = ARG_BASE + args[0].raw;
3264 lim = bp + args[0].raw_length;
3266 /* cpp.texi says for foo ( ) we provide one argument.
3267 However, if foo wants just 0 arguments, treat this as 0. */
3271 while (bp != lim && is_space[(int) *bp])
3281 /* Don't output an error message if we have already output one for
3282 a parse error above. */
3285 if (nargs == 0 && i > 0)
3287 cppReader_error (pfile,
3288 message ("arguments given to macro `%s'", hp->name));
3292 /* traditional C allows foo() if foo wants one argument. */
3293 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
3297 /* the rest args token is allowed to absorb 0 tokens */
3298 else if (i == nargs - 1 && defn->rest_args)
3301 cppReader_error (pfile,
3302 message ("macro `%s' used without args", hp->name));
3304 cppReader_error (pfile,
3305 message ("macro `%s' used with just one arg", hp->name));
3308 cppReader_error (pfile,
3309 message ("macro `%s' used with only %d args",
3315 cppReader_error (pfile,
3316 message ("macro `%s' used with too many (%d) args", hp->name, i));
3324 /* If macro wants zero args, we parsed the arglist for checking only.
3325 Read directly from the macro definition. */
3329 xbuf = defn->expansion;
3330 xbuf_len = defn->length;
3334 char *exp = defn->expansion;
3335 int offset; /* offset in expansion,
3336 copied a piece at a time */
3337 size_t totlen; /* total amount of exp buffer filled so far */
3339 register struct reflist *ap, *last_ap;
3341 assertSet (args); /* args is defined since the nargs > 0 path was taken */
3343 /* Macro really takes args. Compute the expansion of this call. */
3345 /* Compute length in characters of the macro's expansion.
3346 Also count number of times each arg is used. */
3347 xbuf_len = defn->length;
3349 llassert (args != NULL);
3351 for (ap = defn->pattern; ap != NULL; ap = ap->next)
3355 struct argdata *arg = &args[ap->argno];
3357 /* Stringify it it hasn't already been */
3360 if (arg->stringified_length < 0)
3362 int arglen = arg->raw_length;
3363 bool escaped = FALSE;
3364 char in_string = '\0';
3367 /* Initially need_space is -1. Otherwise, 1 means the
3368 previous character was a space, but we suppressed it;
3369 0 means the previous character was a non-space. */
3370 int need_space = -1;
3373 arg->stringified = cppReader_getWritten (pfile);
3374 if (!cppReader_isTraditional (pfile))
3375 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
3376 for (; i < arglen; i++)
3378 c = (ARG_BASE + arg->raw)[i];
3380 if (in_string == '\0')
3382 /* Internal sequences of whitespace are replaced by
3383 one space except within an string or char token.*/
3384 if (is_space[(int) c])
3386 if (cppReader_getWritten (pfile) > arg->stringified
3387 && (cppReader_getPWritten (pfile))[-1] == '@')
3389 /* "@ " escape markers are removed */
3390 cppReader_adjustWritten (pfile, -1);
3391 /*@innercontinue@*/ continue;
3393 if (need_space == 0)
3395 /*@innercontinue@*/ continue;
3397 else if (need_space > 0)
3398 cppReader_putChar (pfile, ' ');
3414 if (in_string != '\0')
3419 else if (c == '\"' || c == '\'')
3429 /* Escape these chars */
3430 if (c == '\"' || (in_string != '\0' && c == '\\'))
3431 cppReader_putChar (pfile, '\\');
3433 cppReader_putChar (pfile, c);
3436 cppReader_reserve (pfile, 4);
3437 sprintf (cppReader_getPWritten (pfile), "\\%03o",
3439 cppReader_adjustWritten (pfile, 4);
3442 if (!cppReader_isTraditional (pfile))
3443 cppReader_putChar (pfile, '\"'); /* insert ending quote */
3444 arg->stringified_length
3445 = size_toInt (cppReader_getWritten (pfile) - arg->stringified);
3448 xbuf_len += args[ap->argno].stringified_length;
3450 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3452 /* Add 4 for two newline-space markers to prevent
3453 token concatenation. */
3454 assertSet (args); /*@i534 shouldn't need this */
3455 xbuf_len += args[ap->argno].raw_length + 4;
3459 /* We have an ordinary (expanded) occurrence of the arg.
3460 So compute its expansion, if we have not already. */
3462 assertSet (args); /*@i534 shouldn't need this */
3464 if (args[ap->argno].expand_length < 0)
3466 args[ap->argno].expanded = cppReader_getWritten (pfile);
3467 cpp_expand_to_buffer (pfile,
3468 ARG_BASE + args[ap->argno].raw,
3469 size_fromInt (args[ap->argno].raw_length));
3471 args[ap->argno].expand_length
3472 = size_toInt (cppReader_getWritten (pfile) - args[ap->argno].expanded);
3475 /* Add 4 for two newline-space markers to prevent
3476 token concatenation. */
3477 xbuf_len += args[ap->argno].expand_length + 4;
3479 if (args[ap->argno].use_count < 10)
3480 args[ap->argno].use_count++;
3483 xbuf = (char *) dmalloc (xbuf_len + 1);
3487 ** Generate in XBUF the complete expansion
3488 ** with arguments substituted in.
3489 ** TOTLEN is the total size generated so far.
3490 ** OFFSET is the index in the definition
3491 ** of where we are copying from.
3497 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
3498 last_ap = ap, ap = ap->next)
3500 register struct argdata *arg = &args[ap->argno];
3501 size_t count_before = totlen;
3503 /* Add chars to XBUF. */
3504 for (i = 0; i < ap->nchars; i++, offset++)
3506 xbuf[totlen++] = exp[offset];
3509 /* If followed by an empty rest arg with concatenation,
3510 delete the last run of nonwhite chars. */
3511 if (rest_zero && totlen > count_before
3512 && ((ap->rest_args && ap->raw_before)
3513 || (last_ap != NULL && last_ap->rest_args
3514 && last_ap->raw_after)))
3516 /* Delete final whitespace. */
3517 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
3522 /* Delete the nonwhites before them. */
3523 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
3529 if (ap->stringify != 0)
3532 memcpy (xbuf + totlen,
3533 ARG_BASE + arg->stringified,
3534 size_fromInt (arg->stringified_length));
3535 totlen += arg->stringified_length;
3537 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3544 p1 = ARG_BASE + arg->raw;
3545 l1 = p1 + arg->raw_length;
3549 while (p1 != l1 && is_space[(int) *p1])
3554 while (p1 != l1 && is_idchar[(int) *p1])
3556 xbuf[totlen++] = *p1++;
3559 /* Delete any no-reexpansion marker that follows
3560 an identifier at the beginning of the argument
3561 if the argument is concatenated with what precedes it. */
3562 if (p1[0] == '@' && p1[1] == '-')
3567 /* Arg is concatenated after: delete trailing whitespace,
3568 whitespace markers, and no-reexpansion markers. */
3571 if (is_space[(int) l1[-1]]) l1--;
3572 else if (l1[-1] == '-')
3575 /* If a `-' is preceded by an odd number of newlines then it
3576 and the last newline are a no-reexpansion marker. */
3577 while (p2 != p1 && p2[-1] == '\n')
3582 if (((l1 - 1 - p2) & 1) != 0)
3588 /*@innerbreak@*/ break;
3593 /*@innerbreak@*/ break;
3598 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
3606 expanded = ARG_BASE + arg->expanded;
3608 if (!ap->raw_before && totlen > 0
3609 && (arg->expand_length != 0)
3610 && !cppReader_isTraditional(pfile)
3611 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3613 xbuf[totlen++] = '@';
3614 xbuf[totlen++] = ' ';
3617 memcpy (xbuf + totlen, expanded,
3618 size_fromInt (arg->expand_length));
3619 totlen += arg->expand_length;
3621 if (!ap->raw_after && totlen > 0
3622 && offset < size_toInt (defn->length)
3623 && !cppReader_isTraditional(pfile)
3624 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3626 xbuf[totlen++] = '@';
3627 xbuf[totlen++] = ' ';
3630 /* If a macro argument with newlines is used multiple times,
3631 then only expand the newlines once. This avoids creating
3632 output lines which don't correspond to any input line,
3633 which confuses gdb and gcov. */
3634 if (arg->use_count > 1 && arg->newlines > 0)
3636 /* Don't bother doing change_newlines for subsequent
3640 = change_newlines (expanded, arg->expand_length);
3644 if (totlen > xbuf_len)
3648 /* if there is anything left of the definition
3649 after handling the arg list, copy that in too. */
3651 for (i = offset; i < size_toInt (defn->length); i++)
3653 /* if we've reached the end of the macro */
3656 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3657 && last_ap->raw_after))
3658 xbuf[totlen++] = exp[i];
3661 xbuf[totlen] = '\0';
3665 pfile->output_escapes--;
3667 /* Now put the expansion on the input stack
3668 so our caller will commence reading from it. */
3669 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3670 cppReader_getBuffer (pfile)->has_escapes = 1;
3672 /* Pop the space we've used in the token_buffer for argument expansion. */
3673 cppReader_setWritten (pfile, old_written);
3675 /* Recursive macro use sometimes works traditionally.
3676 #define foo(x,y) bar (x (y,0), y)
3679 if (!cppReader_isTraditional (pfile))
3680 hp->type = T_DISABLED;
3686 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
3687 /*@dependent@*/ hashNode hp)
3689 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
3696 mbuf->cleanup = cppReader_macroCleanup;
3698 llassert (mbuf->hnode == NULL);
3701 /* The first chars of the expansion should be a "@ " added by
3702 collect_expansion. This is to prevent accidental token-pasting
3703 between the text preceding the macro invocation, and the macro
3706 We would like to avoid adding unneeded spaces (for the sake of
3707 tools that use cpp, such as imake). In some common cases we can
3708 tell that it is safe to omit the space.
3710 The character before the macro invocation cannot have been an
3711 idchar (or else it would have been pasted with the idchars of
3712 the macro name). Therefore, if the first non-space character
3713 of the expansion is an idchar, we do not need the extra space
3714 to prevent token pasting.
3716 Also, we don't need the extra space if the first char is '(',
3717 or some other (less common) characters. */
3719 if (xbuf[0] == '@' && xbuf[1] == ' '
3720 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3721 || xbuf[2] == '\"'))
3723 llassert (mbuf->cur != NULL);
3729 /* Like cppGetToken, except that it does not read past end-of-line.
3730 Also, horizontal space is skipped, and macros are popped. */
3732 static enum cpp_token
3733 get_directive_token (cppReader *pfile)
3737 size_t old_written = cppReader_getWritten (pfile);
3738 enum cpp_token token;
3739 cppSkipHspace (pfile);
3740 if (cppReader_peekC (pfile) == '\n')
3745 token = cppGetToken (pfile);
3750 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
3755 cppReader_setWritten (pfile, old_written);
3756 /*@switchbreak@*/ break;
3764 /* Handle #include and #import.
3765 This function expects to see "fname" or <fname> on the input.
3767 The input is normally in part of the output_buffer following
3768 cppReader_getWritten, and will get overwritten by output_line_command.
3769 I.e. in input file specification has been popped by cppReader_handleDirective.
3773 static void replace_unixdir_with_windir(char *filename)
3777 while(filename[i] != '\0')
3779 if(filename[i] == '/')
3787 do_include (cppReader *pfile, struct directive *keyword,
3788 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
3790 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3792 char *fbeg, *fend; /* Beginning and end of fname */
3793 enum cpp_token token;
3795 /* Chain of dirs to search */
3796 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
3797 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3798 struct file_name_list *searchptr = NULL;
3799 size_t old_written = cppReader_getWritten (pfile);
3803 int f; /* file number */
3804 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3805 f= -1; /* JF we iz paranoid! */
3807 pfile->parsing_include_directive++;
3808 token = get_directive_token (pfile);
3809 pfile->parsing_include_directive--;
3811 if (token == CPP_STRING)
3813 /* FIXME - check no trailing garbage */
3814 fbeg = pfile->token_buffer + old_written + 1;
3815 fend = cppReader_getPWritten (pfile) - 1;
3816 if (fbeg[-1] == '<')
3819 /* If -I-, start with the first -I dir after the -I-. */
3820 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
3821 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3823 /* If -I- was specified, don't search current dir, only spec'd ones. */
3824 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
3826 cppBuffer *fp = CPPBUFFER (pfile);
3827 /* We have "filename". Figure out directory this source
3828 file is coming from and put it on the front of the list. */
3830 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3835 llassert (fp != NULL);
3839 if (cstring_isDefined (fp->nominal_fname))
3841 nam = cstring_toCharsSafe (fp->nominal_fname);
3843 /* Found a named file. Figure out dir of the file,
3844 and put it in front of the search list. */
3845 dsp[0].next = search_start;
3849 ep = strrchr (nam, CONNECTCHAR);
3851 ep = strrchr (nam, ']');
3852 if (ep == NULL) ep = strrchr (nam, '>');
3853 if (ep == NULL) ep = strrchr (nam, ':');
3854 if (ep != NULL) ep++;
3864 /*@-onlytrans@*/ /* This looks like a memory leak... */
3865 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
3869 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3870 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3874 dsp[0].fname = cstring_undefined; /* Current directory */
3877 dsp[0].got_name_map = 0;
3888 else if (token == CPP_NAME)
3891 * Support '#include xyz' like VAX-C to allow for easy use of all the
3892 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3893 * code from case '<' is repeated here) and generates a warning.
3895 cppReader_warning (pfile,
3896 "VAX-C-style include specification found, use '#include <filename.h>' !");
3898 /* If -I-, start with the first -I dir after the -I-. */
3899 if (CPPOPTIONS (pfile)->first_bracket_include)
3900 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3901 fbeg = pfile->token_buffer + old_written;
3902 fend = cppReader_getPWritten (pfile);
3907 cppReader_error (pfile,
3908 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
3911 cppReader_setWritten (pfile, old_written);
3912 cppReader_skipRestOfLine (pfile);
3918 token = get_directive_token (pfile);
3919 if (token != CPP_VSPACE)
3921 cppReader_errorLit (pfile,
3922 cstring_makeLiteralTemp ("Junk at end of #include"));
3924 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3926 token = get_directive_token (pfile);
3931 ** For #include_next, skip in the search path
3932 ** past the dir in which the containing file was found.
3937 cppBuffer *fp = CPPBUFFER (pfile);
3939 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3941 llassert (fp != NULL);
3943 if (fp->fname != NULL)
3945 /* fp->dir is null if the containing file was specified with
3946 an absolute file name. In that case, don't skip anything. */
3947 if (fp->dir == SELF_DIR_DUMMY)
3949 search_start = CPPOPTIONS (pfile)->include;
3951 else if (fp->dir != NULL)
3953 search_start = fp->dir->next;
3965 cppReader_setWritten (pfile, old_written);
3969 DPRINTF (("fbeg: %s", fbeg));
3973 cppReader_error (pfile,
3974 message ("Empty file name in #%s", keyword->name));
3979 ** Allocate this permanently, because it gets stored in the definitions
3983 fname = cstring_undefined;
3985 /* + 2 above for slash and terminating null. */
3986 /* + 2 added for '.h' on VMS (to support '#include filename') */
3988 /* If specified file name is absolute, just open it. */
3990 if (osd_isConnectChar (*fbeg)
3991 # if defined (WIN32) || defined (OS2)
3992 || (*(fbeg + 1) == ':')
3996 fname = cstring_copyLength (fbeg, flen);
3998 if (redundant_include_p (pfile, fname))
4000 cstring_free (fname);
4004 f = open_include_file (pfile, fname, NULL);
4006 if (f == IMPORT_FOUND)
4008 return 0; /* Already included this file */
4013 /* Search directory path, trying to open the file.
4014 Copy each filename tried into FNAME. */
4016 for (searchptr = search_start; searchptr != NULL;
4017 searchptr = searchptr->next)
4019 if (!cstring_isEmpty (searchptr->fname))
4021 /* The empty string in a search path is ignored.
4022 This makes it possible to turn off entirely
4023 a standard piece of the list. */
4024 if (cstring_isEmpty (searchptr->fname))
4027 fname = cstring_copy (searchptr->fname);
4028 fname = cstring_appendChar (fname, CONNECTCHAR);
4029 DPRINTF (("Here: %s", fname));
4036 fname = cstring_concatLength (fname, fbeg, flen);
4038 DPRINTF (("fname: %s", fname));
4040 /* Win32 directory fix from Kay Buschner. */
4042 /* Fix all unixdir slashes to win dir slashes */
4043 if (searchptr->fname && (searchptr->fname[0] != 0))
4045 replace_unixdir_with_windir(fname);
4050 /* Change this 1/2 Unix 1/2 VMS file specification into a
4051 full VMS file specification */
4052 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4053 /* Fix up the filename */
4054 hack_vms_include_specification (fname);
4056 /* This is a normal VMS filespec, so use it unchanged. */
4057 strncpy (fname, fbeg, flen);
4059 /* if it's '#include filename', add the missing .h */
4060 if (strchr (fname,'.') == NULL) {
4061 strcat (fname, ".h");
4065 /* ??? There are currently 3 separate mechanisms for avoiding processing
4066 of redundant include files: #import, #pragma once, and
4067 redundant_include_p. It would be nice if they were unified. */
4069 if (redundant_include_p (pfile, fname))
4071 cstring_free (fname);
4075 DPRINTF (("Trying: %s", fname));
4077 f = open_include_file (pfile, fname, searchptr);
4079 if (f == IMPORT_FOUND)
4081 return 0; /* Already included this file */
4084 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4086 cppReader_warning (pfile,
4087 message ("Header file %s exists, but is not readable", fname));
4100 /* A file that was not found. */
4101 fname = cstring_copyLength (fbeg, flen);
4103 if (search_start != NULL)
4105 cppReader_error (pfile,
4106 message ("Cannot find include file %s on search path: %x",
4108 searchPath_unparse (search_start)));
4112 cppReader_error (pfile,
4113 message ("No include path in which to find %s", fname));
4118 ** Check to see if this include file is a once-only include file.
4122 struct file_name_list *ptr;
4124 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4126 if (cstring_equal (ptr->fname, fname))
4128 /* This file was included before. */
4135 /* This is the first time for this file. */
4136 /* Add it to list of files included. */
4138 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4139 ptr->control_macro = NULL;
4140 ptr->c_system_include_path = NULL;
4141 ptr->next = pfile->all_include_files;
4143 ptr->got_name_map = NULL;
4145 DPRINTF (("Including file: %s", fname));
4146 pfile->all_include_files = ptr;
4147 assertSet (pfile->all_include_files);
4150 if (angle_brackets != 0)
4152 pfile->system_include_depth++;
4155 /* Actually process the file */
4156 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4158 cstring_free (fname);
4162 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4163 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4165 output_line_command (pfile, 0, enter_file);
4166 pfile->only_seen_white = 2;
4171 pfile->system_include_depth--;
4174 } /*@=branchstate@*/
4179 /* Return nonzero if there is no need to include file NAME
4180 because it has already been included and it contains a conditional
4181 to make a repeated include do nothing. */
4184 redundant_include_p (cppReader *pfile, cstring name)
4186 struct file_name_list *l = pfile->all_include_files;
4188 for (; l != NULL; l = l->next)
4190 if (cstring_equal (name, l->fname)
4191 && (l->control_macro != NULL)
4192 && (cppReader_lookup (l->control_macro, -1, -1) != NULL))
4201 /* Return nonzero if the given FILENAME is an absolute pathname which
4202 designates a file within one of the known "system" include file
4203 directories. We assume here that if the given FILENAME looks like
4204 it is the name of a file which resides either directly in a "system"
4205 include file directory, or within any subdirectory thereof, then the
4206 given file must be a "system" include file. This function tells us
4207 if we should suppress pedantic errors/warnings for the given FILENAME.
4209 The value is 2 if the file is a C-language system header file
4210 for which C++ should (on most systems) assume `extern "C"'. */
4213 is_system_include (cppReader *pfile, cstring filename)
4215 struct file_name_list *searchptr;
4217 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4219 searchptr = searchptr->next)
4221 if (!cstring_isEmpty (searchptr->fname)) {
4222 cstring sys_dir = searchptr->fname;
4223 int length = cstring_length (sys_dir);
4225 if (cstring_equalLen (sys_dir, filename, length)
4226 && osd_isConnectChar (cstring_getChar (filename, length)))
4228 if (searchptr->c_system_include_path)
4239 /* Convert a character string literal into a nul-terminated string.
4240 The input string is [IN ... LIMIT).
4241 The result is placed in RESULT. RESULT can be the same as IN.
4242 The value returned in the end of the string written to RESULT,
4243 or NULL on error. */
4245 static /*@null@*/ char *
4246 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4247 char *in, char *limit, int handle_escapes)
4267 /*@switchbreak@*/ break;
4271 char *bpc = (char *) in;
4272 int i = (char) cppReader_parseEscape (pfile, &bpc);
4275 *result++ = (char) c;
4276 /*@switchbreak@*/ break;
4290 * interpret #line command. Remembers previously seen fnames
4291 * in its very own hash table.
4294 /*@constant int FNAME_HASHSIZE@*/
4295 #define FNAME_HASHSIZE 37
4298 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
4300 cppBuffer *ip = cppReader_getBuffer (pfile);
4302 size_t old_written = cppReader_getWritten (pfile);
4303 enum file_change_code file_change = same_file;
4304 enum cpp_token token;
4306 token = get_directive_token (pfile);
4308 if (token != CPP_NUMBER
4309 || !isdigit(pfile->token_buffer[old_written]))
4311 cppReader_errorLit (pfile,
4312 cstring_makeLiteralTemp ("invalid format `#line' command"));
4314 goto bad_line_directive;
4317 /* The Newline at the end of this line remains to be processed.
4318 To put the next line at the specified line number,
4319 we must store a line number now that is one less. */
4320 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
4321 cppReader_setWritten (pfile, old_written);
4323 /* NEW_LINENO is one less than the actual line number here. */
4324 if (cppReader_isPedantic (pfile) && new_lineno < 0)
4325 cppReader_pedwarnLit (pfile,
4326 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
4328 token = get_directive_token (pfile);
4330 if (token == CPP_STRING) {
4331 char *fname = pfile->token_buffer + old_written;
4333 static hashNode fname_table[FNAME_HASHSIZE];
4335 hashNode *hash_bucket;
4340 /* Turn the file name, which is a character string literal,
4341 into a null-terminated string. Do this in place. */
4342 end_name = convert_string (pfile, fname, fname, cppReader_getPWritten (pfile), 1);
4343 if (end_name == NULL)
4345 cppReader_errorLit (pfile,
4346 cstring_makeLiteralTemp ("invalid format `#line' command"));
4347 goto bad_line_directive;
4350 fname_length = end_name - fname;
4351 num_start = cppReader_getWritten (pfile);
4353 token = get_directive_token (pfile);
4354 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
4355 p = pfile->token_buffer + num_start;
4356 if (cppReader_isPedantic (pfile))
4357 cppReader_pedwarnLit (pfile,
4358 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
4360 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
4362 cppReader_errorLit (pfile,
4363 cstring_makeLiteralTemp ("invalid format `#line' command"));
4364 goto bad_line_directive;
4367 file_change = enter_file;
4369 file_change = leave_file;
4371 ip->system_header_p = 1;
4372 else /* if (*p == 4) */
4373 ip->system_header_p = 2;
4375 cppReader_setWritten (pfile, num_start);
4376 token = get_directive_token (pfile);
4377 p = pfile->token_buffer + num_start;
4378 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
4379 ip->system_header_p = *p == 3 ? 1 : 2;
4380 token = get_directive_token (pfile);
4382 if (token != CPP_VSPACE) {
4383 cppReader_errorLit (pfile,
4384 cstring_makeLiteralTemp ("invalid format `#line' command"));
4386 goto bad_line_directive;
4391 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
4392 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
4394 if (hp->length == fname_length &&
4395 strncmp (hp->value.cpval, fname, size_fromInt (fname_length)) == 0) {
4396 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4402 /* Didn't find it; cons up a new one. */
4403 hp = (hashNode) dmalloc (sizeof (*hp));
4406 hp->bucket_hdr = NULL;
4408 hp->name = cstring_undefined;
4409 hp->next = *hash_bucket;
4413 hp->length = fname_length;
4414 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
4415 memcpy (hp->value.cpval, fname, size_fromInt (fname_length));
4416 hp->value.cpval[fname_length] = '\0';
4417 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4420 else if (token != CPP_VSPACE && token != CPP_EOF)
4422 cppReader_errorLit (pfile,
4423 cstring_makeLiteralTemp ("invalid format `#line' command"));
4424 goto bad_line_directive;
4431 ip->lineno = new_lineno;
4433 cppReader_skipRestOfLine (pfile);
4434 cppReader_setWritten (pfile, old_written);
4435 output_line_command (pfile, 0, file_change);
4440 * remove the definition of a symbol from the symbol table.
4441 * according to un*x /lib/cpp, it is not an error to undef
4442 * something that has no definitions, so it isn't one here either.
4446 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
4451 char *orig_buf = buf;
4453 SKIP_WHITE_SPACE (buf);
4455 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
4457 while ((hp = cppReader_lookup (buf, sym_length, -1)) != NULL)
4459 /* If we are generating additional info for debugging (with -g) we
4460 need to pass through all effective #undef commands. */
4461 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
4463 pass_thru_directive (orig_buf, limit, pfile, keyword);
4466 if (hp->type != T_MACRO)
4468 cppReader_warning (pfile,
4469 message ("Undefining preprocessor builtin: %s",
4473 cppReader_deleteMacro (hp);
4476 if (cppReader_isPedantic (pfile)) {
4478 SKIP_WHITE_SPACE (buf);
4481 cppReader_pedwarnLit (pfile,
4482 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
4491 * Report an error detected by the program we are processing.
4492 * Use the text of the line in the error message.
4493 * (We use error because it prints the filename & line#.)
4497 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4498 char *buf, char *limit)
4500 int length = limit - buf;
4501 cstring copy = cstring_copyLength (buf, length);
4502 cstring adv = cstring_advanceWhiteSpace (copy);
4504 cppReader_error (pfile, message ("#error %s", adv));
4505 cstring_free (copy);
4510 * Report a warning detected by the program we are processing.
4511 * Use the text of the line in the warning message, then continue.
4512 * (We use error because it prints the filename & line#.)
4516 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4517 char *buf, char *limit)
4519 int length = limit - buf;
4520 cstring copy = cstring_copyLength (buf, length);
4521 cstring adv = cstring_advanceWhiteSpace (copy);
4522 cppReader_warning (pfile, message ("#warning %s", adv));
4523 cstring_free (copy);
4528 /* #ident has already been copied to the output file, so just ignore it. */
4531 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4532 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4534 /* Allow #ident in system headers, since that's not user's fault. */
4535 if (cppReader_isPedantic (pfile) && !cppReader_getBuffer (pfile)->system_header_p)
4536 cppReader_pedwarnLit (pfile,
4537 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
4539 /* Leave rest of line to be read by later calls to cppGetToken. */
4544 /* #pragma and its argument line have already been copied to the output file.
4545 Just check for some recognized pragmas that need validation here. */
4548 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4549 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4551 while (*buf == ' ' || *buf == '\t')
4556 if (!strncmp (buf, "implementation", 14)) {
4557 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4558 been included yet. */
4559 struct file_name_list *ptr;
4560 char *p = buf + 14, *fname, *inc_fname;
4562 SKIP_WHITE_SPACE (p);
4563 if (*p == '\n' || *p != '\"')
4567 p = (char *) strchr (fname, '\"');
4568 fname_len = p != NULL ? p - fname : mstring_length (fname);
4570 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4572 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
4573 inc_fname = (inc_fname != NULL)
4574 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
4576 if ((inc_fname != NULL)
4577 && (strncmp (inc_fname, fname, size_fromInt (fname_len)) == 0))
4579 cpp_setLocation (pfile);
4581 ppllerror (message ("`#pragma implementation' for `%s' appears "
4582 "after file is included",
4583 cstring_fromChars (fname)));
4592 * handle #if command by
4593 * 1) inserting special `defined' keyword into the hash table
4594 * that gets turned into 0 or 1 by special_symbol (thus,
4595 * if the luser has a symbol called `defined' already, it won't
4596 * work inside the #if command)
4597 * 2) rescan the input into a temporary output buffer
4598 * 3) pass the output buffer to the yacc parser and collect a value
4599 * 4) clean up the mess left from steps 1 and 2.
4600 * 5) call conditional_skip to skip til the next #endif (etc.),
4601 * or not, depending on the value from step 3.
4605 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4606 char *buf, char *limit)
4608 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4609 conditional_skip (pfile, value == 0, T_IF, NULL);
4614 * handle a #elif directive by not changing if_stack either.
4615 * see the comment above do_else.
4618 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4619 char *buf, char *limit)
4621 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4623 cppReader_errorLit (pfile,
4624 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
4629 llassert (pfile->if_stack != NULL);
4631 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4633 cppReader_errorLit (pfile,
4634 cstring_makeLiteralTemp ("`#elif' after `#else'"));
4636 if (pfile->if_stack->fname != NULL
4637 && cppReader_getBuffer (pfile)->fname != NULL
4638 && !cstring_equal (pfile->if_stack->fname,
4639 cppReader_getBuffer (pfile)->nominal_fname))
4640 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
4641 fprintf (stderr, ")\n");
4643 pfile->if_stack->type = T_ELIF;
4646 if (pfile->if_stack->if_succeeded)
4648 skip_if_group (pfile, 0);
4652 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4654 skip_if_group (pfile, 0);
4657 ++pfile->if_stack->if_succeeded; /* continue processing input */
4658 output_line_command (pfile, 1, same_file);
4666 * evaluate a #if expression in BUF, of length LENGTH,
4667 * then parse the result as a C expression and return the value as an int.
4670 static HOST_WIDE_INT
4671 eval_if_expression (cppReader *pfile,
4672 /*@unused@*/ char *buf,
4673 /*@unused@*/ int length)
4675 hashNode save_defined;
4676 HOST_WIDE_INT value;
4677 size_t old_written = cppReader_getWritten (pfile);
4679 save_defined = cppReader_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4680 pfile->pcp_inside_if = 1;
4682 value = cppReader_parseExpression (pfile);
4683 pfile->pcp_inside_if = 0;
4685 /* Clean up special symbol */
4686 cppReader_deleteMacro (save_defined);
4688 cppReader_setWritten (pfile, old_written); /* Pop */
4694 * routine to handle ifdef/ifndef. Try to look up the symbol,
4695 * then do or don't skip to the #endif/#else/#elif depending
4696 * on what directive is actually being processed.
4700 do_xifdef (cppReader *pfile, struct directive *keyword,
4701 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4704 cppBuffer *ip = cppReader_getBuffer (pfile);
4707 enum cpp_token token;
4708 int start_of_file = 0;
4709 char *control_macro = 0;
4710 size_t old_written = cppReader_getWritten (pfile);
4712 DPRINTF (("do xifdef: %d",
4713 keyword->type == T_IFNDEF));
4715 /* Detect a #ifndef at start of file (not counting comments). */
4716 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
4718 start_of_file = pfile->only_seen_white == 2;
4721 pfile->no_macro_expand++;
4722 token = get_directive_token (pfile);
4723 pfile->no_macro_expand--;
4725 ident = pfile->token_buffer + old_written;
4726 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4727 cppReader_setWritten (pfile, old_written); /* Pop */
4729 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4731 skip = (keyword->type == T_IFDEF);
4732 if (! cppReader_isTraditional (pfile))
4734 cppReader_pedwarn (pfile,
4735 message ("`#%s' with no argument", keyword->name));
4738 else if (token == CPP_NAME)
4740 hashNode hp = cppReader_lookup (ident, ident_length, -1);
4741 skip = (keyword->type == T_IFDEF)
4742 ? (hp == NULL) : (hp != NULL);
4744 DPRINTF (("hp null: %d / %d / %d",
4746 (keyword->type == T_IFNDEF),
4749 if (start_of_file && !skip)
4751 DPRINTF (("Not skipping!"));
4752 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
4753 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
4758 skip = (keyword->type == T_IFDEF);
4759 if (! cppReader_isTraditional (pfile))
4761 cppReader_error (pfile,
4762 message ("`#%s' with invalid argument", keyword->name));
4766 if (!cppReader_isTraditional (pfile))
4769 cppSkipHspace (pfile);
4770 c = cppReader_peekC (pfile);
4771 if (c != EOF && c != '\n')
4773 cppReader_pedwarn (pfile,
4774 message ("garbage at end of `#%s' argument", keyword->name));
4778 cppReader_skipRestOfLine (pfile);
4780 DPRINTF (("Conditional skip: %d", skip));
4781 conditional_skip (pfile, skip, T_IF, control_macro);
4785 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4786 If this is a #ifndef starting at the beginning of a file,
4787 CONTROL_MACRO is the macro name tested by the #ifndef.
4788 Otherwise, CONTROL_MACRO is 0. */
4791 conditional_skip (cppReader *pfile, int skip,
4792 enum node_type type,
4793 /*@dependent@*/ char *control_macro)
4795 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4797 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4798 temp->next = pfile->if_stack;
4799 temp->control_macro = control_macro;
4801 temp->if_succeeded = 0;
4803 pfile->if_stack = temp;
4804 pfile->if_stack->type = type;
4808 skip_if_group (pfile, 0);
4813 ++pfile->if_stack->if_succeeded;
4814 output_line_command (pfile, 1, same_file);
4819 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4820 * leaves input ptr at the sharp sign found.
4821 * If ANY is nonzero, return at next directive of any sort.
4825 skip_if_group (cppReader *pfile, int any)
4828 struct directive *kt;
4829 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
4830 register int ident_length;
4832 struct parse_marker line_start_mark;
4834 parseSetMark (&line_start_mark, pfile);
4836 if (CPPOPTIONS (pfile)->output_conditionals) {
4837 static char failed[] = "#failed\n";
4838 cppReader_puts (pfile, failed, sizeof(failed)-1);
4840 output_line_command (pfile, 1, same_file);
4844 if (CPPOPTIONS (pfile)->output_conditionals)
4846 cppBuffer *pbuf = cppReader_getBuffer (pfile);
4849 llassert (pbuf->buf != NULL);
4851 start_line = pbuf->buf + line_start_mark.position;
4852 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
4855 parseMoveMark (&line_start_mark, pfile);
4857 if (!cppReader_isTraditional (pfile))
4859 cppSkipHspace (pfile);
4862 c = cppReader_getC (pfile);
4865 size_t old_written = cppReader_getWritten (pfile);
4866 cppSkipHspace (pfile);
4868 parse_name (pfile, cppReader_getC (pfile));
4869 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4870 ident = pfile->token_buffer + old_written;
4871 pfile->limit = ident;
4873 for (kt = directive_table; kt->length >= 0; kt++)
4875 cppIfStackFrame *temp;
4876 if (ident_length == kt->length
4877 && cstring_equalPrefix (kt->name, ident))
4879 /* If we are asked to return on next directive, do so now. */
4890 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4891 temp->next = pfile->if_stack;
4892 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4893 temp->type = kt->type;
4895 temp->if_succeeded = 0;
4896 temp->control_macro = NULL;
4898 pfile->if_stack = temp;
4899 /*@switchbreak@*/ break;
4902 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
4903 validate_else (pfile,
4904 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
4907 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4909 cppReader_error (pfile,
4910 message ("Preprocessor command #%s is not within a conditional", kt->name));
4911 /*@switchbreak@*/ break;
4913 else if (pfile->if_stack == save_if_stack)
4915 goto done; /* found what we came for */
4922 if (kt->type != T_ENDIF)
4924 llassert (pfile->if_stack != NULL);
4926 if (pfile->if_stack->type == T_ELSE)
4928 cppReader_errorLit (pfile,
4929 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
4932 pfile->if_stack->type = kt->type;
4933 /*@switchbreak@*/ break;
4936 temp = pfile->if_stack;
4937 llassert (temp != NULL);
4938 pfile->if_stack = temp->next;
4940 /*@switchbreak@*/ break;
4948 /* Don't let erroneous code go by. */
4950 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
4951 && cppReader_isPedantic (pfile))
4953 cppReader_pedwarnLit (pfile,
4954 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
4958 c = cppReader_getC (pfile);
4960 /* We're in the middle of a line. Skip the rest of it. */
4968 case '/': /* possible comment */
4969 c = skip_comment (pfile, NULL);
4972 /*@switchbreak@*/ break;
4975 cppReader_forward (pfile, -1);
4976 old = cppReader_getWritten (pfile);
4977 (void) cppGetToken (pfile);
4978 cppReader_setWritten (pfile, old);
4979 /*@switchbreak@*/ break;
4981 /* Char after backslash loses its special meaning. */
4982 if (cppReader_peekC (pfile) == '\n')
4984 cppReader_forward (pfile, 1);
4987 /*@switchbreak@*/ break;
4991 c = cppReader_getC (pfile);
4994 if (CPPOPTIONS (pfile)->output_conditionals) {
4995 static char end_failed[] = "#endfailed\n";
4996 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
4999 pfile->only_seen_white = 1;
5001 parseGotoMark (&line_start_mark, pfile);
5002 parseClearMark (&line_start_mark);
5006 * handle a #else directive. Do this by just continuing processing
5007 * without changing if_stack ; this is so that the error message
5008 * for missing #endif's etc. will point to the original #if. It
5009 * is possible that something different would be better.
5013 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5014 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5016 if (cppReader_isPedantic (pfile))
5018 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5021 cppReader_skipRestOfLine (pfile);
5023 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack) {
5024 cppReader_errorLit (pfile,
5025 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5028 /* #ifndef can't have its special treatment for containing the whole file
5029 if it has a #else clause. */
5031 llassert (pfile->if_stack != NULL);
5033 pfile->if_stack->control_macro = 0;
5035 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5037 cpp_setLocation (pfile);
5038 genppllerrorhint (FLG_PREPROC,
5039 message ("Pre-processor directive #else after #else"),
5040 message ("%q: Location of match",
5041 fileloc_unparseRaw (pfile->if_stack->fname,
5042 pfile->if_stack->lineno)));
5045 pfile->if_stack->type = T_ELSE;
5048 if (pfile->if_stack->if_succeeded)
5049 skip_if_group (pfile, 0);
5051 ++pfile->if_stack->if_succeeded; /* continue processing input */
5052 output_line_command (pfile, 1, same_file);
5059 * unstack after #endif command
5063 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5064 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5066 if (cppReader_isPedantic (pfile))
5068 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5071 cppReader_skipRestOfLine (pfile);
5073 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5075 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5079 cppIfStackFrame *temp = pfile->if_stack;
5081 llassert (temp != NULL);
5083 pfile->if_stack = temp->next;
5084 if (temp->control_macro != 0)
5086 /* This #endif matched a #ifndef at the start of the file.
5087 See if it is at the end of the file. */
5088 struct parse_marker start_mark;
5091 parseSetMark (&start_mark, pfile);
5095 cppSkipHspace (pfile);
5096 c = cppReader_getC (pfile);
5102 parseGotoMark (&start_mark, pfile);
5103 parseClearMark (&start_mark);
5107 /* If we get here, this #endif ends a #ifndef
5108 that contains all of the file (aside from whitespace).
5109 Arrange not to include the file again
5110 if the macro that was tested is defined.
5112 Do not do this for the top-level file in a -include or any
5113 file in a -imacros. */
5114 struct file_name_list *ifile = pfile->all_include_files;
5116 for ( ; ifile != NULL; ifile = ifile->next)
5118 if (cstring_equal (ifile->fname, cppReader_getBuffer (pfile)->fname))
5120 ifile->control_macro = temp->control_macro;
5128 output_line_command (pfile, 1, same_file);
5133 /* When an #else or #endif is found while skipping failed conditional,
5134 if -pedantic was specified, this is called to warn about text after
5135 the command name. P points to the first char after the command name. */
5138 validate_else (cppReader *pfile, cstring directive)
5141 cppSkipHspace (pfile);
5142 c = cppReader_peekC (pfile);
5143 if (c != EOF && c != '\n')
5145 cppReader_pedwarn (pfile,
5146 message ("text following `%s' violates ANSI standard", directive));
5151 ** Get the next token, and add it to the text in pfile->token_buffer.
5152 ** Return the kind of token we got.
5156 cppGetToken (cppReader *pfile)
5159 size_t old_written = 0;
5160 int start_line, start_column;
5161 enum cpp_token token;
5162 struct cppOptions *opts = CPPOPTIONS (pfile);
5163 cppReader_getBuffer (pfile)->prev = cppReader_getBuffer (pfile)->cur;
5166 c = cppReader_getC (pfile);
5171 if (cppReader_getBuffer (pfile)->seen_eof)
5173 cppBuffer *buf = cppReader_popBuffer (pfile);
5175 if (buf != cppReader_nullBuffer (pfile))
5186 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5187 cppReader_getBuffer (pfile)->seen_eof = 1;
5189 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname)
5190 && next_buf != cppReader_nullBuffer (pfile))
5192 /* We're about to return from an #include file.
5193 Emit #line information now (as part of the CPP_POP) result.
5194 But the #line refers to the file we will pop to. */
5195 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5196 CPPBUFFER (pfile) = next_buf;
5197 pfile->input_stack_listing_current = 0;
5198 output_line_command (pfile, 0, leave_file);
5199 CPPBUFFER (pfile) = cur_buffer;
5207 struct parse_marker start_mark;
5212 if (cppReader_peekC (pfile) == '=')
5217 if (opts->put_out_comments)
5219 parseSetMark (&start_mark, pfile);
5223 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5224 &start_line, &start_column);
5225 c = skip_comment (pfile, &newlines);
5227 if (opts->put_out_comments && (c == '/' || c == EOF))
5229 assertSet (&start_mark);
5230 parseClearMark (&start_mark);
5237 cppReader_errorWithLine (pfile, start_line, start_column,
5238 cstring_makeLiteral ("Unterminated comment"));
5241 c = '/'; /* Initial letter of comment. */
5243 /* Comments are equivalent to spaces.
5244 For -traditional, a comment is equivalent to nothing. */
5246 if (opts->put_out_comments)
5250 assertSet (&start_mark);
5251 res = cpp_handleComment (pfile, &start_mark);
5252 pfile->lineno += newlines;
5255 else if (cppReader_isTraditional (pfile))
5261 cppReader_reserve(pfile, 1);
5262 cppReader_putCharQ (pfile, ' ');
5267 if (!pfile->only_seen_white)
5272 if (cppReader_handleDirective (pfile))
5274 return CPP_DIRECTIVE;
5277 pfile->only_seen_white = 0;
5282 /* A single quoted string is treated like a double -- some
5283 programs (e.g., troff) are perverse this way */
5284 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5285 &start_line, &start_column);
5286 old_written = cppReader_getWritten (pfile);
5288 cppReader_putChar (pfile, c);
5291 int cc = cppReader_getC (pfile);
5294 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
5296 /* try harder: this string crosses a macro expansion
5297 boundary. This can happen naturally if -traditional.
5298 Otherwise, only -D can make a macro with an unmatched
5301 = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5302 (*cppReader_getBuffer (pfile)->cleanup)
5303 (cppReader_getBuffer (pfile), pfile);
5304 CPPBUFFER (pfile) = next_buf;
5307 if (!cppReader_isTraditional (pfile))
5309 cpp_setLocation (pfile);
5311 setLine (long_toInt (start_line));
5312 setColumn (long_toInt (start_column));
5314 if (pfile->multiline_string_line != long_toInt (start_line)
5315 && pfile->multiline_string_line != 0)
5319 message ("Unterminated string or character constant"),
5320 message ("%q: Possible real start of unterminated constant",
5322 (fileloc_filename (g_currentloc),
5323 pfile->multiline_string_line)));
5324 pfile->multiline_string_line = 0;
5330 message ("Unterminated string or character constant"));
5333 /*@loopbreak@*/ break;
5335 cppReader_putChar (pfile, cc);
5339 /* Traditionally, end of line ends a string constant with
5340 no error. So exit the loop and record the new line. */
5341 if (cppReader_isTraditional (pfile))
5347 if (cppReader_isPedantic (pfile)
5348 && pfile->multiline_string_line == 0)
5350 cppReader_pedwarnWithLine
5351 (pfile, long_toInt (start_line),
5352 long_toInt (start_column),
5353 cstring_makeLiteral ("String constant runs past end of line"));
5355 if (pfile->multiline_string_line == 0)
5357 pfile->multiline_string_line = start_line;
5360 /*@switchbreak@*/ break;
5363 cc = cppReader_getC (pfile);
5366 /* Backslash newline is replaced by nothing at all. */
5367 cppReader_adjustWritten (pfile, -1);
5372 /* ANSI stupidly requires that in \\ the second \
5373 is *not* prevented from combining with a newline. */
5376 cppReader_putChar (pfile, cc);
5378 /*@switchbreak@*/ break;
5384 /*@switchbreak@*/ break;
5388 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
5389 cppReader_getPWritten (pfile));
5390 pfile->only_seen_white = 0;
5391 return c == '\'' ? CPP_CHAR : CPP_STRING;
5394 if (!opts->dollars_in_ident)
5399 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
5407 c2 = cppReader_peekC (pfile);
5408 if (c2 == c || c2 == '=')
5418 if (cppReader_peekC (pfile) == '=')
5424 c2 = cppReader_peekC (pfile);
5425 if (c2 == '-' && opts->chill)
5427 /* Chill style comment */
5428 if (opts->put_out_comments)
5430 parseSetMark (&start_mark, pfile);
5433 cppReader_forward (pfile, 1); /* Skip second '-'. */
5437 c = cppReader_getC (pfile);
5439 /*@loopbreak@*/ break;
5442 /* Don't consider final '\n' to be part of comment. */
5443 cppReader_forward (pfile, -1);
5444 /*@loopbreak@*/ break;
5448 goto return_comment;
5450 if (c2 == '-' || c2 == '=' || c2 == '>')
5455 if (pfile->parsing_include_directive)
5459 cppReader_putChar (pfile, c);
5461 /*@loopbreak@*/ break;
5462 c = cppReader_getC (pfile);
5464 if (c == '\n' || c == EOF)
5466 cppReader_errorLit (pfile,
5467 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
5468 /*@loopbreak@*/ break;
5476 c2 = cppReader_peekC (pfile);
5481 cppReader_forward (pfile, 1);
5482 cppReader_reserve (pfile, 4);
5483 cppReader_putChar (pfile, c);
5484 cppReader_putChar (pfile, c2);
5486 c3 = cppReader_peekC (pfile);
5488 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5489 cppReader_nullTerminateQ (pfile);
5490 pfile->only_seen_white = 0;
5494 if (cppReader_getBuffer (pfile)->has_escapes)
5496 c = cppReader_getC (pfile);
5499 if (pfile->output_escapes)
5500 cppReader_puts (pfile, "@-", 2);
5501 parse_name (pfile, cppReader_getC (pfile));
5504 else if (is_space [c])
5506 cppReader_reserve (pfile, 2);
5507 if (pfile->output_escapes)
5508 cppReader_putCharQ (pfile, '@');
5509 cppReader_putCharQ (pfile, c);
5517 if (pfile->output_escapes)
5519 cppReader_puts (pfile, "@@", 2);
5525 c2 = cppReader_peekC (pfile);
5528 cppReader_reserve(pfile, 2);
5529 cppReader_putCharQ (pfile, '.');
5530 c = cppReader_getC (pfile);
5534 /* FIXME - misses the case "..\\\n." */
5535 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
5537 cppReader_reserve(pfile, 4);
5538 cppReader_putCharQ (pfile, '.');
5539 cppReader_putCharQ (pfile, '.');
5540 cppReader_putCharQ (pfile, '.');
5541 cppReader_forward (pfile, 2);
5542 cppReader_nullTerminateQ (pfile);
5543 pfile->only_seen_white = 0;
5549 pfile->only_seen_white = 0;
5551 cppReader_reserve(pfile, 3);
5552 cppReader_putCharQ (pfile, c);
5553 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5554 cppReader_nullTerminateQ (pfile);
5559 c2 = cppReader_peekC (pfile);
5560 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
5562 cppReader_putChar (pfile, c);
5563 c = cppReader_getC (pfile);
5568 case '0': case '1': case '2': case '3': case '4':
5569 case '5': case '6': case '7': case '8': case '9':
5574 cppReader_reserve (pfile, 2);
5575 cppReader_putCharQ (pfile, c);
5577 c = cppReader_peekC (pfile);
5579 /*@loopbreak@*/ break;
5580 if (!is_idchar[c] && c != '.'
5581 && ((c2 != 'e' && c2 != 'E'
5582 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
5583 || (c != '+' && c != '-')))
5584 /*@loopbreak@*/ break;
5585 cppReader_forward (pfile, 1);
5589 cppReader_nullTerminateQ (pfile);
5590 pfile->only_seen_white = 0;
5593 case 'b': case 'c': case 'd': case 'h': case 'o':
5594 case 'B': case 'C': case 'D': case 'H': case 'O':
5595 if (opts->chill && cppReader_peekC (pfile) == '\'')
5597 pfile->only_seen_white = 0;
5598 cppReader_reserve (pfile, 2);
5599 cppReader_putCharQ (pfile, c);
5600 cppReader_putCharQ (pfile, '\'');
5601 cppReader_forward (pfile, 1);
5604 c = cppReader_getC (pfile);
5606 goto chill_number_eof;
5609 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5611 cppReader_forward (pfile, 2);
5614 /*@loopbreak@*/ break;
5616 cppReader_putChar (pfile, c);
5620 cppReader_reserve (pfile, 2);
5621 cppReader_putCharQ (pfile, c);
5622 cppReader_nullTerminateQ (pfile);
5627 cppReader_forward (pfile, -1);
5629 cppReader_nullTerminate (pfile);
5636 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5637 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5638 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5639 case 'x': case 'y': case 'z':
5640 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5641 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5642 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5648 size_t before_name_written = cppReader_getWritten (pfile);
5650 parse_name (pfile, c);
5651 pfile->only_seen_white = 0;
5652 if (pfile->no_macro_expand)
5657 ident = pfile->token_buffer + before_name_written;
5658 ident_len = (cppReader_getPWritten (pfile)) - ident;
5660 hp = cppReader_lookupExpand (ident, ident_len, -1);
5667 if (hp->type == T_DISABLED)
5669 if (pfile->output_escapes)
5670 { /* Return "@-IDENT", followed by '\0'. */
5672 cppReader_reserve (pfile, 3);
5673 ident = pfile->token_buffer + before_name_written;
5674 cppReader_adjustWritten (pfile, 2);
5676 for (i = ident_len; i >= 0; i--)
5678 ident[i+2] = ident[i];
5687 /* If macro wants an arglist, verify that a '(' follows.
5688 first skip all whitespace, copying it to the output
5689 after the macro name. Then, if there is no '(',
5690 decide this is not a macro call and leave things that way. */
5692 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5694 struct parse_marker macro_mark;
5697 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
5699 cppBuffer *next_buf;
5700 cppSkipHspace (pfile);
5701 if (cppReader_peekC (pfile) != EOF)
5703 /*@loopbreak@*/ break;
5706 next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5707 (*cppReader_getBuffer (pfile)->cleanup) (cppReader_getBuffer (pfile), pfile);
5708 CPPBUFFER (pfile) = next_buf;
5711 parseSetMark (¯o_mark, pfile);
5715 cppSkipHspace (pfile);
5716 c = cppReader_peekC (pfile);
5717 is_macro_call = c == '(';
5719 /*@loopbreak@*/ break;
5720 cppReader_forward (pfile, 1);
5725 parseGotoMark (¯o_mark, pfile);
5728 parseClearMark (¯o_mark);
5735 /* This is now known to be a macro call. */
5737 /* it might not actually be a macro. */
5738 if (hp->type != T_MACRO)
5743 cppReader_setWritten (pfile, before_name_written);
5744 special_symbol (hp, pfile);
5745 xbuf_len = cppReader_getWritten (pfile) - before_name_written;
5746 xbuf = (char *) dmalloc (xbuf_len + 1);
5747 cppReader_setWritten (pfile, before_name_written);
5748 memcpy (xbuf, cppReader_getPWritten (pfile), xbuf_len + 1);
5749 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5753 /* Expand the macro, reading arguments as needed,
5754 and push the expansion on the input stack. */
5755 macroexpand (pfile, hp);
5756 cppReader_setWritten (pfile, before_name_written);
5759 /* An extra "@ " is added to the end of a macro expansion
5760 to prevent accidental token pasting. We prefer to avoid
5761 unneeded extra spaces (for the sake of cpp-using tools like
5762 imake). Here we remove the space if it is safe to do so. */
5764 llassert (pfile->buffer->rlimit != NULL);
5766 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5767 && pfile->buffer->rlimit[-2] == '@'
5768 && pfile->buffer->rlimit[-1] == ' ')
5770 int c1 = pfile->buffer->rlimit[-3];
5771 int cl2 = cppBufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
5773 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
5774 pfile->buffer->rlimit -= 2;
5780 case ' ': case '\t': case '\v': case '\r':
5783 cppReader_putChar (pfile, c);
5784 c = cppReader_peekC (pfile);
5785 if (c == EOF || !is_hor_space[c])
5786 /*@loopbreak@*/ break;
5787 cppReader_forward (pfile, 1);
5792 c2 = cppReader_peekC (pfile);
5799 cppReader_putChar (pfile, c);
5800 if (pfile->only_seen_white == 0)
5801 pfile->only_seen_white = 1;
5803 output_line_command (pfile, 1, same_file);
5806 case '(': token = CPP_LPAREN; goto char1;
5807 case ')': token = CPP_RPAREN; goto char1;
5808 case '{': token = CPP_LBRACE; goto char1;
5809 case '}': token = CPP_RBRACE; goto char1;
5810 case ',': token = CPP_COMMA; goto char1;
5811 case ';': token = CPP_SEMICOLON; goto char1;
5817 pfile->only_seen_white = 0;
5818 cppReader_putChar (pfile, c);
5827 /* Parse an identifier starting with C. */
5830 parse_name (cppReader *pfile, int c)
5836 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5838 cppReader_forward (pfile, 2);
5842 cppReader_forward (pfile, -1);
5846 if (c == '$' && cppReader_isPedantic (pfile))
5848 cppReader_pedwarnLit (pfile,
5849 cstring_makeLiteralTemp ("`$' in identifier"));
5852 cppReader_reserve(pfile, 2); /* One more for final NUL. */
5853 cppReader_putCharQ (pfile, c);
5854 c = cppReader_getC (pfile);
5860 cppReader_nullTerminateQ (pfile);
5863 /* The file_name_map structure holds a mapping of file names for a
5864 particular directory. This mapping is read from the file named
5865 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5866 map filenames on a file system with severe filename restrictions,
5867 such as DOS. The format of the file name map file is just a series
5868 of lines with two tokens on each line. The first token is the name
5869 to map, and the second token is the actual name to use. */
5871 struct file_name_map
5873 struct file_name_map *map_next;
5878 /*@constant observer char *FILE_NAME_MAP_FILE*/
5879 #define FILE_NAME_MAP_FILE "header.gcc"
5881 /* Read a space delimited string of unlimited length from a stdio
5884 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
5890 set = alloc = dmalloc (len + 1);
5895 while ((ch = getc (f)) != EOF && ! is_space[ch])
5897 if (set - alloc == size_toInt (len))
5900 alloc = drealloc (alloc, len + 1);
5901 set = alloc + len / 2;
5902 /*@-branchstate@*/ }
5905 } /*@=branchstate@*/
5908 check (ungetc (ch, f) != EOF);
5910 return cstring_fromChars (alloc);
5913 /* This structure holds a linked list of file name maps, one per directory. */
5915 struct file_name_map_list
5917 struct file_name_map_list *map_list_next;
5918 cstring map_list_name;
5919 struct file_name_map *map_list_map;
5922 /* Read the file name map file for DIRNAME. */
5924 static struct file_name_map *
5925 read_name_map (cppReader *pfile, cstring dirname)
5927 struct file_name_map_list *map_list_ptr;
5931 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
5932 map_list_ptr != NULL;
5933 map_list_ptr = map_list_ptr->map_list_next)
5935 if (cstring_equal (map_list_ptr->map_list_name, dirname))
5937 return map_list_ptr->map_list_map;
5941 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
5942 map_list_ptr->map_list_name = cstring_copy (dirname);
5943 map_list_ptr->map_list_map = NULL;
5945 name = cstring_copy (dirname);
5947 if (cstring_length (dirname) > 0)
5949 name = cstring_appendChar (name, CONNECTCHAR);
5952 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
5954 f = fopen (cstring_toCharsSafe (name), "r");
5955 cstring_free (name);
5959 map_list_ptr->map_list_map = NULL;
5965 while ((ch = getc (f)) != EOF)
5968 struct file_name_map *ptr;
5975 from = read_filename_string (ch, f);
5976 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5981 to = read_filename_string (ch, f);
5983 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
5984 ptr->map_from = from;
5986 /* Make the real filename absolute. */
5987 if (cstring_length (to) > 1
5988 && osd_isConnectChar (cstring_firstChar (to)))
5994 ptr->map_to = cstring_copy (dirname);
5995 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
5996 ptr->map_to = cstring_concatFree (ptr->map_to, to);
5999 ptr->map_next = map_list_ptr->map_list_map;
6000 map_list_ptr->map_list_map = ptr;
6002 while ((ch = getc (f)) != '\n')
6006 /*@innerbreak@*/ break;
6011 assertSet (map_list_ptr->map_list_map);
6012 check (fclose (f) == 0);
6015 map_list_ptr->map_list_next = pfile->opts->map_list;
6016 pfile->opts->map_list = map_list_ptr;
6018 return map_list_ptr->map_list_map;
6021 /* Try to open include file FILENAME. SEARCHPTR is the directory
6022 being tried from the include file search path. This function maps
6023 filenames on file systems based on information read by
6027 open_include_file (cppReader *pfile,
6029 struct file_name_list *searchptr)
6031 char *filename = cstring_toCharsSafe (fname);
6032 struct file_name_map *map;
6036 cstring_markOwned (fname);
6038 cpp_setLocation (pfile);
6040 if (context_getFlag (FLG_NEVERINCLUDE))
6042 if (isHeaderFile (fname))
6044 return SKIP_INCLUDE;
6048 if ((searchptr != NULL) && ! searchptr->got_name_map)
6050 searchptr->name_map = read_name_map (pfile,
6051 !cstring_isEmpty (searchptr->fname)
6052 ? searchptr->fname :
6053 cstring_makeLiteralTemp ("."));
6054 searchptr->got_name_map = 1;
6057 /* First check the mapping for the directory we are using. */
6059 if ((searchptr != NULL)
6060 && (searchptr->name_map != NULL))
6064 if (!cstring_isEmpty (searchptr->fname))
6066 from += cstring_length (searchptr->fname) + 1;
6069 for (map = searchptr->name_map;
6071 map = map->map_next)
6073 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6076 ** Found a match. Check if the file should be skipped
6079 if (cpp_skipIncludeFile (map->map_to))
6081 return SKIP_INCLUDE;
6085 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6092 ** Try to find a mapping file for the particular directory we are
6093 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6094 ** in /usr/include/header.gcc and look up types.h in
6095 ** /usr/include/sys/header.gcc.
6098 p = strrchr (filename, CONNECTCHAR);
6105 if ((searchptr != NULL)
6106 && (cstring_isDefined (searchptr->fname))
6107 && (cstring_length (searchptr->fname) == p - filename)
6108 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6110 size_fromInt (p - filename)))
6112 /* filename is in SEARCHPTR, which we've already checked. */
6114 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6116 return SKIP_INCLUDE;
6120 return cpp_openIncludeFile (filename);
6126 dir = mstring_copy (".");
6131 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6132 memcpy (dir, filename, size_fromInt (p - filename));
6133 dir[p - filename] = '\0';
6137 for (map = read_name_map (pfile, cstring_fromChars (dir));
6139 map = map->map_next)
6141 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6145 if (cpp_skipIncludeFile (map->map_to))
6147 return SKIP_INCLUDE;
6151 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6158 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6160 return SKIP_INCLUDE;
6164 return cpp_openIncludeFile (filename);
6168 /* Process the contents of include file FNAME, already open on descriptor F,
6170 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6171 "system" include directories (as decided by the `is_system_include'
6173 DIRPTR is the link in the dir path through which this file was found,
6174 or 0 if the file name was absolute or via the current directory.
6175 Return 1 on success, 0 on failure.
6177 The caller is responsible for the cppReader_pushBuffer. */
6180 finclude (cppReader *pfile, int f,
6182 bool system_header_p,
6183 /*@dependent@*/ struct file_name_list *dirptr)
6189 cppBuffer *fp; /* For input stack frame */
6191 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
6193 cppReader_perrorWithName (pfile, fname);
6194 check (close (f) == 0);
6195 (void) cppReader_popBuffer (pfile);
6201 fp = cppReader_getBuffer (pfile);
6203 /*@-temptrans@*/ /* fname shouldn't really be temp */
6204 fp->nominal_fname = fp->fname = fname;
6208 fp->system_header_p = system_header_p;
6211 fp->cleanup = cppReader_fileCleanup;
6213 if (S_ISREG (st_mode))
6216 fp->buf = (char *) dmalloc (st_size + 2);
6217 fp->alimit = fp->buf + st_size + 2;
6220 /* Read the file contents, knowing that st_size is an upper bound
6221 on the number of bytes we can read. */
6222 length = safe_read (f, fp->buf, size_toInt (st_size));
6223 fp->rlimit = fp->buf + length;
6224 if (length < 0) goto nope;
6226 else if (S_ISDIR (st_mode))
6228 cppReader_error (pfile,
6229 message ("Directory specified in #include: %s", fname));
6230 check (close (f) == 0);
6236 ** Cannot count its file size before reading.
6237 ** First read the entire file into heap and
6238 ** copy them into buffer on stack.
6241 size_t bsize = 2000;
6246 fp->buf = (char *) dmalloc (bsize + 2);
6249 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
6252 goto nope; /* error! */
6255 if (st_size != bsize)
6257 break; /* End of file */
6261 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
6265 length = size_toInt (st_size);
6268 if ((length > 0 && fp->buf[length - 1] != '\n')
6269 /* Backslash-newline at end is not good enough. */
6270 || (length > 1 && fp->buf[length - 2] == '\\')) {
6271 fp->buf[length++] = '\n';
6274 fp->buf[length] = '\0';
6275 fp->rlimit = fp->buf + length;
6277 /* Close descriptor now, so nesting does not use lots of descriptors. */
6278 check (close (f) == 0);
6280 /* Must do this before calling trigraph_pcp, so that the correct file name
6281 will be printed in warning messages. */
6283 pfile->input_stack_listing_current = 0;
6288 cppReader_perrorWithName (pfile, fname);
6289 check (close (f) == 0);
6295 cppReader_init (cppReader *pfile)
6297 memset ((char *) pfile, 0, sizeof (*pfile));
6299 pfile->get_token = cppGetToken;
6300 pfile->token_buffer_size = 200;
6301 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
6302 pfile->all_include_files = NULL;
6306 cppReader_setWritten (pfile, 0);
6308 pfile->system_include_depth = 0;
6309 pfile->max_include_len = 0;
6310 pfile->timebuf = NULL;
6311 pfile->only_seen_white = 1;
6313 pfile->buffer = cppReader_nullBuffer (pfile);
6317 cppReader_finish (/*@unused@*/ cppReader *pfile)
6322 /* Free resources used by PFILE.
6323 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
6326 cppCleanup (cppReader *pfile)
6328 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
6330 (void) cppReader_popBuffer (pfile);
6333 if (pfile->token_buffer != NULL)
6335 sfree (pfile->token_buffer);
6336 pfile->token_buffer = NULL;
6339 while (pfile->if_stack != NULL)
6341 cppIfStackFrame *temp = pfile->if_stack;
6342 pfile->if_stack = temp->next;
6346 while (pfile->all_include_files != NULL)
6348 struct file_name_list *temp = pfile->all_include_files;
6349 pfile->all_include_files = temp->next;
6350 /*@-dependenttrans@*/
6351 cstring_free (temp->fname);
6352 /*@=dependenttrans@*/
6356 cppReader_hashCleanup ();
6360 ** Get the file-mode and data size of the file open on FD
6361 ** and store them in *MODE_POINTER and *SIZE_POINTER.
6365 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
6369 if (fstat (fd, &sbuf) < 0) {
6373 if (mode_pointer != NULL)
6375 *mode_pointer = sbuf.st_mode;
6378 if (size_pointer != NULL)
6380 *size_pointer = (size_t) sbuf.st_size;
6386 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
6387 retrying if necessary. Return a negative value if an error occurs,
6388 otherwise return the actual number of bytes read,
6389 which must be LEN unless end-of-file was reached. */
6391 static int safe_read (int desc, char *ptr, int len)
6397 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
6398 /*@-compdef@*/ /* ptr is an out parameter */
6399 int nchars = _read (desc, ptr, (unsigned) left);
6402 ssize_t nchars = read (desc, ptr, size_fromInt (left));
6411 return (int) nchars;
6425 /* Initialize PMARK to remember the current position of PFILE. */
6428 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
6430 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6432 pmark->next = pbuf->marks;
6434 pbuf->marks = pmark;
6438 pmark->position = pbuf->cur - pbuf->buf;
6441 /* Cleanup PMARK - we no longer need it. */
6443 void parseClearMark (struct parse_marker *pmark)
6445 struct parse_marker **pp = &pmark->buf->marks;
6447 for (; ; pp = &(*pp)->next)
6449 llassert (*pp != NULL);
6450 if (*pp == pmark) break;
6456 /* Backup the current position of PFILE to that saved in PMARK. */
6459 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
6461 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6463 if (pbuf != pmark->buf)
6465 cpp_setLocation (pfile);
6466 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
6469 llassert (pbuf->buf != NULL);
6470 pbuf->cur = pbuf->buf + pmark->position;
6473 /* Reset PMARK to point to the current position of PFILE. (Same
6474 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
6477 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
6479 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6481 if (pbuf != pmark->buf)
6483 cpp_setLocation (pfile);
6484 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
6487 pmark->position = pbuf->cur - pbuf->buf;
6490 void cppReader_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
6492 struct cppOptions *opts = CPPOPTIONS (pfile);
6495 /* The code looks at the defaults through this pointer, rather than through
6496 the constant structure above. This pointer gets changed if an environment
6497 variable specifies other defaults. */
6499 struct default_include *include_defaults = include_defaults_array;
6501 /* Add dirs from CPATH after dirs from -I. */
6502 /* There seems to be confusion about what CPATH should do,
6503 so for the moment it is not documented. */
6504 /* Some people say that CPATH should replace the standard include dirs,
6505 but that seems pointless: it comes before them, so it overrides them
6508 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
6510 if (cstring_isDefined (xp) && !opts->no_standard_includes)
6512 path_include (pfile, cstring_toCharsSafe (xp));
6515 /* Now that dollars_in_ident is known, initialize is_idchar. */
6516 initialize_char_syntax (opts);
6518 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
6519 and option processing. */
6521 initialize_builtins (pfile);
6523 /* Do standard #defines and assertions
6524 that identify system and machine type. */
6526 if (!opts->inhibit_predefs) {
6527 char *p = (char *) dmalloc (strlen (predefs) + 1);
6528 strcpy (p, predefs);
6534 while (*p == ' ' || *p == '\t')
6539 /* Handle -D options. */
6540 if (p[0] == '-' && p[1] == 'D')
6544 while (*p && *p != ' ' && *p != '\t')
6554 if (opts->debug_output)
6556 output_line_command (pfile, 0, same_file);
6559 cppReader_define (pfile, q);
6561 while (*p == ' ' || *p == '\t')
6575 opts->done_initializing = 1;
6577 { /* Read the appropriate environment variable and if it exists
6578 replace include_defaults with the listed path. */
6582 int win32_buf_size = 0; /* memory we need to allocate */
6585 if (opts->cplusplus)
6587 epath = getenv ("CPLUS_INCLUDE_PATH");
6591 epath = getenv ("C_INCLUDE_PATH");
6595 ** If the environment var for this language is set,
6596 ** add to the default list of include directories.
6599 if (epath != NULL) {
6600 char *nstore = (char *) dmalloc (strlen (epath) + 2);
6602 char *startp, *endp;
6605 /* if we have a posix path list, convert to win32 path list */
6606 if (cygwin32_posix_path_list_p (epath))
6608 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
6609 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
6610 cygwin32_posix_to_win32_path_list (epath, win32epath);
6614 for (num_dirs = 1, startp = epath; *startp; startp++)
6616 if (*startp == PATH_SEPARATOR)
6622 = (struct default_include *) dmalloc ((num_dirs
6623 * sizeof (struct default_include))
6624 + sizeof (include_defaults_array));
6627 startp = endp = epath;
6630 /* Handle cases like c:/usr/lib:d:/gcc/lib */
6631 if ((*endp == PATH_SEPARATOR) || *endp == 0)
6633 strncpy (nstore, startp, size_fromInt (endp - startp));
6636 strcpy (nstore, ".");
6640 nstore[endp-startp] = '\0';
6643 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
6644 include_defaults[num_dirs].cplusplus = opts->cplusplus;
6645 include_defaults[num_dirs].cxx_aware = 1;
6652 endp = startp = endp + 1;
6659 /* Put the usual defaults back in at the end. */
6660 memcpy ((char *) &include_defaults[num_dirs],
6661 (char *) include_defaults_array,
6662 sizeof (include_defaults_array));
6665 /*@-branchstate@*/ } /*@=branchstate@*/
6668 cppReader_appendIncludeChain (pfile, opts->before_system,
6669 opts->last_before_system);
6670 opts->first_system_include = opts->before_system;
6672 /* Unless -fnostdinc,
6673 tack on the standard include file dirs to the specified list */
6674 if (!opts->no_standard_includes) {
6675 struct default_include *p = include_defaults;
6676 char *specd_prefix = opts->include_prefix;
6677 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
6678 int default_len = 0;
6680 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6681 if (default_prefix != NULL) {
6682 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
6683 default_len = strlen (default_prefix) - 7;
6684 default_prefix[default_len] = 0;
6688 /* Search "translated" versions of GNU directories.
6689 These have /usr/local/lib/gcc... replaced by specd_prefix. */
6690 if (specd_prefix != 0 && default_len != 0)
6691 for (p = include_defaults; p->fname != NULL; p++) {
6692 /* Some standard dirs are only for C++. */
6694 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
6695 /* Does this dir start with the prefix? */
6696 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
6697 size_fromInt (default_len)))
6699 /* Yes; change prefix and add to search list. */
6700 struct file_name_list *nlist
6701 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6702 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
6703 char *str = (char *) dmalloc (this_len + 1);
6704 strcpy (str, specd_prefix);
6705 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
6708 nlist->fname = cstring_fromChars (str);
6709 nlist->control_macro = 0;
6710 nlist->c_system_include_path = !p->cxx_aware;
6711 nlist->got_name_map = 0;
6713 cppReader_addIncludeChain (pfile, nlist);
6714 if (opts->first_system_include == 0)
6716 opts->first_system_include = nlist;
6722 /* Search ordinary names for GNU include directories. */
6724 for (p = include_defaults; p->fname != NULL; p++)
6726 /* Some standard dirs are only for C++. */
6728 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
6730 struct file_name_list *nlist
6731 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6732 nlist->control_macro = 0;
6733 nlist->c_system_include_path = !p->cxx_aware;
6734 nlist->fname = p->fname;
6735 nlist->got_name_map = 0;
6738 cppReader_addIncludeChain (pfile, nlist);
6740 if (opts->first_system_include == 0)
6742 opts->first_system_include = nlist;
6746 sfree (default_prefix);
6749 /* Tack the after_include chain at the end of the include chain. */
6750 cppReader_appendIncludeChain (pfile, opts->after_include,
6751 opts->last_after_include);
6753 if (opts->first_system_include == 0)
6755 opts->first_system_include = opts->after_include;
6758 /* With -v, print the list of dirs to search. */
6759 if (opts->verbose) {
6760 struct file_name_list *p;
6761 fprintf (stderr, "#include \"...\" search starts here:\n");
6763 for (p = opts->include; p != NULL; p = p->next) {
6764 if (p == opts->first_bracket_include)
6765 fprintf (stderr, "#include <...> search starts here:\n");
6767 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
6769 fprintf (stderr, "End of search list.\n");
6773 int cppReader_startProcess (cppReader *pfile, cstring fname)
6777 struct cppOptions *opts = CPPOPTIONS (pfile);
6779 fp = cppReader_pushBuffer (pfile, NULL, 0);
6786 if (opts->in_fname == NULL)
6788 opts->in_fname = cstring_makeLiteralTemp ("");
6791 fp->fname = opts->in_fname;
6792 fp->nominal_fname = fp->fname;
6795 /* Copy the entire contents of the main input file into
6796 the stacked input buffer previously allocated for it. */
6798 if (cstring_isEmpty (fname))
6800 fname = cstring_makeLiteralTemp ("");
6803 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
6805 cppReader_error (pfile,
6806 message ("Error opening %s for reading: %s",
6807 fname, lldecodeerror (errno)));
6816 if (finclude (pfile, f, fname, 0, NULL))
6818 output_line_command (pfile, 0, same_file);
6824 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
6826 return pfile->buffer;
6829 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
6831 llassert (pfile->buffer != NULL);
6832 return pfile->buffer;
6835 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
6837 llassert (buf->buf != NULL);
6838 return (buf->buf + buf->line_base);
6841 int cppBufPeek (cppBuffer *buf)
6843 if (buf->cur == NULL || buf->rlimit == NULL) {
6847 if (buf->cur < buf->rlimit) {
6854 bool cppBuffer_isMacro (cppBuffer *buf)
6858 return (buf->cleanup == cppReader_macroCleanup);
6865 ** Returns true if the macro should be checked, false
6866 ** if it should be expanded normally.
6869 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
6870 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
6871 static bool expectiter = FALSE; /* preceeded by @iter@ */
6872 static bool expectenditer = FALSE; /* second after @iter@ */
6873 static bool expectfunction = FALSE; /* preceeded by @function@ */
6874 static bool expectconstant = FALSE; /* preceeded by @constant@ */
6875 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
6877 static void cpp_setLocation (cppReader *pfile)
6882 if (pfile->buffer != NULL)
6884 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname))
6886 cstring fname = cppReader_getBuffer (pfile)->nominal_fname;
6888 DPRINTF (("Looking up: %s", fname));
6890 if (fileTable_exists (context_fileTable (), fname))
6892 fid = fileTable_lookup (context_fileTable (), fname);
6896 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
6898 fid = fileTable_lookup (context_fileTable (),
6899 cppReader_getBuffer (pfile)->fname);
6904 fid = fileTable_lookup (context_fileTable (),
6905 cppReader_getBuffer (pfile)->fname);
6908 line = cppReader_getBuffer (pfile)->lineno;
6909 fileloc_free (g_currentloc);
6911 if (fileId_isValid (fid))
6913 g_currentloc = fileloc_create (fid, line, 1);
6917 g_currentloc = fileloc_createBuiltin ();
6922 fileloc_free (g_currentloc);
6923 g_currentloc = fileloc_createBuiltin ();
6927 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@*/
6929 bool checkmacro = FALSE;
6930 bool hasParams = FALSE;
6931 bool noexpand = FALSE;
6935 cpp_setLocation (pfile);
6937 DPRINTF (("Should check macro? %s", p));
6939 if (expectiter || expectconstant || expectenditer)
6944 expectenditer = TRUE;
6949 expectconstant = FALSE;
6950 expectenditer = FALSE;
6953 if (notfunction || notparseable)
6955 notfunction = FALSE;
6956 notparseable = FALSE;
6965 llassert (*p == '#');
6968 while (*p == ' ' || *p == '\t')
6973 llassert (*p == 'd'); /* define starts */
6977 while (*p == ' ' || *p == '\t')
6982 sname = cstring_fromChars (p);
6983 DPRINTF (("Check macro: %s", sname));
6985 while (((c = *p) != ' ')
6986 && c != '\0' && c != '('
6987 && c != '\t' && c != '\\' && c != '\n'
6993 hasParams = (c == '(');
6999 notparseable = FALSE;
7001 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7003 DPRINTF (("Clear notfunction"));
7004 notfunction = FALSE;
7019 if (usymtab_existsReal (sname))
7021 uentry ue = usymtab_lookup (sname);
7023 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7025 if (fileloc_isPreproc (uentry_whereLast (ue)))
7031 if (uentry_isSpecified (ue))
7033 checkmacro = context_getFlag (FLG_SPECMACROS);
7039 checkmacro = context_getFlag (FLG_LIBMACROS)
7040 || context_getFlag (FLG_FCNMACROS);
7048 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7050 if (fileloc_isSystemFile (g_currentloc)
7051 && context_getFlag (FLG_SYSTEMDIREXPAND))
7053 ; /* don't check this macro */
7054 DPRINTF (("Don't check 1"));
7062 DPRINTF (("Has params..."));
7064 if (context_getFlag (FLG_FCNMACROS))
7066 if (usymtab_exists (sname))
7069 ** only get here is macro is redefined
7070 ** error reported elsewhere
7073 DPRINTF (("It exists!"));
7078 ** We make it a forward function, since it might be declared elsewhere.
7079 ** After all headers have been processed, we should check the forward
7083 fileloc loc = fileloc_makePreproc (g_currentloc);
7085 /* the line is off-by-one, since the newline was already read */
7090 expectfunction = FALSE;
7093 le = uentry_makeForwardFunction (sname,
7094 typeId_invalid, loc);
7100 /* Do not define here! */
7102 (void) usymtab_addEntry (le);
7106 DPRINTF (("Check: TRUE"));
7110 DPRINTF (("Flag FCN_MACROS not set!"));
7115 DPRINTF (("No params"));
7117 if (context_getFlag (FLG_CONSTMACROS))
7119 bool nocontent = FALSE;
7132 ** Check if there is nothing after the define.
7135 while ((*rest) != '\0' && isspace (*rest))
7142 nocontent = TRUE; /* empty macro, don't check */
7147 if (usymtab_exists (sname))
7153 fileloc loc = fileloc_makePreproc (g_currentloc);
7154 DPRINTF (("Make constant: %s", sname));
7155 le = uentry_makeConstant (sname,
7156 ctype_unknown, loc);
7157 (void) usymtab_addEntry (le);
7160 checkmacro = !nocontent;
7165 if (checkmacro && usymtab_existsType (sname))
7167 DPRINTF (("Making false..."));
7169 ppllerror (message ("Specified type implemented as macro: %s", sname));
7179 if (usymtab_exists (sname))
7181 uentry ue = usymtab_lookupExpose (sname);
7182 fileloc tloc = fileloc_makePreproc (g_currentloc);
7184 uentry_setDefined (ue, tloc);
7185 fileloc_free (tloc);
7186 uentry_setUsed (ue, fileloc_undefined);
7190 fileloc tloc = fileloc_makePreproc (g_currentloc);
7191 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7192 DPRINTF (("Make expanded macro: %s", sname));
7193 DPRINTF (("Not in symbol table: %s", sname));
7195 (void) usymtab_addGlobalEntry (ue);
7196 fileloc_free (tloc);
7201 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7205 static enum cpp_token
7206 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7208 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7211 bool eliminateComment = FALSE;
7213 llassert (pbuf->buf != NULL);
7215 start = pbuf->buf + smark->position;
7217 llassert (pbuf->cur != NULL);
7218 len = pbuf->cur - start;
7221 && start[1] == context_getCommentMarkerChar ())
7225 char *scomment = start + 2;
7227 start[0] = BEFORE_COMMENT_MARKER[0];
7228 start[1] = BEFORE_COMMENT_MARKER[1];
7230 llassert (start[len - 2] == '*');
7231 start[len - 2] = AFTER_COMMENT_MARKER[0];
7233 llassert (start[len - 1] == '/');
7234 start[len - 1] = AFTER_COMMENT_MARKER[1];
7236 cppReader_reserve(pfile, size_fromInt (1 + len));
7237 cppReader_putCharQ (pfile, c);
7239 cpp_setLocation (pfile);
7241 if (mstring_equalPrefix (scomment, "ignore"))
7243 if (!context_getFlag (FLG_NOCOMMENTS))
7245 context_enterSuppressRegion ();
7248 else if (mstring_equalPrefix (scomment, "end"))
7250 if (!context_getFlag (FLG_NOCOMMENTS))
7252 context_exitSuppressRegion ();
7255 else if (mstring_equalPrefix (scomment, "notparseable"))
7257 notparseable = TRUE;
7259 eliminateComment = TRUE;
7261 else if (mstring_equalPrefix (scomment, "notfunction"))
7265 eliminateComment = TRUE;
7267 else if (mstring_equalPrefix (scomment, "iter"))
7271 else if (mstring_equalPrefix (scomment, "function"))
7273 expectfunction = TRUE;
7275 else if (mstring_equalPrefix (scomment, "constant"))
7277 expectconstant = TRUE;
7281 char sChar = *scomment;
7287 char *rest = scomment + 1;
7289 if (mstring_equalPrefix (rest, "commentchar"))
7291 eliminateComment = TRUE;
7295 ppllerror (cstring_makeLiteral
7296 ("Cannot restore commentchar"));
7300 char *next = scomment + 12; /* strlen commentchar = 12 */
7302 if (*next != ' ' && *next != '\t' && *next != '\n')
7306 ("Syntactic commentchar comment is not followed by a "
7307 "whitespace character: %c",
7312 char cchar = *(next + 1);
7317 (cstring_makeLiteral
7318 ("Cannot set commentchar to NUL"));
7322 context_setCommentMarkerChar (cchar);
7323 /* setComment = TRUE; */
7328 else if (mstring_equalPrefix (scomment, "nestcomment"))
7330 /* fix from Mike Miller <MikeM@xata.com> */
7331 context_fileSetFlag (FLG_NESTCOMMENT,
7332 ynm_fromCodeChar (sChar));
7334 else if (mstring_equalPrefix (rest, "namechecks"))
7336 context_fileSetFlag (FLG_NAMECHECKS,
7337 ynm_fromCodeChar (sChar));
7339 else if (mstring_equalPrefix (rest, "macroredef"))
7341 context_fileSetFlag (FLG_MACROREDEF,
7342 ynm_fromCodeChar (sChar));
7344 else if (mstring_equalPrefix (rest, "usevarargs"))
7346 context_fileSetFlag (FLG_USEVARARGS,
7347 ynm_fromCodeChar (sChar));
7349 else if (mstring_equalPrefix (rest, "nextlinemacros"))
7351 context_fileSetFlag (FLG_MACRONEXTLINE,
7352 ynm_fromCodeChar (sChar));
7354 else if (mstring_equalPrefix (rest, "allmacros")
7355 || mstring_equalPrefix (rest, "fcnmacros")
7356 || mstring_equalPrefix (rest, "constmacros"))
7360 if (mstring_equalPrefix (rest, "allmacros"))
7364 else if (mstring_equalPrefix (rest, "fcnmacros"))
7370 llassert (mstring_equalPrefix (rest, "constmacros"));
7371 fl = FLG_CONSTMACROS;
7375 context_fileSetFlag (fl, ynm_fromCodeChar (sChar));
7376 notfunction = FALSE;
7389 if (eliminateComment)
7394 /* Replaces comment char's in start with spaces */
7396 for (i = 2; i < len - 2; i++)
7398 if (start[i] == BEFORE_COMMENT_MARKER[0]
7399 || start[i] == BEFORE_COMMENT_MARKER[1]
7400 || start[i] == context_getCommentMarkerChar ())
7406 cppReader_putStrN (pfile, start, size_fromInt (len));
7407 parseClearMark (smark);
7417 ** Output the comment as all spaces so line/column
7418 ** in output file is still correct.
7422 cstring lintcomment = cstring_undefined;
7424 if (context_getFlag (FLG_LINTCOMMENTS))
7426 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
7428 lintcomment = cstring_makeLiteralTemp ("l_notreach");
7430 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
7432 lintcomment = cstring_makeLiteralTemp ("l_printfli");
7434 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
7436 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
7438 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
7440 lintcomment = cstring_makeLiteralTemp ("l_argsus");
7442 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
7444 lintcomment = cstring_makeLiteralTemp ("l_fallth");
7448 lintcomment = cstring_undefined;
7453 lintcomment = cstring_undefined;
7456 if (cstring_isDefined (lintcomment))
7458 c = BEFORE_COMMENT_MARKER[0];
7459 start[0] = BEFORE_COMMENT_MARKER[1];
7461 llassert (cstring_length (lintcomment) == len - 3);
7463 for (i = 1; i < len - 2; i++)
7465 start[i] = cstring_getChar (lintcomment, i);
7468 start[len - 2] = AFTER_COMMENT_MARKER[0];
7469 start[len - 1] = AFTER_COMMENT_MARKER[1];
7473 /* Replaces char's in start with spaces */
7474 for (i = 0; i < len; i++)
7478 && start[i + 1] == '*') {
7479 (void) cppoptgenerror (FLG_NESTCOMMENT,
7480 message ("Comment starts inside comment"),
7484 if (start[i] != '\n')
7491 cppReader_reserve (pfile, size_fromInt (1 + len));
7492 cppReader_putCharQ (pfile, c);
7493 cppReader_putStrN (pfile, start, size_fromInt (len));
7494 parseClearMark (smark);
7500 static int cpp_openIncludeFile (char *filename)
7502 int res = open (filename, O_RDONLY, 0666);
7506 if (!fileTable_exists (context_fileTable (),
7507 cstring_fromChars (filename)))
7509 (void) fileTable_addHeaderFile (context_fileTable (),
7510 cstring_fromChars (filename));
7514 DPRINTF (("File already exists: %s", filename));
7522 static bool cpp_skipIncludeFile (cstring fname)
7524 if (context_isSystemDir (fname))
7526 DPRINTF (("System dir: %s", fname));
7528 if (lcllib_isSkipHeader (fname))
7530 DPRINTF (("Skip include TRUE: %s", fname));
7534 if (context_getFlag (FLG_SKIPSYSHEADERS))
7536 DPRINTF (("Skip include TRUE: %s", fname));
7541 if (context_getFlag (FLG_SINGLEINCLUDE))
7543 fname = removePreDirs (fname);
7545 # if defined (WIN32) || defined (OS2)
7546 cstring_replaceAll (fname, '\\', '/');
7549 if (fileTable_exists (context_fileTable (), fname))
7551 DPRINTF (("Skip include TRUE: %s", fname));
7556 DPRINTF (("Skip include FALSE: %s", fname));
7560 static int cpp_peekN (cppReader *pfile, int n)
7562 cppBuffer *buf = cppReader_getBuffer (pfile);
7564 llassert (buf->cur != NULL);
7566 return (buf->rlimit - buf->cur >= (n)
7571 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
7576 void cppBuffer_forward (cppBuffer *buf, int n)
7578 llassert (buf->cur != NULL);