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;
1267 char *line_end = NULL;
1268 size_t old_written = cppReader_getWritten (pfile);
1269 int nspaces = cppSkipHspace (pfile);
1271 c = cppReader_peekC (pfile);
1273 if (c >= '0' && c <= '9')
1275 /* Handle # followed by a line number. */
1276 if (cppReader_isPedantic (pfile))
1278 cppReader_pedwarnLit
1280 cstring_makeLiteralTemp ("`#' followed by integer"));
1283 (void) do_line (pfile, NULL);
1284 goto done_a_directive;
1288 /* Now find the directive name. */
1290 cppReader_putChar (pfile, '#');
1292 parse_name (pfile, cppReader_getC (pfile));
1294 llassert (pfile->token_buffer != NULL);
1295 ident = pfile->token_buffer + old_written + 1;
1297 ident_length = cppReader_getPWritten (pfile) - ident;
1299 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1301 /* A line of just `#' becomes blank. */
1305 for (kt = directive_table; ; kt++)
1307 if (kt->length <= 0)
1309 return 0; /* goto not_a_directive; */
1312 if (kt->length == ident_length
1313 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1319 if (kt->command_reads_line)
1325 /* Nonzero means do not delete comments within the directive.
1326 #define needs this when -traditional. */
1327 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1328 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1329 CPPOPTIONS (pfile)->put_out_comments = comments;
1330 after_ident = cppReader_getWritten (pfile);
1331 copy_rest_of_line (pfile);
1332 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1336 /* For #pragma and #define, we may want to pass through the directive.
1337 Other directives may create output, but we don't want the directive
1338 itself out, so we pop it now. For example #include may write a #line
1339 command (see comment in do_include), and conditionals may emit
1340 #failed ... #endfailed stuff. But note that popping the buffer
1341 means the parameters to kt->func may point after pfile->limit
1342 so these parameters are invalid as soon as something gets appended
1343 to the token_buffer. */
1345 line_end = cppReader_getPWritten (pfile);
1348 if (!kt->pass_thru && kt->type != T_DEFINE)
1350 cppReader_setWritten (pfile, old_written);
1353 llassert (pfile->token_buffer != NULL);
1355 /* was kt->pass_thru || */
1357 if (kt->type == T_DEFINE
1358 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1360 char *p = pfile->token_buffer + old_written;
1363 ** Still need to record value for preprocessing, so
1364 ** #ifdef's, etc. using the value behave correctly.
1367 (void) do_defineAux (pfile, kt,
1368 pfile->token_buffer + after_ident,
1377 SKIP_WHITE_SPACE (p);
1379 llassert (*p == 'd');
1382 llassert (*p == 'e');
1385 llassert (*p == 'f');
1388 llassert (*p == 'i');
1391 llassert (*p == 'n');
1394 llassert (*p == 'e');
1397 ** This is way-bogus. We use the last char to record the number of
1398 ** spaces. Its too hard to get them back into the input stream.
1401 if (nspaces > 9) nspaces = 9;
1403 *p++ = '0' + nspaces;
1405 return 0; /* not_a_directive */
1407 else if (kt->pass_thru)
1409 /* Just leave the entire #define in the output stack. */
1410 return 0; /* not_a_directive */
1413 else if (kt->type == T_DEFINE
1414 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1416 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1417 SKIP_WHITE_SPACE (p);
1419 while (is_idchar[(int) *p])
1425 cppReader_putChar (pfile, '\n');
1427 else if (kt->type == T_DEFINE)
1429 cppReader_setWritten (pfile, old_written);
1440 llassert (kt->func != NULL);
1441 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1446 /* Pass a directive through to the output file.
1447 BUF points to the contents of the directive, as a contiguous string.
1448 LIMIT points to the first character past the end of the directive.
1449 KEYWORD is the keyword-table entry for the directive. */
1452 pass_thru_directive (char *buf, char *limit,
1454 struct directive *keyword)
1456 int keyword_length = keyword->length;
1458 cppReader_reserve (pfile,
1459 size_fromInt (2 + keyword_length + (limit - buf)));
1460 cppReader_putCharQ (pfile, '#');
1461 /*@-observertrans@*/
1462 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1463 size_fromInt (keyword_length));
1464 /*:=observertrans@*/
1466 if (limit != buf && buf[0] != ' ')
1468 /* Was a bug, since reserve only used 1 + ... */
1469 cppReader_putCharQ (pfile, ' ');
1472 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1475 /* Read a replacement list for a macro with parameters.
1476 Build the DEFINITION structure.
1477 Reads characters of text starting at BUF until END.
1478 ARGLIST specifies the formal parameters to look for
1479 in the text of the definition; NARGS is the number of args
1480 in that list, or -1 for a macro name that wants no argument list.
1481 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1482 and NAMELEN is its length in characters.
1484 Note that comments, backslash-newlines, and leading white space
1485 have already been deleted from the argument. */
1488 collect_expansion (cppReader *pfile, char *buf, char *limit,
1489 int nargs, /*@null@*/ struct arglist *arglist)
1492 char *p, *lastp, *exp_p;
1493 struct reflist *endpat = NULL;
1494 /* Pointer to first nonspace after last ## seen. */
1496 /* Pointer to first nonspace after last single-# seen. */
1497 char *stringify = 0;
1499 char expected_delimiter = '\0';
1502 /* Scan thru the replacement list, ignoring comments and quoted
1503 strings, picking up on the macro calls. It does a linear search
1504 thru the arg list on every potential symbol. Profiling might say
1505 that something smarter should happen. */
1510 /* Find the beginning of the trailing whitespace. */
1513 while (p < limit && is_space[(int) limit[-1]])
1518 /* Allocate space for the text in the macro definition.
1519 Leading and trailing whitespace chars need 2 bytes each.
1520 Each other input char may or may not need 1 byte,
1521 so this is an upper bound. The extra 5 are for invented
1522 leading and trailing newline-marker and final null. */
1523 maxsize = (sizeof (*defn) + (limit - p) + 5);
1525 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1534 defn = (DEFINITION *) dmalloc (maxsize);
1535 defn->noExpand = FALSE;
1537 defn->pattern = NULL;
1538 defn->nargs = nargs;
1539 defn->predefined = NULL;
1541 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1544 defn->rest_args = NULL;
1545 defn->args.argnames = NULL;
1551 /* Add one initial space escape-marker to prevent accidental
1552 token-pasting (often removed by macroexpand). */
1556 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1557 cppReader_errorLit (pfile,
1558 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1562 /* Process the main body of the definition. */
1564 int skipped_arg = 0;
1565 register char c = *p++;
1569 if (!cppReader_isTraditional (pfile)) {
1573 if (expected_delimiter != '\0')
1575 if (c == expected_delimiter)
1576 expected_delimiter = '\0';
1580 expected_delimiter = c;
1582 /*@switchbreak@*/ break;
1585 if (p < limit && (expected_delimiter != '\0'))
1587 /* In a string, backslash goes through
1588 and makes next char ordinary. */
1591 /*@switchbreak@*/ break;
1594 /* An '@' in a string or character constant stands for itself,
1595 and does not need to be escaped. */
1596 if (expected_delimiter == '\0')
1601 /*@switchbreak@*/ break;
1604 /* # is ordinary inside a string. */
1605 if (expected_delimiter != '\0')
1607 /*@switchbreak@*/ break;
1610 if (p < limit && *p == '#') {
1611 /* ##: concatenate preceding and following tokens. */
1612 /* Take out the first #, discard preceding whitespace. */
1616 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1622 /* Skip the second #. */
1624 /* Discard following whitespace. */
1625 SKIP_WHITE_SPACE (p);
1629 cppReader_errorLit (pfile,
1630 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1632 } else if (nargs >= 0) {
1633 /* Single #: stringify following argument ref.
1634 Don't leave the # in the expansion. */
1636 SKIP_WHITE_SPACE (p);
1637 if (p == limit || ! is_idstart[(int) *p]
1638 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1639 cppReader_errorLit (pfile,
1640 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1647 /*@switchbreak@*/ break;
1650 /* In -traditional mode, recognize arguments inside strings and
1651 and character constants, and ignore special properties of #.
1652 Arguments inside strings are considered "stringified", but no
1653 extra quote marks are supplied. */
1657 if (expected_delimiter != '\0') {
1658 if (c == expected_delimiter)
1659 expected_delimiter = '\0';
1661 expected_delimiter = c;
1662 /*@switchbreak@*/ break;
1665 /* Backslash quotes delimiters and itself, but not macro args. */
1666 if (expected_delimiter != '\0' && p < limit
1667 && (*p == expected_delimiter || *p == '\\')) {
1671 /*@switchbreak@*/ break;
1674 if (expected_delimiter != '\0') /* No comments inside strings. */
1675 /*@switchbreak@*/ break;
1677 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1678 this must be -traditional. So replace the comment with
1682 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1687 /*@switchbreak@*/ break;
1691 /* Handle the start of a symbol. */
1692 if (is_idchar[(int) c] && nargs > 0) {
1693 char *id_beg = p - 1;
1697 while (p != limit && is_idchar[(int) *p])
1702 id_len = p - id_beg;
1704 if (is_idstart[(int) c]
1705 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1706 register struct arglist *arg;
1708 for (arg = arglist; arg != NULL; arg = arg->next) {
1709 struct reflist *tpat;
1711 if (arg->name[0] == c
1712 && arg->length == id_len
1713 && strncmp (arg->name, id_beg, size_fromInt (id_len)) == 0) {
1716 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1717 if (cppReader_isTraditional (pfile)) {
1718 cppReader_warning (pfile,
1719 message ("macro argument `%x' is stringified.",
1720 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1722 cppReader_warning (pfile,
1723 message ("macro arg `%x' would be stringified with -traditional.",
1724 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1727 /* If ANSI, don't actually substitute inside a string. */
1728 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1729 /*@innerbreak@*/ break;
1730 /* make a pat node for this arg and append it to the end of
1732 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1734 tpat->raw_before = (concat == id_beg);
1735 tpat->raw_after = 0;
1736 tpat->rest_args = arg->rest_args;
1737 tpat->stringify = (cppReader_isTraditional (pfile)
1738 ? expected_delimiter != '\0'
1739 : stringify == id_beg);
1743 defn->pattern = tpat;
1747 endpat->next = tpat;
1749 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1753 tpat->argno = arg->argno;
1754 tpat->nchars = exp_p - lastp;
1758 SKIP_WHITE_SPACE (p1);
1760 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1762 tpat->raw_after = 1;
1765 lastp = exp_p; /* place to start copying from next time */
1768 /*@innerbreak@*/ break;
1773 /* If this was not a macro arg, copy it into the expansion. */
1774 if (skipped_arg == 0) {
1775 register char *lim1 = p;
1783 if (stringify == id_beg)
1784 cppReader_errorLit (pfile,
1785 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1790 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1792 /* If ANSI, put in a "@ " marker to prevent token pasting.
1793 But not if "inside a string" (which in ANSI mode
1794 happens only for -D option). */
1801 defn->length = size_fromInt (exp_p - defn->expansion);
1803 /* Crash now if we overrun the allocated size. */
1804 if (defn->length + 1 > maxsize)
1806 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1813 * special extension string that can be added to the last macro argument to
1814 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1815 * #define wow(a, b...) process (b, a, b)
1816 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1817 * { wow (one, two); } -> { process (two, one, two); }
1818 * if this "rest_arg" is used with the concat token '##' and if it is not
1819 * supplied then the token attached to with ## will not be outputted. Ex:
1820 * #define wow (a, b...) process (b ## , a, ## b)
1821 * { wow (1, 2); } -> { process (2, 1, 2); }
1822 * { wow (one); } -> { process (one); {
1825 /*@-readonlytrans@*/
1826 static char rest_extension[] = "...";
1827 /*:=readonlytrans@*/
1830 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1832 /* Create a DEFINITION node from a #define directive. Arguments are
1833 as for do_define. */
1835 static /*@null@*/ MACRODEF
1836 create_definition (char *buf, char *limit,
1837 cppReader *pfile, bool predefinition,
1840 char *bp; /* temp ptr into input buffer */
1841 char *symname; /* remember where symbol name starts */
1842 int sym_length; /* and how long it is */
1843 int rest_args = 0; /* really int! */
1846 cstring file = (CPPBUFFER (pfile) != NULL)
1847 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
1849 int arglengths = 0; /* Accumulate lengths of arg names
1850 plus number of args. */
1853 cppBuffer_lineAndColumn (CPPBUFFER (pfile), &line, &col);
1857 while (is_hor_space[(int) *bp])
1862 symname = bp; /* remember where it starts */
1864 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
1868 /* Lossage will occur if identifiers or control keywords are broken
1869 across lines using backslash. This is not the right place to take
1873 struct arglist *arg_ptrs = NULL;
1876 bp++; /* skip '(' */
1877 SKIP_WHITE_SPACE (bp);
1879 /* Loop over macro argument names. */
1882 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
1884 temp->next = arg_ptrs;
1885 temp->argno = argno++;
1886 temp->rest_args = 0;
1892 cppReader_pedwarn (pfile,
1893 message ("another parameter follows `%s'",
1894 cstring_fromChars (rest_extension)));
1897 if (!is_idstart[(int) *bp])
1899 cppReader_pedwarnLit (pfile,
1900 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
1903 /* Find the end of the arg name. */
1904 while (is_idchar[(int) *bp])
1907 /* do we have a "special" rest-args extension here? */
1908 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
1909 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
1912 temp->rest_args = 1;
1913 /*@innerbreak@*/ break;
1917 temp->length = bp - temp->name;
1921 bp += REST_EXTENSION_LENGTH;
1924 arglengths += temp->length + 2;
1925 SKIP_WHITE_SPACE (bp);
1927 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1928 cppReader_errorLit (pfile,
1929 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
1935 SKIP_WHITE_SPACE (bp);
1938 cppReader_errorLit (pfile,
1939 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
1943 struct arglist *otemp;
1945 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1947 if (temp->length == otemp->length &&
1948 strncmp (temp->name, otemp->name, size_fromInt (temp->length)) == 0) {
1949 cstring name = cstring_copyLength (temp->name, temp->length);
1950 cppReader_error (pfile,
1951 message ("duplicate argument name `%x' in `#define'", name));
1958 ++bp; /* skip paren */
1959 SKIP_WHITE_SPACE (bp);
1960 /* now everything from bp before limit is the definition. */
1961 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1962 defn->rest_args = rest_args;
1964 /* Now set defn->args.argnames to the result of concatenating
1965 the argument names in reverse order
1966 with comma-space between them. */
1967 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
1970 struct arglist *temp;
1972 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
1973 memcpy (&defn->args.argnames[i], temp->name, size_fromInt (temp->length));
1975 if (temp->next != 0) {
1976 defn->args.argnames[i++] = ',';
1977 defn->args.argnames[i++] = ' ';
1981 defn->args.argnames[i] = '\0';
1986 /* Simple expansion or empty definition. */
1990 if (is_hor_space[(int) *bp]) {
1992 SKIP_WHITE_SPACE (bp);
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 '\\': case ']': case '^': case '{':
1999 case '|': case '}': case '~':
2000 cppReader_warning (pfile,
2001 message ("Missing white space after #define %x",
2002 cstring_prefix (cstring_fromChars (symname),
2007 cppReader_pedwarn (pfile,
2008 message ("Missing white space after #define %x",
2009 cstring_prefix (cstring_fromChars (symname),
2015 /* now everything from bp before limit is the definition. */
2016 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2017 defn->args.argnames = mstring_createEmpty ();
2020 defn->noExpand = noExpand;
2021 DPRINTF (("No expand: %d", noExpand));
2025 /* not: llassert (cstring_isUndefined (defn->file)); */
2028 /* OP is null if this is a predefinition */
2029 defn->predefined = predefinition;
2031 mdef.symnam = symname;
2032 mdef.symlen = sym_length;
2042 /* Check a purported macro name SYMNAME, and yield its length.
2043 USAGE is the kind of name this is intended for. */
2045 int cppReader_checkMacroName (cppReader *pfile,
2052 for (p = symname; is_idchar[(int) *p]; p++)
2057 sym_length = size_fromInt (p - symname);
2060 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
2061 cppReader_error (pfile, message ("invalid %s name", usage));
2062 else if (!is_idstart[(int) *symname])
2064 char *msg = (char *) dmalloc (sym_length + 1);
2065 memcpy (msg, symname, sym_length);
2066 msg[sym_length] = '\0';
2067 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2068 cstring_fromChars (msg)));
2073 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2075 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2079 return size_toInt (sym_length);
2082 /* Return zero if two DEFINITIONs are isomorphic. */
2085 compare_defs (DEFINITION *d1, DEFINITION *d2)
2087 register struct reflist *a1, *a2;
2088 register char *p1 = d1->expansion;
2089 register char *p2 = d2->expansion;
2092 if (d1->nargs != d2->nargs)
2097 llassert (d1->args.argnames != NULL);
2098 llassert (d2->args.argnames != NULL);
2100 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2105 for (a1 = d1->pattern, a2 = d2->pattern;
2106 (a1 != NULL) && (a2 != NULL);
2107 a1 = a1->next, a2 = a2->next) {
2108 if (!((a1->nchars == a2->nchars
2109 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2110 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2111 || a1->argno != a2->argno
2112 || a1->stringify != a2->stringify
2113 || a1->raw_before != a2->raw_before
2114 || a1->raw_after != a2->raw_after)
2123 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2124 p2, d2->length - (p2 - d2->expansion), 1))
2130 /* Return TRUE if two parts of two macro definitions are effectively different.
2131 One of the parts starts at BEG1 and has LEN1 chars;
2132 the other has LEN2 chars at BEG2.
2133 Any sequence of whitespace matches any other sequence of whitespace.
2134 FIRST means these parts are the first of a macro definition;
2135 so ignore leading whitespace entirely.
2136 LAST means these parts are the last of a macro definition;
2137 so ignore trailing whitespace entirely. */
2140 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2142 char *end1 = beg1 + len1;
2143 char *end2 = beg2 + len2;
2146 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2147 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2150 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2151 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2153 while (beg1 != end1 && beg2 != end2) {
2154 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2155 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2156 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2157 } else if (*beg1 == *beg2) {
2161 return (beg1 != end1) || (beg2 != end2);
2164 /* Process a #define command.
2165 BUF points to the contents of the #define command, as a contiguous string.
2166 LIMIT points to the first character past the end of the definition.
2167 KEYWORD is the keyword-table entry for #define,
2168 or NULL for a "predefined" macro. */
2171 do_defineAux (cppReader *pfile, struct directive *keyword,
2172 char *buf, char *limit, bool noExpand)
2178 DPRINTF (("Define aux: %d", noExpand));
2180 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2185 hashcode = hashf (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2187 DPRINTF (("Macro: %s / %s",
2188 cstring_copyLength (mdef.symnam, mdef.symlen),
2189 bool_unparse (noExpand)));
2191 if ((hp = cppReader_lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL)
2195 /* Redefining a precompiled key is ok. */
2196 if (hp->type == T_PCSTRING)
2198 /* Redefining a macro is ok if the definitions are the same. */
2199 else if (hp->type == T_MACRO)
2200 ok = !compare_defs (mdef.defn, hp->value.defn);
2201 /* Redefining a constant is ok with -D. */
2202 else if (hp->type == T_CONST)
2203 ok = !CPPOPTIONS (pfile)->done_initializing;
2208 /* Print the warning if it's not ok. */
2212 ** If we are passing through #define and #undef directives, do
2213 ** that for this re-definition now.
2216 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2218 /* llassert (keyword != NULL); */
2219 pass_thru_directive (buf, limit, pfile, keyword);
2222 cpp_setLocation (pfile);
2224 if (hp->type == T_MACRO)
2226 if (hp->value.defn->noExpand)
2228 ; /* error will be reported checking macros */
2234 message ("Macro %q already defined",
2235 cstring_copyLength (mdef.symnam, mdef.symlen)),
2236 message ("%q: Previous definition of %q",
2237 fileloc_unparseRaw (hp->value.defn->file,
2238 (int) hp->value.defn->line),
2239 cstring_copyLength (mdef.symnam, mdef.symlen)));
2244 genppllerror (FLG_MACROREDEF,
2245 message ("Macro %q already defined",
2246 cstring_copyLength (mdef.symnam,
2252 /* Replace the old definition. */
2254 hp->value.defn = mdef.defn;
2259 ** If we are passing through #define and #undef directives, do
2260 ** that for this new definition now.
2265 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2267 pass_thru_directive (buf, limit, pfile, keyword);
2270 DPRINTF (("Define macro: %s / %d",
2271 mdef.symnam, mdef.defn->noExpand));
2273 hn = cppReader_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2275 } /*@=branchstate@*/
2285 do_define (cppReader *pfile, struct directive *keyword,
2286 char *buf, char *limit)
2288 DPRINTF (("Regular do define"));
2289 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2292 /* This structure represents one parsed argument in a macro call.
2293 `raw' points to the argument text as written (`raw_length' is its length).
2294 `expanded' points to the argument's macro-expansion
2295 (its length is `expand_length').
2296 `stringified_length' is the length the argument would have
2298 `use_count' is the number of times this macro arg is substituted
2299 into the macro. If the actual use count exceeds 10,
2300 the value stored is 10. */
2302 /* raw and expanded are relative to ARG_BASE */
2304 #define ARG_BASE ((pfile)->token_buffer)
2307 /* Strings relative to pfile->token_buffer */
2313 int stringified_length;
2318 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2319 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2320 as the new input buffer.
2321 Return the new buffer, or NULL on failure. */
2323 /*@null@*/ /*@exposed@*/ cppBuffer *
2324 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2326 cppBuffer *buf = cppReader_getBuffer (pfile);
2328 if (buf == pfile->buffer_stack)
2330 cppReader_fatalError
2332 message ("%s: macro or `#include' recursion too deep",
2333 (buf->fname != NULL)
2335 : cstring_makeLiteral ("<no name>")));
2336 sfreeEventually (buffer);
2340 llassert (buf != NULL);
2343 memset ((char *) buf, 0, sizeof (*buf));
2344 CPPBUFFER (pfile) = buf;
2346 buf->if_stack = pfile->if_stack;
2347 buf->cleanup = cppReader_nullCleanup;
2348 buf->underflow = cppReader_nullUnderflow;
2350 buf->cur = buf->buf;
2354 buf->alimit = buf->rlimit = buffer + length;
2358 buf->alimit = buf->rlimit = NULL;
2365 cppReader_popBuffer (cppReader *pfile)
2367 cppBuffer *buf = CPPBUFFER (pfile);
2369 llassert (buf != NULL);
2371 (void) (*buf->cleanup) (buf, pfile);
2372 return ++CPPBUFFER (pfile);
2375 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2376 Pop the buffer when done. */
2379 cppReader_scanBuffer (cppReader *pfile)
2381 cppBuffer *buffer = CPPBUFFER (pfile);
2384 enum cpp_token token;
2386 token = cppGetToken (pfile);
2388 if (token == CPP_EOF) /* Should not happen ... */
2393 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2395 (void) cppReader_popBuffer (pfile);
2402 * Rescan a string (which may have escape marks) into pfile's buffer.
2403 * Place the result in pfile->token_buffer.
2405 * The input is copied before it is scanned, so it is safe to pass
2406 * it something from the token_buffer that will get overwritten
2407 * (because it follows cppReader_getWritten). This is used by do_include.
2411 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
2413 register cppBuffer *ip;
2414 char *limit = buf + length;
2415 char *buf1, *p1, *p2;
2417 /* evans - 2001-08-26
2418 ** length is unsigned - this doesn't make sense
2424 /* Set up the input on the input stack. */
2426 buf1 = (char *) dmalloc (length + 1);
2436 buf1[length] = '\0';
2438 ip = cppReader_pushBuffer (pfile, buf1, length);
2443 ip->has_escapes = TRUE;
2445 /* Scan the input, create the output. */
2446 cppReader_scanBuffer (pfile);
2448 cppReader_nullTerminate (pfile);
2452 adjust_position (char *buf, char *limit, int *linep, int *colp)
2458 (*linep)++, (*colp) = 1;
2464 /* Move line_base forward, updating lineno and colno. */
2467 update_position (cppBuffer *pbuf)
2470 char *new_pos = pbuf->cur;
2471 register struct parse_marker *mark;
2473 llassert (pbuf->buf != NULL);
2474 old_pos = pbuf->buf + pbuf->line_base;
2476 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2478 if (pbuf->buf + mark->position < new_pos)
2479 new_pos = pbuf->buf + mark->position;
2481 pbuf->line_base += new_pos - old_pos;
2483 llassert (old_pos != NULL);
2484 llassert (new_pos != NULL);
2486 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2490 cppBuffer_lineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
2491 /*@null@*/ /*@out@*/ int *colp)
2499 } /*@=branchstate@*/
2503 *linep = pbuf->lineno;
2504 *colp = pbuf->colno;
2506 llassert (pbuf->buf != NULL);
2507 llassert (pbuf->cur != NULL);
2509 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2518 /* Return the cppBuffer that corresponds to a file (not a macro). */
2520 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
2522 cppBuffer *ip = cppReader_getBuffer (pfile);
2525 ip != NULL && ip != cppReader_nullBuffer (pfile);
2526 ip = cppBuffer_prevBuffer (ip))
2528 if (ip->fname != NULL)
2538 count_newlines (char *buf, char *limit)
2540 register long count = 0;
2552 * write out a #line command, for instance, after an #include file.
2553 * If CONDITIONAL is nonzero, we can omit the #line if it would
2554 * appear to be a no-op, and we can output a few newlines instead
2555 * if we want to increase the line number by a small amount.
2556 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2560 output_line_command (cppReader *pfile, bool conditional,
2561 enum file_change_code file_change)
2564 cppBuffer *ip = CPPBUFFER (pfile);
2567 llassert (ip != NULL);
2569 if (ip->fname == NULL)
2572 update_position (ip);
2574 if (CPPOPTIONS (pfile)->no_line_commands
2575 || CPPOPTIONS (pfile)->no_output)
2578 buf = CPPBUFFER (pfile);
2580 llassert (buf != NULL);
2585 llassert (ip->cur != NULL);
2587 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2589 if (CPPOPTIONS (pfile)->no_line_commands)
2593 if (line == pfile->lineno)
2596 /* If the inherited line number is a little too small,
2597 output some newlines instead of a #line command. */
2599 if (line > pfile->lineno && line < pfile->lineno + 8)
2601 cppReader_reserve (pfile, 20);
2602 while (line > pfile->lineno)
2604 cppReader_putCharQ (pfile, '\n');
2612 cppReader_reserve (pfile,
2613 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
2616 #ifdef OUTPUT_LINE_COMMANDS
2617 static char sharp_line[] = "#line ";
2619 static char sharp_line[] = "# ";
2621 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
2624 sprintf (cppReader_getPWritten (pfile), "%d ", line);
2625 cppReader_adjustWritten (pfile, strlen (cppReader_getPWritten (pfile)));
2627 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
2629 if (file_change != same_file) {
2630 cppReader_putCharQ (pfile, ' ');
2631 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
2633 /* Tell cc1 if following text comes from a system header file. */
2634 if (ip->system_header_p != '\0') {
2635 cppReader_putCharQ (pfile, ' ');
2636 cppReader_putCharQ (pfile, '3');
2638 #ifndef NO_IMPLICIT_EXTERN_C
2639 /* Tell cc1plus if following text should be treated as C. */
2640 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
2641 cppReader_putCharQ (pfile, ' ');
2642 cppReader_putCharQ (pfile, '4');
2645 cppReader_putCharQ (pfile, '\n');
2646 pfile->lineno = line;
2651 * Parse a macro argument and append the info on PFILE's token_buffer.
2652 * REST_ARGS means to absorb the rest of the args.
2653 * Return nonzero to indicate a syntax error.
2656 static enum cpp_token
2657 macarg (cppReader *pfile, int rest_args)
2660 enum cpp_token token;
2661 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
2662 bool oldexpand = pfile->no_macro_expand;
2663 CPPOPTIONS (pfile)->put_out_comments = 1;
2665 /* Try to parse as much of the argument as exists at this
2666 input stack level. */
2668 pfile->no_macro_expand = TRUE;
2672 token = cppGetToken (pfile);
2679 /* If we've hit end of file, it's an error (reported by caller).
2680 Ditto if it's the end of cpp_expand_to_buffer text.
2681 If we've hit end of macro, just continue. */
2682 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
2684 /*@switchbreak@*/ break;
2687 /*@switchbreak@*/ break;
2691 /*@switchbreak@*/ break;
2693 /* if we've returned to lowest level and
2694 we aren't absorbing all args */
2695 if (paren == 0 && rest_args == 0)
2697 /*@switchbreak@*/ break;
2699 /* Remove ',' or ')' from argument buffer. */
2700 cppReader_adjustWritten (pfile, -1);
2708 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
2709 pfile->no_macro_expand = oldexpand;
2715 /* Turn newlines to spaces in the string of length LENGTH at START,
2716 except inside of string constants.
2717 The string is copied into itself with its beginning staying fixed. */
2720 change_newlines (char *start, int length)
2724 register char *limit;
2728 limit = start + length;
2731 while (ibp < limit) {
2732 *obp++ = c = *ibp++;
2737 /* Notice and skip strings, so that we don't delete newlines in them. */
2740 while (ibp < limit) {
2741 *obp++ = c = *ibp++;
2743 /*@innerbreak@*/ break;
2744 if (c == '\n' && quotec == '\'')
2745 /*@innerbreak@*/ break;
2748 /*@switchbreak@*/ break;
2755 static /*@observer@*/ struct tm *
2756 timestamp (/*@returned@*/ cppReader *pfile)
2758 if (pfile->timebuf == NULL)
2760 time_t t = time ((time_t *) 0);
2761 pfile->timebuf = localtime (&t);
2764 llassert (pfile->timebuf != NULL);
2766 return pfile->timebuf;
2769 static ob_mstring monthnames[] = {
2770 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2771 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2775 * expand things like __FILE__. Place the expansion into the output
2776 * buffer *without* rescanning.
2780 special_symbol (hashNode hp, cppReader *pfile)
2782 cstring buf = cstring_undefined;
2788 int paren = 0; /* For special `defined' keyword */
2790 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
2792 if (ip == cppReader_nullBuffer (pfile))
2794 cppReader_errorLit (pfile,
2795 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
2796 return; /* the show must go on */
2799 if (ip != NULL && ip->fname != NULL)
2811 if (hp->type == T_BASE_FILE)
2813 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
2815 ip = cppBuffer_prevBuffer (ip);
2819 llassert (ip != NULL);
2820 string = cstring_toCharsSafe (ip->nominal_fname);
2827 cppReader_reserve (pfile, 3 + 4 * strlen (string));
2828 quote_string (pfile, string);
2832 case T_INCLUDE_LEVEL:
2834 ip = cppReader_getBuffer (pfile);
2836 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
2837 ip = cppBuffer_prevBuffer (ip))
2839 if (ip != NULL && ip->fname != NULL)
2845 buf = message ("%d", true_indepth - 1);
2849 buf = message ("\"%s\"", cstring_makeLiteralTemp (CPP_VERSION));
2852 #ifndef NO_BUILTIN_SIZE_TYPE
2854 buf = cstring_makeLiteral (SIZE_TYPE);
2858 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2859 case T_PTRDIFF_TYPE:
2860 buf = cstring_makeLiteral (PTRDIFF_TYPE);
2865 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
2868 case T_USER_LABEL_PREFIX_TYPE:
2869 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
2872 case T_REGISTER_PREFIX_TYPE:
2873 buf = cstring_makeLiteral (REGISTER_PREFIX);
2877 buf = message ("%d", hp->value.ival);
2884 int line = ip->lineno;
2885 int col = ip->colno;
2887 llassert (ip->cur != NULL);
2888 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2890 buf = message ("%d", (int) line);
2902 char *sbuf = (char *) dmalloc (20);
2903 timebuf = timestamp (pfile);
2904 if (hp->type == T_DATE)
2906 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2907 timebuf->tm_mday, timebuf->tm_year + 1900);
2911 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2915 buf = cstring_fromCharsNew (sbuf);
2920 case T_SPEC_DEFINED:
2921 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
2922 ip = cppReader_getBuffer (pfile);
2924 llassert (ip->cur != NULL);
2925 SKIP_WHITE_SPACE (ip->cur);
2927 if (*ip->cur == '(')
2930 ip->cur++; /* Skip over the paren */
2931 SKIP_WHITE_SPACE (ip->cur);
2934 if (!is_idstart[(int) *ip->cur])
2936 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2939 if ((hp = cppReader_lookup (ip->cur, -1, -1)) != 0)
2942 buf = cstring_makeLiteral (" 1 ");
2945 while (is_idchar[(int) *ip->cur])
2950 SKIP_WHITE_SPACE (ip->cur);
2954 if (*ip->cur != ')')
2962 cppReader_errorLit (pfile,
2963 cstring_makeLiteralTemp ("`defined' without an identifier"));
2967 cpp_setLocation (pfile);
2968 llfatalerror (message ("Pre-processing error: invalid special hash type"));
2971 len = size_fromInt (cstring_length (buf));
2973 cppReader_reserve (pfile, len + 1);
2974 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
2975 cppReader_nullTerminateQ (pfile);
2981 /* Write out a #define command for the special named MACRO_NAME
2982 to PFILE's token_buffer. */
2985 dump_special_to_buffer (cppReader *pfile, char *macro_name)
2987 static char define_directive[] = "#define ";
2988 size_t macro_name_length = strlen (macro_name);
2989 output_line_command (pfile, 0, same_file);
2990 cppReader_reserve (pfile, sizeof(define_directive) + macro_name_length);
2991 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
2992 cppReader_putStrN (pfile, macro_name, macro_name_length);
2993 cppReader_putCharQ (pfile, ' ');
2994 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2995 cppReader_putChar (pfile, '\n');
2998 /* Initialize the built-in macros. */
3001 cppReader_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3002 int len, enum node_type type,
3003 int ivalue, /*@null@*/ /*@only@*/ char *value,
3006 cstring sname = cstring_fromCharsNew (name);
3008 llassert (usymtab_inGlobalScope ());
3011 ** Be careful here: this is done before the ctype table has
3012 ** been initialized.
3015 if (!usymtab_exists (sname))
3017 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3019 if (ctype_equal (ctyp, ctype_string))
3021 qualList ql = qualList_new ();
3022 ql = qualList_add (ql, qual_createObserver ());
3023 uentry_reflectQualifiers (ue, ql);
3027 usymtab_addGlobalEntry (ue);
3034 (void) cppReader_install (name, len, type, ivalue, value, hash);
3035 cstring_free (sname);
3039 cppReader_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3040 int len, enum node_type type,
3042 /*@only@*/ /*@null@*/ char *value, int hash)
3044 cstring sname = cstring_fromChars (name);
3045 /* evs 2000 07 10 - removed a memory leak, detected by lclint */
3047 llassert (usymtab_inGlobalScope ());
3049 if (!usymtab_existsTypeEither (sname))
3051 uentry ue = uentry_makeDatatype (sname, ctyp,
3053 fileloc_createBuiltin ());
3054 llassert (!usymtab_existsEither (sname));
3055 usymtab_addGlobalEntry (ue);
3058 (void) cppReader_install (name, len, type, ivalue, value, hash);
3062 initialize_builtins (cppReader *pfile)
3064 cppReader_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3065 cppReader_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3066 cppReader_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3067 cppReader_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3068 cppReader_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3069 cppReader_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3070 #ifndef NO_BUILTIN_SIZE_TYPE
3071 cppReader_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3073 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3074 cppReader_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3076 cppReader_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3077 cppReader_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3078 cppReader_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3079 cppReader_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3082 ** No, don't define __STDC__
3085 if (!cppReader_isTraditional (pfile))
3087 cppReader_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3094 cppReader_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3098 ** This is supplied using a -D by the compiler driver
3099 ** so that it is present only when truly compiling with GNU C.
3102 /* cppReader_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3104 cppReader_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3106 if (CPPOPTIONS (pfile)->debug_output)
3108 dump_special_to_buffer (pfile, "__BASE_FILE__");
3109 dump_special_to_buffer (pfile, "__VERSION__");
3110 #ifndef NO_BUILTIN_SIZE_TYPE
3111 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3113 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3114 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3116 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3117 dump_special_to_buffer (pfile, "__DATE__");
3118 dump_special_to_buffer (pfile, "__TIME__");
3119 if (!cppReader_isTraditional (pfile))
3120 dump_special_to_buffer (pfile, "__STDC__");
3125 /* Return 1 iff a token ending in C1 followed directly by a token C2
3126 could cause mis-tokenization. */
3129 unsafe_chars (char c1, char c2)
3134 if (c2 == c1 || c2 == '=')
3138 case '0': case '1': case '2': case '3': case '4':
3139 case '5': case '6': case '7': case '8': case '9':
3140 case 'e': case 'E': case 'p': case 'P':
3141 if (c2 == '-' || c2 == '+')
3142 return 1; /* could extend a pre-processing number */
3145 if (c2 == '\'' || c2 == '\"')
3146 return 1; /* Could turn into L"xxx" or L'xxx'. */
3150 case 'a': case 'b': case 'c': case 'd': case 'f':
3151 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
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 case 'A': case 'B': case 'C': case 'D': case 'F':
3156 case 'G': case 'H': case 'I': case 'J': case 'K':
3157 case 'M': case 'N': case 'O': case 'Q': case 'R':
3158 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3160 /* We're in the middle of either a name or a pre-processing number. */
3161 return (is_idchar[(int) c2] || c2 == '.');
3162 case '<': case '>': case '!': case '%': case '#': case ':':
3163 case '^': case '&': case '|': case '*': case '/': case '=':
3164 return (c2 == c1 || c2 == '=');
3169 /* Expand a macro call.
3170 HP points to the symbol that is the macro being called.
3171 Put the result of expansion onto the input stack
3172 so that subsequent input by our caller will use it.
3174 If macro wants arguments, caller has already verified that
3175 an argument list follows; arguments come from the input stack. */
3178 macroexpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3181 DEFINITION *defn = hp->value.defn;
3187 size_t old_written = cppReader_getWritten (pfile);
3191 struct argdata *args = NULL;
3193 pfile->output_escapes++;
3195 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3197 nargs = defn->nargs;
3201 enum cpp_token token = CPP_EOF;
3203 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3205 for (i = 0; i < nargs; i++)
3207 args[i].expanded = 0;
3209 args[i].raw_length = 0;
3210 args[i].expand_length = args[i].stringified_length = -1;
3211 args[i].use_count = 0;
3215 ** Parse all the macro args that are supplied. I counts them.
3216 ** The first NARGS args are stored in ARGS.
3217 ** The rest are discarded. If rest_args is set then we assume
3218 ** macarg absorbed the rest of the args.
3224 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3232 if (i < nargs || (nargs == 0 && i == 0))
3234 /* if we are working on last arg which absorbs rest of args... */
3235 if (i == nargs - 1 && defn->rest_args)
3240 args[i].raw = size_toLong (cppReader_getWritten (pfile));
3241 token = macarg (pfile, rest_args);
3242 args[i].raw_length = cppReader_getWritten (pfile) - args[i].raw;
3243 args[i].newlines = FALSE; /* FIXME */
3247 token = macarg (pfile, 0);
3250 if (token == CPP_EOF || token == CPP_POP)
3252 cppReader_errorWithLine (pfile, start_line, start_column,
3253 cstring_fromCharsNew ("unterminated macro call"));
3258 } while (token == CPP_COMMA);
3260 /* If we got one arg but it was just whitespace, call that 0 args. */
3268 bp = ARG_BASE + args[0].raw;
3269 lim = bp + args[0].raw_length;
3271 /* cpp.texi says for foo ( ) we provide one argument.
3272 However, if foo wants just 0 arguments, treat this as 0. */
3276 while (bp != lim && is_space[(int) *bp])
3286 /* Don't output an error message if we have already output one for
3287 a parse error above. */
3290 if (nargs == 0 && i > 0)
3292 cppReader_error (pfile,
3293 message ("arguments given to macro `%s'", hp->name));
3297 /* traditional C allows foo() if foo wants one argument. */
3298 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
3302 /* the rest args token is allowed to absorb 0 tokens */
3303 else if (i == nargs - 1 && defn->rest_args)
3306 cppReader_error (pfile,
3307 message ("macro `%s' used without args", hp->name));
3309 cppReader_error (pfile,
3310 message ("macro `%s' used with just one arg", hp->name));
3313 cppReader_error (pfile,
3314 message ("macro `%s' used with only %d args",
3320 cppReader_error (pfile,
3321 message ("macro `%s' used with too many (%d) args", hp->name, i));
3329 /* If macro wants zero args, we parsed the arglist for checking only.
3330 Read directly from the macro definition. */
3334 xbuf = defn->expansion;
3335 xbuf_len = defn->length;
3339 char *exp = defn->expansion;
3340 int offset; /* offset in expansion,
3341 copied a piece at a time */
3342 size_t totlen; /* total amount of exp buffer filled so far */
3344 register struct reflist *ap, *last_ap;
3346 assertSet (args); /* args is defined since the nargs > 0 path was taken */
3348 /* Macro really takes args. Compute the expansion of this call. */
3350 /* Compute length in characters of the macro's expansion.
3351 Also count number of times each arg is used. */
3352 xbuf_len = defn->length;
3354 llassert (args != NULL);
3356 for (ap = defn->pattern; ap != NULL; ap = ap->next)
3360 struct argdata *arg = &args[ap->argno];
3362 /* Stringify it it hasn't already been */
3365 if (arg->stringified_length < 0)
3367 int arglen = arg->raw_length;
3368 bool escaped = FALSE;
3369 char in_string = '\0';
3372 /* Initially need_space is -1. Otherwise, 1 means the
3373 previous character was a space, but we suppressed it;
3374 0 means the previous character was a non-space. */
3375 int need_space = -1;
3378 arg->stringified = cppReader_getWritten (pfile);
3379 if (!cppReader_isTraditional (pfile))
3380 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
3381 for (; i < arglen; i++)
3383 c = (ARG_BASE + arg->raw)[i];
3385 if (in_string == '\0')
3387 /* Internal sequences of whitespace are replaced by
3388 one space except within an string or char token.*/
3389 if (is_space[(int) c])
3391 if (cppReader_getWritten (pfile) > arg->stringified
3392 && (cppReader_getPWritten (pfile))[-1] == '@')
3394 /* "@ " escape markers are removed */
3395 cppReader_adjustWritten (pfile, -1);
3396 /*@innercontinue@*/ continue;
3398 if (need_space == 0)
3400 /*@innercontinue@*/ continue;
3402 else if (need_space > 0)
3403 cppReader_putChar (pfile, ' ');
3419 if (in_string != '\0')
3424 else if (c == '\"' || c == '\'')
3434 /* Escape these chars */
3435 if (c == '\"' || (in_string != '\0' && c == '\\'))
3436 cppReader_putChar (pfile, '\\');
3438 cppReader_putChar (pfile, c);
3441 cppReader_reserve (pfile, 4);
3442 sprintf (cppReader_getPWritten (pfile), "\\%03o",
3444 cppReader_adjustWritten (pfile, 4);
3447 if (!cppReader_isTraditional (pfile))
3448 cppReader_putChar (pfile, '\"'); /* insert ending quote */
3449 arg->stringified_length
3450 = size_toInt (cppReader_getWritten (pfile) - arg->stringified);
3453 xbuf_len += args[ap->argno].stringified_length;
3455 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3457 /* Add 4 for two newline-space markers to prevent
3458 token concatenation. */
3459 assertSet (args); /*@i534 shouldn't need this */
3460 xbuf_len += args[ap->argno].raw_length + 4;
3464 /* We have an ordinary (expanded) occurrence of the arg.
3465 So compute its expansion, if we have not already. */
3467 assertSet (args); /*@i534 shouldn't need this */
3469 if (args[ap->argno].expand_length < 0)
3471 args[ap->argno].expanded = cppReader_getWritten (pfile);
3472 cpp_expand_to_buffer (pfile,
3473 ARG_BASE + args[ap->argno].raw,
3474 size_fromInt (args[ap->argno].raw_length));
3476 args[ap->argno].expand_length
3477 = size_toInt (cppReader_getWritten (pfile) - args[ap->argno].expanded);
3480 /* Add 4 for two newline-space markers to prevent
3481 token concatenation. */
3482 xbuf_len += args[ap->argno].expand_length + 4;
3484 if (args[ap->argno].use_count < 10)
3485 args[ap->argno].use_count++;
3488 xbuf = (char *) dmalloc (xbuf_len + 1);
3492 ** Generate in XBUF the complete expansion
3493 ** with arguments substituted in.
3494 ** TOTLEN is the total size generated so far.
3495 ** OFFSET is the index in the definition
3496 ** of where we are copying from.
3502 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
3503 last_ap = ap, ap = ap->next)
3505 register struct argdata *arg = &args[ap->argno];
3506 size_t count_before = totlen;
3508 /* Add chars to XBUF. */
3509 for (i = 0; i < ap->nchars; i++, offset++)
3511 xbuf[totlen++] = exp[offset];
3514 /* If followed by an empty rest arg with concatenation,
3515 delete the last run of nonwhite chars. */
3516 if (rest_zero && totlen > count_before
3517 && ((ap->rest_args && ap->raw_before)
3518 || (last_ap != NULL && last_ap->rest_args
3519 && last_ap->raw_after)))
3521 /* Delete final whitespace. */
3522 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
3527 /* Delete the nonwhites before them. */
3528 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
3534 if (ap->stringify != 0)
3537 memcpy (xbuf + totlen,
3538 ARG_BASE + arg->stringified,
3539 size_fromInt (arg->stringified_length));
3540 totlen += arg->stringified_length;
3542 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3549 p1 = ARG_BASE + arg->raw;
3550 l1 = p1 + arg->raw_length;
3554 while (p1 != l1 && is_space[(int) *p1])
3559 while (p1 != l1 && is_idchar[(int) *p1])
3561 xbuf[totlen++] = *p1++;
3564 /* Delete any no-reexpansion marker that follows
3565 an identifier at the beginning of the argument
3566 if the argument is concatenated with what precedes it. */
3567 if (p1[0] == '@' && p1[1] == '-')
3572 /* Arg is concatenated after: delete trailing whitespace,
3573 whitespace markers, and no-reexpansion markers. */
3576 if (is_space[(int) l1[-1]]) l1--;
3577 else if (l1[-1] == '-')
3580 /* If a `-' is preceded by an odd number of newlines then it
3581 and the last newline are a no-reexpansion marker. */
3582 while (p2 != p1 && p2[-1] == '\n')
3587 if (((l1 - 1 - p2) & 1) != 0)
3593 /*@innerbreak@*/ break;
3598 /*@innerbreak@*/ break;
3603 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
3611 expanded = ARG_BASE + arg->expanded;
3613 if (!ap->raw_before && totlen > 0
3614 && (arg->expand_length != 0)
3615 && !cppReader_isTraditional(pfile)
3616 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3618 xbuf[totlen++] = '@';
3619 xbuf[totlen++] = ' ';
3622 memcpy (xbuf + totlen, expanded,
3623 size_fromInt (arg->expand_length));
3624 totlen += arg->expand_length;
3626 if (!ap->raw_after && totlen > 0
3627 && offset < size_toInt (defn->length)
3628 && !cppReader_isTraditional(pfile)
3629 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3631 xbuf[totlen++] = '@';
3632 xbuf[totlen++] = ' ';
3635 /* If a macro argument with newlines is used multiple times,
3636 then only expand the newlines once. This avoids creating
3637 output lines which don't correspond to any input line,
3638 which confuses gdb and gcov. */
3639 if (arg->use_count > 1 && arg->newlines > 0)
3641 /* Don't bother doing change_newlines for subsequent
3645 = change_newlines (expanded, arg->expand_length);
3649 if (totlen > xbuf_len)
3653 /* if there is anything left of the definition
3654 after handling the arg list, copy that in too. */
3656 for (i = offset; i < size_toInt (defn->length); i++)
3658 /* if we've reached the end of the macro */
3661 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3662 && last_ap->raw_after))
3663 xbuf[totlen++] = exp[i];
3666 xbuf[totlen] = '\0';
3670 pfile->output_escapes--;
3672 /* Now put the expansion on the input stack
3673 so our caller will commence reading from it. */
3674 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3675 cppReader_getBuffer (pfile)->has_escapes = 1;
3677 /* Pop the space we've used in the token_buffer for argument expansion. */
3678 cppReader_setWritten (pfile, old_written);
3680 /* Recursive macro use sometimes works traditionally.
3681 #define foo(x,y) bar (x (y,0), y)
3684 if (!cppReader_isTraditional (pfile))
3685 hp->type = T_DISABLED;
3691 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
3692 /*@dependent@*/ hashNode hp)
3694 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
3701 mbuf->cleanup = cppReader_macroCleanup;
3703 llassert (mbuf->hnode == NULL);
3706 /* The first chars of the expansion should be a "@ " added by
3707 collect_expansion. This is to prevent accidental token-pasting
3708 between the text preceding the macro invocation, and the macro
3711 We would like to avoid adding unneeded spaces (for the sake of
3712 tools that use cpp, such as imake). In some common cases we can
3713 tell that it is safe to omit the space.
3715 The character before the macro invocation cannot have been an
3716 idchar (or else it would have been pasted with the idchars of
3717 the macro name). Therefore, if the first non-space character
3718 of the expansion is an idchar, we do not need the extra space
3719 to prevent token pasting.
3721 Also, we don't need the extra space if the first char is '(',
3722 or some other (less common) characters. */
3724 if (xbuf[0] == '@' && xbuf[1] == ' '
3725 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3726 || xbuf[2] == '\"'))
3728 llassert (mbuf->cur != NULL);
3734 /* Like cppGetToken, except that it does not read past end-of-line.
3735 Also, horizontal space is skipped, and macros are popped. */
3737 static enum cpp_token
3738 get_directive_token (cppReader *pfile)
3742 size_t old_written = cppReader_getWritten (pfile);
3743 enum cpp_token token;
3744 cppSkipHspace (pfile);
3745 if (cppReader_peekC (pfile) == '\n')
3750 token = cppGetToken (pfile);
3755 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
3760 cppReader_setWritten (pfile, old_written);
3761 /*@switchbreak@*/ break;
3769 /* Handle #include and #import.
3770 This function expects to see "fname" or <fname> on the input.
3772 The input is normally in part of the output_buffer following
3773 cppReader_getWritten, and will get overwritten by output_line_command.
3774 I.e. in input file specification has been popped by cppReader_handleDirective.
3778 do_include (cppReader *pfile, struct directive *keyword,
3779 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
3781 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3783 char *fbeg, *fend; /* Beginning and end of fname */
3784 enum cpp_token token;
3786 /* Chain of dirs to search */
3787 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
3788 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3789 struct file_name_list *searchptr = NULL;
3790 size_t old_written = cppReader_getWritten (pfile);
3794 int f; /* file number */
3795 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3796 f= -1; /* JF we iz paranoid! */
3798 pfile->parsing_include_directive++;
3799 token = get_directive_token (pfile);
3800 pfile->parsing_include_directive--;
3802 if (token == CPP_STRING)
3804 /* FIXME - check no trailing garbage */
3805 fbeg = pfile->token_buffer + old_written + 1;
3806 fend = cppReader_getPWritten (pfile) - 1;
3807 if (fbeg[-1] == '<')
3810 /* If -I-, start with the first -I dir after the -I-. */
3811 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
3812 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3814 /* If -I- was specified, don't search current dir, only spec'd ones. */
3815 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
3817 cppBuffer *fp = CPPBUFFER (pfile);
3818 /* We have "filename". Figure out directory this source
3819 file is coming from and put it on the front of the list. */
3821 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3826 llassert (fp != NULL);
3830 if (cstring_isDefined (fp->nominal_fname))
3832 nam = cstring_toCharsSafe (fp->nominal_fname);
3834 /* Found a named file. Figure out dir of the file,
3835 and put it in front of the search list. */
3836 dsp[0].next = search_start;
3840 ep = strrchr (nam, CONNECTCHAR);
3842 ep = strrchr (nam, ']');
3843 if (ep == NULL) ep = strrchr (nam, '>');
3844 if (ep == NULL) ep = strrchr (nam, ':');
3845 if (ep != NULL) ep++;
3855 /*@-onlytrans@*/ /* This looks like a memory leak... */
3856 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
3860 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3861 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3865 dsp[0].fname = cstring_undefined; /* Current directory */
3868 dsp[0].got_name_map = 0;
3879 else if (token == CPP_NAME)
3882 * Support '#include xyz' like VAX-C to allow for easy use of all the
3883 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3884 * code from case '<' is repeated here) and generates a warning.
3886 cppReader_warning (pfile,
3887 "VAX-C-style include specification found, use '#include <filename.h>' !");
3889 /* If -I-, start with the first -I dir after the -I-. */
3890 if (CPPOPTIONS (pfile)->first_bracket_include)
3891 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3892 fbeg = pfile->token_buffer + old_written;
3893 fend = cppReader_getPWritten (pfile);
3898 cppReader_error (pfile,
3899 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
3902 cppReader_setWritten (pfile, old_written);
3903 cppReader_skipRestOfLine (pfile);
3909 token = get_directive_token (pfile);
3910 if (token != CPP_VSPACE)
3912 cppReader_errorLit (pfile,
3913 cstring_makeLiteralTemp ("Junk at end of #include"));
3915 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3917 token = get_directive_token (pfile);
3922 ** For #include_next, skip in the search path
3923 ** past the dir in which the containing file was found.
3928 cppBuffer *fp = CPPBUFFER (pfile);
3930 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3932 llassert (fp != NULL);
3934 if (fp->fname != NULL)
3936 /* fp->dir is null if the containing file was specified with
3937 an absolute file name. In that case, don't skip anything. */
3938 if (fp->dir == SELF_DIR_DUMMY)
3940 search_start = CPPOPTIONS (pfile)->include;
3942 else if (fp->dir != NULL)
3944 search_start = fp->dir->next;
3956 cppReader_setWritten (pfile, old_written);
3960 DPRINTF (("fbeg: %s", fbeg));
3964 cppReader_error (pfile,
3965 message ("Empty file name in #%s", keyword->name));
3970 ** Allocate this permanently, because it gets stored in the definitions
3974 fname = cstring_undefined;
3976 /* + 2 above for slash and terminating null. */
3977 /* + 2 added for '.h' on VMS (to support '#include filename') */
3979 /* If specified file name is absolute, just open it. */
3981 if (osd_isConnectChar (*fbeg)
3982 # if defined (WIN32) || defined (OS2)
3983 || (*(fbeg + 1) == ':')
3987 fname = cstring_copyLength (fbeg, flen);
3989 if (redundant_include_p (pfile, fname))
3991 cstring_free (fname);
3995 f = open_include_file (pfile, fname, NULL);
3997 if (f == IMPORT_FOUND)
3999 return 0; /* Already included this file */
4004 /* Search directory path, trying to open the file.
4005 Copy each filename tried into FNAME. */
4007 for (searchptr = search_start; searchptr != NULL;
4008 searchptr = searchptr->next)
4010 if (!cstring_isEmpty (searchptr->fname))
4012 /* The empty string in a search path is ignored.
4013 This makes it possible to turn off entirely
4014 a standard piece of the list. */
4015 if (cstring_isEmpty (searchptr->fname))
4018 fname = cstring_copy (searchptr->fname);
4019 fname = cstring_appendChar (fname, CONNECTCHAR);
4020 DPRINTF (("Here: %s", fname));
4027 fname = cstring_concatLength (fname, fbeg, flen);
4029 DPRINTF (("fname: %s", fname));
4031 /* Win32 directory fix from Kay Buschner. */
4032 #if defined (WIN32) || defined (OS2)
4033 /* Fix all unixdir slashes to win dir slashes */
4034 if (searchptr->fname && (searchptr->fname[0] != 0))
4036 cstring_replaceAll (fname, '/', '\\');
4041 /* Change this 1/2 Unix 1/2 VMS file specification into a
4042 full VMS file specification */
4043 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4044 /* Fix up the filename */
4045 hack_vms_include_specification (fname);
4047 /* This is a normal VMS filespec, so use it unchanged. */
4048 strncpy (fname, fbeg, flen);
4050 /* if it's '#include filename', add the missing .h */
4051 if (strchr (fname,'.') == NULL) {
4052 strcat (fname, ".h");
4056 /* ??? There are currently 3 separate mechanisms for avoiding processing
4057 of redundant include files: #import, #pragma once, and
4058 redundant_include_p. It would be nice if they were unified. */
4060 if (redundant_include_p (pfile, fname))
4062 cstring_free (fname);
4066 DPRINTF (("Trying: %s", fname));
4068 f = open_include_file (pfile, fname, searchptr);
4070 if (f == IMPORT_FOUND)
4072 return 0; /* Already included this file */
4075 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4077 cppReader_warning (pfile,
4078 message ("Header file %s exists, but is not readable", fname));
4091 /* A file that was not found. */
4092 fname = cstring_copyLength (fbeg, flen);
4094 if (search_start != NULL)
4096 cppReader_error (pfile,
4097 message ("Cannot find include file %s on search path: %x",
4099 searchPath_unparse (search_start)));
4103 cppReader_error (pfile,
4104 message ("No include path in which to find %s", fname));
4109 ** Check to see if this include file is a once-only include file.
4113 struct file_name_list *ptr;
4115 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4117 if (cstring_equal (ptr->fname, fname))
4119 /* This file was included before. */
4126 /* This is the first time for this file. */
4127 /* Add it to list of files included. */
4129 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4130 ptr->control_macro = NULL;
4131 ptr->c_system_include_path = NULL;
4132 ptr->next = pfile->all_include_files;
4134 ptr->got_name_map = NULL;
4136 DPRINTF (("Including file: %s", fname));
4137 pfile->all_include_files = ptr;
4138 assertSet (pfile->all_include_files);
4141 if (angle_brackets != 0)
4143 pfile->system_include_depth++;
4146 /* Actually process the file */
4147 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4149 cstring_free (fname);
4153 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4154 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4156 output_line_command (pfile, 0, enter_file);
4157 pfile->only_seen_white = 2;
4162 pfile->system_include_depth--;
4165 } /*@=branchstate@*/
4170 /* Return nonzero if there is no need to include file NAME
4171 because it has already been included and it contains a conditional
4172 to make a repeated include do nothing. */
4175 redundant_include_p (cppReader *pfile, cstring name)
4177 struct file_name_list *l = pfile->all_include_files;
4179 for (; l != NULL; l = l->next)
4181 if (cstring_equal (name, l->fname)
4182 && (l->control_macro != NULL)
4183 && (cppReader_lookup (l->control_macro, -1, -1) != NULL))
4192 /* Return nonzero if the given FILENAME is an absolute pathname which
4193 designates a file within one of the known "system" include file
4194 directories. We assume here that if the given FILENAME looks like
4195 it is the name of a file which resides either directly in a "system"
4196 include file directory, or within any subdirectory thereof, then the
4197 given file must be a "system" include file. This function tells us
4198 if we should suppress pedantic errors/warnings for the given FILENAME.
4200 The value is 2 if the file is a C-language system header file
4201 for which C++ should (on most systems) assume `extern "C"'. */
4204 is_system_include (cppReader *pfile, cstring filename)
4206 struct file_name_list *searchptr;
4208 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4210 searchptr = searchptr->next)
4212 if (!cstring_isEmpty (searchptr->fname)) {
4213 cstring sys_dir = searchptr->fname;
4214 int length = cstring_length (sys_dir);
4216 if (cstring_equalLen (sys_dir, filename, length)
4217 && osd_isConnectChar (cstring_getChar (filename, length)))
4219 if (searchptr->c_system_include_path)
4230 /* Convert a character string literal into a nul-terminated string.
4231 The input string is [IN ... LIMIT).
4232 The result is placed in RESULT. RESULT can be the same as IN.
4233 The value returned in the end of the string written to RESULT,
4234 or NULL on error. */
4236 static /*@null@*/ char *
4237 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4238 char *in, char *limit, int handle_escapes)
4258 /*@switchbreak@*/ break;
4262 char *bpc = (char *) in;
4263 int i = (char) cppReader_parseEscape (pfile, &bpc);
4266 *result++ = (char) c;
4267 /*@switchbreak@*/ break;
4281 * interpret #line command. Remembers previously seen fnames
4282 * in its very own hash table.
4285 /*@constant int FNAME_HASHSIZE@*/
4286 #define FNAME_HASHSIZE 37
4289 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
4291 cppBuffer *ip = cppReader_getBuffer (pfile);
4293 size_t old_written = cppReader_getWritten (pfile);
4294 enum file_change_code file_change = same_file;
4295 enum cpp_token token;
4297 token = get_directive_token (pfile);
4299 if (token != CPP_NUMBER
4300 || !isdigit(pfile->token_buffer[old_written]))
4302 cppReader_errorLit (pfile,
4303 cstring_makeLiteralTemp ("invalid format `#line' command"));
4305 goto bad_line_directive;
4308 /* The Newline at the end of this line remains to be processed.
4309 To put the next line at the specified line number,
4310 we must store a line number now that is one less. */
4311 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
4312 cppReader_setWritten (pfile, old_written);
4314 /* NEW_LINENO is one less than the actual line number here. */
4315 if (cppReader_isPedantic (pfile) && new_lineno < 0)
4316 cppReader_pedwarnLit (pfile,
4317 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
4319 token = get_directive_token (pfile);
4321 if (token == CPP_STRING) {
4322 char *fname = pfile->token_buffer + old_written;
4324 static hashNode fname_table[FNAME_HASHSIZE];
4326 hashNode *hash_bucket;
4331 /* Turn the file name, which is a character string literal,
4332 into a null-terminated string. Do this in place. */
4333 end_name = convert_string (pfile, fname, fname, cppReader_getPWritten (pfile), 1);
4334 if (end_name == NULL)
4336 cppReader_errorLit (pfile,
4337 cstring_makeLiteralTemp ("invalid format `#line' command"));
4338 goto bad_line_directive;
4341 fname_length = end_name - fname;
4342 num_start = cppReader_getWritten (pfile);
4344 token = get_directive_token (pfile);
4345 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
4346 p = pfile->token_buffer + num_start;
4347 if (cppReader_isPedantic (pfile))
4348 cppReader_pedwarnLit (pfile,
4349 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
4351 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
4353 cppReader_errorLit (pfile,
4354 cstring_makeLiteralTemp ("invalid format `#line' command"));
4355 goto bad_line_directive;
4358 file_change = enter_file;
4360 file_change = leave_file;
4362 ip->system_header_p = 1;
4363 else /* if (*p == 4) */
4364 ip->system_header_p = 2;
4366 cppReader_setWritten (pfile, num_start);
4367 token = get_directive_token (pfile);
4368 p = pfile->token_buffer + num_start;
4369 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
4370 ip->system_header_p = *p == 3 ? 1 : 2;
4371 token = get_directive_token (pfile);
4373 if (token != CPP_VSPACE) {
4374 cppReader_errorLit (pfile,
4375 cstring_makeLiteralTemp ("invalid format `#line' command"));
4377 goto bad_line_directive;
4382 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
4383 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
4385 if (hp->length == fname_length &&
4386 strncmp (hp->value.cpval, fname, size_fromInt (fname_length)) == 0) {
4387 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4393 /* Didn't find it; cons up a new one. */
4394 hp = (hashNode) dmalloc (sizeof (*hp));
4397 hp->bucket_hdr = NULL;
4399 hp->name = cstring_undefined;
4400 hp->next = *hash_bucket;
4404 hp->length = fname_length;
4405 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
4406 memcpy (hp->value.cpval, fname, size_fromInt (fname_length));
4407 hp->value.cpval[fname_length] = '\0';
4408 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4411 else if (token != CPP_VSPACE && token != CPP_EOF)
4413 cppReader_errorLit (pfile,
4414 cstring_makeLiteralTemp ("invalid format `#line' command"));
4415 goto bad_line_directive;
4422 ip->lineno = new_lineno;
4424 cppReader_skipRestOfLine (pfile);
4425 cppReader_setWritten (pfile, old_written);
4426 output_line_command (pfile, 0, file_change);
4431 * remove the definition of a symbol from the symbol table.
4432 * according to un*x /lib/cpp, it is not an error to undef
4433 * something that has no definitions, so it isn't one here either.
4437 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
4442 char *orig_buf = buf;
4444 SKIP_WHITE_SPACE (buf);
4446 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
4448 while ((hp = cppReader_lookup (buf, sym_length, -1)) != NULL)
4450 /* If we are generating additional info for debugging (with -g) we
4451 need to pass through all effective #undef commands. */
4452 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
4454 pass_thru_directive (orig_buf, limit, pfile, keyword);
4457 if (hp->type != T_MACRO)
4459 cppReader_warning (pfile,
4460 message ("Undefining preprocessor builtin: %s",
4464 cppReader_deleteMacro (hp);
4467 if (cppReader_isPedantic (pfile)) {
4469 SKIP_WHITE_SPACE (buf);
4472 cppReader_pedwarnLit (pfile,
4473 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
4482 * Report an error detected by the program we are processing.
4483 * Use the text of the line in the error message.
4484 * (We use error because it prints the filename & line#.)
4488 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4489 char *buf, char *limit)
4491 int length = limit - buf;
4492 cstring copy = cstring_copyLength (buf, length);
4493 cstring adv = cstring_advanceWhiteSpace (copy);
4495 cppReader_error (pfile, message ("#error %s", adv));
4496 cstring_free (copy);
4501 * Report a warning detected by the program we are processing.
4502 * Use the text of the line in the warning message, then continue.
4503 * (We use error because it prints the filename & line#.)
4507 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4508 char *buf, char *limit)
4510 int length = limit - buf;
4511 cstring copy = cstring_copyLength (buf, length);
4512 cstring adv = cstring_advanceWhiteSpace (copy);
4513 cppReader_warning (pfile, message ("#warning %s", adv));
4514 cstring_free (copy);
4519 /* #ident has already been copied to the output file, so just ignore it. */
4522 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4523 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4525 /* Allow #ident in system headers, since that's not user's fault. */
4526 if (cppReader_isPedantic (pfile) && !cppReader_getBuffer (pfile)->system_header_p)
4527 cppReader_pedwarnLit (pfile,
4528 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
4530 /* Leave rest of line to be read by later calls to cppGetToken. */
4535 /* #pragma and its argument line have already been copied to the output file.
4536 Just check for some recognized pragmas that need validation here. */
4539 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4540 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4542 while (*buf == ' ' || *buf == '\t')
4547 if (!strncmp (buf, "implementation", 14)) {
4548 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4549 been included yet. */
4550 struct file_name_list *ptr;
4551 char *p = buf + 14, *fname, *inc_fname;
4553 SKIP_WHITE_SPACE (p);
4554 if (*p == '\n' || *p != '\"')
4558 p = (char *) strchr (fname, '\"');
4559 fname_len = p != NULL ? p - fname : mstring_length (fname);
4561 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4563 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
4564 inc_fname = (inc_fname != NULL)
4565 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
4567 if ((inc_fname != NULL)
4568 && (strncmp (inc_fname, fname, size_fromInt (fname_len)) == 0))
4570 cpp_setLocation (pfile);
4572 ppllerror (message ("`#pragma implementation' for `%s' appears "
4573 "after file is included",
4574 cstring_fromChars (fname)));
4583 * handle #if command by
4584 * 1) inserting special `defined' keyword into the hash table
4585 * that gets turned into 0 or 1 by special_symbol (thus,
4586 * if the luser has a symbol called `defined' already, it won't
4587 * work inside the #if command)
4588 * 2) rescan the input into a temporary output buffer
4589 * 3) pass the output buffer to the yacc parser and collect a value
4590 * 4) clean up the mess left from steps 1 and 2.
4591 * 5) call conditional_skip to skip til the next #endif (etc.),
4592 * or not, depending on the value from step 3.
4596 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4597 char *buf, char *limit)
4599 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4600 conditional_skip (pfile, value == 0, T_IF, NULL);
4605 * handle a #elif directive by not changing if_stack either.
4606 * see the comment above do_else.
4609 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4610 char *buf, char *limit)
4612 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4614 cppReader_errorLit (pfile,
4615 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
4620 llassert (pfile->if_stack != NULL);
4622 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4624 cppReader_errorLit (pfile,
4625 cstring_makeLiteralTemp ("`#elif' after `#else'"));
4627 if (pfile->if_stack->fname != NULL
4628 && cppReader_getBuffer (pfile)->fname != NULL
4629 && !cstring_equal (pfile->if_stack->fname,
4630 cppReader_getBuffer (pfile)->nominal_fname))
4631 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
4632 fprintf (stderr, ")\n");
4634 pfile->if_stack->type = T_ELIF;
4637 if (pfile->if_stack->if_succeeded)
4639 skip_if_group (pfile, 0);
4643 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4645 skip_if_group (pfile, 0);
4648 ++pfile->if_stack->if_succeeded; /* continue processing input */
4649 output_line_command (pfile, 1, same_file);
4657 * evaluate a #if expression in BUF, of length LENGTH,
4658 * then parse the result as a C expression and return the value as an int.
4661 static HOST_WIDE_INT
4662 eval_if_expression (cppReader *pfile,
4663 /*@unused@*/ char *buf,
4664 /*@unused@*/ int length)
4666 hashNode save_defined;
4667 HOST_WIDE_INT value;
4668 size_t old_written = cppReader_getWritten (pfile);
4670 save_defined = cppReader_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4671 pfile->pcp_inside_if = 1;
4673 value = cppReader_parseExpression (pfile);
4674 pfile->pcp_inside_if = 0;
4676 /* Clean up special symbol */
4677 cppReader_deleteMacro (save_defined);
4679 cppReader_setWritten (pfile, old_written); /* Pop */
4685 * routine to handle ifdef/ifndef. Try to look up the symbol,
4686 * then do or don't skip to the #endif/#else/#elif depending
4687 * on what directive is actually being processed.
4691 do_xifdef (cppReader *pfile, struct directive *keyword,
4692 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4695 cppBuffer *ip = cppReader_getBuffer (pfile);
4698 enum cpp_token token;
4699 int start_of_file = 0;
4700 char *control_macro = 0;
4701 size_t old_written = cppReader_getWritten (pfile);
4703 DPRINTF (("do xifdef: %d",
4704 keyword->type == T_IFNDEF));
4706 /* Detect a #ifndef at start of file (not counting comments). */
4707 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
4709 start_of_file = pfile->only_seen_white == 2;
4712 pfile->no_macro_expand++;
4713 token = get_directive_token (pfile);
4714 pfile->no_macro_expand--;
4716 ident = pfile->token_buffer + old_written;
4717 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4718 cppReader_setWritten (pfile, old_written); /* Pop */
4720 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4722 skip = (keyword->type == T_IFDEF);
4723 if (! cppReader_isTraditional (pfile))
4725 cppReader_pedwarn (pfile,
4726 message ("`#%s' with no argument", keyword->name));
4729 else if (token == CPP_NAME)
4731 hashNode hp = cppReader_lookup (ident, ident_length, -1);
4732 skip = (keyword->type == T_IFDEF)
4733 ? (hp == NULL) : (hp != NULL);
4735 DPRINTF (("hp null: %d / %d / %d",
4737 (keyword->type == T_IFNDEF),
4740 if (start_of_file && !skip)
4742 DPRINTF (("Not skipping!"));
4743 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
4744 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
4749 skip = (keyword->type == T_IFDEF);
4750 if (! cppReader_isTraditional (pfile))
4752 cppReader_error (pfile,
4753 message ("`#%s' with invalid argument", keyword->name));
4757 if (!cppReader_isTraditional (pfile))
4760 cppSkipHspace (pfile);
4761 c = cppReader_peekC (pfile);
4762 if (c != EOF && c != '\n')
4764 cppReader_pedwarn (pfile,
4765 message ("garbage at end of `#%s' argument", keyword->name));
4769 cppReader_skipRestOfLine (pfile);
4771 DPRINTF (("Conditional skip: %d", skip));
4772 conditional_skip (pfile, skip, T_IF, control_macro);
4776 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4777 If this is a #ifndef starting at the beginning of a file,
4778 CONTROL_MACRO is the macro name tested by the #ifndef.
4779 Otherwise, CONTROL_MACRO is 0. */
4782 conditional_skip (cppReader *pfile, int skip,
4783 enum node_type type,
4784 /*@dependent@*/ char *control_macro)
4786 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4788 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4789 temp->next = pfile->if_stack;
4790 temp->control_macro = control_macro;
4792 temp->if_succeeded = 0;
4794 pfile->if_stack = temp;
4795 pfile->if_stack->type = type;
4799 skip_if_group (pfile, 0);
4804 ++pfile->if_stack->if_succeeded;
4805 output_line_command (pfile, 1, same_file);
4810 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4811 * leaves input ptr at the sharp sign found.
4812 * If ANY is nonzero, return at next directive of any sort.
4816 skip_if_group (cppReader *pfile, int any)
4819 struct directive *kt;
4820 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
4821 register int ident_length;
4823 struct parse_marker line_start_mark;
4825 parseSetMark (&line_start_mark, pfile);
4827 if (CPPOPTIONS (pfile)->output_conditionals) {
4828 static char failed[] = "#failed\n";
4829 cppReader_puts (pfile, failed, sizeof(failed)-1);
4831 output_line_command (pfile, 1, same_file);
4835 if (CPPOPTIONS (pfile)->output_conditionals)
4837 cppBuffer *pbuf = cppReader_getBuffer (pfile);
4840 llassert (pbuf->buf != NULL);
4842 start_line = pbuf->buf + line_start_mark.position;
4843 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
4846 parseMoveMark (&line_start_mark, pfile);
4848 if (!cppReader_isTraditional (pfile))
4850 cppSkipHspace (pfile);
4853 c = cppReader_getC (pfile);
4856 size_t old_written = cppReader_getWritten (pfile);
4857 cppSkipHspace (pfile);
4859 parse_name (pfile, cppReader_getC (pfile));
4860 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4861 ident = pfile->token_buffer + old_written;
4862 pfile->limit = ident;
4864 for (kt = directive_table; kt->length >= 0; kt++)
4866 cppIfStackFrame *temp;
4867 if (ident_length == kt->length
4868 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
4870 /* If we are asked to return on next directive, do so now. */
4881 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4882 temp->next = pfile->if_stack;
4883 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4884 temp->type = kt->type;
4886 temp->if_succeeded = 0;
4887 temp->control_macro = NULL;
4889 pfile->if_stack = temp;
4890 /*@switchbreak@*/ break;
4893 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
4894 validate_else (pfile,
4895 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
4898 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4900 cppReader_error (pfile,
4901 message ("Preprocessor command #%s is not within a conditional", kt->name));
4902 /*@switchbreak@*/ break;
4904 else if (pfile->if_stack == save_if_stack)
4906 goto done; /* found what we came for */
4913 if (kt->type != T_ENDIF)
4915 llassert (pfile->if_stack != NULL);
4917 if (pfile->if_stack->type == T_ELSE)
4919 cppReader_errorLit (pfile,
4920 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
4923 pfile->if_stack->type = kt->type;
4924 /*@switchbreak@*/ break;
4927 temp = pfile->if_stack;
4928 llassert (temp != NULL);
4929 pfile->if_stack = temp->next;
4931 /*@switchbreak@*/ break;
4934 #if defined (OS2) && defined (__IBMC__)
4935 /* Dummy code to eleminate optimization problems with icc */
4944 /* Don't let erroneous code go by. */
4946 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
4947 && cppReader_isPedantic (pfile))
4949 cppReader_pedwarnLit (pfile,
4950 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
4954 c = cppReader_getC (pfile);
4956 /* We're in the middle of a line. Skip the rest of it. */
4964 case '/': /* possible comment */
4965 c = skip_comment (pfile, NULL);
4968 /*@switchbreak@*/ break;
4971 cppReader_forward (pfile, -1);
4972 old = cppReader_getWritten (pfile);
4973 (void) cppGetToken (pfile);
4974 cppReader_setWritten (pfile, old);
4975 /*@switchbreak@*/ break;
4977 /* Char after backslash loses its special meaning. */
4978 if (cppReader_peekC (pfile) == '\n')
4980 cppReader_forward (pfile, 1);
4983 /*@switchbreak@*/ break;
4987 c = cppReader_getC (pfile);
4990 if (CPPOPTIONS (pfile)->output_conditionals) {
4991 static char end_failed[] = "#endfailed\n";
4992 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
4995 pfile->only_seen_white = 1;
4997 parseGotoMark (&line_start_mark, pfile);
4998 parseClearMark (&line_start_mark);
5002 * handle a #else directive. Do this by just continuing processing
5003 * without changing if_stack ; this is so that the error message
5004 * for missing #endif's etc. will point to the original #if. It
5005 * is possible that something different would be better.
5009 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5010 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5012 if (cppReader_isPedantic (pfile))
5014 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5017 cppReader_skipRestOfLine (pfile);
5019 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack) {
5020 cppReader_errorLit (pfile,
5021 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5024 /* #ifndef can't have its special treatment for containing the whole file
5025 if it has a #else clause. */
5027 llassert (pfile->if_stack != NULL);
5029 pfile->if_stack->control_macro = 0;
5031 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5033 cpp_setLocation (pfile);
5034 genppllerrorhint (FLG_PREPROC,
5035 message ("Pre-processor directive #else after #else"),
5036 message ("%q: Location of match",
5037 fileloc_unparseRaw (pfile->if_stack->fname,
5038 pfile->if_stack->lineno)));
5041 pfile->if_stack->type = T_ELSE;
5044 if (pfile->if_stack->if_succeeded)
5045 skip_if_group (pfile, 0);
5047 ++pfile->if_stack->if_succeeded; /* continue processing input */
5048 output_line_command (pfile, 1, same_file);
5055 * unstack after #endif command
5059 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5060 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5062 if (cppReader_isPedantic (pfile))
5064 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5067 cppReader_skipRestOfLine (pfile);
5069 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5071 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5075 cppIfStackFrame *temp = pfile->if_stack;
5077 llassert (temp != NULL);
5079 pfile->if_stack = temp->next;
5080 if (temp->control_macro != 0)
5082 /* This #endif matched a #ifndef at the start of the file.
5083 See if it is at the end of the file. */
5084 struct parse_marker start_mark;
5087 parseSetMark (&start_mark, pfile);
5091 cppSkipHspace (pfile);
5092 c = cppReader_getC (pfile);
5098 parseGotoMark (&start_mark, pfile);
5099 parseClearMark (&start_mark);
5103 /* If we get here, this #endif ends a #ifndef
5104 that contains all of the file (aside from whitespace).
5105 Arrange not to include the file again
5106 if the macro that was tested is defined.
5108 Do not do this for the top-level file in a -include or any
5109 file in a -imacros. */
5110 struct file_name_list *ifile = pfile->all_include_files;
5112 for ( ; ifile != NULL; ifile = ifile->next)
5114 if (cstring_equal (ifile->fname, cppReader_getBuffer (pfile)->fname))
5116 ifile->control_macro = temp->control_macro;
5124 output_line_command (pfile, 1, same_file);
5129 /* When an #else or #endif is found while skipping failed conditional,
5130 if -pedantic was specified, this is called to warn about text after
5131 the command name. P points to the first char after the command name. */
5134 validate_else (cppReader *pfile, cstring directive)
5137 cppSkipHspace (pfile);
5138 c = cppReader_peekC (pfile);
5139 if (c != EOF && c != '\n')
5141 cppReader_pedwarn (pfile,
5142 message ("text following `%s' violates ANSI standard", directive));
5147 ** Get the next token, and add it to the text in pfile->token_buffer.
5148 ** Return the kind of token we got.
5152 cppGetToken (cppReader *pfile)
5155 size_t old_written = 0;
5156 int start_line, start_column;
5157 enum cpp_token token;
5158 struct cppOptions *opts = CPPOPTIONS (pfile);
5159 cppReader_getBuffer (pfile)->prev = cppReader_getBuffer (pfile)->cur;
5162 c = cppReader_getC (pfile);
5167 if (cppReader_getBuffer (pfile)->seen_eof)
5169 cppBuffer *buf = cppReader_popBuffer (pfile);
5171 if (buf != cppReader_nullBuffer (pfile))
5182 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5183 cppReader_getBuffer (pfile)->seen_eof = 1;
5185 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname)
5186 && next_buf != cppReader_nullBuffer (pfile))
5188 /* We're about to return from an #include file.
5189 Emit #line information now (as part of the CPP_POP) result.
5190 But the #line refers to the file we will pop to. */
5191 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5192 CPPBUFFER (pfile) = next_buf;
5193 pfile->input_stack_listing_current = 0;
5194 output_line_command (pfile, 0, leave_file);
5195 CPPBUFFER (pfile) = cur_buffer;
5203 struct parse_marker start_mark;
5208 if (cppReader_peekC (pfile) == '=')
5213 if (opts->put_out_comments)
5215 parseSetMark (&start_mark, pfile);
5219 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5220 &start_line, &start_column);
5221 c = skip_comment (pfile, &newlines);
5223 if (opts->put_out_comments && (c == '/' || c == EOF))
5225 assertSet (&start_mark);
5226 parseClearMark (&start_mark);
5233 cppReader_errorWithLine (pfile, start_line, start_column,
5234 cstring_makeLiteral ("Unterminated comment"));
5237 c = '/'; /* Initial letter of comment. */
5239 /* Comments are equivalent to spaces.
5240 For -traditional, a comment is equivalent to nothing. */
5242 if (opts->put_out_comments)
5246 assertSet (&start_mark);
5247 res = cpp_handleComment (pfile, &start_mark);
5248 pfile->lineno += newlines;
5251 else if (cppReader_isTraditional (pfile))
5257 cppReader_reserve(pfile, 1);
5258 cppReader_putCharQ (pfile, ' ');
5263 if (!pfile->only_seen_white)
5268 if (cppReader_handleDirective (pfile))
5270 return CPP_DIRECTIVE;
5273 pfile->only_seen_white = 0;
5278 /* A single quoted string is treated like a double -- some
5279 programs (e.g., troff) are perverse this way */
5280 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5281 &start_line, &start_column);
5282 old_written = cppReader_getWritten (pfile);
5284 cppReader_putChar (pfile, c);
5287 int cc = cppReader_getC (pfile);
5290 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
5292 /* try harder: this string crosses a macro expansion
5293 boundary. This can happen naturally if -traditional.
5294 Otherwise, only -D can make a macro with an unmatched
5297 = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5298 (*cppReader_getBuffer (pfile)->cleanup)
5299 (cppReader_getBuffer (pfile), pfile);
5300 CPPBUFFER (pfile) = next_buf;
5303 if (!cppReader_isTraditional (pfile))
5305 cpp_setLocation (pfile);
5307 setLine (long_toInt (start_line));
5308 setColumn (long_toInt (start_column));
5310 if (pfile->multiline_string_line != long_toInt (start_line)
5311 && pfile->multiline_string_line != 0)
5315 message ("Unterminated string or character constant"),
5316 message ("%q: Possible real start of unterminated constant",
5318 (fileloc_filename (g_currentloc),
5319 pfile->multiline_string_line)));
5320 pfile->multiline_string_line = 0;
5326 message ("Unterminated string or character constant"));
5329 /*@loopbreak@*/ break;
5331 cppReader_putChar (pfile, cc);
5335 /* Traditionally, end of line ends a string constant with
5336 no error. So exit the loop and record the new line. */
5337 if (cppReader_isTraditional (pfile))
5343 if (cppReader_isPedantic (pfile)
5344 && pfile->multiline_string_line == 0)
5346 cppReader_pedwarnWithLine
5347 (pfile, long_toInt (start_line),
5348 long_toInt (start_column),
5349 cstring_makeLiteral ("String constant runs past end of line"));
5351 if (pfile->multiline_string_line == 0)
5353 pfile->multiline_string_line = start_line;
5356 /*@switchbreak@*/ break;
5359 cc = cppReader_getC (pfile);
5362 /* Backslash newline is replaced by nothing at all. */
5363 cppReader_adjustWritten (pfile, -1);
5368 /* ANSI stupidly requires that in \\ the second \
5369 is *not* prevented from combining with a newline. */
5372 cppReader_putChar (pfile, cc);
5374 /*@switchbreak@*/ break;
5380 /*@switchbreak@*/ break;
5384 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
5385 cppReader_getPWritten (pfile));
5386 pfile->only_seen_white = 0;
5387 return c == '\'' ? CPP_CHAR : CPP_STRING;
5390 if (!opts->dollars_in_ident)
5395 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
5403 c2 = cppReader_peekC (pfile);
5404 if (c2 == c || c2 == '=')
5414 if (cppReader_peekC (pfile) == '=')
5420 c2 = cppReader_peekC (pfile);
5421 if (c2 == '-' && opts->chill)
5423 /* Chill style comment */
5424 if (opts->put_out_comments)
5426 parseSetMark (&start_mark, pfile);
5429 cppReader_forward (pfile, 1); /* Skip second '-'. */
5433 c = cppReader_getC (pfile);
5435 /*@loopbreak@*/ break;
5438 /* Don't consider final '\n' to be part of comment. */
5439 cppReader_forward (pfile, -1);
5440 /*@loopbreak@*/ break;
5444 goto return_comment;
5446 if (c2 == '-' || c2 == '=' || c2 == '>')
5451 if (pfile->parsing_include_directive)
5455 cppReader_putChar (pfile, c);
5457 /*@loopbreak@*/ break;
5458 c = cppReader_getC (pfile);
5460 if (c == '\n' || c == EOF)
5462 cppReader_errorLit (pfile,
5463 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
5464 /*@loopbreak@*/ break;
5472 c2 = cppReader_peekC (pfile);
5477 cppReader_forward (pfile, 1);
5478 cppReader_reserve (pfile, 4);
5479 cppReader_putChar (pfile, c);
5480 cppReader_putChar (pfile, c2);
5482 c3 = cppReader_peekC (pfile);
5484 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5485 cppReader_nullTerminateQ (pfile);
5486 pfile->only_seen_white = 0;
5490 if (cppReader_getBuffer (pfile)->has_escapes)
5492 c = cppReader_getC (pfile);
5495 if (pfile->output_escapes)
5496 cppReader_puts (pfile, "@-", 2);
5497 parse_name (pfile, cppReader_getC (pfile));
5500 else if (is_space [c])
5502 cppReader_reserve (pfile, 2);
5503 if (pfile->output_escapes)
5504 cppReader_putCharQ (pfile, '@');
5505 cppReader_putCharQ (pfile, c);
5513 if (pfile->output_escapes)
5515 cppReader_puts (pfile, "@@", 2);
5521 c2 = cppReader_peekC (pfile);
5524 cppReader_reserve(pfile, 2);
5525 cppReader_putCharQ (pfile, '.');
5526 c = cppReader_getC (pfile);
5530 /* FIXME - misses the case "..\\\n." */
5531 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
5533 cppReader_reserve(pfile, 4);
5534 cppReader_putCharQ (pfile, '.');
5535 cppReader_putCharQ (pfile, '.');
5536 cppReader_putCharQ (pfile, '.');
5537 cppReader_forward (pfile, 2);
5538 cppReader_nullTerminateQ (pfile);
5539 pfile->only_seen_white = 0;
5545 pfile->only_seen_white = 0;
5547 cppReader_reserve(pfile, 3);
5548 cppReader_putCharQ (pfile, c);
5549 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5550 cppReader_nullTerminateQ (pfile);
5555 c2 = cppReader_peekC (pfile);
5556 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
5558 cppReader_putChar (pfile, c);
5559 c = cppReader_getC (pfile);
5564 case '0': case '1': case '2': case '3': case '4':
5565 case '5': case '6': case '7': case '8': case '9':
5570 cppReader_reserve (pfile, 2);
5571 cppReader_putCharQ (pfile, c);
5573 c = cppReader_peekC (pfile);
5575 /*@loopbreak@*/ break;
5576 if (!is_idchar[c] && c != '.'
5577 && ((c2 != 'e' && c2 != 'E'
5578 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
5579 || (c != '+' && c != '-')))
5580 /*@loopbreak@*/ break;
5581 cppReader_forward (pfile, 1);
5585 cppReader_nullTerminateQ (pfile);
5586 pfile->only_seen_white = 0;
5589 case 'b': case 'c': case 'd': case 'h': case 'o':
5590 case 'B': case 'C': case 'D': case 'H': case 'O':
5591 if (opts->chill && cppReader_peekC (pfile) == '\'')
5593 pfile->only_seen_white = 0;
5594 cppReader_reserve (pfile, 2);
5595 cppReader_putCharQ (pfile, c);
5596 cppReader_putCharQ (pfile, '\'');
5597 cppReader_forward (pfile, 1);
5600 c = cppReader_getC (pfile);
5602 goto chill_number_eof;
5605 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5607 cppReader_forward (pfile, 2);
5610 /*@loopbreak@*/ break;
5612 cppReader_putChar (pfile, c);
5616 cppReader_reserve (pfile, 2);
5617 cppReader_putCharQ (pfile, c);
5618 cppReader_nullTerminateQ (pfile);
5623 cppReader_forward (pfile, -1);
5625 cppReader_nullTerminate (pfile);
5632 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5633 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5634 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5635 case 'x': case 'y': case 'z':
5636 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5637 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5638 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5644 size_t before_name_written = cppReader_getWritten (pfile);
5646 parse_name (pfile, c);
5647 pfile->only_seen_white = 0;
5648 if (pfile->no_macro_expand)
5653 ident = pfile->token_buffer + before_name_written;
5654 ident_len = (cppReader_getPWritten (pfile)) - ident;
5656 hp = cppReader_lookupExpand (ident, ident_len, -1);
5663 if (hp->type == T_DISABLED)
5665 if (pfile->output_escapes)
5666 { /* Return "@-IDENT", followed by '\0'. */
5668 cppReader_reserve (pfile, 3);
5669 ident = pfile->token_buffer + before_name_written;
5670 cppReader_adjustWritten (pfile, 2);
5672 for (i = ident_len; i >= 0; i--)
5674 ident[i+2] = ident[i];
5683 /* If macro wants an arglist, verify that a '(' follows.
5684 first skip all whitespace, copying it to the output
5685 after the macro name. Then, if there is no '(',
5686 decide this is not a macro call and leave things that way. */
5688 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5690 struct parse_marker macro_mark;
5693 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
5695 cppBuffer *next_buf;
5696 cppSkipHspace (pfile);
5697 if (cppReader_peekC (pfile) != EOF)
5699 /*@loopbreak@*/ break;
5702 next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5703 (*cppReader_getBuffer (pfile)->cleanup) (cppReader_getBuffer (pfile), pfile);
5704 CPPBUFFER (pfile) = next_buf;
5707 parseSetMark (¯o_mark, pfile);
5711 cppSkipHspace (pfile);
5712 c = cppReader_peekC (pfile);
5713 is_macro_call = c == '(';
5715 /*@loopbreak@*/ break;
5716 cppReader_forward (pfile, 1);
5721 parseGotoMark (¯o_mark, pfile);
5724 parseClearMark (¯o_mark);
5731 /* This is now known to be a macro call. */
5733 /* it might not actually be a macro. */
5734 if (hp->type != T_MACRO)
5739 cppReader_setWritten (pfile, before_name_written);
5740 special_symbol (hp, pfile);
5741 xbuf_len = cppReader_getWritten (pfile) - before_name_written;
5742 xbuf = (char *) dmalloc (xbuf_len + 1);
5743 cppReader_setWritten (pfile, before_name_written);
5744 memcpy (xbuf, cppReader_getPWritten (pfile), xbuf_len + 1);
5745 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5749 /* Expand the macro, reading arguments as needed,
5750 and push the expansion on the input stack. */
5751 macroexpand (pfile, hp);
5752 cppReader_setWritten (pfile, before_name_written);
5755 /* An extra "@ " is added to the end of a macro expansion
5756 to prevent accidental token pasting. We prefer to avoid
5757 unneeded extra spaces (for the sake of cpp-using tools like
5758 imake). Here we remove the space if it is safe to do so. */
5760 llassert (pfile->buffer->rlimit != NULL);
5762 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5763 && pfile->buffer->rlimit[-2] == '@'
5764 && pfile->buffer->rlimit[-1] == ' ')
5766 int c1 = pfile->buffer->rlimit[-3];
5767 int cl2 = cppBufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
5769 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
5770 pfile->buffer->rlimit -= 2;
5776 case ' ': case '\t': case '\v': case '\r':
5779 cppReader_putChar (pfile, c);
5780 c = cppReader_peekC (pfile);
5781 if (c == EOF || !is_hor_space[c])
5782 /*@loopbreak@*/ break;
5783 cppReader_forward (pfile, 1);
5788 c2 = cppReader_peekC (pfile);
5795 cppReader_putChar (pfile, c);
5796 if (pfile->only_seen_white == 0)
5797 pfile->only_seen_white = 1;
5799 output_line_command (pfile, 1, same_file);
5802 case '(': token = CPP_LPAREN; goto char1;
5803 case ')': token = CPP_RPAREN; goto char1;
5804 case '{': token = CPP_LBRACE; goto char1;
5805 case '}': token = CPP_RBRACE; goto char1;
5806 case ',': token = CPP_COMMA; goto char1;
5807 case ';': token = CPP_SEMICOLON; goto char1;
5813 pfile->only_seen_white = 0;
5814 cppReader_putChar (pfile, c);
5823 /* Parse an identifier starting with C. */
5826 parse_name (cppReader *pfile, int c)
5832 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5834 cppReader_forward (pfile, 2);
5838 cppReader_forward (pfile, -1);
5842 if (c == '$' && cppReader_isPedantic (pfile))
5844 cppReader_pedwarnLit (pfile,
5845 cstring_makeLiteralTemp ("`$' in identifier"));
5848 cppReader_reserve(pfile, 2); /* One more for final NUL. */
5849 cppReader_putCharQ (pfile, c);
5850 c = cppReader_getC (pfile);
5856 cppReader_nullTerminateQ (pfile);
5859 /* The file_name_map structure holds a mapping of file names for a
5860 particular directory. This mapping is read from the file named
5861 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5862 map filenames on a file system with severe filename restrictions,
5863 such as DOS. The format of the file name map file is just a series
5864 of lines with two tokens on each line. The first token is the name
5865 to map, and the second token is the actual name to use. */
5867 struct file_name_map
5869 struct file_name_map *map_next;
5874 /*@constant observer char *FILE_NAME_MAP_FILE*/
5875 #define FILE_NAME_MAP_FILE "header.gcc"
5877 /* Read a space delimited string of unlimited length from a stdio
5880 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
5886 set = alloc = dmalloc (len + 1);
5891 while ((ch = getc (f)) != EOF && ! is_space[ch])
5893 if (set - alloc == size_toInt (len))
5896 alloc = drealloc (alloc, len + 1);
5897 set = alloc + len / 2;
5898 /*@-branchstate@*/ }
5901 } /*@=branchstate@*/
5904 check (ungetc (ch, f) != EOF);
5906 return cstring_fromChars (alloc);
5909 /* This structure holds a linked list of file name maps, one per directory. */
5911 struct file_name_map_list
5913 struct file_name_map_list *map_list_next;
5914 cstring map_list_name;
5915 struct file_name_map *map_list_map;
5918 /* Read the file name map file for DIRNAME. */
5920 static struct file_name_map *
5921 read_name_map (cppReader *pfile, cstring dirname)
5923 struct file_name_map_list *map_list_ptr;
5927 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
5928 map_list_ptr != NULL;
5929 map_list_ptr = map_list_ptr->map_list_next)
5931 if (cstring_equal (map_list_ptr->map_list_name, dirname))
5933 return map_list_ptr->map_list_map;
5937 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
5938 map_list_ptr->map_list_name = cstring_copy (dirname);
5939 map_list_ptr->map_list_map = NULL;
5941 name = cstring_copy (dirname);
5943 if (cstring_length (dirname) > 0)
5945 name = cstring_appendChar (name, CONNECTCHAR);
5948 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
5950 f = fileTable_openFile (context_fileTable (), name, "r");
5951 cstring_free (name);
5955 map_list_ptr->map_list_map = NULL;
5961 while ((ch = getc (f)) != EOF)
5964 struct file_name_map *ptr;
5971 from = read_filename_string (ch, f);
5972 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5977 to = read_filename_string (ch, f);
5979 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
5980 ptr->map_from = from;
5982 /* Make the real filename absolute. */
5983 if (cstring_length (to) > 1
5984 && osd_isConnectChar (cstring_firstChar (to)))
5990 ptr->map_to = cstring_copy (dirname);
5991 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
5992 ptr->map_to = cstring_concatFree (ptr->map_to, to);
5995 ptr->map_next = map_list_ptr->map_list_map;
5996 map_list_ptr->map_list_map = ptr;
5998 while ((ch = getc (f)) != '\n')
6002 /*@innerbreak@*/ break;
6007 assertSet (map_list_ptr->map_list_map);
6008 check (fileTable_closeFile (context_fileTable (),f) == 0);
6011 map_list_ptr->map_list_next = pfile->opts->map_list;
6012 pfile->opts->map_list = map_list_ptr;
6014 return map_list_ptr->map_list_map;
6017 /* Try to open include file FILENAME. SEARCHPTR is the directory
6018 being tried from the include file search path. This function maps
6019 filenames on file systems based on information read by
6023 open_include_file (cppReader *pfile,
6025 struct file_name_list *searchptr)
6027 char *filename = cstring_toCharsSafe (fname);
6028 struct file_name_map *map;
6032 cstring_markOwned (fname);
6034 cpp_setLocation (pfile);
6036 if (context_getFlag (FLG_NEVERINCLUDE))
6038 if (isHeaderFile (fname))
6040 return SKIP_INCLUDE;
6044 if ((searchptr != NULL) && ! searchptr->got_name_map)
6046 searchptr->name_map = read_name_map (pfile,
6047 !cstring_isEmpty (searchptr->fname)
6048 ? searchptr->fname :
6049 cstring_makeLiteralTemp ("."));
6050 searchptr->got_name_map = 1;
6053 /* First check the mapping for the directory we are using. */
6055 if ((searchptr != NULL)
6056 && (searchptr->name_map != NULL))
6060 if (!cstring_isEmpty (searchptr->fname))
6062 from += cstring_length (searchptr->fname) + 1;
6065 for (map = searchptr->name_map;
6067 map = map->map_next)
6069 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6072 ** Found a match. Check if the file should be skipped
6075 if (cpp_skipIncludeFile (map->map_to))
6077 return SKIP_INCLUDE;
6081 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6088 ** Try to find a mapping file for the particular directory we are
6089 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6090 ** in /usr/include/header.gcc and look up types.h in
6091 ** /usr/include/sys/header.gcc.
6094 p = strrchr (filename, CONNECTCHAR);
6101 if ((searchptr != NULL)
6102 && (cstring_isDefined (searchptr->fname))
6103 && (cstring_length (searchptr->fname) == p - filename)
6104 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6106 size_fromInt (p - filename)))
6108 /* filename is in SEARCHPTR, which we've already checked. */
6110 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6112 return SKIP_INCLUDE;
6116 return cpp_openIncludeFile (filename);
6122 dir = mstring_copy (".");
6127 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6128 memcpy (dir, filename, size_fromInt (p - filename));
6129 dir[p - filename] = '\0';
6133 for (map = read_name_map (pfile, cstring_fromChars (dir));
6135 map = map->map_next)
6137 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6141 if (cpp_skipIncludeFile (map->map_to))
6143 return SKIP_INCLUDE;
6147 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6154 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6156 return SKIP_INCLUDE;
6160 return cpp_openIncludeFile (filename);
6164 /* Process the contents of include file FNAME, already open on descriptor F,
6166 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6167 "system" include directories (as decided by the `is_system_include'
6169 DIRPTR is the link in the dir path through which this file was found,
6170 or 0 if the file name was absolute or via the current directory.
6171 Return 1 on success, 0 on failure.
6173 The caller is responsible for the cppReader_pushBuffer. */
6176 finclude (cppReader *pfile, int f,
6178 bool system_header_p,
6179 /*@dependent@*/ struct file_name_list *dirptr)
6185 cppBuffer *fp; /* For input stack frame */
6187 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
6189 cppReader_perrorWithName (pfile, fname);
6190 check (close (f) == 0);
6191 (void) cppReader_popBuffer (pfile);
6197 fp = cppReader_getBuffer (pfile);
6199 /*@-temptrans@*/ /* fname shouldn't really be temp */
6200 fp->nominal_fname = fp->fname = fname;
6204 fp->system_header_p = system_header_p;
6207 fp->cleanup = cppReader_fileCleanup;
6209 if (S_ISREG (st_mode))
6212 fp->buf = (char *) dmalloc (st_size + 2);
6213 fp->alimit = fp->buf + st_size + 2;
6216 /* Read the file contents, knowing that st_size is an upper bound
6217 on the number of bytes we can read. */
6218 length = safe_read (f, fp->buf, size_toInt (st_size));
6219 fp->rlimit = fp->buf + length;
6220 if (length < 0) goto nope;
6222 else if (S_ISDIR (st_mode))
6224 cppReader_error (pfile,
6225 message ("Directory specified in #include: %s", fname));
6226 check (close (f) == 0);
6232 ** Cannot count its file size before reading.
6233 ** First read the entire file into heap and
6234 ** copy them into buffer on stack.
6237 size_t bsize = 2000;
6242 fp->buf = (char *) dmalloc (bsize + 2);
6245 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
6248 goto nope; /* error! */
6251 if (st_size != bsize)
6253 break; /* End of file */
6257 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
6261 length = size_toInt (st_size);
6264 if ((length > 0 && fp->buf[length - 1] != '\n')
6265 /* Backslash-newline at end is not good enough. */
6266 || (length > 1 && fp->buf[length - 2] == '\\')) {
6267 fp->buf[length++] = '\n';
6270 fp->buf[length] = '\0';
6271 fp->rlimit = fp->buf + length;
6273 /* Close descriptor now, so nesting does not use lots of descriptors. */
6274 check (close (f) == 0);
6276 /* Must do this before calling trigraph_pcp, so that the correct file name
6277 will be printed in warning messages. */
6279 pfile->input_stack_listing_current = 0;
6284 cppReader_perrorWithName (pfile, fname);
6285 check (close (f) == 0);
6291 cppReader_init (cppReader *pfile)
6293 memset ((char *) pfile, 0, sizeof (*pfile));
6295 pfile->get_token = cppGetToken;
6296 pfile->token_buffer_size = 200;
6297 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
6298 pfile->all_include_files = NULL;
6302 cppReader_setWritten (pfile, 0);
6304 pfile->system_include_depth = 0;
6305 pfile->max_include_len = 0;
6306 pfile->timebuf = NULL;
6307 pfile->only_seen_white = 1;
6309 pfile->buffer = cppReader_nullBuffer (pfile);
6313 cppReader_finish (/*@unused@*/ cppReader *pfile)
6318 /* Free resources used by PFILE.
6319 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
6322 cppCleanup (cppReader *pfile)
6324 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
6326 (void) cppReader_popBuffer (pfile);
6329 if (pfile->token_buffer != NULL)
6331 sfree (pfile->token_buffer);
6332 pfile->token_buffer = NULL;
6335 while (pfile->if_stack != NULL)
6337 cppIfStackFrame *temp = pfile->if_stack;
6338 pfile->if_stack = temp->next;
6342 while (pfile->all_include_files != NULL)
6344 struct file_name_list *temp = pfile->all_include_files;
6345 pfile->all_include_files = temp->next;
6346 /*@-dependenttrans@*/
6347 cstring_free (temp->fname);
6348 /*@=dependenttrans@*/
6352 cppReader_hashCleanup ();
6356 ** Get the file-mode and data size of the file open on FD
6357 ** and store them in *MODE_POINTER and *SIZE_POINTER.
6361 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
6365 if (fstat (fd, &sbuf) < 0) {
6369 if (mode_pointer != NULL)
6371 *mode_pointer = sbuf.st_mode;
6374 if (size_pointer != NULL)
6376 *size_pointer = (size_t) sbuf.st_size;
6382 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
6383 retrying if necessary. Return a negative value if an error occurs,
6384 otherwise return the actual number of bytes read,
6385 which must be LEN unless end-of-file was reached. */
6387 static int safe_read (int desc, char *ptr, int len)
6393 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
6394 /*@-compdef@*/ /* ptr is an out parameter */
6395 int nchars = _read (desc, ptr, (unsigned) left);
6398 ssize_t nchars = read (desc, ptr, size_fromInt (left));
6407 return (int) nchars;
6421 /* Initialize PMARK to remember the current position of PFILE. */
6424 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
6426 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6428 pmark->next = pbuf->marks;
6430 pbuf->marks = pmark;
6434 pmark->position = pbuf->cur - pbuf->buf;
6437 /* Cleanup PMARK - we no longer need it. */
6439 void parseClearMark (struct parse_marker *pmark)
6441 struct parse_marker **pp = &pmark->buf->marks;
6443 for (; ; pp = &(*pp)->next)
6445 llassert (*pp != NULL);
6446 if (*pp == pmark) break;
6452 /* Backup the current position of PFILE to that saved in PMARK. */
6455 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
6457 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6459 if (pbuf != pmark->buf)
6461 cpp_setLocation (pfile);
6462 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
6465 llassert (pbuf->buf != NULL);
6466 pbuf->cur = pbuf->buf + pmark->position;
6469 /* Reset PMARK to point to the current position of PFILE. (Same
6470 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
6473 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
6475 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6477 if (pbuf != pmark->buf)
6479 cpp_setLocation (pfile);
6480 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
6483 pmark->position = pbuf->cur - pbuf->buf;
6486 void cppReader_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
6488 struct cppOptions *opts = CPPOPTIONS (pfile);
6491 /* The code looks at the defaults through this pointer, rather than through
6492 the constant structure above. This pointer gets changed if an environment
6493 variable specifies other defaults. */
6495 struct default_include *include_defaults = include_defaults_array;
6497 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
6498 /* There seems to be confusion about what CPATH should do,
6499 so for the moment it is not documented. */
6500 /* Some people say that CPATH should replace the standard include dirs,
6501 but that seems pointless: it comes before them, so it overrides them
6504 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
6506 if (cstring_isDefined (xp) && !opts->no_standard_includes)
6508 path_include (pfile, cstring_toCharsSafe (xp));
6511 /* Now that dollars_in_ident is known, initialize is_idchar. */
6512 initialize_char_syntax (opts);
6514 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
6515 and option processing. */
6517 initialize_builtins (pfile);
6519 /* Do standard #defines and assertions
6520 that identify system and machine type. */
6522 if (!opts->inhibit_predefs) {
6523 char *p = (char *) dmalloc (strlen (predefs) + 1);
6524 strcpy (p, predefs);
6530 while (*p == ' ' || *p == '\t')
6535 /* Handle -D options. */
6536 if (p[0] == '-' && p[1] == 'D')
6540 while (*p && *p != ' ' && *p != '\t')
6550 if (opts->debug_output)
6552 output_line_command (pfile, 0, same_file);
6555 cppReader_define (pfile, q);
6557 while (*p == ' ' || *p == '\t')
6571 opts->done_initializing = 1;
6573 { /* Read the appropriate environment variable and if it exists
6574 replace include_defaults with the listed path. */
6578 int win32_buf_size = 0; /* memory we need to allocate */
6581 if (opts->cplusplus)
6583 epath = getenv ("CPLUS_INCLUDE_PATH");
6587 epath = getenv ("C_INCLUDE_PATH");
6591 ** If the environment var for this language is set,
6592 ** add to the default list of include directories.
6595 if (epath != NULL) {
6596 char *nstore = (char *) dmalloc (strlen (epath) + 2);
6598 char *startp, *endp;
6601 /* if we have a posix path list, convert to win32 path list */
6602 if (cygwin32_posix_path_list_p (epath))
6604 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
6605 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
6606 cygwin32_posix_to_win32_path_list (epath, win32epath);
6610 for (num_dirs = 1, startp = epath; *startp; startp++)
6612 if (*startp == PATH_SEPARATOR)
6618 = (struct default_include *) dmalloc ((num_dirs
6619 * sizeof (struct default_include))
6620 + sizeof (include_defaults_array));
6623 startp = endp = epath;
6626 /* Handle cases like c:/usr/lib:d:/gcc/lib */
6627 if ((*endp == PATH_SEPARATOR) || *endp == 0)
6629 strncpy (nstore, startp, size_fromInt (endp - startp));
6632 strcpy (nstore, ".");
6636 nstore[endp-startp] = '\0';
6639 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
6640 include_defaults[num_dirs].cplusplus = opts->cplusplus;
6641 include_defaults[num_dirs].cxx_aware = 1;
6648 endp = startp = endp + 1;
6655 /* Put the usual defaults back in at the end. */
6656 memcpy ((char *) &include_defaults[num_dirs],
6657 (char *) include_defaults_array,
6658 sizeof (include_defaults_array));
6661 /*@-branchstate@*/ } /*@=branchstate@*/
6664 cppReader_appendIncludeChain (pfile, opts->before_system,
6665 opts->last_before_system);
6666 opts->first_system_include = opts->before_system;
6668 /* Unless -fnostdinc,
6669 tack on the standard include file dirs to the specified list */
6670 if (!opts->no_standard_includes) {
6671 struct default_include *p = include_defaults;
6672 char *specd_prefix = opts->include_prefix;
6673 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
6674 int default_len = 0;
6676 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6677 if (default_prefix != NULL) {
6678 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
6679 default_len = strlen (default_prefix) - 7;
6680 default_prefix[default_len] = 0;
6684 /* Search "translated" versions of GNU directories.
6685 These have /usr/local/lib/gcc... replaced by specd_prefix. */
6686 if (specd_prefix != 0 && default_len != 0)
6687 for (p = include_defaults; p->fname != NULL; p++) {
6688 /* Some standard dirs are only for C++. */
6690 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
6691 /* Does this dir start with the prefix? */
6692 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
6693 size_fromInt (default_len)))
6695 /* Yes; change prefix and add to search list. */
6696 struct file_name_list *nlist
6697 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6698 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
6699 char *str = (char *) dmalloc (this_len + 1);
6700 strcpy (str, specd_prefix);
6701 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
6704 nlist->fname = cstring_fromChars (str);
6705 nlist->control_macro = 0;
6706 nlist->c_system_include_path = !p->cxx_aware;
6707 nlist->got_name_map = 0;
6709 cppReader_addIncludeChain (pfile, nlist);
6710 if (opts->first_system_include == 0)
6712 opts->first_system_include = nlist;
6718 /* Search ordinary names for GNU include directories. */
6720 for (p = include_defaults; p->fname != NULL; p++)
6722 /* Some standard dirs are only for C++. */
6724 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
6726 struct file_name_list *nlist
6727 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6728 nlist->control_macro = 0;
6729 nlist->c_system_include_path = !p->cxx_aware;
6730 nlist->fname = p->fname;
6731 nlist->got_name_map = 0;
6734 cppReader_addIncludeChain (pfile, nlist);
6736 if (opts->first_system_include == 0)
6738 opts->first_system_include = nlist;
6742 sfree (default_prefix);
6745 /* Tack the after_include chain at the end of the include chain. */
6746 cppReader_appendIncludeChain (pfile, opts->after_include,
6747 opts->last_after_include);
6749 if (opts->first_system_include == 0)
6751 opts->first_system_include = opts->after_include;
6754 /* With -v, print the list of dirs to search. */
6755 if (opts->verbose) {
6756 struct file_name_list *p;
6757 fprintf (stderr, "#include \"...\" search starts here:\n");
6759 for (p = opts->include; p != NULL; p = p->next) {
6760 if (p == opts->first_bracket_include)
6761 fprintf (stderr, "#include <...> search starts here:\n");
6763 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
6765 fprintf (stderr, "End of search list.\n");
6769 int cppReader_startProcess (cppReader *pfile, cstring fname)
6773 struct cppOptions *opts = CPPOPTIONS (pfile);
6775 fp = cppReader_pushBuffer (pfile, NULL, 0);
6782 if (opts->in_fname == NULL)
6784 opts->in_fname = cstring_makeLiteralTemp ("");
6787 fp->fname = opts->in_fname;
6788 fp->nominal_fname = fp->fname;
6791 /* Copy the entire contents of the main input file into
6792 the stacked input buffer previously allocated for it. */
6794 if (cstring_isEmpty (fname))
6796 fname = cstring_makeLiteralTemp ("");
6799 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
6801 cppReader_error (pfile,
6802 message ("Error opening %s for reading: %s",
6803 fname, lldecodeerror (errno)));
6812 if (finclude (pfile, f, fname, 0, NULL))
6814 output_line_command (pfile, 0, same_file);
6820 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
6822 return pfile->buffer;
6825 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
6827 llassert (pfile->buffer != NULL);
6828 return pfile->buffer;
6831 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
6833 llassert (buf->buf != NULL);
6834 return (buf->buf + buf->line_base);
6837 int cppBufPeek (cppBuffer *buf)
6839 if (buf->cur == NULL || buf->rlimit == NULL) {
6843 if (buf->cur < buf->rlimit) {
6850 bool cppBuffer_isMacro (cppBuffer *buf)
6854 return (buf->cleanup == cppReader_macroCleanup);
6861 ** Returns true if the macro should be checked, false
6862 ** if it should be expanded normally.
6865 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
6866 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
6867 static bool expectiter = FALSE; /* preceeded by @iter@ */
6868 static bool expectenditer = FALSE; /* second after @iter@ */
6869 static bool expectfunction = FALSE; /* preceeded by @function@ */
6870 static bool expectconstant = FALSE; /* preceeded by @constant@ */
6871 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
6873 static void cpp_setLocation (cppReader *pfile)
6878 if (pfile->buffer != NULL)
6880 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname))
6882 cstring fname = cppReader_getBuffer (pfile)->nominal_fname;
6884 DPRINTF (("Looking up: %s", fname));
6886 if (fileTable_exists (context_fileTable (), fname))
6888 fid = fileTable_lookup (context_fileTable (), fname);
6892 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
6894 fid = fileTable_lookup (context_fileTable (),
6895 cppReader_getBuffer (pfile)->fname);
6900 fid = fileTable_lookup (context_fileTable (),
6901 cppReader_getBuffer (pfile)->fname);
6904 line = cppReader_getBuffer (pfile)->lineno;
6905 fileloc_free (g_currentloc);
6907 if (fileId_isValid (fid))
6909 g_currentloc = fileloc_create (fid, line, 1);
6913 g_currentloc = fileloc_createBuiltin ();
6918 fileloc_free (g_currentloc);
6919 g_currentloc = fileloc_createBuiltin ();
6923 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@*/
6925 bool checkmacro = FALSE;
6926 bool hasParams = FALSE;
6927 bool noexpand = FALSE;
6931 cpp_setLocation (pfile);
6933 DPRINTF (("Should check macro? %s", p));
6935 if (expectiter || expectconstant || expectenditer)
6940 expectenditer = TRUE;
6945 expectconstant = FALSE;
6946 expectenditer = FALSE;
6949 if (notfunction || notparseable)
6951 notfunction = FALSE;
6952 notparseable = FALSE;
6961 llassert (*p == '#');
6964 while (*p == ' ' || *p == '\t')
6969 llassert (*p == 'd'); /* define starts */
6973 while (*p == ' ' || *p == '\t')
6978 sname = cstring_fromChars (p);
6979 DPRINTF (("Check macro: %s", sname));
6981 while (((c = *p) != ' ')
6982 && c != '\0' && c != '('
6983 && c != '\t' && c != '\\' && c != '\n'
6989 hasParams = (c == '(');
6995 notparseable = FALSE;
6997 else if (notfunction || fileloc_isStandardLib (g_currentloc))
6999 DPRINTF (("Clear notfunction"));
7000 notfunction = FALSE;
7015 if (usymtab_existsReal (sname))
7017 uentry ue = usymtab_lookup (sname);
7019 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7021 if (fileloc_isPreproc (uentry_whereLast (ue)))
7027 if (uentry_isSpecified (ue))
7029 checkmacro = context_getFlag (FLG_SPECMACROS);
7035 checkmacro = context_getFlag (FLG_LIBMACROS)
7036 || context_getFlag (FLG_FCNMACROS);
7044 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7046 if (fileloc_isSystemFile (g_currentloc)
7047 && context_getFlag (FLG_SYSTEMDIREXPAND))
7049 ; /* don't check this macro */
7050 DPRINTF (("Don't check 1"));
7058 DPRINTF (("Has params..."));
7060 if (context_getFlag (FLG_FCNMACROS))
7062 if (usymtab_exists (sname))
7065 ** only get here is macro is redefined
7066 ** error reported elsewhere
7069 DPRINTF (("It exists!"));
7074 ** We make it a forward function, since it might be declared elsewhere.
7075 ** After all headers have been processed, we should check the forward
7079 fileloc loc = fileloc_makePreproc (g_currentloc);
7081 /* the line is off-by-one, since the newline was already read */
7086 expectfunction = FALSE;
7089 le = uentry_makeForwardFunction (sname,
7090 typeId_invalid, loc);
7096 /* Do not define here! */
7098 (void) usymtab_addEntry (le);
7102 DPRINTF (("Check: TRUE"));
7106 DPRINTF (("Flag FCN_MACROS not set!"));
7111 DPRINTF (("No params"));
7113 if (context_getFlag (FLG_CONSTMACROS))
7115 bool nocontent = FALSE;
7128 ** Check if there is nothing after the define.
7131 while ((*rest) != '\0' && isspace (*rest))
7138 nocontent = TRUE; /* empty macro, don't check */
7143 if (usymtab_exists (sname))
7149 fileloc loc = fileloc_makePreproc (g_currentloc);
7150 DPRINTF (("Make constant: %s", sname));
7151 le = uentry_makeConstant (sname,
7152 ctype_unknown, loc);
7153 (void) usymtab_addEntry (le);
7156 checkmacro = !nocontent;
7161 if (checkmacro && usymtab_existsType (sname))
7163 DPRINTF (("Making false..."));
7165 ppllerror (message ("Specified type implemented as macro: %s", sname));
7175 if (usymtab_exists (sname))
7177 uentry ue = usymtab_lookupExpose (sname);
7178 fileloc tloc = fileloc_makePreproc (g_currentloc);
7180 uentry_setDefined (ue, tloc);
7181 fileloc_free (tloc);
7182 uentry_setUsed (ue, fileloc_undefined);
7186 fileloc tloc = fileloc_makePreproc (g_currentloc);
7187 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7188 DPRINTF (("Make expanded macro: %s", sname));
7189 DPRINTF (("Not in symbol table: %s", sname));
7191 (void) usymtab_addGlobalEntry (ue);
7192 fileloc_free (tloc);
7197 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7201 static enum cpp_token
7202 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7204 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7207 bool eliminateComment = FALSE;
7209 llassert (pbuf->buf != NULL);
7211 start = pbuf->buf + smark->position;
7213 llassert (pbuf->cur != NULL);
7214 len = pbuf->cur - start;
7217 && start[1] == context_getCommentMarkerChar ())
7221 char *scomment = start + 2;
7222 char savec = start[len];
7224 start[0] = BEFORE_COMMENT_MARKER[0];
7225 start[1] = BEFORE_COMMENT_MARKER[1];
7227 llassert (start[len - 2] == '*');
7228 start[len - 2] = AFTER_COMMENT_MARKER[0];
7230 llassert (start[len - 1] == '/');
7231 start[len - 1] = AFTER_COMMENT_MARKER[1];
7233 cppReader_reserve(pfile, size_fromInt (1 + len));
7234 cppReader_putCharQ (pfile, c);
7236 cpp_setLocation (pfile);
7240 if (mstring_containsString (scomment, "/*"))
7242 (void) cppoptgenerror
7244 message ("Comment starts inside syntactic comment: %s",
7245 cstring_fromChars (scomment)),
7251 if (mstring_equalPrefix (scomment, "ignore"))
7253 if (!context_getFlag (FLG_NOCOMMENTS))
7255 context_enterSuppressRegion ();
7258 else if (mstring_equalPrefix (scomment, "end"))
7260 if (!context_getFlag (FLG_NOCOMMENTS))
7262 context_exitSuppressRegion ();
7265 else if (mstring_equalPrefix (scomment, "notparseable"))
7267 notparseable = TRUE;
7269 eliminateComment = TRUE;
7271 else if (mstring_equalPrefix (scomment, "notfunction"))
7275 eliminateComment = TRUE;
7277 else if (mstring_equalPrefix (scomment, "iter"))
7281 else if (mstring_equalPrefix (scomment, "function"))
7283 expectfunction = TRUE;
7285 else if (mstring_equalPrefix (scomment, "constant"))
7287 expectconstant = TRUE;
7291 char sChar = *scomment;
7297 char *rest = scomment + 1;
7299 if (mstring_equalPrefix (rest, "commentchar"))
7301 eliminateComment = TRUE;
7305 ppllerror (cstring_makeLiteral
7306 ("Cannot restore commentchar"));
7310 char *next = scomment + 12; /* strlen commentchar = 12 */
7312 if (*next != ' ' && *next != '\t' && *next != '\n')
7316 ("Syntactic commentchar comment is not followed by a "
7317 "whitespace character: %c",
7322 char cchar = *(next + 1);
7327 (cstring_makeLiteral
7328 ("Cannot set commentchar to NUL"));
7332 context_setCommentMarkerChar (cchar);
7333 /* setComment = TRUE; */
7338 else if (mstring_equalPrefix (scomment, "nestcomment"))
7340 /* fix from Mike Miller <MikeM@xata.com> */
7341 context_fileSetFlag (FLG_NESTCOMMENT,
7342 ynm_fromCodeChar (sChar));
7344 else if (mstring_equalPrefix (rest, "namechecks"))
7346 context_fileSetFlag (FLG_NAMECHECKS,
7347 ynm_fromCodeChar (sChar));
7349 else if (mstring_equalPrefix (rest, "macroredef"))
7351 context_fileSetFlag (FLG_MACROREDEF,
7352 ynm_fromCodeChar (sChar));
7354 else if (mstring_equalPrefix (rest, "usevarargs"))
7356 context_fileSetFlag (FLG_USEVARARGS,
7357 ynm_fromCodeChar (sChar));
7359 else if (mstring_equalPrefix (rest, "nextlinemacros"))
7361 context_fileSetFlag (FLG_MACRONEXTLINE,
7362 ynm_fromCodeChar (sChar));
7364 else if (mstring_equalPrefix (rest, "allmacros")
7365 || mstring_equalPrefix (rest, "fcnmacros")
7366 || mstring_equalPrefix (rest, "constmacros"))
7370 if (mstring_equalPrefix (rest, "allmacros"))
7374 else if (mstring_equalPrefix (rest, "fcnmacros"))
7380 llassert (mstring_equalPrefix (rest, "constmacros"));
7381 fl = FLG_CONSTMACROS;
7385 context_fileSetFlag (fl, ynm_fromCodeChar (sChar));
7386 notfunction = FALSE;
7399 if (eliminateComment)
7404 /* Replaces comment char's in start with spaces */
7406 for (i = 2; i < len - 2; i++)
7408 if (start[i] == BEFORE_COMMENT_MARKER[0]
7409 || start[i] == BEFORE_COMMENT_MARKER[1]
7410 || start[i] == context_getCommentMarkerChar ())
7416 cppReader_putStrN (pfile, start, size_fromInt (len));
7417 parseClearMark (smark);
7427 ** Output the comment as all spaces so line/column
7428 ** in output file is still correct.
7432 cstring lintcomment = cstring_undefined;
7434 if (context_getFlag (FLG_LINTCOMMENTS))
7436 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
7438 lintcomment = cstring_makeLiteralTemp ("l_notreach");
7440 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
7442 lintcomment = cstring_makeLiteralTemp ("l_printfli");
7444 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
7446 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
7448 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
7450 lintcomment = cstring_makeLiteralTemp ("l_argsus");
7452 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
7454 lintcomment = cstring_makeLiteralTemp ("l_fallth");
7458 lintcomment = cstring_undefined;
7463 lintcomment = cstring_undefined;
7466 if (cstring_isDefined (lintcomment))
7468 c = BEFORE_COMMENT_MARKER[0];
7469 start[0] = BEFORE_COMMENT_MARKER[1];
7471 llassert (cstring_length (lintcomment) == len - 3);
7473 for (i = 1; i < len - 2; i++)
7475 start[i] = cstring_getChar (lintcomment, i);
7478 start[len - 2] = AFTER_COMMENT_MARKER[0];
7479 start[len - 1] = AFTER_COMMENT_MARKER[1];
7483 /* Replaces char's in start with spaces */
7484 for (i = 0; i < len; i++)
7488 && start[i + 1] == '*') {
7489 (void) cppoptgenerror (FLG_NESTCOMMENT,
7490 message ("Comment starts inside comment"),
7494 if (start[i] != '\n')
7501 cppReader_reserve (pfile, size_fromInt (1 + len));
7502 cppReader_putCharQ (pfile, c);
7503 cppReader_putStrN (pfile, start, size_fromInt (len));
7504 parseClearMark (smark);
7510 static int cpp_openIncludeFile (char *filename)
7512 int res = open (filename, O_RDONLY, 0666);
7514 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
7517 if (!fileTable_exists (context_fileTable (),
7518 cstring_fromChars (filename)))
7520 (void) fileTable_addHeaderFile (context_fileTable (),
7521 cstring_fromChars (filename));
7525 DPRINTF (("File already exists: %s", filename));
7532 static bool cpp_skipIncludeFile (cstring fname)
7534 if (context_isSystemDir (fname))
7536 DPRINTF (("System dir: %s", fname));
7538 if (lcllib_isSkipHeader (fname))
7540 DPRINTF (("Skip include TRUE: %s", fname));
7544 if (context_getFlag (FLG_SKIPSYSHEADERS))
7546 DPRINTF (("Skip include TRUE: %s", fname));
7551 if (context_getFlag (FLG_SINGLEINCLUDE))
7553 fname = removePreDirs (fname);
7555 # if defined (WIN32) || defined (OS2)
7556 cstring_replaceAll (fname, '\\', '/');
7559 if (fileTable_exists (context_fileTable (), fname))
7561 DPRINTF (("Skip include TRUE: %s", fname));
7566 DPRINTF (("Skip include FALSE: %s", fname));
7570 static int cpp_peekN (cppReader *pfile, int n)
7572 cppBuffer *buf = cppReader_getBuffer (pfile);
7574 llassert (buf->cur != NULL);
7576 return (buf->rlimit - buf->cur >= (n)
7581 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
7586 void cppBuffer_forward (cppBuffer *buf, int n)
7588 llassert (buf->cur != NULL);