3 http://src.openresources.com/debian/src/devel/HTML/S/altgcc_2.7.2.2.orig%20altgcc-2.7.2.2.orig%20protoize.c.html
5 abspath (cwd, rel_filename)
10 *** cpplib.c.old Tue Nov 28 2000 09:04:09 AM
11 --- cpplib.c Tue Nov 28 2000 08:55:18 AM
14 c2 = cppReader_peekC (pfile)
21 c2 = cppReader_peekC (pfile)
24 ! cppReader_forward (pfile, 1)
30 cppReader_putChar (pfile, c)
33 Carl J. Appellof ( mailto:cappello@legato.com <mailto:cappello@legato.com> )
37 ** LCLint - annotation-assisted static program checker
38 ** Copyright (C) 1994-2001 University of Virginia,
39 ** Massachusetts Institute of Technology
41 ** This program is free software; you can redistribute it and/or modify it
42 ** under the terms of the GNU General Public License as published by the
43 ** Free Software Foundation; either version 2 of the License, or (at your
44 ** option) any later version.
46 ** This program is distributed in the hope that it will be useful, but
47 ** WITHOUT ANY WARRANTY; without even the implied warranty of
48 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 ** General Public License for more details.
51 ** The GNU General Public License is available from http://www.gnu.org/ or
52 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
53 ** MA 02111-1307, USA.
55 ** For information on lclint: lclint-request@cs.virginia.edu
56 ** To report a bug: lclint-bug@cs.virginia.edu
57 ** For more information: http://lclint.cs.virginia.edu
63 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
64 Contributed by Per Bothner, 1994-95.
65 Based on CCCP program by Paul Rubin, June 1986
66 Adapted to ANSI C, Richard Stallman, Jan 1987
68 This program is free software; you can redistribute it and/or modify it
69 under the terms of the GNU General Public License as published by the
70 Free Software Foundation; either version 2, or (at your option) any
73 This program is distributed in the hope that it will be useful,
74 but WITHOUT ANY WARRANTY; without even the implied warranty of
75 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76 GNU General Public License for more details.
78 You should have received a copy of the GNU General Public License
79 along with this program; if not, write to the Free Software
80 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
82 In other words, you are welcome to use, share and improve this program.
83 You are forbidden to forbid anyone else to use, share and improve
84 what you give them. Help stamp out software-hoarding! */
88 * - OS2 drive specs like WIN32
89 * - Includes for IBMs OS/2 compiler
101 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
105 # include <sys/types.h>
106 # include <sys/stat.h>
109 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
111 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
116 # include <time.h> /* Reported by Paul Smith */
117 # include <sys/time.h>
118 # include <sys/resource.h>
120 # include <sys/times.h>
124 # endif /* not VMS */
125 # endif /* not WIN32 */
127 /* This defines "errno" properly for VMS, and gives us EACCES. */
130 # include "lclintMacros.nf"
131 # include "llbasic.h"
134 # include "cpperror.h"
135 # include "cpphash.h"
137 # include "version.h"
142 ** This is really kludgey code...
148 #define NO_SHORTNAMES
156 /*@constant int IMPORT_FOUND@*/
157 # define IMPORT_FOUND -2
159 /*@constant int SKIP_INCLUDE@*/
160 # define SKIP_INCLUDE IMPORT_FOUND
162 /*@constant unused int IMPORT_NOT_FOUND@*/
163 # define IMPORT_NOT_FOUND -1
166 /*@constant unused int STDC_VALUE@*/
170 /* By default, colon separates directories in a path. */
171 #ifndef PATH_SEPARATOR
172 /*@constant char PATH_SEPARATOR@*/
173 #define PATH_SEPARATOR ':'
176 static void parse_name (cppReader *, int);
178 static int cpp_openIncludeFile (char *p_filename)
179 /*@modifies fileSystem @*/ ;
181 static void cpp_setLocation (cppReader *p_pfile)
182 /*@modifies g_currentloc@*/ ;
184 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
185 struct parse_marker *p_smark)
186 /*@modifies p_pfile, p_smark@*/;
188 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@*/ ;
190 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
196 /* Symbols to predefine. */
198 #ifdef CPP_PREDEFINES
199 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
201 static /*@observer@*/ char *predefs = "";
204 /* We let tm.h override the types used here, to handle trivial differences
205 such as the choice of unsigned int or long unsigned int for size_t.
206 When machines start needing nontrivial differences in the size type,
207 it would be best to do something here to figure out automatically
208 from other information what type to use. */
210 /* The string value for __SIZE_TYPE__. */
213 /*@constant observer char *SIZE_TYPE@*/
214 #define SIZE_TYPE "long unsigned int"
217 /* The string value for __PTRDIFF_TYPE__. */
220 /*@constant observer char *PTRDIFF_TYPE@*/
221 #define PTRDIFF_TYPE "long int"
224 /* The string value for __WCHAR_TYPE__. */
227 /*@constant observer char *WCHAR_TYPE@*/
228 #define WCHAR_TYPE "int"
231 /* The string value for __USER_LABEL_PREFIX__ */
233 #ifndef USER_LABEL_PREFIX
234 /*@constant observer char *USER_LABEL_PREFIX@*/
235 #define USER_LABEL_PREFIX ""
238 /* The string value for __REGISTER_PREFIX__ */
240 #ifndef REGISTER_PREFIX
241 /*@constant observer char *REGISTER_PREFIX@*/
242 #define REGISTER_PREFIX ""
245 /* table to tell if char can be part of a C identifier. */
246 static bool is_idchar[256];
247 /* table to tell if char can be first char of a c identifier. */
248 static bool is_idstart[256];
249 /* table to tell if c is horizontal space. */
250 static bool is_hor_space[256];
251 /* table to tell if c is horizontal or vertical space. */
252 static bool is_space[256];
254 static /*@exposed@*/ /*@null@*/ cppBuffer *
255 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
256 /*@uses p_pfile->buffer@*/
257 /*@modifies nothing@*/ ;
260 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
263 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
265 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
267 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
268 # define cppBuffer_get(BUFFER) \
269 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
271 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
272 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
273 modifies *p_file; @*/
274 # define cppReader_puts(PFILE, STR, N) \
275 cppReader_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
277 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
279 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
280 modifies *p_file; @*/
281 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
283 /* Append character CH to PFILE's output buffer. Make space if need be. */
285 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
286 modifies *p_file; @*/
287 #define cppReader_putChar(PFILE, CH) (cppReader_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
289 /* Make sure PFILE->limit is followed by '\0'. */
290 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
291 modifies *p_file; @*/
293 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
295 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
296 modifies *p_file; @*/
297 # define cppReader_nullTerminate(PFILE) \
298 (cppReader_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
300 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
301 modifies *p_file; @*/
302 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
304 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
305 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
307 /*@function static observer char *cppReader_wcharType (cppReader *)
308 modifies nothing; @*/
310 # define cppReader_wcharType(PFILE) \
311 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
313 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
315 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
316 # define cppReader_forward(pfile, N) \
317 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
319 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
320 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
322 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
323 # define cppReader_peekC(pfile) (cppBufPeek (cppReader_getBufferSafe (pfile)))
325 /* Move all backslash-newline pairs out of embarrassing places.
326 Exchange all such pairs following BP
327 with any potentially-embarrassing characters that follow them.
328 Potentially-embarrassing characters are / and *
329 (because a backslash-newline inside a comment delimiter
330 would cause it not to be recognized). */
333 # define NEWLINE_FIX \
334 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
336 /* Same, but assume we've already read the potential '\\' into C. */
338 # define NEWLINE_FIX1(C) do { \
339 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
342 static void parseSetMark (/*@out@*/ struct parse_marker *,
344 static void parseClearMark (struct parse_marker *);
345 static void parseGotoMark (struct parse_marker *, cppReader *);
346 static void parseMoveMark (struct parse_marker *, cppReader *);
348 /* If we have a huge buffer, may need to cache more recent counts */
349 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
351 static /*@exposed@*/ /*@null@*/ cppBuffer *
352 cppReader_pushBuffer (cppReader *p_pfile,
353 /*@owned@*/ /*@null@*/ char *, size_t)
354 /*@modifies p_pfile@*/ ;
356 static void cppReader_appendIncludeChain
358 /*@keep@*/ struct file_name_list *p_first,
359 /*@dependent@*/ struct file_name_list *p_last);
361 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
362 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
364 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
365 /*@unused@*/ cppReader *p_pfile);
367 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
368 /*@unused@*/ cppReader *p_pfile);
370 static int cppReader_handleDirective (cppReader *p_pfile);
372 static void cppReader_scanBuffer (cppReader *p_pfile);
374 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
377 ** WIN32 (at least the VC++ include files) does not define mode_t.
380 /*@-incondefs@*/ /*@-czechtypes@*/
381 typedef unsigned int mode_t;
382 /*@=incondefs@*/ /*@=czechtypes@*/
386 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
387 /*@out@*/ size_t *p_size_pointer);
388 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
392 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
393 ** (Note that it is false while we're expanding marco *arguments*.)
396 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
398 static void path_include (cppReader *p_pfile, char *p_path)
399 /*@modifies p_pfile@*/ ;
401 static void initialize_builtins (cppReader *p_pfile)
402 /*@modifies p_pfile@*/ ;
404 static void initialize_char_syntax (struct cppOptions *p_opts) ;
406 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
408 bool p_system_header_p,
409 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
411 static void validate_else (cppReader *p_pfile, cstring p_directive);
413 static void conditional_skip (cppReader *p_pfile, int p_skip,
414 enum node_type p_type,
415 /*@dependent@*/ /*@null@*/ char *p_control_macro);
417 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
421 static void skip_if_group (cppReader *p_pfile, int p_any);
423 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
424 char *p_beg2, int p_len2, bool p_last);
427 extern void fancy_abort ();
430 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
431 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
433 static /*@observer@*/ /*@null@*/ struct file_name_map *
434 read_name_map (cppReader *p_pfile, cstring p_dirname);
436 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
438 static int open_include_file (cppReader *p_pfile,
439 /*@owned@*/ cstring p_fname,
440 /*@null@*/ struct file_name_list *p_searchptr);
442 static void push_macro_expansion (cppReader *,
443 /*@owned@*/ char *, size_t,
444 /*@dependent@*/ hashNode);
446 /* Last arg to output_line_command. */
447 enum file_change_code {
448 same_file, enter_file, leave_file
451 /* `struct directive' defines one #-directive, including how to handle it. */
454 int length; /* Length of name */
455 /*@null@*/ int (*func)(); /* Function to handle directive */
456 /*@observer@*/ cstring name; /* Name of directive */
457 enum node_type type; /* Code which describes which directive. */
458 bool command_reads_line; /* One if rest of line is read by func. */
459 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
460 bool pass_thru; /* Copy preprocessed directive to output file.*/
463 /* These functions are declared to return int instead of void since they
464 are going to be placed in a table and some old compilers have trouble with
465 pointers to functions returning void. */
467 static int do_define (cppReader *, /*@null@*/ struct directive *,
469 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
470 char *, char *, bool);
472 static int do_line (cppReader *, /*@null@*/ struct directive *);
473 static int do_include (cppReader *, struct directive *, char *, char *);
474 static int do_undef (cppReader *, struct directive *, char *, char *);
475 static int do_error (cppReader *, struct directive *, char *, char *);
476 static int do_pragma (cppReader *, struct directive *, char *, char *);
477 static int do_ident (cppReader *, struct directive *, char *, char *);
478 static int do_if (cppReader *, struct directive *, char *, char *);
479 static int do_xifdef (cppReader *, struct directive *, char *, char *);
480 static int do_else (cppReader *, struct directive *, char *, char *);
481 static int do_elif (cppReader *, struct directive *, char *, char *);
482 static int do_endif (cppReader *, struct directive *, char *, char *);
483 static int do_warning (cppReader *, struct directive *, char *, char *);
485 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
486 via the same directory as the file that #included it. */
488 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
489 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
491 /* #include "file" looks in source file dir, then stack. */
492 /* #include <file> just looks in the stack. */
493 /* -I directories are added to the end, then the defaults are added. */
497 static struct default_include {
498 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
499 int cplusplus; /* Only look here if we're compiling C++. */
500 int cxx_aware; /* Includes in this directory don't need to
501 be wrapped in extern "C" when compiling
503 } include_defaults_array[]
505 /* This is the dir for fixincludes. Put it just before
506 the files that we fix. */
507 { GCC_INCLUDE_DIR, 0, 0 },
508 { GCC_INCLUDE_DIR2, 0, 0 },
509 { cstring_undefined, 0, 0 }
512 /*@noaccess cstring@*/
514 /* Here is the actual list of #-directives, most-often-used first.
515 The initialize_builtins function assumes #define is the very first. */
519 static struct directive directive_table[] = {
520 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
521 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
522 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
523 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
524 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
525 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
526 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
527 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
528 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
529 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
530 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
531 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
532 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
533 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
534 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
535 { -1, 0, "", T_UNUSED, FALSE, FALSE, FALSE },
537 /*@noaccess cstring@*/
539 static cstring searchPath_unparse (struct file_name_list *search_start)
541 cstring res = cstring_newEmpty ();
542 struct file_name_list *searchptr = NULL;
544 for (searchptr = search_start; searchptr != NULL;
545 searchptr = searchptr->next)
547 if (!cstring_isEmpty (searchptr->fname)) {
548 res = cstring_concatFree1 (res, searchptr->fname);
549 if (searchptr->next != NULL) {
550 res = cstring_appendChar (res, ';');
560 initialize_char_syntax (struct cppOptions *opts)
565 * Set up is_idchar and is_idstart tables. These should be
566 * faster than saying (is_alpha (c) || c == '_'), etc.
567 * Set up these things before calling any routines tthat
571 for (i = 'a'; i <= 'z'; i++) {
572 is_idchar[i - 'a' + 'A'] = TRUE;
573 is_idchar[(int) i] = TRUE;
574 is_idstart[i - 'a' + 'A'] = TRUE;
575 is_idstart[(int) i] = TRUE;
578 for (i = '0'; i <= '9'; i++)
580 is_idchar[(int) i] = TRUE;
583 is_idchar['_'] = TRUE;
584 is_idstart['_'] = TRUE;
585 is_idchar['$'] = opts->dollars_in_ident;
586 is_idstart['$'] = opts->dollars_in_ident;
588 /* horizontal space table */
589 is_hor_space[' '] = TRUE;
590 is_hor_space['\t'] = TRUE;
591 is_hor_space['\v'] = TRUE;
592 is_hor_space['\f'] = TRUE;
593 is_hor_space['\r'] = TRUE;
595 is_space[' '] = TRUE;
596 is_space['\t'] = TRUE;
597 is_space['\v'] = TRUE;
598 is_space['\f'] = TRUE;
599 is_space['\n'] = TRUE;
600 is_space['\r'] = TRUE;
603 bool isIdentifierChar (char c)
605 return is_idchar[(int) c];
608 /* Place into P_PFILE a quoted string representing the string SRC.
609 Caller must reserve enough space in pfile->token_buffer. */
612 quote_string (cppReader *pfile, char *src)
616 cppReader_putCharQ (pfile, '\"');
619 switch ((c = *src++))
623 cppReader_putCharQ (pfile, c);
626 sprintf (cppReader_getPWritten (pfile), "\\%03o",
628 cppReader_adjustWritten (pfile, (size_t) 4);
630 /*@switchbreak@*/ break;
634 cppReader_putCharQ (pfile, '\\');
635 cppReader_putCharQ (pfile, c);
636 /*@switchbreak@*/ break;
639 cppReader_putCharQ (pfile, '\"');
640 cppReader_nullTerminateQ (pfile);
646 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
649 cppReader_growBuffer (cppReader *pfile, size_t n)
651 size_t old_written = cppReader_getWritten (pfile);
652 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
653 pfile->token_buffer = (char *)
654 drealloc (pfile->token_buffer, pfile->token_buffer_size);
655 cppReader_setWritten (pfile, old_written);
659 * process a given definition string, for initialization
660 * If STR is just an identifier, define it with value 1.
661 * If STR has anything after the identifier, then it should
662 * be identifier=definition.
666 cppReader_define (cppReader *pfile, char *str)
671 DPRINTF (("Cpp reader define: %s", str));
673 if (!is_idstart[(int) *p])
675 DPRINTF (("ERROR 1"));
676 cppReader_error (pfile,
677 message ("Malformed option `-D%s'",
678 cstring_fromChars (str)));
685 DPRINTF (("Here 2"));
687 while (is_idchar[(int) *p])
694 while (*p != ')' && *p != '\0') {
703 message ("Malformed option: -D%s (no closing parenthesis)",
704 cstring_fromChars (str)));
708 DPRINTF (("Here 2"));
712 buf = (char *) dmalloc (size_fromInt (p - str + 4));
713 strcpy ((char *) buf, str);
714 strcat ((char *) buf, " 1");
718 DPRINTF (("ERROR 2"));
719 cppReader_error (pfile,
720 message ("Malformed option: -D%s (expected '=', found '%c')",
721 cstring_fromChars (str),
728 /* Copy the entire option so we can modify it. */
729 DPRINTF (("Copying..."));
730 buf = (char *) dmalloc (2 * strlen (str) + 1);
731 strncpy (buf, str, size_fromInt (p - str));
733 /* Change the = to a space. */
735 /* Scan for any backslash-newline and remove it. */
741 if (*p == '\\' && p[1] == '\n')
747 DPRINTF (("Here we are..."));
751 llassert (buf != NULL);
752 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
753 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
757 /* Append a chain of `struct file_name_list's
758 to the end of the main include chain.
759 FIRST is gthe beginning of the chain to append, and LAST is the end. */
762 cppReader_appendIncludeChain (cppReader *pfile,
763 struct file_name_list *first,
764 struct file_name_list *last)
766 struct cppOptions *opts = CPPOPTIONS (pfile);
767 struct file_name_list *dir;
769 if (first == NULL || last == NULL)
774 if (opts->include == 0)
776 opts->include = first;
780 llassert (opts->last_include->next == NULL);
781 opts->last_include->next = first;
784 if (opts->first_bracket_include == 0)
786 opts->first_bracket_include = first;
788 for (dir = first; ; dir = dir->next) {
789 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
790 if (len > pfile->max_include_len)
791 pfile->max_include_len = len;
797 llassert (last->next == NULL);
798 /* last->next = NULL; */
799 opts->last_include = last;
803 static /*@unused@*/ void
804 cppReader_showIncludeChain (cppReader *pfile)
806 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
812 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
816 fprintf (stderr, "\n");
820 fprintf (stderr, "No includes\n");
826 cppReader_getIncludePath ()
828 cppReader *pfile = &g_cppState;
829 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
830 cstring res = cstring_undefined;
836 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
842 res = cstring_makeLiteral ("<no include path>");
849 cppReader_addIncludeChain (cppReader *pfile, struct file_name_list *dir)
851 struct cppOptions *opts = CPPOPTIONS (pfile);
858 if (opts->include == 0)
864 llassert (opts->last_include->next == NULL);
865 opts->last_include->next = dir;
868 if (opts->first_bracket_include == 0)
870 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
871 opts->first_bracket_include = dir;
872 if (len > pfile->max_include_len)
874 pfile->max_include_len = len;
879 opts->last_include = dir;
880 /* cppReader_showIncludeChain (pfile); */
883 /* Given a colon-separated list of file names PATH,
884 add all the names to the search path for include files. */
887 path_include (cppReader *pfile, char *path)
894 /* if we have a posix path list, convert to win32 path list */
895 win32temp = (char *) dmalloc /*@i4@*/
896 (cygwin32_posix_to_win32_path_list_buf_size (path));
897 cygwin32_posix_to_win32_path_list (path, win32temp);
907 struct file_name_list *dirtmp;
909 /* Find the end of this name. */
910 while (*q != '\0' && *q != PATH_SEPARATOR)
917 /* An empty name in the path stands for the current directory. */
918 name = (char *) dmalloc ((size_t) 2);
924 /* Otherwise use the directory that is named. */
925 name = (char *) dmalloc (size_fromInt (q - p + 1));
926 memcpy (name, p, size_fromInt (q - p));
930 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
931 dirtmp->next = 0; /* New one goes on the end */
932 dirtmp->control_macro = 0;
933 dirtmp->c_system_include_path = 0;
934 dirtmp->fname = cstring_fromChars (name);
935 dirtmp->got_name_map = 0;
936 cppReader_addIncludeChain (pfile, dirtmp);
938 /* Advance past this name. */
942 /* Skip the colon. */
948 cppOptions_init (cppOptions *opts)
950 memset ((char *) opts, 0, sizeof *opts);
953 opts->in_fname = NULL;
954 opts->out_fname = NULL;
956 /* Initialize is_idchar to allow $. */
957 opts->dollars_in_ident = TRUE;
959 opts->no_line_commands = 0;
960 opts->no_trigraphs = TRUE;
961 opts->put_out_comments = 1;
962 opts->print_include_names = 0;
963 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
967 opts->cplusplus_comments = 1;
972 opts->pedantic_errors = 0;
973 opts->warn_comments = 0;
974 opts->warnings_are_errors = 0;
976 initialize_char_syntax (opts);
980 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
986 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
987 /*@unused@*/ cppReader *pfile)
993 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
995 hashNode macro = pbuf->hnode;
997 if (macro->type == T_DISABLED)
999 macro->type = T_MACRO;
1002 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
1010 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
1012 if (pbuf->buf != NULL)
1019 /* Assuming we have read '/'.
1020 If this is the start of a comment (followed by '*' or '/'),
1021 skip to the end of the comment, and return ' '.
1022 Return EOF if we reached the end of file before the end of the comment.
1023 If not the start of a comment, return '/'. */
1026 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1030 llassert (pfile->buffer != NULL);
1031 llassert (pfile->buffer->cur != NULL);
1033 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1040 cppReader_forward (pfile, 2);
1043 if (cppReader_peekC (pfile) == '*')
1045 cppReader_forward (pfile, 1);
1050 c = cppReader_getC (pfile);
1057 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1064 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1067 if (prev_c == (int) '*' && c == (int) '/')
1072 if (c == (int) '\n' && (linep != NULL))
1078 else if (cppReader_peekC (pfile) == '/'
1079 && CPPOPTIONS (pfile)->cplusplus_comments)
1081 cppReader_forward (pfile, 1);
1085 c = cppReader_getC (pfile);
1089 /* Allow hash comment to be terminated by EOF. */
1093 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1095 cppReader_forward (pfile, 1);
1096 c = cppReader_getC (pfile);
1104 if (c == (int) '\n')
1106 /* Don't consider final '\n' to be part of comment. */
1107 cppReader_forward (pfile, -1);
1118 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1120 cppSkipHspace (cppReader *pfile)
1128 llassert (pfile->buffer != NULL);
1130 c = cppReader_peekC (pfile);
1134 return 0; /* FIXME */
1137 if (is_hor_space[c])
1139 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1140 cppReader_pedwarn (pfile,
1141 message ("%s in preprocessing directive",
1143 ? cstring_makeLiteralTemp ("formfeed")
1144 : cstring_makeLiteralTemp ("vertical tab")));
1147 cppReader_forward (pfile, 1);
1151 cppReader_forward (pfile, 1);
1152 c = skip_comment (pfile, NULL);
1156 cppReader_forward (pfile, -1);
1159 if (c == EOF || c == '/')
1164 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1166 cppReader_forward (pfile, 2);
1168 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1169 && is_hor_space [cpp_peekN (pfile, 1)])
1171 cppReader_forward (pfile, 2);
1180 /* Read the rest of the current line.
1181 The line is appended to PFILE's output buffer. */
1184 copy_rest_of_line (cppReader *pfile)
1186 struct cppOptions *opts = CPPOPTIONS (pfile);
1193 llassert (pfile->buffer != NULL);
1195 c = cppReader_getC (pfile);
1201 if (cppReader_peekC (pfile) == '\n')
1203 cppReader_forward (pfile, 1);
1207 /*@fallthrough@*/ case '\'': case '\"':
1208 goto scan_directive_token;
1211 nextc = cppReader_peekC (pfile);
1214 ** was (opts->cplusplus_comments && nextc == '*')
1219 || (opts->cplusplus_comments && nextc == '/'))
1221 goto scan_directive_token;
1223 /*@switchbreak@*/ break;
1226 if (cppReader_isPedantic (pfile))
1227 cppReader_pedwarn (pfile,
1228 message ("%s in preprocessing directive",
1230 ? cstring_makeLiteralTemp ("formfeed")
1231 : cstring_makeLiteralTemp ("vertical tab")));
1232 /*@switchbreak@*/ break;
1235 cppReader_forward (pfile, -1);
1237 scan_directive_token:
1238 cppReader_forward (pfile, -1);
1239 (void) cppGetToken (pfile);
1242 cppReader_putChar (pfile, c);
1245 cppReader_nullTerminate (pfile);
1249 cppReader_skipRestOfLine (cppReader *pfile)
1251 size_t old = cppReader_getWritten (pfile);
1252 copy_rest_of_line (pfile);
1253 cppReader_setWritten (pfile, old);
1256 /* Handle a possible # directive.
1257 '#' has already been read. */
1260 cppReader_handleDirective (cppReader *pfile)
1263 struct directive *kt = NULL;
1265 size_t after_ident = 0;
1266 char *ident, *line_end = NULL;
1267 size_t old_written = cppReader_getWritten (pfile);
1268 int nspaces = cppSkipHspace (pfile);
1270 c = cppReader_peekC (pfile);
1272 if (c >= '0' && c <= '9')
1274 /* Handle # followed by a line number. */
1275 if (cppReader_isPedantic (pfile))
1277 cppReader_pedwarnLit
1279 cstring_makeLiteralTemp ("`#' followed by integer"));
1282 (void) do_line (pfile, NULL);
1283 goto done_a_directive;
1287 /* Now find the directive name. */
1289 cppReader_putChar (pfile, '#');
1291 parse_name (pfile, cppReader_getC (pfile));
1293 llassert (pfile->token_buffer != NULL);
1294 ident = pfile->token_buffer + old_written + 1;
1296 ident_length = cppReader_getPWritten (pfile) - ident;
1298 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1300 /* A line of just `#' becomes blank. */
1304 for (kt = directive_table; ; kt++)
1306 if (kt->length <= 0)
1308 return 0; /* goto not_a_directive; */
1311 if (kt->length == ident_length
1312 && (cstring_equalPrefix (kt->name, ident)))
1318 if (kt->command_reads_line)
1324 /* Nonzero means do not delete comments within the directive.
1325 #define needs this when -traditional. */
1326 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1327 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1328 CPPOPTIONS (pfile)->put_out_comments = comments;
1329 after_ident = cppReader_getWritten (pfile);
1330 copy_rest_of_line (pfile);
1331 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1335 /* For #pragma and #define, we may want to pass through the directive.
1336 Other directives may create output, but we don't want the directive
1337 itself out, so we pop it now. For example #include may write a #line
1338 command (see comment in do_include), and conditionals may emit
1339 #failed ... #endfailed stuff. But note that popping the buffer
1340 means the parameters to kt->func may point after pfile->limit
1341 so these parameters are invalid as soon as something gets appended
1342 to the token_buffer. */
1344 line_end = cppReader_getPWritten (pfile);
1347 if (!kt->pass_thru && kt->type != T_DEFINE)
1349 cppReader_setWritten (pfile, old_written);
1352 llassert (pfile->token_buffer != NULL);
1354 /* was kt->pass_thru || */
1356 if (kt->type == T_DEFINE
1357 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1359 char *p = pfile->token_buffer + old_written;
1362 ** Still need to record value for preprocessing, so
1363 ** #ifdef's, etc. using the value behave correctly.
1366 (void) do_defineAux (pfile, kt,
1367 pfile->token_buffer + after_ident,
1376 SKIP_WHITE_SPACE (p);
1378 llassert (*p == 'd');
1381 llassert (*p == 'e');
1384 llassert (*p == 'f');
1387 llassert (*p == 'i');
1390 llassert (*p == 'n');
1393 llassert (*p == 'e');
1396 ** This is way-bogus. We use the last char to record the number of
1397 ** spaces. Its too hard to get them back into the input stream.
1400 if (nspaces > 9) nspaces = 9;
1402 *p++ = '0' + nspaces;
1404 return 0; /* not_a_directive */
1406 else if (kt->pass_thru)
1408 /* Just leave the entire #define in the output stack. */
1409 return 0; /* not_a_directive */
1412 else if (kt->type == T_DEFINE
1413 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1415 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1416 SKIP_WHITE_SPACE (p);
1418 while (is_idchar[(int) *p])
1424 cppReader_putChar (pfile, '\n');
1426 else if (kt->type == T_DEFINE)
1428 cppReader_setWritten (pfile, old_written);
1439 llassert (kt->func != NULL);
1440 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1445 /* Pass a directive through to the output file.
1446 BUF points to the contents of the directive, as a contiguous string.
1447 LIMIT points to the first character past the end of the directive.
1448 KEYWORD is the keyword-table entry for the directive. */
1451 pass_thru_directive (char *buf, char *limit,
1453 struct directive *keyword)
1455 int keyword_length = keyword->length;
1457 cppReader_reserve (pfile,
1458 size_fromInt (2 + keyword_length + (limit - buf)));
1459 cppReader_putCharQ (pfile, '#');
1460 /*@-observertrans@*/
1461 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1462 size_fromInt (keyword_length));
1463 /*:=observertrans@*/
1465 if (limit != buf && buf[0] != ' ')
1467 /* Was a bug, since reserve only used 1 + ... */
1468 cppReader_putCharQ (pfile, ' ');
1471 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1474 /* Read a replacement list for a macro with parameters.
1475 Build the DEFINITION structure.
1476 Reads characters of text starting at BUF until END.
1477 ARGLIST specifies the formal parameters to look for
1478 in the text of the definition; NARGS is the number of args
1479 in that list, or -1 for a macro name that wants no argument list.
1480 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1481 and NAMELEN is its length in characters.
1483 Note that comments, backslash-newlines, and leading white space
1484 have already been deleted from the argument. */
1487 collect_expansion (cppReader *pfile, char *buf, char *limit,
1488 int nargs, /*@null@*/ struct arglist *arglist)
1491 char *p, *lastp, *exp_p;
1492 struct reflist *endpat = NULL;
1493 /* Pointer to first nonspace after last ## seen. */
1495 /* Pointer to first nonspace after last single-# seen. */
1496 char *stringify = 0;
1498 char expected_delimiter = '\0';
1501 /* Scan thru the replacement list, ignoring comments and quoted
1502 strings, picking up on the macro calls. It does a linear search
1503 thru the arg list on every potential symbol. Profiling might say
1504 that something smarter should happen. */
1509 /* Find the beginning of the trailing whitespace. */
1512 while (p < limit && is_space[(int) limit[-1]])
1517 /* Allocate space for the text in the macro definition.
1518 Leading and trailing whitespace chars need 2 bytes each.
1519 Each other input char may or may not need 1 byte,
1520 so this is an upper bound. The extra 5 are for invented
1521 leading and trailing newline-marker and final null. */
1522 maxsize = (sizeof (*defn) + (limit - p) + 5);
1524 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1533 defn = (DEFINITION *) dmalloc (maxsize);
1534 defn->noExpand = FALSE;
1536 defn->pattern = NULL;
1537 defn->nargs = nargs;
1538 defn->predefined = NULL;
1540 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1543 defn->rest_args = NULL;
1544 defn->args.argnames = NULL;
1550 /* Add one initial space escape-marker to prevent accidental
1551 token-pasting (often removed by macroexpand). */
1555 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1556 cppReader_errorLit (pfile,
1557 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1561 /* Process the main body of the definition. */
1563 int skipped_arg = 0;
1564 register char c = *p++;
1568 if (!cppReader_isTraditional (pfile)) {
1572 if (expected_delimiter != '\0')
1574 if (c == expected_delimiter)
1575 expected_delimiter = '\0';
1579 expected_delimiter = c;
1581 /*@switchbreak@*/ break;
1584 if (p < limit && (expected_delimiter != '\0'))
1586 /* In a string, backslash goes through
1587 and makes next char ordinary. */
1590 /*@switchbreak@*/ break;
1593 /* An '@' in a string or character constant stands for itself,
1594 and does not need to be escaped. */
1595 if (expected_delimiter == '\0')
1600 /*@switchbreak@*/ break;
1603 /* # is ordinary inside a string. */
1604 if (expected_delimiter != '\0')
1606 /*@switchbreak@*/ break;
1609 if (p < limit && *p == '#') {
1610 /* ##: concatenate preceding and following tokens. */
1611 /* Take out the first #, discard preceding whitespace. */
1615 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1621 /* Skip the second #. */
1623 /* Discard following whitespace. */
1624 SKIP_WHITE_SPACE (p);
1628 cppReader_errorLit (pfile,
1629 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1631 } else if (nargs >= 0) {
1632 /* Single #: stringify following argument ref.
1633 Don't leave the # in the expansion. */
1635 SKIP_WHITE_SPACE (p);
1636 if (p == limit || ! is_idstart[(int) *p]
1637 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1638 cppReader_errorLit (pfile,
1639 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1646 /*@switchbreak@*/ break;
1649 /* In -traditional mode, recognize arguments inside strings and
1650 and character constants, and ignore special properties of #.
1651 Arguments inside strings are considered "stringified", but no
1652 extra quote marks are supplied. */
1656 if (expected_delimiter != '\0') {
1657 if (c == expected_delimiter)
1658 expected_delimiter = '\0';
1660 expected_delimiter = c;
1661 /*@switchbreak@*/ break;
1664 /* Backslash quotes delimiters and itself, but not macro args. */
1665 if (expected_delimiter != '\0' && p < limit
1666 && (*p == expected_delimiter || *p == '\\')) {
1670 /*@switchbreak@*/ break;
1673 if (expected_delimiter != '\0') /* No comments inside strings. */
1674 /*@switchbreak@*/ break;
1676 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1677 this must be -traditional. So replace the comment with
1681 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1686 /*@switchbreak@*/ break;
1690 /* Handle the start of a symbol. */
1691 if (is_idchar[(int) c] && nargs > 0) {
1692 char *id_beg = p - 1;
1696 while (p != limit && is_idchar[(int) *p])
1701 id_len = p - id_beg;
1703 if (is_idstart[(int) c]
1704 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1705 register struct arglist *arg;
1707 for (arg = arglist; arg != NULL; arg = arg->next) {
1708 struct reflist *tpat;
1710 if (arg->name[0] == c
1711 && arg->length == id_len
1712 && strncmp (arg->name, id_beg, size_fromInt (id_len)) == 0) {
1715 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1716 if (cppReader_isTraditional (pfile)) {
1717 cppReader_warning (pfile,
1718 message ("macro argument `%x' is stringified.",
1719 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1721 cppReader_warning (pfile,
1722 message ("macro arg `%x' would be stringified with -traditional.",
1723 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1726 /* If ANSI, don't actually substitute inside a string. */
1727 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1728 /*@innerbreak@*/ break;
1729 /* make a pat node for this arg and append it to the end of
1731 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1733 tpat->raw_before = (concat == id_beg);
1734 tpat->raw_after = 0;
1735 tpat->rest_args = arg->rest_args;
1736 tpat->stringify = (cppReader_isTraditional (pfile)
1737 ? expected_delimiter != '\0'
1738 : stringify == id_beg);
1742 defn->pattern = tpat;
1746 endpat->next = tpat;
1748 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1752 tpat->argno = arg->argno;
1753 tpat->nchars = exp_p - lastp;
1757 SKIP_WHITE_SPACE (p1);
1759 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1761 tpat->raw_after = 1;
1764 lastp = exp_p; /* place to start copying from next time */
1767 /*@innerbreak@*/ break;
1772 /* If this was not a macro arg, copy it into the expansion. */
1773 if (skipped_arg == 0) {
1774 register char *lim1 = p;
1782 if (stringify == id_beg)
1783 cppReader_errorLit (pfile,
1784 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1789 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1791 /* If ANSI, put in a "@ " marker to prevent token pasting.
1792 But not if "inside a string" (which in ANSI mode
1793 happens only for -D option). */
1800 defn->length = size_fromInt (exp_p - defn->expansion);
1802 /* Crash now if we overrun the allocated size. */
1803 if (defn->length + 1 > maxsize)
1805 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1812 * special extension string that can be added to the last macro argument to
1813 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1814 * #define wow(a, b...) process (b, a, b)
1815 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1816 * { wow (one, two); } -> { process (two, one, two); }
1817 * if this "rest_arg" is used with the concat token '##' and if it is not
1818 * supplied then the token attached to with ## will not be outputted. Ex:
1819 * #define wow (a, b...) process (b ## , a, ## b)
1820 * { wow (1, 2); } -> { process (2, 1, 2); }
1821 * { wow (one); } -> { process (one); {
1824 /*@-readonlytrans@*/
1825 static char rest_extension[] = "...";
1826 /*:=readonlytrans@*/
1829 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1831 /* Create a DEFINITION node from a #define directive. Arguments are
1832 as for do_define. */
1834 static /*@null@*/ MACRODEF
1835 create_definition (char *buf, char *limit,
1836 cppReader *pfile, bool predefinition,
1839 char *bp; /* temp ptr into input buffer */
1840 char *symname; /* remember where symbol name starts */
1841 int sym_length; /* and how long it is */
1842 int rest_args = 0; /* really int! */
1845 cstring file = (CPPBUFFER (pfile) != NULL)
1846 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
1848 int arglengths = 0; /* Accumulate lengths of arg names
1849 plus number of args. */
1852 cppBuffer_lineAndColumn (CPPBUFFER (pfile), &line, &col);
1856 while (is_hor_space[(int) *bp])
1861 symname = bp; /* remember where it starts */
1863 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
1867 /* Lossage will occur if identifiers or control keywords are broken
1868 across lines using backslash. This is not the right place to take
1872 struct arglist *arg_ptrs = NULL;
1875 bp++; /* skip '(' */
1876 SKIP_WHITE_SPACE (bp);
1878 /* Loop over macro argument names. */
1881 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
1883 temp->next = arg_ptrs;
1884 temp->argno = argno++;
1885 temp->rest_args = 0;
1891 cppReader_pedwarn (pfile,
1892 message ("another parameter follows `%s'",
1893 cstring_fromChars (rest_extension)));
1896 if (!is_idstart[(int) *bp])
1898 cppReader_pedwarnLit (pfile,
1899 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
1902 /* Find the end of the arg name. */
1903 while (is_idchar[(int) *bp])
1906 /* do we have a "special" rest-args extension here? */
1907 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
1908 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
1911 temp->rest_args = 1;
1912 /*@innerbreak@*/ break;
1916 temp->length = bp - temp->name;
1920 bp += REST_EXTENSION_LENGTH;
1923 arglengths += temp->length + 2;
1924 SKIP_WHITE_SPACE (bp);
1926 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1927 cppReader_errorLit (pfile,
1928 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
1934 SKIP_WHITE_SPACE (bp);
1937 cppReader_errorLit (pfile,
1938 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
1942 struct arglist *otemp;
1944 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1946 if (temp->length == otemp->length &&
1947 strncmp (temp->name, otemp->name, size_fromInt (temp->length)) == 0) {
1948 cstring name = cstring_copyLength (temp->name, temp->length);
1949 cppReader_error (pfile,
1950 message ("duplicate argument name `%x' in `#define'", name));
1957 ++bp; /* skip paren */
1958 SKIP_WHITE_SPACE (bp);
1959 /* now everything from bp before limit is the definition. */
1960 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1961 defn->rest_args = rest_args;
1963 /* Now set defn->args.argnames to the result of concatenating
1964 the argument names in reverse order
1965 with comma-space between them. */
1966 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
1969 struct arglist *temp;
1971 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
1972 memcpy (&defn->args.argnames[i], temp->name, size_fromInt (temp->length));
1974 if (temp->next != 0) {
1975 defn->args.argnames[i++] = ',';
1976 defn->args.argnames[i++] = ' ';
1980 defn->args.argnames[i] = '\0';
1985 /* Simple expansion or empty definition. */
1989 if (is_hor_space[(int) *bp]) {
1991 SKIP_WHITE_SPACE (bp);
1994 case '!': case '"': case '#': case '%': case '&': case '\'':
1995 case ')': case '*': case '+': case ',': case '-': case '.':
1996 case '/': case ':': case ';': case '<': case '=': case '>':
1997 case '?': case '[': case '\\': case ']': case '^': case '{':
1998 case '|': case '}': case '~':
1999 cppReader_warning (pfile,
2000 message ("Missing white space after #define %x",
2001 cstring_prefix (cstring_fromChars (symname),
2006 cppReader_pedwarn (pfile,
2007 message ("Missing white space after #define %x",
2008 cstring_prefix (cstring_fromChars (symname),
2014 /* now everything from bp before limit is the definition. */
2015 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2016 defn->args.argnames = mstring_createEmpty ();
2019 defn->noExpand = noExpand;
2020 DPRINTF (("No expand: %d", noExpand));
2024 /* not: llassert (cstring_isUndefined (defn->file)); */
2027 /* OP is null if this is a predefinition */
2028 defn->predefined = predefinition;
2030 mdef.symnam = symname;
2031 mdef.symlen = sym_length;
2041 /* Check a purported macro name SYMNAME, and yield its length.
2042 USAGE is the kind of name this is intended for. */
2044 int cppReader_checkMacroName (cppReader *pfile,
2051 for (p = symname; is_idchar[(int) *p]; p++)
2056 sym_length = size_fromInt (p - symname);
2059 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
2060 cppReader_error (pfile, message ("invalid %s name", usage));
2061 else if (!is_idstart[(int) *symname])
2063 char *msg = (char *) dmalloc (sym_length + 1);
2064 memcpy (msg, symname, sym_length);
2065 msg[sym_length] = '\0';
2066 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2067 cstring_fromChars (msg)));
2072 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2074 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2078 return size_toInt (sym_length);
2081 /* Return zero if two DEFINITIONs are isomorphic. */
2084 compare_defs (DEFINITION *d1, DEFINITION *d2)
2086 register struct reflist *a1, *a2;
2087 register char *p1 = d1->expansion;
2088 register char *p2 = d2->expansion;
2091 if (d1->nargs != d2->nargs)
2096 llassert (d1->args.argnames != NULL);
2097 llassert (d2->args.argnames != NULL);
2099 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2104 for (a1 = d1->pattern, a2 = d2->pattern;
2105 (a1 != NULL) && (a2 != NULL);
2106 a1 = a1->next, a2 = a2->next) {
2107 if (!((a1->nchars == a2->nchars
2108 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2109 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2110 || a1->argno != a2->argno
2111 || a1->stringify != a2->stringify
2112 || a1->raw_before != a2->raw_before
2113 || a1->raw_after != a2->raw_after)
2122 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2123 p2, d2->length - (p2 - d2->expansion), 1))
2129 /* Return TRUE if two parts of two macro definitions are effectively different.
2130 One of the parts starts at BEG1 and has LEN1 chars;
2131 the other has LEN2 chars at BEG2.
2132 Any sequence of whitespace matches any other sequence of whitespace.
2133 FIRST means these parts are the first of a macro definition;
2134 so ignore leading whitespace entirely.
2135 LAST means these parts are the last of a macro definition;
2136 so ignore trailing whitespace entirely. */
2139 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2141 char *end1 = beg1 + len1;
2142 char *end2 = beg2 + len2;
2145 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2146 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2149 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2150 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2152 while (beg1 != end1 && beg2 != end2) {
2153 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2154 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2155 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2156 } else if (*beg1 == *beg2) {
2160 return (beg1 != end1) || (beg2 != end2);
2163 /* Process a #define command.
2164 BUF points to the contents of the #define command, as a contiguous string.
2165 LIMIT points to the first character past the end of the definition.
2166 KEYWORD is the keyword-table entry for #define,
2167 or NULL for a "predefined" macro. */
2170 do_defineAux (cppReader *pfile, struct directive *keyword,
2171 char *buf, char *limit, bool noExpand)
2177 DPRINTF (("Define aux: %d", noExpand));
2179 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2184 hashcode = hashf (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2186 DPRINTF (("Macro: %s / %s",
2187 cstring_copyLength (mdef.symnam, mdef.symlen),
2188 bool_unparse (noExpand)));
2190 if ((hp = cppReader_lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL)
2194 /* Redefining a precompiled key is ok. */
2195 if (hp->type == T_PCSTRING)
2197 /* Redefining a macro is ok if the definitions are the same. */
2198 else if (hp->type == T_MACRO)
2199 ok = !compare_defs (mdef.defn, hp->value.defn);
2200 /* Redefining a constant is ok with -D. */
2201 else if (hp->type == T_CONST)
2202 ok = !CPPOPTIONS (pfile)->done_initializing;
2207 /* Print the warning if it's not ok. */
2211 ** If we are passing through #define and #undef directives, do
2212 ** that for this re-definition now.
2215 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2217 /* llassert (keyword != NULL); */
2218 pass_thru_directive (buf, limit, pfile, keyword);
2221 cpp_setLocation (pfile);
2223 if (hp->type == T_MACRO)
2225 if (hp->value.defn->noExpand)
2227 ; /* error will be reported checking macros */
2233 message ("Macro %q already defined",
2234 cstring_copyLength (mdef.symnam, mdef.symlen)),
2235 message ("%q: Previous definition of %q",
2236 fileloc_unparseRaw (hp->value.defn->file,
2237 (int) hp->value.defn->line),
2238 cstring_copyLength (mdef.symnam, mdef.symlen)));
2243 genppllerror (FLG_MACROREDEF,
2244 message ("Macro %q already defined",
2245 cstring_copyLength (mdef.symnam,
2251 /* Replace the old definition. */
2253 hp->value.defn = mdef.defn;
2258 ** If we are passing through #define and #undef directives, do
2259 ** that for this new definition now.
2264 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2266 pass_thru_directive (buf, limit, pfile, keyword);
2269 DPRINTF (("Define macro: %s / %d",
2270 mdef.symnam, mdef.defn->noExpand));
2272 hn = cppReader_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2274 } /*@=branchstate@*/
2284 do_define (cppReader *pfile, struct directive *keyword,
2285 char *buf, char *limit)
2287 DPRINTF (("Regular do define"));
2288 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2291 /* This structure represents one parsed argument in a macro call.
2292 `raw' points to the argument text as written (`raw_length' is its length).
2293 `expanded' points to the argument's macro-expansion
2294 (its length is `expand_length').
2295 `stringified_length' is the length the argument would have
2297 `use_count' is the number of times this macro arg is substituted
2298 into the macro. If the actual use count exceeds 10,
2299 the value stored is 10. */
2301 /* raw and expanded are relative to ARG_BASE */
2303 #define ARG_BASE ((pfile)->token_buffer)
2306 /* Strings relative to pfile->token_buffer */
2312 int stringified_length;
2317 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2318 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2319 as the new input buffer.
2320 Return the new buffer, or NULL on failure. */
2322 /*@null@*/ /*@exposed@*/ cppBuffer *
2323 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2325 cppBuffer *buf = cppReader_getBuffer (pfile);
2327 if (buf == pfile->buffer_stack)
2329 cppReader_fatalError
2331 message ("%s: macro or `#include' recursion too deep",
2332 (buf->fname != NULL)
2334 : cstring_makeLiteral ("<no name>")));
2335 sfreeEventually (buffer);
2339 llassert (buf != NULL);
2342 memset ((char *) buf, 0, sizeof (*buf));
2343 CPPBUFFER (pfile) = buf;
2345 buf->if_stack = pfile->if_stack;
2346 buf->cleanup = cppReader_nullCleanup;
2347 buf->underflow = cppReader_nullUnderflow;
2349 buf->cur = buf->buf;
2353 buf->alimit = buf->rlimit = buffer + length;
2357 buf->alimit = buf->rlimit = NULL;
2364 cppReader_popBuffer (cppReader *pfile)
2366 cppBuffer *buf = CPPBUFFER (pfile);
2368 llassert (buf != NULL);
2370 (void) (*buf->cleanup) (buf, pfile);
2371 return ++CPPBUFFER (pfile);
2374 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2375 Pop the buffer when done. */
2378 cppReader_scanBuffer (cppReader *pfile)
2380 cppBuffer *buffer = CPPBUFFER (pfile);
2383 enum cpp_token token;
2385 token = cppGetToken (pfile);
2387 if (token == CPP_EOF) /* Should not happen ... */
2392 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2394 (void) cppReader_popBuffer (pfile);
2401 * Rescan a string (which may have escape marks) into pfile's buffer.
2402 * Place the result in pfile->token_buffer.
2404 * The input is copied before it is scanned, so it is safe to pass
2405 * it something from the token_buffer that will get overwritten
2406 * (because it follows cppReader_getWritten). This is used by do_include.
2410 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
2412 register cppBuffer *ip;
2413 char *limit = buf + length;
2414 char *buf1, *p1, *p2;
2416 /* evans - 2001-08-26
2417 ** length is unsigned - this doesn't make sense
2423 /* Set up the input on the input stack. */
2425 buf1 = (char *) dmalloc (length + 1);
2435 buf1[length] = '\0';
2437 ip = cppReader_pushBuffer (pfile, buf1, length);
2442 ip->has_escapes = TRUE;
2444 /* Scan the input, create the output. */
2445 cppReader_scanBuffer (pfile);
2447 cppReader_nullTerminate (pfile);
2451 adjust_position (char *buf, char *limit, int *linep, int *colp)
2457 (*linep)++, (*colp) = 1;
2463 /* Move line_base forward, updating lineno and colno. */
2466 update_position (cppBuffer *pbuf)
2469 char *new_pos = pbuf->cur;
2470 register struct parse_marker *mark;
2472 llassert (pbuf->buf != NULL);
2473 old_pos = pbuf->buf + pbuf->line_base;
2475 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2477 if (pbuf->buf + mark->position < new_pos)
2478 new_pos = pbuf->buf + mark->position;
2480 pbuf->line_base += new_pos - old_pos;
2482 llassert (old_pos != NULL);
2483 llassert (new_pos != NULL);
2485 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2489 cppBuffer_lineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
2490 /*@null@*/ /*@out@*/ int *colp)
2498 } /*@=branchstate@*/
2502 *linep = pbuf->lineno;
2503 *colp = pbuf->colno;
2505 llassert (pbuf->buf != NULL);
2506 llassert (pbuf->cur != NULL);
2508 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2517 /* Return the cppBuffer that corresponds to a file (not a macro). */
2519 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
2521 cppBuffer *ip = cppReader_getBuffer (pfile);
2524 ip != NULL && ip != cppReader_nullBuffer (pfile);
2525 ip = cppBuffer_prevBuffer (ip))
2527 if (ip->fname != NULL)
2537 count_newlines (char *buf, char *limit)
2539 register long count = 0;
2551 * write out a #line command, for instance, after an #include file.
2552 * If CONDITIONAL is nonzero, we can omit the #line if it would
2553 * appear to be a no-op, and we can output a few newlines instead
2554 * if we want to increase the line number by a small amount.
2555 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2559 output_line_command (cppReader *pfile, bool conditional,
2560 enum file_change_code file_change)
2563 cppBuffer *ip = CPPBUFFER (pfile);
2566 llassert (ip != NULL);
2568 if (ip->fname == NULL)
2571 update_position (ip);
2573 if (CPPOPTIONS (pfile)->no_line_commands
2574 || CPPOPTIONS (pfile)->no_output)
2577 buf = CPPBUFFER (pfile);
2579 llassert (buf != NULL);
2584 llassert (ip->cur != NULL);
2586 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2588 if (CPPOPTIONS (pfile)->no_line_commands)
2592 if (line == pfile->lineno)
2595 /* If the inherited line number is a little too small,
2596 output some newlines instead of a #line command. */
2598 if (line > pfile->lineno && line < pfile->lineno + 8)
2600 cppReader_reserve (pfile, 20);
2601 while (line > pfile->lineno)
2603 cppReader_putCharQ (pfile, '\n');
2611 cppReader_reserve (pfile,
2612 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
2615 #ifdef OUTPUT_LINE_COMMANDS
2616 static char sharp_line[] = "#line ";
2618 static char sharp_line[] = "# ";
2620 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
2623 sprintf (cppReader_getPWritten (pfile), "%d ", line);
2624 cppReader_adjustWritten (pfile, strlen (cppReader_getPWritten (pfile)));
2626 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
2628 if (file_change != same_file) {
2629 cppReader_putCharQ (pfile, ' ');
2630 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
2632 /* Tell cc1 if following text comes from a system header file. */
2633 if (ip->system_header_p != '\0') {
2634 cppReader_putCharQ (pfile, ' ');
2635 cppReader_putCharQ (pfile, '3');
2637 #ifndef NO_IMPLICIT_EXTERN_C
2638 /* Tell cc1plus if following text should be treated as C. */
2639 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
2640 cppReader_putCharQ (pfile, ' ');
2641 cppReader_putCharQ (pfile, '4');
2644 cppReader_putCharQ (pfile, '\n');
2645 pfile->lineno = line;
2650 * Parse a macro argument and append the info on PFILE's token_buffer.
2651 * REST_ARGS means to absorb the rest of the args.
2652 * Return nonzero to indicate a syntax error.
2655 static enum cpp_token
2656 macarg (cppReader *pfile, int rest_args)
2659 enum cpp_token token;
2660 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
2661 bool oldexpand = pfile->no_macro_expand;
2662 CPPOPTIONS (pfile)->put_out_comments = 1;
2664 /* Try to parse as much of the argument as exists at this
2665 input stack level. */
2667 pfile->no_macro_expand = TRUE;
2671 token = cppGetToken (pfile);
2678 /* If we've hit end of file, it's an error (reported by caller).
2679 Ditto if it's the end of cpp_expand_to_buffer text.
2680 If we've hit end of macro, just continue. */
2681 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
2683 /*@switchbreak@*/ break;
2686 /*@switchbreak@*/ break;
2690 /*@switchbreak@*/ break;
2692 /* if we've returned to lowest level and
2693 we aren't absorbing all args */
2694 if (paren == 0 && rest_args == 0)
2696 /*@switchbreak@*/ break;
2698 /* Remove ',' or ')' from argument buffer. */
2699 cppReader_adjustWritten (pfile, -1);
2707 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
2708 pfile->no_macro_expand = oldexpand;
2714 /* Turn newlines to spaces in the string of length LENGTH at START,
2715 except inside of string constants.
2716 The string is copied into itself with its beginning staying fixed. */
2719 change_newlines (char *start, int length)
2723 register char *limit;
2727 limit = start + length;
2730 while (ibp < limit) {
2731 *obp++ = c = *ibp++;
2736 /* Notice and skip strings, so that we don't delete newlines in them. */
2739 while (ibp < limit) {
2740 *obp++ = c = *ibp++;
2742 /*@innerbreak@*/ break;
2743 if (c == '\n' && quotec == '\'')
2744 /*@innerbreak@*/ break;
2747 /*@switchbreak@*/ break;
2754 static /*@observer@*/ struct tm *
2755 timestamp (/*@returned@*/ cppReader *pfile)
2757 if (pfile->timebuf == NULL)
2759 time_t t = time ((time_t *) 0);
2760 pfile->timebuf = localtime (&t);
2763 llassert (pfile->timebuf != NULL);
2765 return pfile->timebuf;
2768 static ob_mstring monthnames[] = {
2769 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2770 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2774 * expand things like __FILE__. Place the expansion into the output
2775 * buffer *without* rescanning.
2779 special_symbol (hashNode hp, cppReader *pfile)
2781 cstring buf = cstring_undefined;
2787 int paren = 0; /* For special `defined' keyword */
2789 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
2791 if (ip == cppReader_nullBuffer (pfile))
2793 cppReader_errorLit (pfile,
2794 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
2795 return; /* the show must go on */
2798 if (ip != NULL && ip->fname != NULL)
2810 if (hp->type == T_BASE_FILE)
2812 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
2814 ip = cppBuffer_prevBuffer (ip);
2818 llassert (ip != NULL);
2819 string = cstring_toCharsSafe (ip->nominal_fname);
2826 cppReader_reserve (pfile, 3 + 4 * strlen (string));
2827 quote_string (pfile, string);
2831 case T_INCLUDE_LEVEL:
2833 ip = cppReader_getBuffer (pfile);
2835 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
2836 ip = cppBuffer_prevBuffer (ip))
2838 if (ip != NULL && ip->fname != NULL)
2844 buf = message ("%d", true_indepth - 1);
2848 buf = message ("\"%s\"", cstring_makeLiteralTemp (CPP_VERSION));
2851 #ifndef NO_BUILTIN_SIZE_TYPE
2853 buf = cstring_makeLiteral (SIZE_TYPE);
2857 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2858 case T_PTRDIFF_TYPE:
2859 buf = cstring_makeLiteral (PTRDIFF_TYPE);
2864 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
2867 case T_USER_LABEL_PREFIX_TYPE:
2868 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
2871 case T_REGISTER_PREFIX_TYPE:
2872 buf = cstring_makeLiteral (REGISTER_PREFIX);
2876 buf = message ("%d", hp->value.ival);
2883 int line = ip->lineno;
2884 int col = ip->colno;
2886 llassert (ip->cur != NULL);
2887 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
2889 buf = message ("%d", (int) line);
2901 char *sbuf = (char *) dmalloc (20);
2902 timebuf = timestamp (pfile);
2903 if (hp->type == T_DATE)
2905 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2906 timebuf->tm_mday, timebuf->tm_year + 1900);
2910 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2914 buf = cstring_fromCharsNew (sbuf);
2919 case T_SPEC_DEFINED:
2920 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
2921 ip = cppReader_getBuffer (pfile);
2923 llassert (ip->cur != NULL);
2924 SKIP_WHITE_SPACE (ip->cur);
2926 if (*ip->cur == '(')
2929 ip->cur++; /* Skip over the paren */
2930 SKIP_WHITE_SPACE (ip->cur);
2933 if (!is_idstart[(int) *ip->cur])
2935 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2938 if ((hp = cppReader_lookup (ip->cur, -1, -1)) != 0)
2941 buf = cstring_makeLiteral (" 1 ");
2944 while (is_idchar[(int) *ip->cur])
2949 SKIP_WHITE_SPACE (ip->cur);
2953 if (*ip->cur != ')')
2961 cppReader_errorLit (pfile,
2962 cstring_makeLiteralTemp ("`defined' without an identifier"));
2966 cpp_setLocation (pfile);
2967 llfatalerror (message ("Pre-processing error: invalid special hash type"));
2970 len = size_fromInt (cstring_length (buf));
2972 cppReader_reserve (pfile, len + 1);
2973 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
2974 cppReader_nullTerminateQ (pfile);
2980 /* Write out a #define command for the special named MACRO_NAME
2981 to PFILE's token_buffer. */
2984 dump_special_to_buffer (cppReader *pfile, char *macro_name)
2986 static char define_directive[] = "#define ";
2987 size_t macro_name_length = strlen (macro_name);
2988 output_line_command (pfile, 0, same_file);
2989 cppReader_reserve (pfile, sizeof(define_directive) + macro_name_length);
2990 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
2991 cppReader_putStrN (pfile, macro_name, macro_name_length);
2992 cppReader_putCharQ (pfile, ' ');
2993 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2994 cppReader_putChar (pfile, '\n');
2997 /* Initialize the built-in macros. */
3000 cppReader_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3001 int len, enum node_type type,
3002 int ivalue, /*@null@*/ /*@only@*/ char *value,
3005 cstring sname = cstring_fromCharsNew (name);
3007 llassert (usymtab_inGlobalScope ());
3010 ** Be careful here: this is done before the ctype table has
3011 ** been initialized.
3014 if (!usymtab_exists (sname))
3016 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3018 if (ctype_equal (ctyp, ctype_string))
3020 qualList ql = qualList_new ();
3021 ql = qualList_add (ql, qual_createObserver ());
3022 uentry_reflectQualifiers (ue, ql);
3026 usymtab_addGlobalEntry (ue);
3033 (void) cppReader_install (name, len, type, ivalue, value, hash);
3034 cstring_free (sname);
3038 cppReader_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3039 int len, enum node_type type,
3041 /*@only@*/ /*@null@*/ char *value, int hash)
3043 cstring sname = cstring_fromChars (name);
3044 /* evs 2000 07 10 - removed a memory leak, detected by lclint */
3046 llassert (usymtab_inGlobalScope ());
3048 if (!usymtab_existsTypeEither (sname))
3050 uentry ue = uentry_makeDatatype (sname, ctyp,
3052 fileloc_createBuiltin ());
3053 llassert (!usymtab_existsEither (sname));
3054 usymtab_addGlobalEntry (ue);
3057 (void) cppReader_install (name, len, type, ivalue, value, hash);
3061 initialize_builtins (cppReader *pfile)
3063 cppReader_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3064 cppReader_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3065 cppReader_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3066 cppReader_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3067 cppReader_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3068 cppReader_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3069 #ifndef NO_BUILTIN_SIZE_TYPE
3070 cppReader_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3072 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3073 cppReader_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3075 cppReader_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3076 cppReader_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3077 cppReader_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3078 cppReader_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3081 ** No, don't define __STDC__
3084 if (!cppReader_isTraditional (pfile))
3086 cppReader_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3093 cppReader_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3097 ** This is supplied using a -D by the compiler driver
3098 ** so that it is present only when truly compiling with GNU C.
3101 /* cppReader_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3103 cppReader_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3105 if (CPPOPTIONS (pfile)->debug_output)
3107 dump_special_to_buffer (pfile, "__BASE_FILE__");
3108 dump_special_to_buffer (pfile, "__VERSION__");
3109 #ifndef NO_BUILTIN_SIZE_TYPE
3110 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3112 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3113 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3115 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3116 dump_special_to_buffer (pfile, "__DATE__");
3117 dump_special_to_buffer (pfile, "__TIME__");
3118 if (!cppReader_isTraditional (pfile))
3119 dump_special_to_buffer (pfile, "__STDC__");
3124 /* Return 1 iff a token ending in C1 followed directly by a token C2
3125 could cause mis-tokenization. */
3128 unsafe_chars (char c1, char c2)
3133 if (c2 == c1 || c2 == '=')
3137 case '0': case '1': case '2': case '3': case '4':
3138 case '5': case '6': case '7': case '8': case '9':
3139 case 'e': case 'E': case 'p': case 'P':
3140 if (c2 == '-' || c2 == '+')
3141 return 1; /* could extend a pre-processing number */
3144 if (c2 == '\'' || c2 == '\"')
3145 return 1; /* Could turn into L"xxx" or L'xxx'. */
3149 case 'a': case 'b': case 'c': case 'd': case 'f':
3150 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3151 case 'm': case 'n': case 'o': case 'q': case 'r':
3152 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3154 case 'A': case 'B': case 'C': case 'D': case 'F':
3155 case 'G': case 'H': case 'I': case 'J': case 'K':
3156 case 'M': case 'N': case 'O': case 'Q': case 'R':
3157 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3159 /* We're in the middle of either a name or a pre-processing number. */
3160 return (is_idchar[(int) c2] || c2 == '.');
3161 case '<': case '>': case '!': case '%': case '#': case ':':
3162 case '^': case '&': case '|': case '*': case '/': case '=':
3163 return (c2 == c1 || c2 == '=');
3168 /* Expand a macro call.
3169 HP points to the symbol that is the macro being called.
3170 Put the result of expansion onto the input stack
3171 so that subsequent input by our caller will use it.
3173 If macro wants arguments, caller has already verified that
3174 an argument list follows; arguments come from the input stack. */
3177 macroexpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3180 DEFINITION *defn = hp->value.defn;
3186 size_t old_written = cppReader_getWritten (pfile);
3190 struct argdata *args = NULL;
3192 pfile->output_escapes++;
3194 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3196 nargs = defn->nargs;
3200 enum cpp_token token = CPP_EOF;
3202 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3204 for (i = 0; i < nargs; i++)
3206 args[i].expanded = 0;
3208 args[i].raw_length = 0;
3209 args[i].expand_length = args[i].stringified_length = -1;
3210 args[i].use_count = 0;
3214 ** Parse all the macro args that are supplied. I counts them.
3215 ** The first NARGS args are stored in ARGS.
3216 ** The rest are discarded. If rest_args is set then we assume
3217 ** macarg absorbed the rest of the args.
3223 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3231 if (i < nargs || (nargs == 0 && i == 0))
3233 /* if we are working on last arg which absorbs rest of args... */
3234 if (i == nargs - 1 && defn->rest_args)
3239 args[i].raw = size_toLong (cppReader_getWritten (pfile));
3240 token = macarg (pfile, rest_args);
3241 args[i].raw_length = cppReader_getWritten (pfile) - args[i].raw;
3242 args[i].newlines = FALSE; /* FIXME */
3246 token = macarg (pfile, 0);
3249 if (token == CPP_EOF || token == CPP_POP)
3251 cppReader_errorWithLine (pfile, start_line, start_column,
3252 cstring_fromCharsNew ("unterminated macro call"));
3257 } while (token == CPP_COMMA);
3259 /* If we got one arg but it was just whitespace, call that 0 args. */
3267 bp = ARG_BASE + args[0].raw;
3268 lim = bp + args[0].raw_length;
3270 /* cpp.texi says for foo ( ) we provide one argument.
3271 However, if foo wants just 0 arguments, treat this as 0. */
3275 while (bp != lim && is_space[(int) *bp])
3285 /* Don't output an error message if we have already output one for
3286 a parse error above. */
3289 if (nargs == 0 && i > 0)
3291 cppReader_error (pfile,
3292 message ("arguments given to macro `%s'", hp->name));
3296 /* traditional C allows foo() if foo wants one argument. */
3297 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
3301 /* the rest args token is allowed to absorb 0 tokens */
3302 else if (i == nargs - 1 && defn->rest_args)
3305 cppReader_error (pfile,
3306 message ("macro `%s' used without args", hp->name));
3308 cppReader_error (pfile,
3309 message ("macro `%s' used with just one arg", hp->name));
3312 cppReader_error (pfile,
3313 message ("macro `%s' used with only %d args",
3319 cppReader_error (pfile,
3320 message ("macro `%s' used with too many (%d) args", hp->name, i));
3328 /* If macro wants zero args, we parsed the arglist for checking only.
3329 Read directly from the macro definition. */
3333 xbuf = defn->expansion;
3334 xbuf_len = defn->length;
3338 char *exp = defn->expansion;
3339 int offset; /* offset in expansion,
3340 copied a piece at a time */
3341 size_t totlen; /* total amount of exp buffer filled so far */
3343 register struct reflist *ap, *last_ap;
3345 assertSet (args); /* args is defined since the nargs > 0 path was taken */
3347 /* Macro really takes args. Compute the expansion of this call. */
3349 /* Compute length in characters of the macro's expansion.
3350 Also count number of times each arg is used. */
3351 xbuf_len = defn->length;
3353 llassert (args != NULL);
3355 for (ap = defn->pattern; ap != NULL; ap = ap->next)
3359 struct argdata *arg = &args[ap->argno];
3361 /* Stringify it it hasn't already been */
3364 if (arg->stringified_length < 0)
3366 int arglen = arg->raw_length;
3367 bool escaped = FALSE;
3368 char in_string = '\0';
3371 /* Initially need_space is -1. Otherwise, 1 means the
3372 previous character was a space, but we suppressed it;
3373 0 means the previous character was a non-space. */
3374 int need_space = -1;
3377 arg->stringified = cppReader_getWritten (pfile);
3378 if (!cppReader_isTraditional (pfile))
3379 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
3380 for (; i < arglen; i++)
3382 c = (ARG_BASE + arg->raw)[i];
3384 if (in_string == '\0')
3386 /* Internal sequences of whitespace are replaced by
3387 one space except within an string or char token.*/
3388 if (is_space[(int) c])
3390 if (cppReader_getWritten (pfile) > arg->stringified
3391 && (cppReader_getPWritten (pfile))[-1] == '@')
3393 /* "@ " escape markers are removed */
3394 cppReader_adjustWritten (pfile, -1);
3395 /*@innercontinue@*/ continue;
3397 if (need_space == 0)
3399 /*@innercontinue@*/ continue;
3401 else if (need_space > 0)
3402 cppReader_putChar (pfile, ' ');
3418 if (in_string != '\0')
3423 else if (c == '\"' || c == '\'')
3433 /* Escape these chars */
3434 if (c == '\"' || (in_string != '\0' && c == '\\'))
3435 cppReader_putChar (pfile, '\\');
3437 cppReader_putChar (pfile, c);
3440 cppReader_reserve (pfile, 4);
3441 sprintf (cppReader_getPWritten (pfile), "\\%03o",
3443 cppReader_adjustWritten (pfile, 4);
3446 if (!cppReader_isTraditional (pfile))
3447 cppReader_putChar (pfile, '\"'); /* insert ending quote */
3448 arg->stringified_length
3449 = size_toInt (cppReader_getWritten (pfile) - arg->stringified);
3452 xbuf_len += args[ap->argno].stringified_length;
3454 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3456 /* Add 4 for two newline-space markers to prevent
3457 token concatenation. */
3458 assertSet (args); /*@i534 shouldn't need this */
3459 xbuf_len += args[ap->argno].raw_length + 4;
3463 /* We have an ordinary (expanded) occurrence of the arg.
3464 So compute its expansion, if we have not already. */
3466 assertSet (args); /*@i534 shouldn't need this */
3468 if (args[ap->argno].expand_length < 0)
3470 args[ap->argno].expanded = cppReader_getWritten (pfile);
3471 cpp_expand_to_buffer (pfile,
3472 ARG_BASE + args[ap->argno].raw,
3473 size_fromInt (args[ap->argno].raw_length));
3475 args[ap->argno].expand_length
3476 = size_toInt (cppReader_getWritten (pfile) - args[ap->argno].expanded);
3479 /* Add 4 for two newline-space markers to prevent
3480 token concatenation. */
3481 xbuf_len += args[ap->argno].expand_length + 4;
3483 if (args[ap->argno].use_count < 10)
3484 args[ap->argno].use_count++;
3487 xbuf = (char *) dmalloc (xbuf_len + 1);
3491 ** Generate in XBUF the complete expansion
3492 ** with arguments substituted in.
3493 ** TOTLEN is the total size generated so far.
3494 ** OFFSET is the index in the definition
3495 ** of where we are copying from.
3501 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
3502 last_ap = ap, ap = ap->next)
3504 register struct argdata *arg = &args[ap->argno];
3505 size_t count_before = totlen;
3507 /* Add chars to XBUF. */
3508 for (i = 0; i < ap->nchars; i++, offset++)
3510 xbuf[totlen++] = exp[offset];
3513 /* If followed by an empty rest arg with concatenation,
3514 delete the last run of nonwhite chars. */
3515 if (rest_zero && totlen > count_before
3516 && ((ap->rest_args && ap->raw_before)
3517 || (last_ap != NULL && last_ap->rest_args
3518 && last_ap->raw_after)))
3520 /* Delete final whitespace. */
3521 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
3526 /* Delete the nonwhites before them. */
3527 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
3533 if (ap->stringify != 0)
3536 memcpy (xbuf + totlen,
3537 ARG_BASE + arg->stringified,
3538 size_fromInt (arg->stringified_length));
3539 totlen += arg->stringified_length;
3541 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3548 p1 = ARG_BASE + arg->raw;
3549 l1 = p1 + arg->raw_length;
3553 while (p1 != l1 && is_space[(int) *p1])
3558 while (p1 != l1 && is_idchar[(int) *p1])
3560 xbuf[totlen++] = *p1++;
3563 /* Delete any no-reexpansion marker that follows
3564 an identifier at the beginning of the argument
3565 if the argument is concatenated with what precedes it. */
3566 if (p1[0] == '@' && p1[1] == '-')
3571 /* Arg is concatenated after: delete trailing whitespace,
3572 whitespace markers, and no-reexpansion markers. */
3575 if (is_space[(int) l1[-1]]) l1--;
3576 else if (l1[-1] == '-')
3579 /* If a `-' is preceded by an odd number of newlines then it
3580 and the last newline are a no-reexpansion marker. */
3581 while (p2 != p1 && p2[-1] == '\n')
3586 if (((l1 - 1 - p2) & 1) != 0)
3592 /*@innerbreak@*/ break;
3597 /*@innerbreak@*/ break;
3602 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
3610 expanded = ARG_BASE + arg->expanded;
3612 if (!ap->raw_before && totlen > 0
3613 && (arg->expand_length != 0)
3614 && !cppReader_isTraditional(pfile)
3615 && unsafe_chars (xbuf[totlen-1], expanded[0]))
3617 xbuf[totlen++] = '@';
3618 xbuf[totlen++] = ' ';
3621 memcpy (xbuf + totlen, expanded,
3622 size_fromInt (arg->expand_length));
3623 totlen += arg->expand_length;
3625 if (!ap->raw_after && totlen > 0
3626 && offset < size_toInt (defn->length)
3627 && !cppReader_isTraditional(pfile)
3628 && unsafe_chars (xbuf[totlen-1], exp[offset]))
3630 xbuf[totlen++] = '@';
3631 xbuf[totlen++] = ' ';
3634 /* If a macro argument with newlines is used multiple times,
3635 then only expand the newlines once. This avoids creating
3636 output lines which don't correspond to any input line,
3637 which confuses gdb and gcov. */
3638 if (arg->use_count > 1 && arg->newlines > 0)
3640 /* Don't bother doing change_newlines for subsequent
3644 = change_newlines (expanded, arg->expand_length);
3648 if (totlen > xbuf_len)
3652 /* if there is anything left of the definition
3653 after handling the arg list, copy that in too. */
3655 for (i = offset; i < size_toInt (defn->length); i++)
3657 /* if we've reached the end of the macro */
3660 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3661 && last_ap->raw_after))
3662 xbuf[totlen++] = exp[i];
3665 xbuf[totlen] = '\0';
3669 pfile->output_escapes--;
3671 /* Now put the expansion on the input stack
3672 so our caller will commence reading from it. */
3673 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3674 cppReader_getBuffer (pfile)->has_escapes = 1;
3676 /* Pop the space we've used in the token_buffer for argument expansion. */
3677 cppReader_setWritten (pfile, old_written);
3679 /* Recursive macro use sometimes works traditionally.
3680 #define foo(x,y) bar (x (y,0), y)
3683 if (!cppReader_isTraditional (pfile))
3684 hp->type = T_DISABLED;
3690 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
3691 /*@dependent@*/ hashNode hp)
3693 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
3700 mbuf->cleanup = cppReader_macroCleanup;
3702 llassert (mbuf->hnode == NULL);
3705 /* The first chars of the expansion should be a "@ " added by
3706 collect_expansion. This is to prevent accidental token-pasting
3707 between the text preceding the macro invocation, and the macro
3710 We would like to avoid adding unneeded spaces (for the sake of
3711 tools that use cpp, such as imake). In some common cases we can
3712 tell that it is safe to omit the space.
3714 The character before the macro invocation cannot have been an
3715 idchar (or else it would have been pasted with the idchars of
3716 the macro name). Therefore, if the first non-space character
3717 of the expansion is an idchar, we do not need the extra space
3718 to prevent token pasting.
3720 Also, we don't need the extra space if the first char is '(',
3721 or some other (less common) characters. */
3723 if (xbuf[0] == '@' && xbuf[1] == ' '
3724 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3725 || xbuf[2] == '\"'))
3727 llassert (mbuf->cur != NULL);
3733 /* Like cppGetToken, except that it does not read past end-of-line.
3734 Also, horizontal space is skipped, and macros are popped. */
3736 static enum cpp_token
3737 get_directive_token (cppReader *pfile)
3741 size_t old_written = cppReader_getWritten (pfile);
3742 enum cpp_token token;
3743 cppSkipHspace (pfile);
3744 if (cppReader_peekC (pfile) == '\n')
3749 token = cppGetToken (pfile);
3754 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
3759 cppReader_setWritten (pfile, old_written);
3760 /*@switchbreak@*/ break;
3768 /* Handle #include and #import.
3769 This function expects to see "fname" or <fname> on the input.
3771 The input is normally in part of the output_buffer following
3772 cppReader_getWritten, and will get overwritten by output_line_command.
3773 I.e. in input file specification has been popped by cppReader_handleDirective.
3777 static void replace_unixdir_with_windir(char *filename)
3781 while(filename[i] != '\0')
3783 if(filename[i] == '/')
3791 do_include (cppReader *pfile, struct directive *keyword,
3792 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
3794 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3796 char *fbeg, *fend; /* Beginning and end of fname */
3797 enum cpp_token token;
3799 /* Chain of dirs to search */
3800 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
3801 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3802 struct file_name_list *searchptr = NULL;
3803 size_t old_written = cppReader_getWritten (pfile);
3807 int f; /* file number */
3808 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3809 f= -1; /* JF we iz paranoid! */
3811 pfile->parsing_include_directive++;
3812 token = get_directive_token (pfile);
3813 pfile->parsing_include_directive--;
3815 if (token == CPP_STRING)
3817 /* FIXME - check no trailing garbage */
3818 fbeg = pfile->token_buffer + old_written + 1;
3819 fend = cppReader_getPWritten (pfile) - 1;
3820 if (fbeg[-1] == '<')
3823 /* If -I-, start with the first -I dir after the -I-. */
3824 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
3825 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3827 /* If -I- was specified, don't search current dir, only spec'd ones. */
3828 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
3830 cppBuffer *fp = CPPBUFFER (pfile);
3831 /* We have "filename". Figure out directory this source
3832 file is coming from and put it on the front of the list. */
3834 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3839 llassert (fp != NULL);
3843 if (cstring_isDefined (fp->nominal_fname))
3845 nam = cstring_toCharsSafe (fp->nominal_fname);
3847 /* Found a named file. Figure out dir of the file,
3848 and put it in front of the search list. */
3849 dsp[0].next = search_start;
3853 ep = strrchr (nam, CONNECTCHAR);
3855 ep = strrchr (nam, ']');
3856 if (ep == NULL) ep = strrchr (nam, '>');
3857 if (ep == NULL) ep = strrchr (nam, ':');
3858 if (ep != NULL) ep++;
3868 /*@-onlytrans@*/ /* This looks like a memory leak... */
3869 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
3873 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3874 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3878 dsp[0].fname = cstring_undefined; /* Current directory */
3881 dsp[0].got_name_map = 0;
3892 else if (token == CPP_NAME)
3895 * Support '#include xyz' like VAX-C to allow for easy use of all the
3896 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3897 * code from case '<' is repeated here) and generates a warning.
3899 cppReader_warning (pfile,
3900 "VAX-C-style include specification found, use '#include <filename.h>' !");
3902 /* If -I-, start with the first -I dir after the -I-. */
3903 if (CPPOPTIONS (pfile)->first_bracket_include)
3904 search_start = CPPOPTIONS (pfile)->first_bracket_include;
3905 fbeg = pfile->token_buffer + old_written;
3906 fend = cppReader_getPWritten (pfile);
3911 cppReader_error (pfile,
3912 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
3915 cppReader_setWritten (pfile, old_written);
3916 cppReader_skipRestOfLine (pfile);
3922 token = get_directive_token (pfile);
3923 if (token != CPP_VSPACE)
3925 cppReader_errorLit (pfile,
3926 cstring_makeLiteralTemp ("Junk at end of #include"));
3928 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3930 token = get_directive_token (pfile);
3935 ** For #include_next, skip in the search path
3936 ** past the dir in which the containing file was found.
3941 cppBuffer *fp = CPPBUFFER (pfile);
3943 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
3945 llassert (fp != NULL);
3947 if (fp->fname != NULL)
3949 /* fp->dir is null if the containing file was specified with
3950 an absolute file name. In that case, don't skip anything. */
3951 if (fp->dir == SELF_DIR_DUMMY)
3953 search_start = CPPOPTIONS (pfile)->include;
3955 else if (fp->dir != NULL)
3957 search_start = fp->dir->next;
3969 cppReader_setWritten (pfile, old_written);
3973 DPRINTF (("fbeg: %s", fbeg));
3977 cppReader_error (pfile,
3978 message ("Empty file name in #%s", keyword->name));
3983 ** Allocate this permanently, because it gets stored in the definitions
3987 fname = cstring_undefined;
3989 /* + 2 above for slash and terminating null. */
3990 /* + 2 added for '.h' on VMS (to support '#include filename') */
3992 /* If specified file name is absolute, just open it. */
3994 if (osd_isConnectChar (*fbeg)
3995 # if defined (WIN32) || defined (OS2)
3996 || (*(fbeg + 1) == ':')
4000 fname = cstring_copyLength (fbeg, flen);
4002 if (redundant_include_p (pfile, fname))
4004 cstring_free (fname);
4008 f = open_include_file (pfile, fname, NULL);
4010 if (f == IMPORT_FOUND)
4012 return 0; /* Already included this file */
4017 /* Search directory path, trying to open the file.
4018 Copy each filename tried into FNAME. */
4020 for (searchptr = search_start; searchptr != NULL;
4021 searchptr = searchptr->next)
4023 if (!cstring_isEmpty (searchptr->fname))
4025 /* The empty string in a search path is ignored.
4026 This makes it possible to turn off entirely
4027 a standard piece of the list. */
4028 if (cstring_isEmpty (searchptr->fname))
4031 fname = cstring_copy (searchptr->fname);
4032 fname = cstring_appendChar (fname, CONNECTCHAR);
4033 DPRINTF (("Here: %s", fname));
4040 fname = cstring_concatLength (fname, fbeg, flen);
4042 DPRINTF (("fname: %s", fname));
4044 /* Win32 directory fix from Kay Buschner. */
4046 /* Fix all unixdir slashes to win dir slashes */
4047 if (searchptr->fname && (searchptr->fname[0] != 0))
4049 replace_unixdir_with_windir(fname);
4054 /* Change this 1/2 Unix 1/2 VMS file specification into a
4055 full VMS file specification */
4056 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4057 /* Fix up the filename */
4058 hack_vms_include_specification (fname);
4060 /* This is a normal VMS filespec, so use it unchanged. */
4061 strncpy (fname, fbeg, flen);
4063 /* if it's '#include filename', add the missing .h */
4064 if (strchr (fname,'.') == NULL) {
4065 strcat (fname, ".h");
4069 /* ??? There are currently 3 separate mechanisms for avoiding processing
4070 of redundant include files: #import, #pragma once, and
4071 redundant_include_p. It would be nice if they were unified. */
4073 if (redundant_include_p (pfile, fname))
4075 cstring_free (fname);
4079 DPRINTF (("Trying: %s", fname));
4081 f = open_include_file (pfile, fname, searchptr);
4083 if (f == IMPORT_FOUND)
4085 return 0; /* Already included this file */
4088 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4090 cppReader_warning (pfile,
4091 message ("Header file %s exists, but is not readable", fname));
4104 /* A file that was not found. */
4105 fname = cstring_copyLength (fbeg, flen);
4107 if (search_start != NULL)
4109 cppReader_error (pfile,
4110 message ("Cannot find include file %s on search path: %x",
4112 searchPath_unparse (search_start)));
4116 cppReader_error (pfile,
4117 message ("No include path in which to find %s", fname));
4122 ** Check to see if this include file is a once-only include file.
4126 struct file_name_list *ptr;
4128 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4130 if (cstring_equal (ptr->fname, fname))
4132 /* This file was included before. */
4139 /* This is the first time for this file. */
4140 /* Add it to list of files included. */
4142 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4143 ptr->control_macro = NULL;
4144 ptr->c_system_include_path = NULL;
4145 ptr->next = pfile->all_include_files;
4147 ptr->got_name_map = NULL;
4149 DPRINTF (("Including file: %s", fname));
4150 pfile->all_include_files = ptr;
4151 assertSet (pfile->all_include_files);
4154 if (angle_brackets != 0)
4156 pfile->system_include_depth++;
4159 /* Actually process the file */
4160 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4162 cstring_free (fname);
4166 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4167 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4169 output_line_command (pfile, 0, enter_file);
4170 pfile->only_seen_white = 2;
4175 pfile->system_include_depth--;
4178 } /*@=branchstate@*/
4183 /* Return nonzero if there is no need to include file NAME
4184 because it has already been included and it contains a conditional
4185 to make a repeated include do nothing. */
4188 redundant_include_p (cppReader *pfile, cstring name)
4190 struct file_name_list *l = pfile->all_include_files;
4192 for (; l != NULL; l = l->next)
4194 if (cstring_equal (name, l->fname)
4195 && (l->control_macro != NULL)
4196 && (cppReader_lookup (l->control_macro, -1, -1) != NULL))
4205 /* Return nonzero if the given FILENAME is an absolute pathname which
4206 designates a file within one of the known "system" include file
4207 directories. We assume here that if the given FILENAME looks like
4208 it is the name of a file which resides either directly in a "system"
4209 include file directory, or within any subdirectory thereof, then the
4210 given file must be a "system" include file. This function tells us
4211 if we should suppress pedantic errors/warnings for the given FILENAME.
4213 The value is 2 if the file is a C-language system header file
4214 for which C++ should (on most systems) assume `extern "C"'. */
4217 is_system_include (cppReader *pfile, cstring filename)
4219 struct file_name_list *searchptr;
4221 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4223 searchptr = searchptr->next)
4225 if (!cstring_isEmpty (searchptr->fname)) {
4226 cstring sys_dir = searchptr->fname;
4227 int length = cstring_length (sys_dir);
4229 if (cstring_equalLen (sys_dir, filename, length)
4230 && osd_isConnectChar (cstring_getChar (filename, length)))
4232 if (searchptr->c_system_include_path)
4243 /* Convert a character string literal into a nul-terminated string.
4244 The input string is [IN ... LIMIT).
4245 The result is placed in RESULT. RESULT can be the same as IN.
4246 The value returned in the end of the string written to RESULT,
4247 or NULL on error. */
4249 static /*@null@*/ char *
4250 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4251 char *in, char *limit, int handle_escapes)
4271 /*@switchbreak@*/ break;
4275 char *bpc = (char *) in;
4276 int i = (char) cppReader_parseEscape (pfile, &bpc);
4279 *result++ = (char) c;
4280 /*@switchbreak@*/ break;
4294 * interpret #line command. Remembers previously seen fnames
4295 * in its very own hash table.
4298 /*@constant int FNAME_HASHSIZE@*/
4299 #define FNAME_HASHSIZE 37
4302 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
4304 cppBuffer *ip = cppReader_getBuffer (pfile);
4306 size_t old_written = cppReader_getWritten (pfile);
4307 enum file_change_code file_change = same_file;
4308 enum cpp_token token;
4310 token = get_directive_token (pfile);
4312 if (token != CPP_NUMBER
4313 || !isdigit(pfile->token_buffer[old_written]))
4315 cppReader_errorLit (pfile,
4316 cstring_makeLiteralTemp ("invalid format `#line' command"));
4318 goto bad_line_directive;
4321 /* The Newline at the end of this line remains to be processed.
4322 To put the next line at the specified line number,
4323 we must store a line number now that is one less. */
4324 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
4325 cppReader_setWritten (pfile, old_written);
4327 /* NEW_LINENO is one less than the actual line number here. */
4328 if (cppReader_isPedantic (pfile) && new_lineno < 0)
4329 cppReader_pedwarnLit (pfile,
4330 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
4332 token = get_directive_token (pfile);
4334 if (token == CPP_STRING) {
4335 char *fname = pfile->token_buffer + old_written;
4337 static hashNode fname_table[FNAME_HASHSIZE];
4339 hashNode *hash_bucket;
4344 /* Turn the file name, which is a character string literal,
4345 into a null-terminated string. Do this in place. */
4346 end_name = convert_string (pfile, fname, fname, cppReader_getPWritten (pfile), 1);
4347 if (end_name == NULL)
4349 cppReader_errorLit (pfile,
4350 cstring_makeLiteralTemp ("invalid format `#line' command"));
4351 goto bad_line_directive;
4354 fname_length = end_name - fname;
4355 num_start = cppReader_getWritten (pfile);
4357 token = get_directive_token (pfile);
4358 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
4359 p = pfile->token_buffer + num_start;
4360 if (cppReader_isPedantic (pfile))
4361 cppReader_pedwarnLit (pfile,
4362 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
4364 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
4366 cppReader_errorLit (pfile,
4367 cstring_makeLiteralTemp ("invalid format `#line' command"));
4368 goto bad_line_directive;
4371 file_change = enter_file;
4373 file_change = leave_file;
4375 ip->system_header_p = 1;
4376 else /* if (*p == 4) */
4377 ip->system_header_p = 2;
4379 cppReader_setWritten (pfile, num_start);
4380 token = get_directive_token (pfile);
4381 p = pfile->token_buffer + num_start;
4382 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
4383 ip->system_header_p = *p == 3 ? 1 : 2;
4384 token = get_directive_token (pfile);
4386 if (token != CPP_VSPACE) {
4387 cppReader_errorLit (pfile,
4388 cstring_makeLiteralTemp ("invalid format `#line' command"));
4390 goto bad_line_directive;
4395 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
4396 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
4398 if (hp->length == fname_length &&
4399 strncmp (hp->value.cpval, fname, size_fromInt (fname_length)) == 0) {
4400 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4406 /* Didn't find it; cons up a new one. */
4407 hp = (hashNode) dmalloc (sizeof (*hp));
4410 hp->bucket_hdr = NULL;
4412 hp->name = cstring_undefined;
4413 hp->next = *hash_bucket;
4417 hp->length = fname_length;
4418 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
4419 memcpy (hp->value.cpval, fname, size_fromInt (fname_length));
4420 hp->value.cpval[fname_length] = '\0';
4421 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4424 else if (token != CPP_VSPACE && token != CPP_EOF)
4426 cppReader_errorLit (pfile,
4427 cstring_makeLiteralTemp ("invalid format `#line' command"));
4428 goto bad_line_directive;
4435 ip->lineno = new_lineno;
4437 cppReader_skipRestOfLine (pfile);
4438 cppReader_setWritten (pfile, old_written);
4439 output_line_command (pfile, 0, file_change);
4444 * remove the definition of a symbol from the symbol table.
4445 * according to un*x /lib/cpp, it is not an error to undef
4446 * something that has no definitions, so it isn't one here either.
4450 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
4455 char *orig_buf = buf;
4457 SKIP_WHITE_SPACE (buf);
4459 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
4461 while ((hp = cppReader_lookup (buf, sym_length, -1)) != NULL)
4463 /* If we are generating additional info for debugging (with -g) we
4464 need to pass through all effective #undef commands. */
4465 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
4467 pass_thru_directive (orig_buf, limit, pfile, keyword);
4470 if (hp->type != T_MACRO)
4472 cppReader_warning (pfile,
4473 message ("Undefining preprocessor builtin: %s",
4477 cppReader_deleteMacro (hp);
4480 if (cppReader_isPedantic (pfile)) {
4482 SKIP_WHITE_SPACE (buf);
4485 cppReader_pedwarnLit (pfile,
4486 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
4495 * Report an error detected by the program we are processing.
4496 * Use the text of the line in the error message.
4497 * (We use error because it prints the filename & line#.)
4501 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4502 char *buf, char *limit)
4504 int length = limit - buf;
4505 cstring copy = cstring_copyLength (buf, length);
4506 cstring adv = cstring_advanceWhiteSpace (copy);
4508 cppReader_error (pfile, message ("#error %s", adv));
4509 cstring_free (copy);
4514 * Report a warning detected by the program we are processing.
4515 * Use the text of the line in the warning message, then continue.
4516 * (We use error because it prints the filename & line#.)
4520 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4521 char *buf, char *limit)
4523 int length = limit - buf;
4524 cstring copy = cstring_copyLength (buf, length);
4525 cstring adv = cstring_advanceWhiteSpace (copy);
4526 cppReader_warning (pfile, message ("#warning %s", adv));
4527 cstring_free (copy);
4532 /* #ident has already been copied to the output file, so just ignore it. */
4535 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4536 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4538 /* Allow #ident in system headers, since that's not user's fault. */
4539 if (cppReader_isPedantic (pfile) && !cppReader_getBuffer (pfile)->system_header_p)
4540 cppReader_pedwarnLit (pfile,
4541 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
4543 /* Leave rest of line to be read by later calls to cppGetToken. */
4548 /* #pragma and its argument line have already been copied to the output file.
4549 Just check for some recognized pragmas that need validation here. */
4552 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4553 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
4555 while (*buf == ' ' || *buf == '\t')
4560 if (!strncmp (buf, "implementation", 14)) {
4561 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4562 been included yet. */
4563 struct file_name_list *ptr;
4564 char *p = buf + 14, *fname, *inc_fname;
4566 SKIP_WHITE_SPACE (p);
4567 if (*p == '\n' || *p != '\"')
4571 p = (char *) strchr (fname, '\"');
4572 fname_len = p != NULL ? p - fname : mstring_length (fname);
4574 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4576 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
4577 inc_fname = (inc_fname != NULL)
4578 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
4580 if ((inc_fname != NULL)
4581 && (strncmp (inc_fname, fname, size_fromInt (fname_len)) == 0))
4583 cpp_setLocation (pfile);
4585 ppllerror (message ("`#pragma implementation' for `%s' appears "
4586 "after file is included",
4587 cstring_fromChars (fname)));
4596 * handle #if command by
4597 * 1) inserting special `defined' keyword into the hash table
4598 * that gets turned into 0 or 1 by special_symbol (thus,
4599 * if the luser has a symbol called `defined' already, it won't
4600 * work inside the #if command)
4601 * 2) rescan the input into a temporary output buffer
4602 * 3) pass the output buffer to the yacc parser and collect a value
4603 * 4) clean up the mess left from steps 1 and 2.
4604 * 5) call conditional_skip to skip til the next #endif (etc.),
4605 * or not, depending on the value from step 3.
4609 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4610 char *buf, char *limit)
4612 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4613 conditional_skip (pfile, value == 0, T_IF, NULL);
4618 * handle a #elif directive by not changing if_stack either.
4619 * see the comment above do_else.
4622 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
4623 char *buf, char *limit)
4625 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4627 cppReader_errorLit (pfile,
4628 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
4633 llassert (pfile->if_stack != NULL);
4635 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
4637 cppReader_errorLit (pfile,
4638 cstring_makeLiteralTemp ("`#elif' after `#else'"));
4640 if (pfile->if_stack->fname != NULL
4641 && cppReader_getBuffer (pfile)->fname != NULL
4642 && !cstring_equal (pfile->if_stack->fname,
4643 cppReader_getBuffer (pfile)->nominal_fname))
4644 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
4645 fprintf (stderr, ")\n");
4647 pfile->if_stack->type = T_ELIF;
4650 if (pfile->if_stack->if_succeeded)
4652 skip_if_group (pfile, 0);
4656 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4658 skip_if_group (pfile, 0);
4661 ++pfile->if_stack->if_succeeded; /* continue processing input */
4662 output_line_command (pfile, 1, same_file);
4670 * evaluate a #if expression in BUF, of length LENGTH,
4671 * then parse the result as a C expression and return the value as an int.
4674 static HOST_WIDE_INT
4675 eval_if_expression (cppReader *pfile,
4676 /*@unused@*/ char *buf,
4677 /*@unused@*/ int length)
4679 hashNode save_defined;
4680 HOST_WIDE_INT value;
4681 size_t old_written = cppReader_getWritten (pfile);
4683 save_defined = cppReader_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4684 pfile->pcp_inside_if = 1;
4686 value = cppReader_parseExpression (pfile);
4687 pfile->pcp_inside_if = 0;
4689 /* Clean up special symbol */
4690 cppReader_deleteMacro (save_defined);
4692 cppReader_setWritten (pfile, old_written); /* Pop */
4698 * routine to handle ifdef/ifndef. Try to look up the symbol,
4699 * then do or don't skip to the #endif/#else/#elif depending
4700 * on what directive is actually being processed.
4704 do_xifdef (cppReader *pfile, struct directive *keyword,
4705 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4708 cppBuffer *ip = cppReader_getBuffer (pfile);
4711 enum cpp_token token;
4712 int start_of_file = 0;
4713 char *control_macro = 0;
4714 size_t old_written = cppReader_getWritten (pfile);
4716 DPRINTF (("do xifdef: %d",
4717 keyword->type == T_IFNDEF));
4719 /* Detect a #ifndef at start of file (not counting comments). */
4720 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
4722 start_of_file = pfile->only_seen_white == 2;
4725 pfile->no_macro_expand++;
4726 token = get_directive_token (pfile);
4727 pfile->no_macro_expand--;
4729 ident = pfile->token_buffer + old_written;
4730 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4731 cppReader_setWritten (pfile, old_written); /* Pop */
4733 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4735 skip = (keyword->type == T_IFDEF);
4736 if (! cppReader_isTraditional (pfile))
4738 cppReader_pedwarn (pfile,
4739 message ("`#%s' with no argument", keyword->name));
4742 else if (token == CPP_NAME)
4744 hashNode hp = cppReader_lookup (ident, ident_length, -1);
4745 skip = (keyword->type == T_IFDEF)
4746 ? (hp == NULL) : (hp != NULL);
4748 DPRINTF (("hp null: %d / %d / %d",
4750 (keyword->type == T_IFNDEF),
4753 if (start_of_file && !skip)
4755 DPRINTF (("Not skipping!"));
4756 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
4757 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
4762 skip = (keyword->type == T_IFDEF);
4763 if (! cppReader_isTraditional (pfile))
4765 cppReader_error (pfile,
4766 message ("`#%s' with invalid argument", keyword->name));
4770 if (!cppReader_isTraditional (pfile))
4773 cppSkipHspace (pfile);
4774 c = cppReader_peekC (pfile);
4775 if (c != EOF && c != '\n')
4777 cppReader_pedwarn (pfile,
4778 message ("garbage at end of `#%s' argument", keyword->name));
4782 cppReader_skipRestOfLine (pfile);
4784 DPRINTF (("Conditional skip: %d", skip));
4785 conditional_skip (pfile, skip, T_IF, control_macro);
4789 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4790 If this is a #ifndef starting at the beginning of a file,
4791 CONTROL_MACRO is the macro name tested by the #ifndef.
4792 Otherwise, CONTROL_MACRO is 0. */
4795 conditional_skip (cppReader *pfile, int skip,
4796 enum node_type type,
4797 /*@dependent@*/ char *control_macro)
4799 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4801 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4802 temp->next = pfile->if_stack;
4803 temp->control_macro = control_macro;
4805 temp->if_succeeded = 0;
4807 pfile->if_stack = temp;
4808 pfile->if_stack->type = type;
4812 skip_if_group (pfile, 0);
4817 ++pfile->if_stack->if_succeeded;
4818 output_line_command (pfile, 1, same_file);
4823 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4824 * leaves input ptr at the sharp sign found.
4825 * If ANY is nonzero, return at next directive of any sort.
4829 skip_if_group (cppReader *pfile, int any)
4832 struct directive *kt;
4833 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
4834 register int ident_length;
4836 struct parse_marker line_start_mark;
4838 parseSetMark (&line_start_mark, pfile);
4840 if (CPPOPTIONS (pfile)->output_conditionals) {
4841 static char failed[] = "#failed\n";
4842 cppReader_puts (pfile, failed, sizeof(failed)-1);
4844 output_line_command (pfile, 1, same_file);
4848 if (CPPOPTIONS (pfile)->output_conditionals)
4850 cppBuffer *pbuf = cppReader_getBuffer (pfile);
4853 llassert (pbuf->buf != NULL);
4855 start_line = pbuf->buf + line_start_mark.position;
4856 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
4859 parseMoveMark (&line_start_mark, pfile);
4861 if (!cppReader_isTraditional (pfile))
4863 cppSkipHspace (pfile);
4866 c = cppReader_getC (pfile);
4869 size_t old_written = cppReader_getWritten (pfile);
4870 cppSkipHspace (pfile);
4872 parse_name (pfile, cppReader_getC (pfile));
4873 ident_length = size_toInt (cppReader_getWritten (pfile) - old_written);
4874 ident = pfile->token_buffer + old_written;
4875 pfile->limit = ident;
4877 for (kt = directive_table; kt->length >= 0; kt++)
4879 cppIfStackFrame *temp;
4880 if (ident_length == kt->length
4881 && cstring_equalPrefix (kt->name, ident))
4883 /* If we are asked to return on next directive, do so now. */
4894 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
4895 temp->next = pfile->if_stack;
4896 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
4897 temp->type = kt->type;
4899 temp->if_succeeded = 0;
4900 temp->control_macro = NULL;
4902 pfile->if_stack = temp;
4903 /*@switchbreak@*/ break;
4906 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
4907 validate_else (pfile,
4908 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
4911 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
4913 cppReader_error (pfile,
4914 message ("Preprocessor command #%s is not within a conditional", kt->name));
4915 /*@switchbreak@*/ break;
4917 else if (pfile->if_stack == save_if_stack)
4919 goto done; /* found what we came for */
4926 if (kt->type != T_ENDIF)
4928 llassert (pfile->if_stack != NULL);
4930 if (pfile->if_stack->type == T_ELSE)
4932 cppReader_errorLit (pfile,
4933 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
4936 pfile->if_stack->type = kt->type;
4937 /*@switchbreak@*/ break;
4940 temp = pfile->if_stack;
4941 llassert (temp != NULL);
4942 pfile->if_stack = temp->next;
4944 /*@switchbreak@*/ break;
4952 /* Don't let erroneous code go by. */
4954 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
4955 && cppReader_isPedantic (pfile))
4957 cppReader_pedwarnLit (pfile,
4958 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
4962 c = cppReader_getC (pfile);
4964 /* We're in the middle of a line. Skip the rest of it. */
4972 case '/': /* possible comment */
4973 c = skip_comment (pfile, NULL);
4976 /*@switchbreak@*/ break;
4979 cppReader_forward (pfile, -1);
4980 old = cppReader_getWritten (pfile);
4981 (void) cppGetToken (pfile);
4982 cppReader_setWritten (pfile, old);
4983 /*@switchbreak@*/ break;
4985 /* Char after backslash loses its special meaning. */
4986 if (cppReader_peekC (pfile) == '\n')
4988 cppReader_forward (pfile, 1);
4991 /*@switchbreak@*/ break;
4995 c = cppReader_getC (pfile);
4998 if (CPPOPTIONS (pfile)->output_conditionals) {
4999 static char end_failed[] = "#endfailed\n";
5000 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5003 pfile->only_seen_white = 1;
5005 parseGotoMark (&line_start_mark, pfile);
5006 parseClearMark (&line_start_mark);
5010 * handle a #else directive. Do this by just continuing processing
5011 * without changing if_stack ; this is so that the error message
5012 * for missing #endif's etc. will point to the original #if. It
5013 * is possible that something different would be better.
5017 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5018 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5020 if (cppReader_isPedantic (pfile))
5022 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5025 cppReader_skipRestOfLine (pfile);
5027 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack) {
5028 cppReader_errorLit (pfile,
5029 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5032 /* #ifndef can't have its special treatment for containing the whole file
5033 if it has a #else clause. */
5035 llassert (pfile->if_stack != NULL);
5037 pfile->if_stack->control_macro = 0;
5039 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5041 cpp_setLocation (pfile);
5042 genppllerrorhint (FLG_PREPROC,
5043 message ("Pre-processor directive #else after #else"),
5044 message ("%q: Location of match",
5045 fileloc_unparseRaw (pfile->if_stack->fname,
5046 pfile->if_stack->lineno)));
5049 pfile->if_stack->type = T_ELSE;
5052 if (pfile->if_stack->if_succeeded)
5053 skip_if_group (pfile, 0);
5055 ++pfile->if_stack->if_succeeded; /* continue processing input */
5056 output_line_command (pfile, 1, same_file);
5063 * unstack after #endif command
5067 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5068 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5070 if (cppReader_isPedantic (pfile))
5072 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5075 cppReader_skipRestOfLine (pfile);
5077 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5079 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5083 cppIfStackFrame *temp = pfile->if_stack;
5085 llassert (temp != NULL);
5087 pfile->if_stack = temp->next;
5088 if (temp->control_macro != 0)
5090 /* This #endif matched a #ifndef at the start of the file.
5091 See if it is at the end of the file. */
5092 struct parse_marker start_mark;
5095 parseSetMark (&start_mark, pfile);
5099 cppSkipHspace (pfile);
5100 c = cppReader_getC (pfile);
5106 parseGotoMark (&start_mark, pfile);
5107 parseClearMark (&start_mark);
5111 /* If we get here, this #endif ends a #ifndef
5112 that contains all of the file (aside from whitespace).
5113 Arrange not to include the file again
5114 if the macro that was tested is defined.
5116 Do not do this for the top-level file in a -include or any
5117 file in a -imacros. */
5118 struct file_name_list *ifile = pfile->all_include_files;
5120 for ( ; ifile != NULL; ifile = ifile->next)
5122 if (cstring_equal (ifile->fname, cppReader_getBuffer (pfile)->fname))
5124 ifile->control_macro = temp->control_macro;
5132 output_line_command (pfile, 1, same_file);
5137 /* When an #else or #endif is found while skipping failed conditional,
5138 if -pedantic was specified, this is called to warn about text after
5139 the command name. P points to the first char after the command name. */
5142 validate_else (cppReader *pfile, cstring directive)
5145 cppSkipHspace (pfile);
5146 c = cppReader_peekC (pfile);
5147 if (c != EOF && c != '\n')
5149 cppReader_pedwarn (pfile,
5150 message ("text following `%s' violates ANSI standard", directive));
5155 ** Get the next token, and add it to the text in pfile->token_buffer.
5156 ** Return the kind of token we got.
5160 cppGetToken (cppReader *pfile)
5163 size_t old_written = 0;
5164 int start_line, start_column;
5165 enum cpp_token token;
5166 struct cppOptions *opts = CPPOPTIONS (pfile);
5167 cppReader_getBuffer (pfile)->prev = cppReader_getBuffer (pfile)->cur;
5170 c = cppReader_getC (pfile);
5175 if (cppReader_getBuffer (pfile)->seen_eof)
5177 cppBuffer *buf = cppReader_popBuffer (pfile);
5179 if (buf != cppReader_nullBuffer (pfile))
5190 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5191 cppReader_getBuffer (pfile)->seen_eof = 1;
5193 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname)
5194 && next_buf != cppReader_nullBuffer (pfile))
5196 /* We're about to return from an #include file.
5197 Emit #line information now (as part of the CPP_POP) result.
5198 But the #line refers to the file we will pop to. */
5199 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5200 CPPBUFFER (pfile) = next_buf;
5201 pfile->input_stack_listing_current = 0;
5202 output_line_command (pfile, 0, leave_file);
5203 CPPBUFFER (pfile) = cur_buffer;
5211 struct parse_marker start_mark;
5216 if (cppReader_peekC (pfile) == '=')
5221 if (opts->put_out_comments)
5223 parseSetMark (&start_mark, pfile);
5227 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5228 &start_line, &start_column);
5229 c = skip_comment (pfile, &newlines);
5231 if (opts->put_out_comments && (c == '/' || c == EOF))
5233 assertSet (&start_mark);
5234 parseClearMark (&start_mark);
5241 cppReader_errorWithLine (pfile, start_line, start_column,
5242 cstring_makeLiteral ("Unterminated comment"));
5245 c = '/'; /* Initial letter of comment. */
5247 /* Comments are equivalent to spaces.
5248 For -traditional, a comment is equivalent to nothing. */
5250 if (opts->put_out_comments)
5254 assertSet (&start_mark);
5255 res = cpp_handleComment (pfile, &start_mark);
5256 pfile->lineno += newlines;
5259 else if (cppReader_isTraditional (pfile))
5265 cppReader_reserve(pfile, 1);
5266 cppReader_putCharQ (pfile, ' ');
5271 if (!pfile->only_seen_white)
5276 if (cppReader_handleDirective (pfile))
5278 return CPP_DIRECTIVE;
5281 pfile->only_seen_white = 0;
5286 /* A single quoted string is treated like a double -- some
5287 programs (e.g., troff) are perverse this way */
5288 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5289 &start_line, &start_column);
5290 old_written = cppReader_getWritten (pfile);
5292 cppReader_putChar (pfile, c);
5295 int cc = cppReader_getC (pfile);
5298 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
5300 /* try harder: this string crosses a macro expansion
5301 boundary. This can happen naturally if -traditional.
5302 Otherwise, only -D can make a macro with an unmatched
5305 = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5306 (*cppReader_getBuffer (pfile)->cleanup)
5307 (cppReader_getBuffer (pfile), pfile);
5308 CPPBUFFER (pfile) = next_buf;
5311 if (!cppReader_isTraditional (pfile))
5313 cpp_setLocation (pfile);
5315 setLine (long_toInt (start_line));
5316 setColumn (long_toInt (start_column));
5318 if (pfile->multiline_string_line != long_toInt (start_line)
5319 && pfile->multiline_string_line != 0)
5323 message ("Unterminated string or character constant"),
5324 message ("%q: Possible real start of unterminated constant",
5326 (fileloc_filename (g_currentloc),
5327 pfile->multiline_string_line)));
5328 pfile->multiline_string_line = 0;
5334 message ("Unterminated string or character constant"));
5337 /*@loopbreak@*/ break;
5339 cppReader_putChar (pfile, cc);
5343 /* Traditionally, end of line ends a string constant with
5344 no error. So exit the loop and record the new line. */
5345 if (cppReader_isTraditional (pfile))
5351 if (cppReader_isPedantic (pfile)
5352 && pfile->multiline_string_line == 0)
5354 cppReader_pedwarnWithLine
5355 (pfile, long_toInt (start_line),
5356 long_toInt (start_column),
5357 cstring_makeLiteral ("String constant runs past end of line"));
5359 if (pfile->multiline_string_line == 0)
5361 pfile->multiline_string_line = start_line;
5364 /*@switchbreak@*/ break;
5367 cc = cppReader_getC (pfile);
5370 /* Backslash newline is replaced by nothing at all. */
5371 cppReader_adjustWritten (pfile, -1);
5376 /* ANSI stupidly requires that in \\ the second \
5377 is *not* prevented from combining with a newline. */
5380 cppReader_putChar (pfile, cc);
5382 /*@switchbreak@*/ break;
5388 /*@switchbreak@*/ break;
5392 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
5393 cppReader_getPWritten (pfile));
5394 pfile->only_seen_white = 0;
5395 return c == '\'' ? CPP_CHAR : CPP_STRING;
5398 if (!opts->dollars_in_ident)
5403 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
5411 c2 = cppReader_peekC (pfile);
5412 if (c2 == c || c2 == '=')
5422 if (cppReader_peekC (pfile) == '=')
5428 c2 = cppReader_peekC (pfile);
5429 if (c2 == '-' && opts->chill)
5431 /* Chill style comment */
5432 if (opts->put_out_comments)
5434 parseSetMark (&start_mark, pfile);
5437 cppReader_forward (pfile, 1); /* Skip second '-'. */
5441 c = cppReader_getC (pfile);
5443 /*@loopbreak@*/ break;
5446 /* Don't consider final '\n' to be part of comment. */
5447 cppReader_forward (pfile, -1);
5448 /*@loopbreak@*/ break;
5452 goto return_comment;
5454 if (c2 == '-' || c2 == '=' || c2 == '>')
5459 if (pfile->parsing_include_directive)
5463 cppReader_putChar (pfile, c);
5465 /*@loopbreak@*/ break;
5466 c = cppReader_getC (pfile);
5468 if (c == '\n' || c == EOF)
5470 cppReader_errorLit (pfile,
5471 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
5472 /*@loopbreak@*/ break;
5480 c2 = cppReader_peekC (pfile);
5485 cppReader_forward (pfile, 1);
5486 cppReader_reserve (pfile, 4);
5487 cppReader_putChar (pfile, c);
5488 cppReader_putChar (pfile, c2);
5490 c3 = cppReader_peekC (pfile);
5492 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5493 cppReader_nullTerminateQ (pfile);
5494 pfile->only_seen_white = 0;
5498 if (cppReader_getBuffer (pfile)->has_escapes)
5500 c = cppReader_getC (pfile);
5503 if (pfile->output_escapes)
5504 cppReader_puts (pfile, "@-", 2);
5505 parse_name (pfile, cppReader_getC (pfile));
5508 else if (is_space [c])
5510 cppReader_reserve (pfile, 2);
5511 if (pfile->output_escapes)
5512 cppReader_putCharQ (pfile, '@');
5513 cppReader_putCharQ (pfile, c);
5521 if (pfile->output_escapes)
5523 cppReader_puts (pfile, "@@", 2);
5529 c2 = cppReader_peekC (pfile);
5532 cppReader_reserve(pfile, 2);
5533 cppReader_putCharQ (pfile, '.');
5534 c = cppReader_getC (pfile);
5538 /* FIXME - misses the case "..\\\n." */
5539 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
5541 cppReader_reserve(pfile, 4);
5542 cppReader_putCharQ (pfile, '.');
5543 cppReader_putCharQ (pfile, '.');
5544 cppReader_putCharQ (pfile, '.');
5545 cppReader_forward (pfile, 2);
5546 cppReader_nullTerminateQ (pfile);
5547 pfile->only_seen_white = 0;
5553 pfile->only_seen_white = 0;
5555 cppReader_reserve(pfile, 3);
5556 cppReader_putCharQ (pfile, c);
5557 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5558 cppReader_nullTerminateQ (pfile);
5563 c2 = cppReader_peekC (pfile);
5564 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
5566 cppReader_putChar (pfile, c);
5567 c = cppReader_getC (pfile);
5572 case '0': case '1': case '2': case '3': case '4':
5573 case '5': case '6': case '7': case '8': case '9':
5578 cppReader_reserve (pfile, 2);
5579 cppReader_putCharQ (pfile, c);
5581 c = cppReader_peekC (pfile);
5583 /*@loopbreak@*/ break;
5584 if (!is_idchar[c] && c != '.'
5585 && ((c2 != 'e' && c2 != 'E'
5586 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
5587 || (c != '+' && c != '-')))
5588 /*@loopbreak@*/ break;
5589 cppReader_forward (pfile, 1);
5593 cppReader_nullTerminateQ (pfile);
5594 pfile->only_seen_white = 0;
5597 case 'b': case 'c': case 'd': case 'h': case 'o':
5598 case 'B': case 'C': case 'D': case 'H': case 'O':
5599 if (opts->chill && cppReader_peekC (pfile) == '\'')
5601 pfile->only_seen_white = 0;
5602 cppReader_reserve (pfile, 2);
5603 cppReader_putCharQ (pfile, c);
5604 cppReader_putCharQ (pfile, '\'');
5605 cppReader_forward (pfile, 1);
5608 c = cppReader_getC (pfile);
5610 goto chill_number_eof;
5613 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5615 cppReader_forward (pfile, 2);
5618 /*@loopbreak@*/ break;
5620 cppReader_putChar (pfile, c);
5624 cppReader_reserve (pfile, 2);
5625 cppReader_putCharQ (pfile, c);
5626 cppReader_nullTerminateQ (pfile);
5631 cppReader_forward (pfile, -1);
5633 cppReader_nullTerminate (pfile);
5640 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5641 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5642 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5643 case 'x': case 'y': case 'z':
5644 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5645 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5646 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5652 size_t before_name_written = cppReader_getWritten (pfile);
5654 parse_name (pfile, c);
5655 pfile->only_seen_white = 0;
5656 if (pfile->no_macro_expand)
5661 ident = pfile->token_buffer + before_name_written;
5662 ident_len = (cppReader_getPWritten (pfile)) - ident;
5664 hp = cppReader_lookupExpand (ident, ident_len, -1);
5671 if (hp->type == T_DISABLED)
5673 if (pfile->output_escapes)
5674 { /* Return "@-IDENT", followed by '\0'. */
5676 cppReader_reserve (pfile, 3);
5677 ident = pfile->token_buffer + before_name_written;
5678 cppReader_adjustWritten (pfile, 2);
5680 for (i = ident_len; i >= 0; i--)
5682 ident[i+2] = ident[i];
5691 /* If macro wants an arglist, verify that a '(' follows.
5692 first skip all whitespace, copying it to the output
5693 after the macro name. Then, if there is no '(',
5694 decide this is not a macro call and leave things that way. */
5696 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5698 struct parse_marker macro_mark;
5701 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
5703 cppBuffer *next_buf;
5704 cppSkipHspace (pfile);
5705 if (cppReader_peekC (pfile) != EOF)
5707 /*@loopbreak@*/ break;
5710 next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5711 (*cppReader_getBuffer (pfile)->cleanup) (cppReader_getBuffer (pfile), pfile);
5712 CPPBUFFER (pfile) = next_buf;
5715 parseSetMark (¯o_mark, pfile);
5719 cppSkipHspace (pfile);
5720 c = cppReader_peekC (pfile);
5721 is_macro_call = c == '(';
5723 /*@loopbreak@*/ break;
5724 cppReader_forward (pfile, 1);
5729 parseGotoMark (¯o_mark, pfile);
5732 parseClearMark (¯o_mark);
5739 /* This is now known to be a macro call. */
5741 /* it might not actually be a macro. */
5742 if (hp->type != T_MACRO)
5747 cppReader_setWritten (pfile, before_name_written);
5748 special_symbol (hp, pfile);
5749 xbuf_len = cppReader_getWritten (pfile) - before_name_written;
5750 xbuf = (char *) dmalloc (xbuf_len + 1);
5751 cppReader_setWritten (pfile, before_name_written);
5752 memcpy (xbuf, cppReader_getPWritten (pfile), xbuf_len + 1);
5753 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5757 /* Expand the macro, reading arguments as needed,
5758 and push the expansion on the input stack. */
5759 macroexpand (pfile, hp);
5760 cppReader_setWritten (pfile, before_name_written);
5763 /* An extra "@ " is added to the end of a macro expansion
5764 to prevent accidental token pasting. We prefer to avoid
5765 unneeded extra spaces (for the sake of cpp-using tools like
5766 imake). Here we remove the space if it is safe to do so. */
5768 llassert (pfile->buffer->rlimit != NULL);
5770 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5771 && pfile->buffer->rlimit[-2] == '@'
5772 && pfile->buffer->rlimit[-1] == ' ')
5774 int c1 = pfile->buffer->rlimit[-3];
5775 int cl2 = cppBufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
5777 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
5778 pfile->buffer->rlimit -= 2;
5784 case ' ': case '\t': case '\v': case '\r':
5787 cppReader_putChar (pfile, c);
5788 c = cppReader_peekC (pfile);
5789 if (c == EOF || !is_hor_space[c])
5790 /*@loopbreak@*/ break;
5791 cppReader_forward (pfile, 1);
5796 c2 = cppReader_peekC (pfile);
5803 cppReader_putChar (pfile, c);
5804 if (pfile->only_seen_white == 0)
5805 pfile->only_seen_white = 1;
5807 output_line_command (pfile, 1, same_file);
5810 case '(': token = CPP_LPAREN; goto char1;
5811 case ')': token = CPP_RPAREN; goto char1;
5812 case '{': token = CPP_LBRACE; goto char1;
5813 case '}': token = CPP_RBRACE; goto char1;
5814 case ',': token = CPP_COMMA; goto char1;
5815 case ';': token = CPP_SEMICOLON; goto char1;
5821 pfile->only_seen_white = 0;
5822 cppReader_putChar (pfile, c);
5831 /* Parse an identifier starting with C. */
5834 parse_name (cppReader *pfile, int c)
5840 if (c == '\\' && cppReader_peekC (pfile) == '\n')
5842 cppReader_forward (pfile, 2);
5846 cppReader_forward (pfile, -1);
5850 if (c == '$' && cppReader_isPedantic (pfile))
5852 cppReader_pedwarnLit (pfile,
5853 cstring_makeLiteralTemp ("`$' in identifier"));
5856 cppReader_reserve(pfile, 2); /* One more for final NUL. */
5857 cppReader_putCharQ (pfile, c);
5858 c = cppReader_getC (pfile);
5864 cppReader_nullTerminateQ (pfile);
5867 /* The file_name_map structure holds a mapping of file names for a
5868 particular directory. This mapping is read from the file named
5869 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5870 map filenames on a file system with severe filename restrictions,
5871 such as DOS. The format of the file name map file is just a series
5872 of lines with two tokens on each line. The first token is the name
5873 to map, and the second token is the actual name to use. */
5875 struct file_name_map
5877 struct file_name_map *map_next;
5882 /*@constant observer char *FILE_NAME_MAP_FILE*/
5883 #define FILE_NAME_MAP_FILE "header.gcc"
5885 /* Read a space delimited string of unlimited length from a stdio
5888 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
5894 set = alloc = dmalloc (len + 1);
5899 while ((ch = getc (f)) != EOF && ! is_space[ch])
5901 if (set - alloc == size_toInt (len))
5904 alloc = drealloc (alloc, len + 1);
5905 set = alloc + len / 2;
5906 /*@-branchstate@*/ }
5909 } /*@=branchstate@*/
5912 check (ungetc (ch, f) != EOF);
5914 return cstring_fromChars (alloc);
5917 /* This structure holds a linked list of file name maps, one per directory. */
5919 struct file_name_map_list
5921 struct file_name_map_list *map_list_next;
5922 cstring map_list_name;
5923 struct file_name_map *map_list_map;
5926 /* Read the file name map file for DIRNAME. */
5928 static struct file_name_map *
5929 read_name_map (cppReader *pfile, cstring dirname)
5931 struct file_name_map_list *map_list_ptr;
5935 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
5936 map_list_ptr != NULL;
5937 map_list_ptr = map_list_ptr->map_list_next)
5939 if (cstring_equal (map_list_ptr->map_list_name, dirname))
5941 return map_list_ptr->map_list_map;
5945 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
5946 map_list_ptr->map_list_name = cstring_copy (dirname);
5947 map_list_ptr->map_list_map = NULL;
5949 name = cstring_copy (dirname);
5951 if (cstring_length (dirname) > 0)
5953 name = cstring_appendChar (name, CONNECTCHAR);
5956 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
5958 f = fopen (cstring_toCharsSafe (name), "r");
5959 cstring_free (name);
5963 map_list_ptr->map_list_map = NULL;
5969 while ((ch = getc (f)) != EOF)
5972 struct file_name_map *ptr;
5979 from = read_filename_string (ch, f);
5980 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5985 to = read_filename_string (ch, f);
5987 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
5988 ptr->map_from = from;
5990 /* Make the real filename absolute. */
5991 if (cstring_length (to) > 1
5992 && osd_isConnectChar (cstring_firstChar (to)))
5998 ptr->map_to = cstring_copy (dirname);
5999 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6000 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6003 ptr->map_next = map_list_ptr->map_list_map;
6004 map_list_ptr->map_list_map = ptr;
6006 while ((ch = getc (f)) != '\n')
6010 /*@innerbreak@*/ break;
6015 assertSet (map_list_ptr->map_list_map);
6016 check (fclose (f) == 0);
6019 map_list_ptr->map_list_next = pfile->opts->map_list;
6020 pfile->opts->map_list = map_list_ptr;
6022 return map_list_ptr->map_list_map;
6025 /* Try to open include file FILENAME. SEARCHPTR is the directory
6026 being tried from the include file search path. This function maps
6027 filenames on file systems based on information read by
6031 open_include_file (cppReader *pfile,
6033 struct file_name_list *searchptr)
6035 char *filename = cstring_toCharsSafe (fname);
6036 struct file_name_map *map;
6040 cstring_markOwned (fname);
6042 cpp_setLocation (pfile);
6044 if (context_getFlag (FLG_NEVERINCLUDE))
6046 if (isHeaderFile (fname))
6048 return SKIP_INCLUDE;
6052 if ((searchptr != NULL) && ! searchptr->got_name_map)
6054 searchptr->name_map = read_name_map (pfile,
6055 !cstring_isEmpty (searchptr->fname)
6056 ? searchptr->fname :
6057 cstring_makeLiteralTemp ("."));
6058 searchptr->got_name_map = 1;
6061 /* First check the mapping for the directory we are using. */
6063 if ((searchptr != NULL)
6064 && (searchptr->name_map != NULL))
6068 if (!cstring_isEmpty (searchptr->fname))
6070 from += cstring_length (searchptr->fname) + 1;
6073 for (map = searchptr->name_map;
6075 map = map->map_next)
6077 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6080 ** Found a match. Check if the file should be skipped
6083 if (cpp_skipIncludeFile (map->map_to))
6085 return SKIP_INCLUDE;
6089 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6096 ** Try to find a mapping file for the particular directory we are
6097 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6098 ** in /usr/include/header.gcc and look up types.h in
6099 ** /usr/include/sys/header.gcc.
6102 p = strrchr (filename, CONNECTCHAR);
6109 if ((searchptr != NULL)
6110 && (cstring_isDefined (searchptr->fname))
6111 && (cstring_length (searchptr->fname) == p - filename)
6112 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6114 size_fromInt (p - filename)))
6116 /* filename is in SEARCHPTR, which we've already checked. */
6118 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6120 return SKIP_INCLUDE;
6124 return cpp_openIncludeFile (filename);
6130 dir = mstring_copy (".");
6135 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6136 memcpy (dir, filename, size_fromInt (p - filename));
6137 dir[p - filename] = '\0';
6141 for (map = read_name_map (pfile, cstring_fromChars (dir));
6143 map = map->map_next)
6145 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6149 if (cpp_skipIncludeFile (map->map_to))
6151 return SKIP_INCLUDE;
6155 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6162 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6164 return SKIP_INCLUDE;
6168 return cpp_openIncludeFile (filename);
6172 /* Process the contents of include file FNAME, already open on descriptor F,
6174 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6175 "system" include directories (as decided by the `is_system_include'
6177 DIRPTR is the link in the dir path through which this file was found,
6178 or 0 if the file name was absolute or via the current directory.
6179 Return 1 on success, 0 on failure.
6181 The caller is responsible for the cppReader_pushBuffer. */
6184 finclude (cppReader *pfile, int f,
6186 bool system_header_p,
6187 /*@dependent@*/ struct file_name_list *dirptr)
6193 cppBuffer *fp; /* For input stack frame */
6195 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
6197 cppReader_perrorWithName (pfile, fname);
6198 check (close (f) == 0);
6199 (void) cppReader_popBuffer (pfile);
6205 fp = cppReader_getBuffer (pfile);
6207 /*@-temptrans@*/ /* fname shouldn't really be temp */
6208 fp->nominal_fname = fp->fname = fname;
6212 fp->system_header_p = system_header_p;
6215 fp->cleanup = cppReader_fileCleanup;
6217 if (S_ISREG (st_mode))
6220 fp->buf = (char *) dmalloc (st_size + 2);
6221 fp->alimit = fp->buf + st_size + 2;
6224 /* Read the file contents, knowing that st_size is an upper bound
6225 on the number of bytes we can read. */
6226 length = safe_read (f, fp->buf, size_toInt (st_size));
6227 fp->rlimit = fp->buf + length;
6228 if (length < 0) goto nope;
6230 else if (S_ISDIR (st_mode))
6232 cppReader_error (pfile,
6233 message ("Directory specified in #include: %s", fname));
6234 check (close (f) == 0);
6240 ** Cannot count its file size before reading.
6241 ** First read the entire file into heap and
6242 ** copy them into buffer on stack.
6245 size_t bsize = 2000;
6250 fp->buf = (char *) dmalloc (bsize + 2);
6253 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
6256 goto nope; /* error! */
6259 if (st_size != bsize)
6261 break; /* End of file */
6265 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
6269 length = size_toInt (st_size);
6272 if ((length > 0 && fp->buf[length - 1] != '\n')
6273 /* Backslash-newline at end is not good enough. */
6274 || (length > 1 && fp->buf[length - 2] == '\\')) {
6275 fp->buf[length++] = '\n';
6278 fp->buf[length] = '\0';
6279 fp->rlimit = fp->buf + length;
6281 /* Close descriptor now, so nesting does not use lots of descriptors. */
6282 check (close (f) == 0);
6284 /* Must do this before calling trigraph_pcp, so that the correct file name
6285 will be printed in warning messages. */
6287 pfile->input_stack_listing_current = 0;
6292 cppReader_perrorWithName (pfile, fname);
6293 check (close (f) == 0);
6299 cppReader_init (cppReader *pfile)
6301 memset ((char *) pfile, 0, sizeof (*pfile));
6303 pfile->get_token = cppGetToken;
6304 pfile->token_buffer_size = 200;
6305 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
6306 pfile->all_include_files = NULL;
6310 cppReader_setWritten (pfile, 0);
6312 pfile->system_include_depth = 0;
6313 pfile->max_include_len = 0;
6314 pfile->timebuf = NULL;
6315 pfile->only_seen_white = 1;
6317 pfile->buffer = cppReader_nullBuffer (pfile);
6321 cppReader_finish (/*@unused@*/ cppReader *pfile)
6326 /* Free resources used by PFILE.
6327 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
6330 cppCleanup (cppReader *pfile)
6332 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
6334 (void) cppReader_popBuffer (pfile);
6337 if (pfile->token_buffer != NULL)
6339 sfree (pfile->token_buffer);
6340 pfile->token_buffer = NULL;
6343 while (pfile->if_stack != NULL)
6345 cppIfStackFrame *temp = pfile->if_stack;
6346 pfile->if_stack = temp->next;
6350 while (pfile->all_include_files != NULL)
6352 struct file_name_list *temp = pfile->all_include_files;
6353 pfile->all_include_files = temp->next;
6354 /*@-dependenttrans@*/
6355 cstring_free (temp->fname);
6356 /*@=dependenttrans@*/
6360 cppReader_hashCleanup ();
6364 ** Get the file-mode and data size of the file open on FD
6365 ** and store them in *MODE_POINTER and *SIZE_POINTER.
6369 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
6373 if (fstat (fd, &sbuf) < 0) {
6377 if (mode_pointer != NULL)
6379 *mode_pointer = sbuf.st_mode;
6382 if (size_pointer != NULL)
6384 *size_pointer = (size_t) sbuf.st_size;
6390 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
6391 retrying if necessary. Return a negative value if an error occurs,
6392 otherwise return the actual number of bytes read,
6393 which must be LEN unless end-of-file was reached. */
6395 static int safe_read (int desc, char *ptr, int len)
6401 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
6402 /*@-compdef@*/ /* ptr is an out parameter */
6403 int nchars = _read (desc, ptr, (unsigned) left);
6406 ssize_t nchars = read (desc, ptr, size_fromInt (left));
6415 return (int) nchars;
6429 /* Initialize PMARK to remember the current position of PFILE. */
6432 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
6434 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6436 pmark->next = pbuf->marks;
6438 pbuf->marks = pmark;
6442 pmark->position = pbuf->cur - pbuf->buf;
6445 /* Cleanup PMARK - we no longer need it. */
6447 void parseClearMark (struct parse_marker *pmark)
6449 struct parse_marker **pp = &pmark->buf->marks;
6451 for (; ; pp = &(*pp)->next)
6453 llassert (*pp != NULL);
6454 if (*pp == pmark) break;
6460 /* Backup the current position of PFILE to that saved in PMARK. */
6463 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
6465 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6467 if (pbuf != pmark->buf)
6469 cpp_setLocation (pfile);
6470 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
6473 llassert (pbuf->buf != NULL);
6474 pbuf->cur = pbuf->buf + pmark->position;
6477 /* Reset PMARK to point to the current position of PFILE. (Same
6478 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
6481 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
6483 cppBuffer *pbuf = cppReader_getBuffer (pfile);
6485 if (pbuf != pmark->buf)
6487 cpp_setLocation (pfile);
6488 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
6491 pmark->position = pbuf->cur - pbuf->buf;
6494 void cppReader_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
6496 struct cppOptions *opts = CPPOPTIONS (pfile);
6499 /* The code looks at the defaults through this pointer, rather than through
6500 the constant structure above. This pointer gets changed if an environment
6501 variable specifies other defaults. */
6503 struct default_include *include_defaults = include_defaults_array;
6505 /* Add dirs from CPATH after dirs from -I. */
6506 /* There seems to be confusion about what CPATH should do,
6507 so for the moment it is not documented. */
6508 /* Some people say that CPATH should replace the standard include dirs,
6509 but that seems pointless: it comes before them, so it overrides them
6512 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
6514 if (cstring_isDefined (xp) && !opts->no_standard_includes)
6516 path_include (pfile, cstring_toCharsSafe (xp));
6519 /* Now that dollars_in_ident is known, initialize is_idchar. */
6520 initialize_char_syntax (opts);
6522 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
6523 and option processing. */
6525 initialize_builtins (pfile);
6527 /* Do standard #defines and assertions
6528 that identify system and machine type. */
6530 if (!opts->inhibit_predefs) {
6531 char *p = (char *) dmalloc (strlen (predefs) + 1);
6532 strcpy (p, predefs);
6538 while (*p == ' ' || *p == '\t')
6543 /* Handle -D options. */
6544 if (p[0] == '-' && p[1] == 'D')
6548 while (*p && *p != ' ' && *p != '\t')
6558 if (opts->debug_output)
6560 output_line_command (pfile, 0, same_file);
6563 cppReader_define (pfile, q);
6565 while (*p == ' ' || *p == '\t')
6579 opts->done_initializing = 1;
6581 { /* Read the appropriate environment variable and if it exists
6582 replace include_defaults with the listed path. */
6586 int win32_buf_size = 0; /* memory we need to allocate */
6589 if (opts->cplusplus)
6591 epath = getenv ("CPLUS_INCLUDE_PATH");
6595 epath = getenv ("C_INCLUDE_PATH");
6599 ** If the environment var for this language is set,
6600 ** add to the default list of include directories.
6603 if (epath != NULL) {
6604 char *nstore = (char *) dmalloc (strlen (epath) + 2);
6606 char *startp, *endp;
6609 /* if we have a posix path list, convert to win32 path list */
6610 if (cygwin32_posix_path_list_p (epath))
6612 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
6613 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
6614 cygwin32_posix_to_win32_path_list (epath, win32epath);
6618 for (num_dirs = 1, startp = epath; *startp; startp++)
6620 if (*startp == PATH_SEPARATOR)
6626 = (struct default_include *) dmalloc ((num_dirs
6627 * sizeof (struct default_include))
6628 + sizeof (include_defaults_array));
6631 startp = endp = epath;
6634 /* Handle cases like c:/usr/lib:d:/gcc/lib */
6635 if ((*endp == PATH_SEPARATOR) || *endp == 0)
6637 strncpy (nstore, startp, size_fromInt (endp - startp));
6640 strcpy (nstore, ".");
6644 nstore[endp-startp] = '\0';
6647 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
6648 include_defaults[num_dirs].cplusplus = opts->cplusplus;
6649 include_defaults[num_dirs].cxx_aware = 1;
6656 endp = startp = endp + 1;
6663 /* Put the usual defaults back in at the end. */
6664 memcpy ((char *) &include_defaults[num_dirs],
6665 (char *) include_defaults_array,
6666 sizeof (include_defaults_array));
6669 /*@-branchstate@*/ } /*@=branchstate@*/
6672 cppReader_appendIncludeChain (pfile, opts->before_system,
6673 opts->last_before_system);
6674 opts->first_system_include = opts->before_system;
6676 /* Unless -fnostdinc,
6677 tack on the standard include file dirs to the specified list */
6678 if (!opts->no_standard_includes) {
6679 struct default_include *p = include_defaults;
6680 char *specd_prefix = opts->include_prefix;
6681 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
6682 int default_len = 0;
6684 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6685 if (default_prefix != NULL) {
6686 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
6687 default_len = strlen (default_prefix) - 7;
6688 default_prefix[default_len] = 0;
6692 /* Search "translated" versions of GNU directories.
6693 These have /usr/local/lib/gcc... replaced by specd_prefix. */
6694 if (specd_prefix != 0 && default_len != 0)
6695 for (p = include_defaults; p->fname != NULL; p++) {
6696 /* Some standard dirs are only for C++. */
6698 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
6699 /* Does this dir start with the prefix? */
6700 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
6701 size_fromInt (default_len)))
6703 /* Yes; change prefix and add to search list. */
6704 struct file_name_list *nlist
6705 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6706 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
6707 char *str = (char *) dmalloc (this_len + 1);
6708 strcpy (str, specd_prefix);
6709 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
6712 nlist->fname = cstring_fromChars (str);
6713 nlist->control_macro = 0;
6714 nlist->c_system_include_path = !p->cxx_aware;
6715 nlist->got_name_map = 0;
6717 cppReader_addIncludeChain (pfile, nlist);
6718 if (opts->first_system_include == 0)
6720 opts->first_system_include = nlist;
6726 /* Search ordinary names for GNU include directories. */
6728 for (p = include_defaults; p->fname != NULL; p++)
6730 /* Some standard dirs are only for C++. */
6732 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
6734 struct file_name_list *nlist
6735 = (struct file_name_list *) dmalloc (sizeof (*nlist));
6736 nlist->control_macro = 0;
6737 nlist->c_system_include_path = !p->cxx_aware;
6738 nlist->fname = p->fname;
6739 nlist->got_name_map = 0;
6742 cppReader_addIncludeChain (pfile, nlist);
6744 if (opts->first_system_include == 0)
6746 opts->first_system_include = nlist;
6750 sfree (default_prefix);
6753 /* Tack the after_include chain at the end of the include chain. */
6754 cppReader_appendIncludeChain (pfile, opts->after_include,
6755 opts->last_after_include);
6757 if (opts->first_system_include == 0)
6759 opts->first_system_include = opts->after_include;
6762 /* With -v, print the list of dirs to search. */
6763 if (opts->verbose) {
6764 struct file_name_list *p;
6765 fprintf (stderr, "#include \"...\" search starts here:\n");
6767 for (p = opts->include; p != NULL; p = p->next) {
6768 if (p == opts->first_bracket_include)
6769 fprintf (stderr, "#include <...> search starts here:\n");
6771 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
6773 fprintf (stderr, "End of search list.\n");
6777 int cppReader_startProcess (cppReader *pfile, cstring fname)
6781 struct cppOptions *opts = CPPOPTIONS (pfile);
6783 fp = cppReader_pushBuffer (pfile, NULL, 0);
6790 if (opts->in_fname == NULL)
6792 opts->in_fname = cstring_makeLiteralTemp ("");
6795 fp->fname = opts->in_fname;
6796 fp->nominal_fname = fp->fname;
6799 /* Copy the entire contents of the main input file into
6800 the stacked input buffer previously allocated for it. */
6802 if (cstring_isEmpty (fname))
6804 fname = cstring_makeLiteralTemp ("");
6807 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
6809 cppReader_error (pfile,
6810 message ("Error opening %s for reading: %s",
6811 fname, lldecodeerror (errno)));
6820 if (finclude (pfile, f, fname, 0, NULL))
6822 output_line_command (pfile, 0, same_file);
6828 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
6830 return pfile->buffer;
6833 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
6835 llassert (pfile->buffer != NULL);
6836 return pfile->buffer;
6839 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
6841 llassert (buf->buf != NULL);
6842 return (buf->buf + buf->line_base);
6845 int cppBufPeek (cppBuffer *buf)
6847 if (buf->cur == NULL || buf->rlimit == NULL) {
6851 if (buf->cur < buf->rlimit) {
6858 bool cppBuffer_isMacro (cppBuffer *buf)
6862 return (buf->cleanup == cppReader_macroCleanup);
6869 ** Returns true if the macro should be checked, false
6870 ** if it should be expanded normally.
6873 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
6874 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
6875 static bool expectiter = FALSE; /* preceeded by @iter@ */
6876 static bool expectenditer = FALSE; /* second after @iter@ */
6877 static bool expectfunction = FALSE; /* preceeded by @function@ */
6878 static bool expectconstant = FALSE; /* preceeded by @constant@ */
6879 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
6881 static void cpp_setLocation (cppReader *pfile)
6886 if (pfile->buffer != NULL)
6888 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname))
6890 cstring fname = cppReader_getBuffer (pfile)->nominal_fname;
6892 DPRINTF (("Looking up: %s", fname));
6894 if (fileTable_exists (context_fileTable (), fname))
6896 fid = fileTable_lookup (context_fileTable (), fname);
6900 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
6902 fid = fileTable_lookup (context_fileTable (),
6903 cppReader_getBuffer (pfile)->fname);
6908 fid = fileTable_lookup (context_fileTable (),
6909 cppReader_getBuffer (pfile)->fname);
6912 line = cppReader_getBuffer (pfile)->lineno;
6913 fileloc_free (g_currentloc);
6915 if (fileId_isValid (fid))
6917 g_currentloc = fileloc_create (fid, line, 1);
6921 g_currentloc = fileloc_createBuiltin ();
6926 fileloc_free (g_currentloc);
6927 g_currentloc = fileloc_createBuiltin ();
6931 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@*/
6933 bool checkmacro = FALSE;
6934 bool hasParams = FALSE;
6935 bool noexpand = FALSE;
6939 cpp_setLocation (pfile);
6941 DPRINTF (("Should check macro? %s", p));
6943 if (expectiter || expectconstant || expectenditer)
6948 expectenditer = TRUE;
6953 expectconstant = FALSE;
6954 expectenditer = FALSE;
6957 if (notfunction || notparseable)
6959 notfunction = FALSE;
6960 notparseable = FALSE;
6969 llassert (*p == '#');
6972 while (*p == ' ' || *p == '\t')
6977 llassert (*p == 'd'); /* define starts */
6981 while (*p == ' ' || *p == '\t')
6986 sname = cstring_fromChars (p);
6987 DPRINTF (("Check macro: %s", sname));
6989 while (((c = *p) != ' ')
6990 && c != '\0' && c != '('
6991 && c != '\t' && c != '\\' && c != '\n'
6997 hasParams = (c == '(');
7003 notparseable = FALSE;
7005 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7007 DPRINTF (("Clear notfunction"));
7008 notfunction = FALSE;
7023 if (usymtab_existsReal (sname))
7025 uentry ue = usymtab_lookup (sname);
7027 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7029 if (fileloc_isPreproc (uentry_whereLast (ue)))
7035 if (uentry_isSpecified (ue))
7037 checkmacro = context_getFlag (FLG_SPECMACROS);
7043 checkmacro = context_getFlag (FLG_LIBMACROS)
7044 || context_getFlag (FLG_FCNMACROS);
7052 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7054 if (fileloc_isSystemFile (g_currentloc)
7055 && context_getFlag (FLG_SYSTEMDIREXPAND))
7057 ; /* don't check this macro */
7058 DPRINTF (("Don't check 1"));
7066 DPRINTF (("Has params..."));
7068 if (context_getFlag (FLG_FCNMACROS))
7070 if (usymtab_exists (sname))
7073 ** only get here is macro is redefined
7074 ** error reported elsewhere
7077 DPRINTF (("It exists!"));
7082 ** We make it a forward function, since it might be declared elsewhere.
7083 ** After all headers have been processed, we should check the forward
7087 fileloc loc = fileloc_makePreproc (g_currentloc);
7089 /* the line is off-by-one, since the newline was already read */
7094 expectfunction = FALSE;
7097 le = uentry_makeForwardFunction (sname,
7098 typeId_invalid, loc);
7104 /* Do not define here! */
7106 (void) usymtab_addEntry (le);
7110 DPRINTF (("Check: TRUE"));
7114 DPRINTF (("Flag FCN_MACROS not set!"));
7119 DPRINTF (("No params"));
7121 if (context_getFlag (FLG_CONSTMACROS))
7123 bool nocontent = FALSE;
7136 ** Check if there is nothing after the define.
7139 while ((*rest) != '\0' && isspace (*rest))
7146 nocontent = TRUE; /* empty macro, don't check */
7151 if (usymtab_exists (sname))
7157 fileloc loc = fileloc_makePreproc (g_currentloc);
7158 DPRINTF (("Make constant: %s", sname));
7159 le = uentry_makeConstant (sname,
7160 ctype_unknown, loc);
7161 (void) usymtab_addEntry (le);
7164 checkmacro = !nocontent;
7169 if (checkmacro && usymtab_existsType (sname))
7171 DPRINTF (("Making false..."));
7173 ppllerror (message ("Specified type implemented as macro: %s", sname));
7183 if (usymtab_exists (sname))
7185 uentry ue = usymtab_lookupExpose (sname);
7186 fileloc tloc = fileloc_makePreproc (g_currentloc);
7188 uentry_setDefined (ue, tloc);
7189 fileloc_free (tloc);
7190 uentry_setUsed (ue, fileloc_undefined);
7194 fileloc tloc = fileloc_makePreproc (g_currentloc);
7195 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7196 DPRINTF (("Make expanded macro: %s", sname));
7197 DPRINTF (("Not in symbol table: %s", sname));
7199 (void) usymtab_addGlobalEntry (ue);
7200 fileloc_free (tloc);
7205 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7209 static enum cpp_token
7210 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7212 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7215 bool eliminateComment = FALSE;
7217 llassert (pbuf->buf != NULL);
7219 start = pbuf->buf + smark->position;
7221 llassert (pbuf->cur != NULL);
7222 len = pbuf->cur - start;
7225 && start[1] == context_getCommentMarkerChar ())
7229 char *scomment = start + 2;
7231 start[0] = BEFORE_COMMENT_MARKER[0];
7232 start[1] = BEFORE_COMMENT_MARKER[1];
7234 llassert (start[len - 2] == '*');
7235 start[len - 2] = AFTER_COMMENT_MARKER[0];
7237 llassert (start[len - 1] == '/');
7238 start[len - 1] = AFTER_COMMENT_MARKER[1];
7240 cppReader_reserve(pfile, size_fromInt (1 + len));
7241 cppReader_putCharQ (pfile, c);
7243 cpp_setLocation (pfile);
7245 if (mstring_equalPrefix (scomment, "ignore"))
7247 if (!context_getFlag (FLG_NOCOMMENTS))
7249 context_enterSuppressRegion ();
7252 else if (mstring_equalPrefix (scomment, "end"))
7254 if (!context_getFlag (FLG_NOCOMMENTS))
7256 context_exitSuppressRegion ();
7259 else if (mstring_equalPrefix (scomment, "notparseable"))
7261 notparseable = TRUE;
7263 eliminateComment = TRUE;
7265 else if (mstring_equalPrefix (scomment, "notfunction"))
7269 eliminateComment = TRUE;
7271 else if (mstring_equalPrefix (scomment, "iter"))
7275 else if (mstring_equalPrefix (scomment, "function"))
7277 expectfunction = TRUE;
7279 else if (mstring_equalPrefix (scomment, "constant"))
7281 expectconstant = TRUE;
7285 char sChar = *scomment;
7291 char *rest = scomment + 1;
7293 if (mstring_equalPrefix (rest, "commentchar"))
7295 eliminateComment = TRUE;
7299 ppllerror (cstring_makeLiteral
7300 ("Cannot restore commentchar"));
7304 char *next = scomment + 12; /* strlen commentchar = 12 */
7306 if (*next != ' ' && *next != '\t' && *next != '\n')
7310 ("Syntactic commentchar comment is not followed by a "
7311 "whitespace character: %c",
7316 char cchar = *(next + 1);
7321 (cstring_makeLiteral
7322 ("Cannot set commentchar to NUL"));
7326 context_setCommentMarkerChar (cchar);
7327 /* setComment = TRUE; */
7332 else if (mstring_equalPrefix (scomment, "nestcomment"))
7334 /* fix from Mike Miller <MikeM@xata.com> */
7335 context_fileSetFlag (FLG_NESTCOMMENT,
7336 ynm_fromCodeChar (sChar));
7338 else if (mstring_equalPrefix (rest, "namechecks"))
7340 context_fileSetFlag (FLG_NAMECHECKS,
7341 ynm_fromCodeChar (sChar));
7343 else if (mstring_equalPrefix (rest, "macroredef"))
7345 context_fileSetFlag (FLG_MACROREDEF,
7346 ynm_fromCodeChar (sChar));
7348 else if (mstring_equalPrefix (rest, "usevarargs"))
7350 context_fileSetFlag (FLG_USEVARARGS,
7351 ynm_fromCodeChar (sChar));
7353 else if (mstring_equalPrefix (rest, "nextlinemacros"))
7355 context_fileSetFlag (FLG_MACRONEXTLINE,
7356 ynm_fromCodeChar (sChar));
7358 else if (mstring_equalPrefix (rest, "allmacros")
7359 || mstring_equalPrefix (rest, "fcnmacros")
7360 || mstring_equalPrefix (rest, "constmacros"))
7364 if (mstring_equalPrefix (rest, "allmacros"))
7368 else if (mstring_equalPrefix (rest, "fcnmacros"))
7374 llassert (mstring_equalPrefix (rest, "constmacros"));
7375 fl = FLG_CONSTMACROS;
7379 context_fileSetFlag (fl, ynm_fromCodeChar (sChar));
7380 notfunction = FALSE;
7393 if (eliminateComment)
7398 /* Replaces comment char's in start with spaces */
7400 for (i = 2; i < len - 2; i++)
7402 if (start[i] == BEFORE_COMMENT_MARKER[0]
7403 || start[i] == BEFORE_COMMENT_MARKER[1]
7404 || start[i] == context_getCommentMarkerChar ())
7410 cppReader_putStrN (pfile, start, size_fromInt (len));
7411 parseClearMark (smark);
7421 ** Output the comment as all spaces so line/column
7422 ** in output file is still correct.
7426 cstring lintcomment = cstring_undefined;
7428 if (context_getFlag (FLG_LINTCOMMENTS))
7430 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
7432 lintcomment = cstring_makeLiteralTemp ("l_notreach");
7434 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
7436 lintcomment = cstring_makeLiteralTemp ("l_printfli");
7438 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
7440 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
7442 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
7444 lintcomment = cstring_makeLiteralTemp ("l_argsus");
7446 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
7448 lintcomment = cstring_makeLiteralTemp ("l_fallth");
7452 lintcomment = cstring_undefined;
7457 lintcomment = cstring_undefined;
7460 if (cstring_isDefined (lintcomment))
7462 c = BEFORE_COMMENT_MARKER[0];
7463 start[0] = BEFORE_COMMENT_MARKER[1];
7465 llassert (cstring_length (lintcomment) == len - 3);
7467 for (i = 1; i < len - 2; i++)
7469 start[i] = cstring_getChar (lintcomment, i);
7472 start[len - 2] = AFTER_COMMENT_MARKER[0];
7473 start[len - 1] = AFTER_COMMENT_MARKER[1];
7477 /* Replaces char's in start with spaces */
7478 for (i = 0; i < len; i++)
7482 && start[i + 1] == '*') {
7483 (void) cppoptgenerror (FLG_NESTCOMMENT,
7484 message ("Comment starts inside comment"),
7488 if (start[i] != '\n')
7495 cppReader_reserve (pfile, size_fromInt (1 + len));
7496 cppReader_putCharQ (pfile, c);
7497 cppReader_putStrN (pfile, start, size_fromInt (len));
7498 parseClearMark (smark);
7504 static int cpp_openIncludeFile (char *filename)
7506 int res = open (filename, O_RDONLY, 0666);
7508 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
7511 if (!fileTable_exists (context_fileTable (),
7512 cstring_fromChars (filename)))
7514 (void) fileTable_addHeaderFile (context_fileTable (),
7515 cstring_fromChars (filename));
7519 DPRINTF (("File already exists: %s", filename));
7526 static bool cpp_skipIncludeFile (cstring fname)
7528 if (context_isSystemDir (fname))
7530 DPRINTF (("System dir: %s", fname));
7532 if (lcllib_isSkipHeader (fname))
7534 DPRINTF (("Skip include TRUE: %s", fname));
7538 if (context_getFlag (FLG_SKIPSYSHEADERS))
7540 DPRINTF (("Skip include TRUE: %s", fname));
7545 if (context_getFlag (FLG_SINGLEINCLUDE))
7547 fname = removePreDirs (fname);
7549 # if defined (WIN32) || defined (OS2)
7550 cstring_replaceAll (fname, '\\', '/');
7553 if (fileTable_exists (context_fileTable (), fname))
7555 DPRINTF (("Skip include TRUE: %s", fname));
7560 DPRINTF (("Skip include FALSE: %s", fname));
7564 static int cpp_peekN (cppReader *pfile, int n)
7566 cppBuffer *buf = cppReader_getBuffer (pfile);
7568 llassert (buf->cur != NULL);
7570 return (buf->rlimit - buf->cur >= (n)
7575 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
7580 void cppBuffer_forward (cppBuffer *buf, int n)
7582 llassert (buf->cur != NULL);