2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
29 Contributed by Per Bothner, 1994-95.
30 Based on CCCP program by Paul Rubin, June 1986
31 Adapted to ANSI C, Richard Stallman, Jan 1987
33 This program is free software; you can redistribute it and/or modify it
34 under the terms of the GNU General Public License as published by the
35 Free Software Foundation; either version 2, or (at your option) any
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 In other words, you are welcome to use, share and improve this program.
48 You are forbidden to forbid anyone else to use, share and improve
49 what you give them. Help stamp out software-hoarding! */
53 * - OS2 drive specs like WIN32
54 * - Includes for IBMs OS/2 compiler
65 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
68 # include <sys/types.h>
69 # include <sys/stat.h>
71 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
73 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
78 ** evans 2002-07-03: exception for WATCOM 10.6 compiler suggest by Adam Clarke
80 # if !defined (USG) && !defined (__WATCOMC__)
81 # include <time.h> /* Reported by Paul Smith */
82 # include <sys/time.h>
83 # include <sys/resource.h>
85 # include <sys/times.h>
90 # endif /* not WIN32 */
92 /* This defines "errno" properly for VMS, and gives us EACCES. */
95 # include "splintMacros.nf"
99 # include "cpperror.h"
100 # include "cpphash.h"
102 # include "version.h"
107 ** This is really kludgey code...
113 /* Warnings for using sprintf - suppress them all for now... */
114 /*@-bufferoverflowhigh@*/
116 #define NO_SHORTNAMES
124 /*@constant int IMPORT_FOUND@*/
125 # define IMPORT_FOUND -2
127 /*@constant int SKIP_INCLUDE@*/
128 # define SKIP_INCLUDE IMPORT_FOUND
130 /*@constant unused int IMPORT_NOT_FOUND@*/
131 # define IMPORT_NOT_FOUND -1
134 /*@constant unused int STDC_VALUE@*/
138 /* By default, colon separates directories in a path. */
139 #ifndef PATH_SEPARATOR
140 /*@constant char PATH_SEPARATOR@*/
141 #define PATH_SEPARATOR ':'
144 static void parse_name (cppReader *, int);
146 static int cpp_openIncludeFile (char *p_filename)
147 /*@modifies fileSystem @*/ ;
149 static void cpp_setLocation (cppReader *p_pfile)
150 /*@modifies g_currentloc@*/ ;
152 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
153 struct parse_marker *p_smark)
154 /*@modifies p_pfile, p_smark@*/;
156 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
158 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
160 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
166 /* Symbols to predefine. */
168 #ifdef CPP_PREDEFINES
169 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
171 static /*@observer@*/ char *predefs = "";
174 /* We let tm.h override the types used here, to handle trivial differences
175 such as the choice of unsigned int or long unsigned int for size_t.
176 When machines start needing nontrivial differences in the size type,
177 it would be best to do something here to figure out automatically
178 from other information what type to use. */
180 /* The string value for __SIZE_TYPE__. */
183 /*@constant observer char *SIZE_TYPE@*/
184 #define SIZE_TYPE "long unsigned int"
187 /* The string value for __PTRDIFF_TYPE__. */
190 /*@constant observer char *PTRDIFF_TYPE@*/
191 #define PTRDIFF_TYPE "long int"
194 /* The string value for __WCHAR_TYPE__. */
197 /*@constant observer char *WCHAR_TYPE@*/
198 #define WCHAR_TYPE "int"
201 /* The string value for __USER_LABEL_PREFIX__ */
203 #ifndef USER_LABEL_PREFIX
204 /*@constant observer char *USER_LABEL_PREFIX@*/
205 #define USER_LABEL_PREFIX ""
208 /* The string value for __REGISTER_PREFIX__ */
210 #ifndef REGISTER_PREFIX
211 /*@constant observer char *REGISTER_PREFIX@*/
212 #define REGISTER_PREFIX ""
215 /* table to tell if char can be part of a C identifier. */
216 static bool is_idchar[256];
217 /* table to tell if char can be first char of a c identifier. */
218 static bool is_idstart[256];
219 /* table to tell if c is horizontal space. */
220 static bool is_hor_space[256];
221 /* table to tell if c is horizontal or vertical space. */
222 static bool is_space[256];
224 static /*@exposed@*/ /*@null@*/ cppBuffer *
225 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
226 /*@uses p_pfile->buffer@*/
227 /*@modifies nothing@*/ ;
230 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
233 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
235 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
237 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
238 # define cppBuffer_get(BUFFER) \
239 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
241 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
242 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
243 modifies *p_file; @*/
244 # define cppReader_puts(PFILE, STR, N) \
245 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
247 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
249 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
250 modifies *p_file; @*/
251 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
253 static void cppReader_putCharQ (cppReader *p_file, char p_ch)
255 fprintf (stderr, "put char: %c\n", p_ch);
256 (*(p_file)->limit++ = (p_ch));
259 /* Append character CH to PFILE's output buffer. Make space if need be. */
261 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
262 modifies *p_file; @*/
263 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
265 /* Make sure PFILE->limit is followed by '\0'. */
266 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
267 modifies *p_file; @*/
269 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
271 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
272 modifies *p_file; @*/
273 # define cppReader_nullTerminate(PFILE) \
274 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
276 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
277 modifies *p_file; @*/
278 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
280 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
281 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
283 /*@function static observer char *cppReader_wcharType (cppReader *)
284 modifies nothing; @*/
286 # define cppReader_wcharType(PFILE) \
287 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
289 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
291 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
292 # define cppReader_forward(pfile, N) \
293 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
295 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
296 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
298 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
299 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
301 /* Move all backslash-newline pairs out of embarrassing places.
302 Exchange all such pairs following BP
303 with any potentially-embarrassing characters that follow them.
304 Potentially-embarrassing characters are / and *
305 (because a backslash-newline inside a comment delimiter
306 would cause it not to be recognized). */
309 # define NEWLINE_FIX \
310 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
312 /* Same, but assume we've already read the potential '\\' into C. */
314 # define NEWLINE_FIX1(C) do { \
315 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
318 static void parseSetMark (/*@out@*/ struct parse_marker *,
320 static void parseClearMark (struct parse_marker *);
321 static void parseGotoMark (struct parse_marker *, cppReader *);
322 static void parseMoveMark (struct parse_marker *, cppReader *);
324 /* If we have a huge buffer, may need to cache more recent counts */
325 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
327 static /*@exposed@*/ /*@null@*/ cppBuffer *
328 cppReader_pushBuffer (cppReader *p_pfile,
329 /*@owned@*/ /*@null@*/ char *, size_t)
330 /*@modifies p_pfile@*/ ;
332 static void cppReader_appendIncludeChain
334 /*@keep@*/ struct file_name_list *p_first,
335 /*@dependent@*/ struct file_name_list *p_last);
337 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
338 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
340 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
341 /*@unused@*/ cppReader *p_pfile);
343 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
344 /*@unused@*/ cppReader *p_pfile);
346 static int cppReader_handleDirective (cppReader *p_pfile);
348 static void cppReader_scanBuffer (cppReader *p_pfile);
350 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
353 ** WIN32 (at least the VC++ include files) does not define mode_t.
356 /*@-incondefs@*/ /*@-czechtypes@*/
357 typedef unsigned int mode_t;
358 /*@=incondefs@*/ /*@=czechtypes@*/
362 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
363 /*@out@*/ size_t *p_size_pointer);
364 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
368 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
369 ** (Note that it is false while we're expanding marco *arguments*.)
372 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
374 static void path_include (cppReader *p_pfile, char *p_path)
375 /*@modifies p_pfile@*/ ;
377 static void initialize_builtins (cppReader *p_pfile)
378 /*@modifies p_pfile@*/ ;
380 static void initialize_char_syntax (struct cppOptions *p_opts) ;
382 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
384 bool p_system_header_p,
385 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
387 static void validate_else (cppReader *p_pfile, cstring p_directive);
389 static void conditional_skip (cppReader *p_pfile, int p_skip,
390 enum node_type p_type,
391 /*@dependent@*/ /*@null@*/ char *p_control_macro);
393 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
397 static void skip_if_group (cppReader *p_pfile, int p_any);
399 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
400 char *p_beg2, int p_len2, bool p_last);
403 extern void fancy_abort ();
406 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
407 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
409 static /*@observer@*/ /*@null@*/ struct file_name_map *
410 read_name_map (cppReader *p_pfile, cstring p_dirname);
412 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
414 static int open_include_file (cppReader *p_pfile,
415 /*@owned@*/ cstring p_fname,
416 /*@null@*/ struct file_name_list *p_searchptr);
418 static void push_macro_expansion (cppReader *,
419 /*@owned@*/ char *, size_t,
420 /*@dependent@*/ hashNode);
422 /* Last arg to output_line_command. */
423 enum file_change_code {
424 same_file, enter_file, leave_file
427 /* `struct directive' defines one #-directive, including how to handle it. */
430 int length; /* Length of name */
431 /*@null@*/ int (*func)(); /* Function to handle directive */
432 /*@observer@*/ cstring name; /* Name of directive */
433 enum node_type type; /* Code which describes which directive. */
434 bool command_reads_line; /* One if rest of line is read by func. */
435 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
436 bool pass_thru; /* Copy preprocessed directive to output file.*/
439 /* These functions are declared to return int instead of void since they
440 are going to be placed in a table and some old compilers have trouble with
441 pointers to functions returning void. */
443 static int do_define (cppReader *, /*@null@*/ struct directive *,
444 /*@exposed@*/ char *, char *);
445 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
446 /*@exposed@*/ char *, char *, bool);
448 static int do_line (cppReader *, /*@null@*/ struct directive *);
449 static int do_include (cppReader *, struct directive *, char *, char *);
450 static int do_undef (cppReader *, struct directive *, char *, char *);
451 static int do_error (cppReader *, struct directive *, char *, char *);
452 static int do_pragma (cppReader *, struct directive *, char *, char *);
453 static int do_ident (cppReader *, struct directive *, char *, char *);
454 static int do_if (cppReader *, struct directive *, char *, char *);
455 static int do_xifdef (cppReader *, struct directive *, char *, char *);
456 static int do_else (cppReader *, struct directive *, char *, char *);
457 static int do_elif (cppReader *, struct directive *, char *, char *);
458 static int do_endif (cppReader *, struct directive *, char *, char *);
459 static int do_warning (cppReader *, struct directive *, char *, char *);
461 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
462 via the same directory as the file that #included it. */
464 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
465 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
467 /* #include "file" looks in source file dir, then stack. */
468 /* #include <file> just looks in the stack. */
469 /* -I directories are added to the end, then the defaults are added. */
473 static struct default_include {
474 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
475 int cplusplus; /* Only look here if we're compiling C++. */
476 int cxx_aware; /* Includes in this directory don't need to
477 be wrapped in extern "C" when compiling
479 } include_defaults_array[]
481 /* This is the dir for fixincludes. Put it just before
482 the files that we fix. */
483 { GCC_INCLUDE_DIR, 0, 0 },
484 { GCC_INCLUDE_DIR2, 0, 0 },
485 { cstring_undefined, 0, 0 }
488 /*@noaccess cstring@*/
490 /* Here is the actual list of #-directives, most-often-used first.
491 The initialize_builtins function assumes #define is the very first. */
495 static struct directive directive_table[] = {
496 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
497 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
498 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
499 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
500 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
501 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
502 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
503 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
504 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
505 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
506 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
507 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
508 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
509 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
510 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
511 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
513 /*@noaccess cstring@*/
515 static cstring searchPath_unparse (struct file_name_list *search_start)
517 cstring res = cstring_newEmpty ();
518 struct file_name_list *searchptr = NULL;
520 for (searchptr = search_start; searchptr != NULL;
521 searchptr = searchptr->next)
523 if (!cstring_isEmpty (searchptr->fname)) {
524 res = cstring_concatFree1 (res, searchptr->fname);
525 if (searchptr->next != NULL) {
526 res = cstring_appendChar (res, ';');
536 initialize_char_syntax (struct cppOptions *opts)
541 * Set up is_idchar and is_idstart tables. These should be
542 * faster than saying (is_alpha (c) || c == '_'), etc.
543 * Set up these things before calling any routines tthat
547 for (i = 'a'; i <= 'z'; i++) {
548 is_idchar[i - 'a' + 'A'] = TRUE;
549 is_idchar[(int) i] = TRUE;
550 is_idstart[i - 'a' + 'A'] = TRUE;
551 is_idstart[(int) i] = TRUE;
554 for (i = '0'; i <= '9'; i++)
556 is_idchar[(int) i] = TRUE;
559 is_idchar['_'] = TRUE;
560 is_idstart['_'] = TRUE;
561 is_idchar['$'] = opts->dollars_in_ident;
562 is_idstart['$'] = opts->dollars_in_ident;
564 /* horizontal space table */
565 is_hor_space[' '] = TRUE;
566 is_hor_space['\t'] = TRUE;
567 is_hor_space['\v'] = TRUE;
568 is_hor_space['\f'] = TRUE;
569 is_hor_space['\r'] = TRUE;
571 is_space[' '] = TRUE;
572 is_space['\t'] = TRUE;
573 is_space['\v'] = TRUE;
574 is_space['\f'] = TRUE;
575 is_space['\n'] = TRUE;
576 is_space['\r'] = TRUE;
579 bool isIdentifierChar (char c)
581 return is_idchar[(int) c];
584 /* Place into P_PFILE a quoted string representing the string SRC.
585 Caller must reserve enough space in pfile->token_buffer. */
588 quote_string (cppReader *pfile, char *src)
592 cppReader_putCharQ (pfile, '\"');
595 switch ((c = *src++))
599 cppReader_putCharQ (pfile, c);
602 sprintf (cpplib_getPWritten (pfile), "\\%03o",
604 cppReader_adjustWritten (pfile, (size_t) 4);
606 /*@switchbreak@*/ break;
610 cppReader_putCharQ (pfile, '\\');
611 cppReader_putCharQ (pfile, c);
612 /*@switchbreak@*/ break;
615 cppReader_putCharQ (pfile, '\"');
616 cppReader_nullTerminateQ (pfile);
622 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
625 cppReader_growBuffer (cppReader *pfile, size_t n)
627 size_t old_written = cpplib_getWritten (pfile);
628 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
629 pfile->token_buffer = (char *)
630 drealloc (pfile->token_buffer, pfile->token_buffer_size);
631 cppReader_setWritten (pfile, old_written);
635 * process a given definition string, for initialization
636 * If STR is just an identifier, define it with value 1.
637 * If STR has anything after the identifier, then it should
638 * be identifier=definition.
642 cppReader_define (cppReader *pfile, char *str)
647 DPRINTF (("Cpp reader define: %s", str));
649 if (!is_idstart[(int) *p])
651 DPRINTF (("ERROR 1"));
652 cppReader_error (pfile,
653 message ("Malformed option `-D%s'",
654 cstring_fromChars (str)));
661 DPRINTF (("Here 2"));
663 while (is_idchar[(int) *p])
670 while (*p != ')' && *p != '\0') {
679 message ("Malformed option: -D%s (no closing parenthesis)",
680 cstring_fromChars (str)));
684 DPRINTF (("Here 2"));
688 buf = (char *) dmalloc (size_fromInt (p - str + 4));
689 strcpy ((char *) buf, str);
690 strcat ((char *) buf, " 1");
694 DPRINTF (("ERROR 2"));
695 cppReader_error (pfile,
696 message ("Malformed option: -D%s (expected '=', found '%c')",
697 cstring_fromChars (str),
704 /* Copy the entire option so we can modify it. */
705 DPRINTF (("Copying..."));
706 buf = (char *) dmalloc (2 * strlen (str) + 1);
707 strncpy (buf, str, size_fromInt (p - str));
709 /* Change the = to a space. */
711 /* Scan for any backslash-newline and remove it. */
717 if (*p == '\\' && p[1] == '\n')
723 DPRINTF (("Here we are..."));
727 llassert (buf != NULL);
728 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
729 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
733 /* Append a chain of `struct file_name_list's
734 to the end of the main include chain.
735 FIRST is gthe beginning of the chain to append, and LAST is the end. */
738 cppReader_appendIncludeChain (cppReader *pfile,
739 struct file_name_list *first,
740 struct file_name_list *last)
742 struct cppOptions *opts = CPPOPTIONS (pfile);
743 struct file_name_list *dir;
745 if (first == NULL || last == NULL)
750 if (opts->include == 0)
752 opts->include = first;
756 llassert (opts->last_include->next == NULL);
757 opts->last_include->next = first;
760 if (opts->first_bracket_include == 0)
762 opts->first_bracket_include = first;
764 for (dir = first; ; dir = dir->next) {
765 size_t len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
767 if (len > pfile->max_include_len)
769 pfile->max_include_len = len;
779 llassert (last->next == NULL);
780 /* last->next = NULL; */
781 opts->last_include = last;
785 static /*@unused@*/ void
786 cppReader_showIncludeChain (cppReader *pfile)
788 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
794 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
798 fprintf (stderr, "\n");
802 fprintf (stderr, "No includes\n");
808 cppReader_getIncludePath ()
810 cppReader *pfile = &g_cppState;
811 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
812 cstring res = cstring_undefined;
818 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
824 res = cstring_makeLiteral ("<no include path>");
831 cppReader_addIncludeChain (cppReader *pfile, /*@only@*/ struct file_name_list *dir)
833 struct cppOptions *opts = CPPOPTIONS (pfile);
840 if (opts->include == 0)
846 llassert (opts->last_include->next == NULL);
847 opts->last_include->next = dir;
850 if (opts->first_bracket_include == 0)
852 size_t len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
853 opts->first_bracket_include = dir;
855 if (len > pfile->max_include_len)
857 pfile->max_include_len = len;
862 opts->last_include = dir;
863 /* cppReader_showIncludeChain (pfile); */
866 /* Given a colon-separated list of file names PATH,
867 add all the names to the search path for include files. */
870 path_include (cppReader *pfile, char *path)
877 /* if we have a posix path list, convert to win32 path list */
878 win32temp = (char *) dmalloc /*@i4@*/
879 (cygwin32_posix_to_win32_path_list_buf_size (path));
880 cygwin32_posix_to_win32_path_list (path, win32temp);
890 struct file_name_list *dirtmp;
892 /* Find the end of this name. */
893 while (*q != '\0' && *q != PATH_SEPARATOR)
900 /* An empty name in the path stands for the current directory. */
901 name = (char *) dmalloc ((size_t) 2);
907 /* Otherwise use the directory that is named. */
908 name = (char *) dmalloc (size_fromInt (q - p + 1));
909 memcpy (name, p, size_fromInt (q - p));
913 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
914 dirtmp->next = 0; /* New one goes on the end */
915 dirtmp->control_macro = 0;
916 dirtmp->c_system_include_path = 0;
917 dirtmp->fname = cstring_fromChars (name);
918 dirtmp->got_name_map = 0;
919 cppReader_addIncludeChain (pfile, dirtmp);
921 /* Advance past this name. */
925 /* Skip the colon. */
931 cppOptions_init (cppOptions *opts)
933 memset ((char *) opts, 0, sizeof *opts);
936 opts->in_fname = NULL;
937 opts->out_fname = NULL;
939 /* Initialize is_idchar to allow $. */
940 opts->dollars_in_ident = TRUE;
942 opts->no_line_commands = 0;
943 opts->no_trigraphs = TRUE;
944 opts->put_out_comments = 1;
945 opts->print_include_names = 0;
946 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
950 opts->cplusplus_comments = 1;
955 opts->pedantic_errors = 0;
956 opts->warn_comments = 0;
957 opts->warnings_are_errors = 0;
959 initialize_char_syntax (opts);
963 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
969 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
970 /*@unused@*/ cppReader *pfile)
976 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
978 hashNode macro = pbuf->hnode;
980 if (macro->type == T_DISABLED)
982 macro->type = T_MACRO;
985 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
993 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
995 if (pbuf->buf != NULL)
1002 /* Assuming we have read '/'.
1003 If this is the start of a comment (followed by '*' or '/'),
1004 skip to the end of the comment, and return ' '.
1005 Return EOF if we reached the end of file before the end of the comment.
1006 If not the start of a comment, return '/'. */
1009 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1013 llassert (pfile->buffer != NULL);
1014 llassert (pfile->buffer->cur != NULL);
1016 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1023 cppReader_forward (pfile, 2);
1026 if (cppReader_peekC (pfile) == '*')
1028 cppReader_forward (pfile, 1);
1033 c = cppReader_getC (pfile);
1040 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1047 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1050 if (prev_c == (int) '*' && c == (int) '/')
1055 if (c == (int) '\n' && (linep != NULL))
1061 else if (cppReader_peekC (pfile) == '/'
1062 && CPPOPTIONS (pfile)->cplusplus_comments)
1065 (void) cppoptgenerror
1066 (FLG_SLASHSLASHCOMMENT,
1067 message ("C++ style // comment"
1071 cppReader_forward (pfile, 1);
1075 c = cppReader_getC (pfile);
1079 /* Allow hash comment to be terminated by EOF. */
1083 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1085 cppReader_forward (pfile, 1);
1086 c = cppReader_getC (pfile);
1094 if (c == (int) '\n')
1096 /* Don't consider final '\n' to be part of comment. */
1097 cppReader_forward (pfile, -1);
1108 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1110 cppSkipHspace (cppReader *pfile)
1118 llassert (pfile->buffer != NULL);
1120 c = cppReader_peekC (pfile);
1124 return 0; /* FIXME */
1127 if (is_hor_space[c])
1129 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1130 cppReader_pedwarn (pfile,
1131 message ("%s in preprocessing directive",
1133 ? cstring_makeLiteralTemp ("formfeed")
1134 : cstring_makeLiteralTemp ("vertical tab")));
1137 cppReader_forward (pfile, 1);
1141 cppReader_forward (pfile, 1);
1142 c = skip_comment (pfile, NULL);
1146 cppReader_forward (pfile, -1);
1149 if (c == EOF || c == '/')
1154 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1156 cppReader_forward (pfile, 2);
1158 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1159 && is_hor_space [cpp_peekN (pfile, 1)])
1161 cppReader_forward (pfile, 2);
1170 /* Read the rest of the current line.
1171 The line is appended to PFILE's output buffer. */
1174 copy_rest_of_line (cppReader *pfile)
1176 struct cppOptions *opts = CPPOPTIONS (pfile);
1183 llassert (pfile->buffer != NULL);
1185 c = cppReader_getC (pfile);
1193 ** Patch from Brian St. Pierre for handling MS-DOS files.
1196 DPRINTF (("Reading directive: %d", (int) c));
1198 if (cppReader_peekC (pfile) == '\n'
1199 || cppReader_peekC (pfile) == '\r')
1201 DPRINTF (("Reading directive..."));
1202 if (cppReader_peekC (pfile) == '\r')
1204 DPRINTF (("Reading directive..."));
1205 cppReader_forward (pfile, 1);
1208 DPRINTF (("Reading directive..."));
1209 cppReader_forward (pfile, 1);
1213 DPRINTF (("Falling..."));
1214 /*@fallthrough@*/ case '\'': case '\"':
1215 goto scan_directive_token;
1218 nextc = cppReader_peekC (pfile);
1221 ** was (opts->cplusplus_comments && nextc == '*')
1226 || (opts->cplusplus_comments && nextc == '/'))
1228 goto scan_directive_token;
1230 /*@switchbreak@*/ break;
1233 if (cppReader_isPedantic (pfile))
1234 cppReader_pedwarn (pfile,
1235 message ("%s in preprocessing directive",
1237 ? cstring_makeLiteralTemp ("formfeed")
1238 : cstring_makeLiteralTemp ("vertical tab")));
1239 /*@switchbreak@*/ break;
1242 cppReader_forward (pfile, -1);
1244 scan_directive_token:
1245 cppReader_forward (pfile, -1);
1246 (void) cpplib_getToken (pfile);
1249 cppReader_putChar (pfile, c);
1252 cppReader_nullTerminate (pfile);
1256 cppReader_skipRestOfLine (cppReader *pfile)
1258 size_t old = cpplib_getWritten (pfile);
1259 copy_rest_of_line (pfile);
1260 cppReader_setWritten (pfile, old);
1263 /* Handle a possible # directive.
1264 '#' has already been read. */
1267 cppReader_handleDirective (cppReader *pfile)
1270 struct directive *kt = NULL;
1272 size_t after_ident = 0;
1274 char *line_end = NULL;
1275 size_t old_written = cpplib_getWritten (pfile);
1276 int nspaces = cppSkipHspace (pfile);
1278 c = cppReader_peekC (pfile);
1280 if (c >= '0' && c <= '9')
1282 /* Handle # followed by a line number. */
1283 if (cppReader_isPedantic (pfile))
1285 cppReader_pedwarnLit
1287 cstring_makeLiteralTemp ("`#' followed by integer"));
1290 (void) do_line (pfile, NULL);
1291 goto done_a_directive;
1295 /* Now find the directive name. */
1297 cppReader_putChar (pfile, '#');
1299 parse_name (pfile, cppReader_getC (pfile));
1301 llassert (pfile->token_buffer != NULL);
1302 ident = pfile->token_buffer + old_written + 1;
1304 ident_length = cpplib_getPWritten (pfile) - ident;
1306 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1308 /* A line of just `#' becomes blank. */
1312 for (kt = directive_table; ; kt++)
1314 if (kt->length <= 0)
1316 return 0; /* goto not_a_directive; */
1319 if (kt->length == ident_length
1320 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1326 if (kt->command_reads_line)
1332 /* Nonzero means do not delete comments within the directive.
1333 #define needs this when -traditional. */
1334 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1335 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1336 CPPOPTIONS (pfile)->put_out_comments = comments;
1337 after_ident = cpplib_getWritten (pfile);
1338 copy_rest_of_line (pfile);
1339 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1343 /* For #pragma and #define, we may want to pass through the directive.
1344 Other directives may create output, but we don't want the directive
1345 itself out, so we pop it now. For example #include may write a #line
1346 command (see comment in do_include), and conditionals may emit
1347 #failed ... #endfailed stuff. But note that popping the buffer
1348 means the parameters to kt->func may point after pfile->limit
1349 so these parameters are invalid as soon as something gets appended
1350 to the token_buffer. */
1352 line_end = cpplib_getPWritten (pfile);
1355 if (!kt->pass_thru && kt->type != T_DEFINE)
1357 cppReader_setWritten (pfile, old_written);
1360 llassert (pfile->token_buffer != NULL);
1362 /* was kt->pass_thru || */
1364 if (kt->type == T_DEFINE
1365 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1367 char *p = pfile->token_buffer + old_written;
1370 ** Still need to record value for preprocessing, so
1371 ** #ifdef's, etc. using the value behave correctly.
1374 (void) do_defineAux (pfile, kt,
1375 pfile->token_buffer + after_ident,
1384 SKIP_WHITE_SPACE (p);
1386 llassert (*p == 'd');
1389 llassert (*p == 'e');
1392 llassert (*p == 'f');
1395 llassert (*p == 'i');
1398 llassert (*p == 'n');
1401 llassert (*p == 'e');
1404 ** This is way-bogus. We use the last char to record the number of
1405 ** spaces. Its too hard to get them back into the input stream.
1408 if (nspaces > 9) nspaces = 9;
1410 *p++ = '0' + nspaces;
1412 return 0; /* not_a_directive */
1414 else if (kt->pass_thru)
1416 /* Just leave the entire #define in the output stack. */
1417 return 0; /* not_a_directive */
1420 else if (kt->type == T_DEFINE
1421 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1423 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1424 SKIP_WHITE_SPACE (p);
1426 while (is_idchar[(int) *p])
1432 cppReader_putChar (pfile, '\n');
1434 else if (kt->type == T_DEFINE)
1436 cppReader_setWritten (pfile, old_written);
1447 llassert (kt->func != NULL);
1448 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1453 /* Pass a directive through to the output file.
1454 BUF points to the contents of the directive, as a contiguous string.
1455 LIMIT points to the first character past the end of the directive.
1456 KEYWORD is the keyword-table entry for the directive. */
1459 pass_thru_directive (char *buf, char *limit,
1461 struct directive *keyword)
1463 int keyword_length = keyword->length;
1465 cpplib_reserve (pfile,
1466 size_fromInt (2 + keyword_length + (limit - buf)));
1467 cppReader_putCharQ (pfile, '#');
1468 /*@-observertrans@*/
1469 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1470 size_fromInt (keyword_length));
1471 /*:=observertrans@*/
1473 if (limit != buf && buf[0] != ' ')
1475 /* Was a bug, since reserve only used 1 + ... */
1476 cppReader_putCharQ (pfile, ' ');
1479 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1482 /* Read a replacement list for a macro with parameters.
1483 Build the DEFINITION structure.
1484 Reads characters of text starting at BUF until END.
1485 ARGLIST specifies the formal parameters to look for
1486 in the text of the definition; NARGS is the number of args
1487 in that list, or -1 for a macro name that wants no argument list.
1488 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1489 and NAMELEN is its length in characters.
1491 Note that comments, backslash-newlines, and leading white space
1492 have already been deleted from the argument. */
1495 collect_expansion (cppReader *pfile, char *buf, char *limit,
1496 int nargs, /*@null@*/ struct arglist *arglist)
1499 char *p, *lastp, *exp_p;
1500 struct reflist *endpat = NULL;
1501 /* Pointer to first nonspace after last ## seen. */
1503 /* Pointer to first nonspace after last single-# seen. */
1504 char *stringify = 0;
1506 char expected_delimiter = '\0';
1509 /* Scan thru the replacement list, ignoring comments and quoted
1510 strings, picking up on the macro calls. It does a linear search
1511 thru the arg list on every potential symbol. Profiling might say
1512 that something smarter should happen. */
1517 /* Find the beginning of the trailing whitespace. */
1520 while (p < limit && is_space[(int) limit[-1]])
1525 /* Allocate space for the text in the macro definition.
1526 Leading and trailing whitespace chars need 2 bytes each.
1527 Each other input char may or may not need 1 byte,
1528 so this is an upper bound. The extra 5 are for invented
1529 leading and trailing newline-marker and final null. */
1530 maxsize = (sizeof (*defn) + (limit - p) + 5);
1532 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1541 defn = (DEFINITION *) dmalloc (maxsize);
1542 defn->noExpand = FALSE;
1544 defn->pattern = NULL;
1545 defn->nargs = nargs;
1546 defn->predefined = NULL;
1548 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1551 defn->rest_args = NULL;
1552 defn->args.argnames = NULL;
1558 /* Add one initial space escape-marker to prevent accidental
1559 token-pasting (often removed by cpplib_macroExpand). */
1563 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1564 cppReader_errorLit (pfile,
1565 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1569 /* Process the main body of the definition. */
1571 int skipped_arg = 0;
1572 register char c = *p++;
1576 if (!cppReader_isTraditional (pfile)) {
1580 if (expected_delimiter != '\0')
1582 if (c == expected_delimiter)
1583 expected_delimiter = '\0';
1587 expected_delimiter = c;
1589 /*@switchbreak@*/ break;
1592 if (p < limit && (expected_delimiter != '\0'))
1594 /* In a string, backslash goes through
1595 and makes next char ordinary. */
1598 /*@switchbreak@*/ break;
1601 /* An '@' in a string or character constant stands for itself,
1602 and does not need to be escaped. */
1603 if (expected_delimiter == '\0')
1608 /*@switchbreak@*/ break;
1611 /* # is ordinary inside a string. */
1612 if (expected_delimiter != '\0')
1614 /*@switchbreak@*/ break;
1617 if (p < limit && *p == '#') {
1618 /* ##: concatenate preceding and following tokens. */
1619 /* Take out the first #, discard preceding whitespace. */
1623 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1629 /* Skip the second #. */
1631 /* Discard following whitespace. */
1632 SKIP_WHITE_SPACE (p);
1636 cppReader_errorLit (pfile,
1637 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1639 } else if (nargs >= 0) {
1640 /* Single #: stringify following argument ref.
1641 Don't leave the # in the expansion. */
1643 SKIP_WHITE_SPACE (p);
1644 if (p == limit || ! is_idstart[(int) *p]
1645 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1646 cppReader_errorLit (pfile,
1647 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1654 /*@switchbreak@*/ break;
1657 /* In -traditional mode, recognize arguments inside strings and
1658 and character constants, and ignore special properties of #.
1659 Arguments inside strings are considered "stringified", but no
1660 extra quote marks are supplied. */
1664 if (expected_delimiter != '\0') {
1665 if (c == expected_delimiter)
1666 expected_delimiter = '\0';
1668 expected_delimiter = c;
1669 /*@switchbreak@*/ break;
1672 /* Backslash quotes delimiters and itself, but not macro args. */
1673 if (expected_delimiter != '\0' && p < limit
1674 && (*p == expected_delimiter || *p == '\\')) {
1678 /*@switchbreak@*/ break;
1681 if (expected_delimiter != '\0') /* No comments inside strings. */
1682 /*@switchbreak@*/ break;
1684 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1685 this must be -traditional. So replace the comment with
1689 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1694 /*@switchbreak@*/ break;
1698 /* Handle the start of a symbol. */
1699 if (is_idchar[(int) c] && nargs > 0) {
1700 char *id_beg = p - 1;
1704 while (p != limit && is_idchar[(int) *p])
1709 id_len = size_fromInt (p - id_beg);
1711 if (is_idstart[(int) c]
1712 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1713 register struct arglist *arg;
1715 for (arg = arglist; arg != NULL; arg = arg->next) {
1716 struct reflist *tpat;
1718 if (arg->name[0] == c
1719 && arg->length == id_len
1720 && strncmp (arg->name, id_beg, id_len) == 0) {
1723 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1724 if (cppReader_isTraditional (pfile)) {
1725 cppReader_warning (pfile,
1726 message ("macro argument `%x' is stringified.",
1727 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1729 cppReader_warning (pfile,
1730 message ("macro arg `%x' would be stringified with -traditional.",
1731 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1734 /* If ANSI, don't actually substitute inside a string. */
1735 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1736 /*@innerbreak@*/ break;
1737 /* make a pat node for this arg and append it to the end of
1739 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1741 tpat->raw_before = (concat == id_beg);
1742 tpat->raw_after = 0;
1743 tpat->rest_args = arg->rest_args;
1744 tpat->stringify = (cppReader_isTraditional (pfile)
1745 ? expected_delimiter != '\0'
1746 : stringify == id_beg);
1750 defn->pattern = tpat;
1754 endpat->next = tpat;
1756 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1760 tpat->argno = arg->argno;
1761 tpat->nchars = exp_p - lastp;
1765 SKIP_WHITE_SPACE (p1);
1767 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1769 tpat->raw_after = 1;
1772 lastp = exp_p; /* place to start copying from next time */
1775 /*@innerbreak@*/ break;
1780 /* If this was not a macro arg, copy it into the expansion. */
1781 if (skipped_arg == 0) {
1782 register char *lim1 = p;
1790 if (stringify == id_beg)
1791 cppReader_errorLit (pfile,
1792 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1797 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1799 /* If ANSI, put in a "@ " marker to prevent token pasting.
1800 But not if "inside a string" (which in ANSI mode
1801 happens only for -D option). */
1808 defn->length = size_fromInt (exp_p - defn->expansion);
1810 /* Crash now if we overrun the allocated size. */
1811 if (defn->length + 1 > maxsize)
1813 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1821 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1825 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1826 int nargs, /*@null@*/ struct arglist *arglist)
1829 char *p, *lastp, *exp_p;
1830 struct reflist *endpat = NULL;
1831 /* Pointer to first nonspace after last ## seen. */
1833 /* Pointer to first nonspace after last single-# seen. */
1834 char *stringify = 0;
1836 char expected_delimiter = '\0';
1839 /* Scan thru the replacement list, ignoring comments and quoted
1840 strings, picking up on the macro calls. It does a linear search
1841 thru the arg list on every potential symbol. Profiling might say
1842 that something smarter should happen. */
1846 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1847 fileloc_unparse (loc)));
1850 /* Find the beginning of the trailing whitespace. */
1853 while (p < limit && is_space[(int) limit[-1]])
1858 /* Allocate space for the text in the macro definition.
1859 Leading and trailing whitespace chars need 2 bytes each.
1860 Each other input char may or may not need 1 byte,
1861 so this is an upper bound. The extra 5 are for invented
1862 leading and trailing newline-marker and final null. */
1863 maxsize = (sizeof (*defn) + (limit - p) + 5);
1865 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1874 defn = (DEFINITION *) dmalloc (maxsize);
1875 defn->noExpand = FALSE;
1877 defn->pattern = NULL;
1878 defn->nargs = nargs;
1879 defn->predefined = NULL;
1881 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1884 defn->rest_args = NULL;
1885 defn->args.argnames = NULL;
1891 /* Add one initial space escape-marker to prevent accidental
1892 token-pasting (often removed by cpplib_macroExpand). */
1896 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1897 voptgenerror (FLG_PREPROC,
1898 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1903 /* Process the main body of the definition. */
1905 int skipped_arg = 0;
1906 register char c = *p++;
1910 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1914 if (expected_delimiter != '\0')
1916 if (c == expected_delimiter)
1917 expected_delimiter = '\0';
1921 expected_delimiter = c;
1923 /*@switchbreak@*/ break;
1926 if (p < limit && (expected_delimiter != '\0'))
1928 /* In a string, backslash goes through
1929 and makes next char ordinary. */
1932 /*@switchbreak@*/ break;
1935 /* An '@' in a string or character constant stands for itself,
1936 and does not need to be escaped. */
1937 if (expected_delimiter == '\0')
1942 /*@switchbreak@*/ break;
1945 /* # is ordinary inside a string. */
1946 if (expected_delimiter != '\0')
1948 /*@switchbreak@*/ break;
1951 if (p < limit && *p == '#') {
1952 /* ##: concatenate preceding and following tokens. */
1953 /* Take out the first #, discard preceding whitespace. */
1957 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1963 /* Skip the second #. */
1965 /* Discard following whitespace. */
1966 SKIP_WHITE_SPACE (p);
1970 voptgenerror (FLG_PREPROC,
1971 cstring_makeLiteral ("`##' at end of macro definition"),
1974 } else if (nargs >= 0) {
1975 /* Single #: stringify following argument ref.
1976 Don't leave the # in the expansion. */
1978 SKIP_WHITE_SPACE (p);
1979 if (p == limit || ! is_idstart[(int) *p]
1980 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1984 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1993 /*@switchbreak@*/ break;
1996 /* In -traditional mode, recognize arguments inside strings and
1997 and character constants, and ignore special properties of #.
1998 Arguments inside strings are considered "stringified", but no
1999 extra quote marks are supplied. */
2003 if (expected_delimiter != '\0') {
2004 if (c == expected_delimiter)
2005 expected_delimiter = '\0';
2007 expected_delimiter = c;
2008 /*@switchbreak@*/ break;
2011 /* Backslash quotes delimiters and itself, but not macro args. */
2012 if (expected_delimiter != '\0' && p < limit
2013 && (*p == expected_delimiter || *p == '\\')) {
2017 /*@switchbreak@*/ break;
2020 if (expected_delimiter != '\0') /* No comments inside strings. */
2021 /*@switchbreak@*/ break;
2023 /* If we find a comment that wasn't removed by cppReader_handleDirective,
2024 this must be -traditional. So replace the comment with
2028 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2033 /*@switchbreak@*/ break;
2037 /* Handle the start of a symbol. */
2038 if (is_idchar[(int) c] && nargs > 0) {
2039 char *id_beg = p - 1;
2043 while (p != limit && is_idchar[(int) *p])
2048 id_len = size_fromInt (p - id_beg);
2050 if (is_idstart[(int) c]
2051 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2052 register struct arglist *arg;
2054 for (arg = arglist; arg != NULL; arg = arg->next) {
2055 struct reflist *tpat;
2057 if (arg->name[0] == c
2058 && arg->length == id_len
2059 && strncmp (arg->name, id_beg, id_len) == 0) {
2062 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2063 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2064 voptgenerror (FLG_PREPROC,
2065 message ("macro argument `%x' is stringified.",
2066 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2070 voptgenerror (FLG_PREPROC,
2071 message ("Macro arg `%x' would be stringified with -traditional.",
2072 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2077 /* If ANSI, don't actually substitute inside a string. */
2078 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2079 /*@innerbreak@*/ break;
2080 /* make a pat node for this arg and append it to the end of
2082 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2084 tpat->raw_before = (concat == id_beg);
2085 tpat->raw_after = 0;
2086 tpat->rest_args = arg->rest_args;
2087 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2088 ? expected_delimiter != '\0'
2089 : stringify == id_beg);
2093 defn->pattern = tpat;
2097 endpat->next = tpat;
2099 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2103 tpat->argno = arg->argno;
2104 tpat->nchars = exp_p - lastp;
2108 SKIP_WHITE_SPACE (p1);
2110 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2112 tpat->raw_after = 1;
2115 lastp = exp_p; /* place to start copying from next time */
2118 /*@innerbreak@*/ break;
2123 /* If this was not a macro arg, copy it into the expansion. */
2124 if (skipped_arg == 0) {
2125 register char *lim1 = p;
2133 if (stringify == id_beg)
2137 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2144 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2146 /* If ANSI, put in a "@ " marker to prevent token pasting.
2147 But not if "inside a string" (which in ANSI mode
2148 happens only for -D option). */
2155 defn->length = size_fromInt (exp_p - defn->expansion);
2157 /* Crash now if we overrun the allocated size. */
2158 if (defn->length + 1 > maxsize)
2160 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2167 * special extension string that can be added to the last macro argument to
2168 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2169 * #define wow(a, b...) process (b, a, b)
2170 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2171 * { wow (one, two); } -> { process (two, one, two); }
2172 * if this "rest_arg" is used with the concat token '##' and if it is not
2173 * supplied then the token attached to with ## will not be outputted. Ex:
2174 * #define wow (a, b...) process (b ## , a, ## b)
2175 * { wow (1, 2); } -> { process (2, 1, 2); }
2176 * { wow (one); } -> { process (one); {
2179 /*@-readonlytrans@*/
2180 static char rest_extension[] = "...";
2181 /*:=readonlytrans@*/
2184 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2186 /* Create a DEFINITION node from a #define directive. Arguments are
2187 as for do_define. */
2190 static /*@null@*/ macroDef
2191 create_definition (/*@exposed@*/ char *buf, char *limit,
2192 cppReader *pfile, bool predefinition,
2195 char *bp; /* temp ptr into input buffer */
2196 char *symname; /* remember where symbol name starts */
2197 size_t sym_length; /* and how long it is */
2198 int rest_args = 0; /* really int! */
2201 cstring file = (CPPBUFFER (pfile) != NULL)
2202 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2204 int arglengths = 0; /* Accumulate lengths of arg names
2205 plus number of args. */
2209 DPRINTF (("Create definition: %s", buf));
2212 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2216 while (is_hor_space[(int) *bp])
2221 symname = bp; /* remember where it starts */
2223 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2227 /* Lossage will occur if identifiers or control keywords are broken
2228 across lines using backslash. This is not the right place to take
2232 struct arglist *arg_ptrs = NULL;
2235 bp++; /* skip '(' */
2236 SKIP_WHITE_SPACE (bp);
2238 /* Loop over macro argument names. */
2241 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2243 temp->next = arg_ptrs;
2244 temp->argno = argno++;
2245 temp->rest_args = 0;
2251 cppReader_pedwarn (pfile,
2252 message ("another parameter follows `%s'",
2253 cstring_fromChars (rest_extension)));
2256 if (!is_idstart[(int) *bp])
2258 cppReader_pedwarnLit (pfile,
2259 cstring_makeLiteralTemp ("Invalid character in macro parameter name"));
2262 /* Find the end of the arg name. */
2263 while (is_idchar[(int) *bp])
2266 /* do we have a "special" rest-args extension here? */
2267 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2268 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2271 temp->rest_args = 1;
2272 /*@innerbreak@*/ break;
2276 temp->length = size_fromInt (bp - temp->name);
2280 bp += REST_EXTENSION_LENGTH;
2283 arglengths += temp->length + 2;
2284 SKIP_WHITE_SPACE (bp);
2286 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2287 cppReader_errorLit (pfile,
2288 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2294 SKIP_WHITE_SPACE (bp);
2297 cppReader_errorLit (pfile,
2298 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2302 struct arglist *otemp;
2304 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2306 if (temp->length == otemp->length &&
2307 strncmp (temp->name, otemp->name, temp->length) == 0) {
2308 cstring name = cstring_copyLength (temp->name, temp->length);
2309 cppReader_error (pfile,
2310 message ("duplicate argument name `%x' in `#define'", name));
2317 ++bp; /* skip paren */
2318 SKIP_WHITE_SPACE (bp);
2319 /* now everything from bp before limit is the definition. */
2320 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2321 defn->rest_args = rest_args;
2323 /* Now set defn->args.argnames to the result of concatenating
2324 the argument names in reverse order
2325 with comma-space between them. */
2326 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2329 struct arglist *temp;
2331 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2333 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2335 if (temp->next != 0)
2337 defn->args.argnames[i++] = ',';
2338 defn->args.argnames[i++] = ' ';
2342 defn->args.argnames[i] = '\0';
2347 /* Simple expansion or empty definition. */
2351 if (is_hor_space[(int) *bp]) {
2353 SKIP_WHITE_SPACE (bp);
2356 case '!': case '\"': case '#': case '%': case '&': case '\'':
2357 case ')': case '*': case '+': case ',': case '-': case '.':
2358 case '/': case ':': case ';': case '<': case '=': case '>':
2359 case '?': case '[': case '\\': case ']': case '^': case '{':
2360 case '|': case '}': case '~':
2361 cppReader_warning (pfile,
2362 message ("Missing white space after #define %x",
2363 cstring_prefix (cstring_fromChars (symname),
2368 cppReader_pedwarn (pfile,
2369 message ("Missing white space after #define %x",
2370 cstring_prefix (cstring_fromChars (symname),
2376 /* now everything from bp before limit is the definition. */
2377 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2378 defn->args.argnames = mstring_createEmpty ();
2381 defn->noExpand = noExpand;
2382 DPRINTF (("No expand: %d", noExpand));
2386 /* not: llassert (cstring_isUndefined (defn->file)); */
2389 /* OP is null if this is a predefinition */
2390 defn->predefined = predefinition;
2392 mdef.symnam = symname;
2393 mdef.symlen = sym_length;
2404 cpplib_createDefinition (cstring def,
2409 char *buf = cstring_toCharsSafe (def);
2410 char *limit = buf + cstring_length (def);
2411 char *bp; /* temp ptr into input buffer */
2412 char *symname; /* remember where symbol name starts */
2413 size_t sym_length; /* and how long it is */
2414 int rest_args = 0; /* really int! */
2415 int line = fileloc_lineno (loc);
2416 cstring file = fileloc_filename (loc);
2418 int arglengths = 0; /* Accumulate lengths of arg names
2419 plus number of args. */
2424 DPRINTF (("Creating definition: %s", buf));
2426 while (is_hor_space[(int) *bp])
2431 symname = bp; /* remember where it starts */
2433 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2435 DPRINTF (("length: %d", sym_length));
2439 DPRINTF (("Here: %s", bp));
2441 /* Lossage will occur if identifiers or control keywords are broken
2442 across lines using backslash. This is not the right place to take
2446 struct arglist *arg_ptrs = NULL;
2449 bp++; /* skip '(' */
2450 SKIP_WHITE_SPACE (bp);
2452 /* Loop over macro argument names. */
2455 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2457 temp->next = arg_ptrs;
2458 temp->argno = argno++;
2459 temp->rest_args = 0;
2465 voptgenerror (FLG_PREPROC,
2466 message ("Another parameter follows %s",
2467 cstring_fromChars (rest_extension)),
2471 if (!is_idstart[(int) *bp])
2473 voptgenerror (FLG_PREPROC,
2474 message ("Invalid character in macro parameter name: %c", *bp),
2478 /* Find the end of the arg name. */
2479 while (is_idchar[(int) *bp])
2482 /* do we have a "special" rest-args extension here? */
2483 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2484 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2487 temp->rest_args = 1;
2488 /*@innerbreak@*/ break;
2492 temp->length = size_fromInt (bp - temp->name);
2496 bp += REST_EXTENSION_LENGTH;
2499 arglengths += temp->length + 2;
2500 SKIP_WHITE_SPACE (bp);
2502 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2503 voptgenerror (FLG_PREPROC,
2504 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2511 SKIP_WHITE_SPACE (bp);
2514 voptgenerror (FLG_PREPROC,
2515 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2520 struct arglist *otemp;
2522 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2524 if (temp->length == otemp->length &&
2525 strncmp (temp->name, otemp->name, temp->length) == 0) {
2526 cstring name = cstring_copyLength (temp->name, temp->length);
2528 voptgenerror (FLG_PREPROC,
2529 message ("Duplicate argument name in #define: %s", name),
2537 ++bp; /* skip paren */
2538 SKIP_WHITE_SPACE (bp);
2539 /* now everything from bp before limit is the definition. */
2540 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2541 defn->rest_args = rest_args;
2543 /* Now set defn->args.argnames to the result of concatenating
2544 the argument names in reverse order
2545 with comma-space between them. */
2546 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2549 struct arglist *temp;
2551 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2552 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2554 if (temp->next != 0) {
2555 defn->args.argnames[i++] = ',';
2556 defn->args.argnames[i++] = ' ';
2560 defn->args.argnames[i] = '\0';
2565 /* Simple expansion or empty definition. */
2569 if (is_hor_space[(int) *bp]) {
2571 SKIP_WHITE_SPACE (bp);
2574 case '!': case '\"': case '#': case '%': case '&': case '\'':
2575 case ')': case '*': case '+': case ',': case '-': case '.':
2576 case '/': case ':': case ';': case '<': case '=': case '>':
2577 case '?': case '[': case '\\': case ']': case '^': case '{':
2578 case '|': case '}': case '~':
2579 voptgenerror (FLG_PREPROC,
2580 message ("Missing white space after #define %x",
2581 cstring_prefix (cstring_fromChars (symname),
2587 voptgenerror (FLG_PREPROC,
2588 message ("Missing white space after #define %x",
2589 cstring_prefix (cstring_fromChars (symname),
2597 /* now everything from bp before limit is the definition. */
2598 llassert (limit > bp);
2599 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2600 defn->args.argnames = mstring_createEmpty ();
2603 defn->noExpand = noExpand;
2604 DPRINTF (("No expand: %d", noExpand));
2608 /* not: llassert (cstring_isUndefined (defn->file)); */
2611 /* OP is null if this is a predefinition */
2612 defn->predefined = predefinition;
2615 mdef.symnam = symname;
2616 mdef.symlen = sym_length;
2626 /* Check a purported macro name SYMNAME, and yield its length.
2627 USAGE is the kind of name this is intended for. */
2629 size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2634 for (p = symname; is_idchar[(int) *p]; p++)
2639 sym_length = size_fromInt (p - symname);
2642 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2644 cppReader_error (pfile, message ("invalid %s name", usage));
2646 else if (!is_idstart[(int) *symname])
2648 char *msg = (char *) dmalloc (sym_length + 1);
2649 memcpy (msg, symname, sym_length);
2650 msg[sym_length] = '\0';
2651 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2652 cstring_fromChars (msg)));
2657 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2659 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2668 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2671 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2676 for (p = symname; is_idchar[(int) *p]; p++)
2681 sym_length = size_fromInt (p - symname);
2684 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2686 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2687 cstring_fromChars (symname)), loc);
2689 else if (!is_idstart[(int) *symname])
2691 char *msg = (char *) dmalloc (sym_length + 1);
2692 memcpy (msg, symname, sym_length);
2693 msg[sym_length] = '\0';
2694 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2695 cstring_fromChars (msg)),
2701 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2703 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2710 /* Return zero if two DEFINITIONs are isomorphic. */
2713 compare_defs (DEFINITION *d1, DEFINITION *d2)
2715 register struct reflist *a1, *a2;
2716 register char *p1 = d1->expansion;
2717 register char *p2 = d2->expansion;
2720 if (d1->nargs != d2->nargs)
2725 llassert (d1->args.argnames != NULL);
2726 llassert (d2->args.argnames != NULL);
2728 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2733 for (a1 = d1->pattern, a2 = d2->pattern;
2734 (a1 != NULL) && (a2 != NULL);
2735 a1 = a1->next, a2 = a2->next) {
2736 if (!((a1->nchars == a2->nchars
2737 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2738 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2739 || a1->argno != a2->argno
2740 || a1->stringify != a2->stringify
2741 || a1->raw_before != a2->raw_before
2742 || a1->raw_after != a2->raw_after)
2751 if (comp_def_part (first, p1, size_toInt (d1->length - (p1 - d1->expansion)),
2752 p2, size_toInt (d2->length - (p2 - d2->expansion)), 1))
2758 /* Return TRUE if two parts of two macro definitions are effectively different.
2759 One of the parts starts at BEG1 and has LEN1 chars;
2760 the other has LEN2 chars at BEG2.
2761 Any sequence of whitespace matches any other sequence of whitespace.
2762 FIRST means these parts are the first of a macro definition;
2763 so ignore leading whitespace entirely.
2764 LAST means these parts are the last of a macro definition;
2765 so ignore trailing whitespace entirely. */
2768 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2770 char *end1 = beg1 + len1;
2771 char *end2 = beg2 + len2;
2774 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2775 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2778 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2779 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2781 while (beg1 != end1 && beg2 != end2) {
2782 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2783 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2784 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2785 } else if (*beg1 == *beg2) {
2789 return (beg1 != end1) || (beg2 != end2);
2792 /* Process a #define command.
2793 BUF points to the contents of the #define command, as a contiguous string.
2794 LIMIT points to the first character past the end of the definition.
2795 KEYWORD is the keyword-table entry for #define,
2796 or NULL for a "predefined" macro. */
2799 do_defineAux (cppReader *pfile, struct directive *keyword,
2800 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2806 DPRINTF (("Define aux: %d", noExpand));
2808 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2813 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2815 DPRINTF (("Macro: %s / %s",
2816 cstring_copyLength (mdef.symnam, mdef.symlen),
2817 bool_unparse (noExpand)));
2819 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2823 /* Redefining a precompiled key is ok. */
2824 if (hp->type == T_PCSTRING)
2826 /* Redefining a macro is ok if the definitions are the same. */
2827 else if (hp->type == T_MACRO)
2828 ok = !compare_defs (mdef.defn, hp->value.defn);
2829 /* Redefining a constant is ok with -D. */
2830 else if (hp->type == T_CONST)
2831 ok = !CPPOPTIONS (pfile)->done_initializing;
2836 /* Print the warning if it's not ok. */
2840 ** If we are passing through #define and #undef directives, do
2841 ** that for this re-definition now.
2844 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2846 /* llassert (keyword != NULL); */
2847 pass_thru_directive (buf, limit, pfile, keyword);
2850 cpp_setLocation (pfile);
2852 if (hp->type == T_MACRO)
2854 if (hp->value.defn->noExpand)
2856 ; /* error will be reported checking macros */
2862 message ("Macro %q already defined",
2863 cstring_copyLength (mdef.symnam, mdef.symlen)),
2864 message ("%q: Previous definition of %q",
2865 fileloc_unparseRaw (hp->value.defn->file,
2866 (int) hp->value.defn->line),
2867 cstring_copyLength (mdef.symnam, mdef.symlen)));
2872 genppllerror (FLG_MACROREDEF,
2873 message ("Macro %q already defined",
2874 cstring_copyLength (mdef.symnam,
2880 /* Replace the old definition. */
2882 hp->value.defn = mdef.defn;
2887 ** If we are passing through #define and #undef directives, do
2888 ** that for this new definition now.
2893 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2895 pass_thru_directive (buf, limit, pfile, keyword);
2898 DPRINTF (("Define macro: %s / %d",
2899 mdef.symnam, mdef.defn->noExpand));
2901 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2903 } /*@=branchstate@*/
2913 do_define (cppReader *pfile, struct directive *keyword,
2914 /*@exposed@*/ char *buf, char *limit)
2916 DPRINTF (("Regular do define"));
2917 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2920 /* This structure represents one parsed argument in a macro call.
2921 `raw' points to the argument text as written (`raw_length' is its length).
2922 `expanded' points to the argument's macro-expansion
2923 (its length is `expand_length').
2924 `stringified_length' is the length the argument would have
2926 `use_count' is the number of times this macro arg is substituted
2927 into the macro. If the actual use count exceeds 10,
2928 the value stored is 10. */
2930 /* raw and expanded are relative to ARG_BASE */
2932 #define ARG_BASE ((pfile)->token_buffer)
2935 /* Strings relative to pfile->token_buffer */
2941 int stringified_length;
2946 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2947 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2948 as the new input buffer.
2949 Return the new buffer, or NULL on failure. */
2951 /*@null@*/ /*@exposed@*/ cppBuffer *
2952 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2954 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2956 if (buf == pfile->buffer_stack)
2958 cppReader_fatalError
2960 message ("%s: macro or `#include' recursion too deep",
2961 (buf->fname != NULL)
2963 : cstring_makeLiteral ("<no name>")));
2964 sfreeEventually (buffer);
2968 llassert (buf != NULL);
2971 memset ((char *) buf, 0, sizeof (*buf));
2972 DPRINTF (("Pushing buffer: %s", cstring_copyLength (buffer, length)));
2973 CPPBUFFER (pfile) = buf;
2975 buf->if_stack = pfile->if_stack;
2976 buf->cleanup = cppReader_nullCleanup;
2977 buf->underflow = cppReader_nullUnderflow;
2979 buf->cur = buf->buf;
2983 buf->alimit = buf->rlimit = buffer + length;
2987 buf->alimit = buf->rlimit = NULL;
2994 cppReader_popBuffer (cppReader *pfile)
2996 cppBuffer *buf = CPPBUFFER (pfile);
2998 llassert (buf != NULL);
3000 (void) (*buf->cleanup) (buf, pfile);
3001 return ++CPPBUFFER (pfile);
3004 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
3005 Pop the buffer when done. */
3008 cppReader_scanBuffer (cppReader *pfile)
3010 cppBuffer *buffer = CPPBUFFER (pfile);
3013 enum cpp_token token;
3015 token = cpplib_getToken (pfile);
3017 if (token == CPP_EOF) /* Should not happen ... */
3022 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
3024 (void) cppReader_popBuffer (pfile);
3032 * Rescan a string (which may have escape marks) into pfile's buffer.
3033 * Place the result in pfile->token_buffer.
3035 * The input is copied before it is scanned, so it is safe to pass
3036 * it something from the token_buffer that will get overwritten
3037 * (because it follows cpplib_getWritten). This is used by do_include.
3041 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3043 register cppBuffer *ip;
3044 char *limit = buf + length;
3045 char *buf1, *p1, *p2;
3047 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
3049 /* evans - 2001-08-26
3050 ** length is unsigned - this doesn't make sense
3056 /* Set up the input on the input stack. */
3058 buf1 = (char *) dmalloc (length + 1);
3068 buf1[length] = '\0';
3070 ip = cppReader_pushBuffer (pfile, buf1, length);
3075 ip->has_escapes = TRUE;
3077 /* Scan the input, create the output. */
3078 cppReader_scanBuffer (pfile);
3080 cppReader_nullTerminate (pfile);
3084 adjust_position (char *buf, char *limit, int *linep, int *colp)
3090 (*linep)++, (*colp) = 1;
3096 /* Move line_base forward, updating lineno and colno. */
3099 update_position (cppBuffer *pbuf)
3102 char *new_pos = pbuf->cur;
3103 register struct parse_marker *mark;
3105 llassert (pbuf->buf != NULL);
3106 old_pos = pbuf->buf + pbuf->line_base;
3108 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3110 if (pbuf->buf + mark->position < new_pos)
3111 new_pos = pbuf->buf + mark->position;
3113 pbuf->line_base += new_pos - old_pos;
3115 llassert (old_pos != NULL);
3116 llassert (new_pos != NULL);
3118 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3122 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3123 /*@null@*/ /*@out@*/ int *colp)
3131 } /*@=branchstate@*/
3135 *linep = pbuf->lineno;
3136 *colp = pbuf->colno;
3138 llassert (pbuf->buf != NULL);
3139 llassert (pbuf->cur != NULL);
3141 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3150 /* Return the cppBuffer that corresponds to a file (not a macro). */
3152 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3154 cppBuffer *ip = cppReader_getBuffer (pfile);
3157 ip != NULL && ip != cppReader_nullBuffer (pfile);
3158 ip = cppBuffer_prevBuffer (ip))
3160 if (ip->fname != NULL)
3170 count_newlines (char *buf, char *limit)
3172 register long count = 0;
3184 * write out a #line command, for instance, after an #include file.
3185 * If CONDITIONAL is nonzero, we can omit the #line if it would
3186 * appear to be a no-op, and we can output a few newlines instead
3187 * if we want to increase the line number by a small amount.
3188 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3192 output_line_command (cppReader *pfile, bool conditional,
3193 enum file_change_code file_change)
3196 cppBuffer *ip = CPPBUFFER (pfile);
3199 llassert (ip != NULL);
3201 if (ip->fname == NULL)
3204 update_position (ip);
3206 if (CPPOPTIONS (pfile)->no_line_commands
3207 || CPPOPTIONS (pfile)->no_output)
3210 buf = CPPBUFFER (pfile);
3212 llassert (buf != NULL);
3217 llassert (ip->cur != NULL);
3219 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3221 if (CPPOPTIONS (pfile)->no_line_commands)
3225 if (line == pfile->lineno)
3228 /* If the inherited line number is a little too small,
3229 output some newlines instead of a #line command. */
3231 if (line > pfile->lineno && line < pfile->lineno + 8)
3233 cpplib_reserve (pfile, 20);
3234 while (line > pfile->lineno)
3236 cppReader_putCharQ (pfile, '\n');
3244 cpplib_reserve (pfile, 4 * cstring_length (ip->nominal_fname) + 50);
3247 #ifdef OUTPUT_LINE_COMMANDS
3248 static char sharp_line[] = "#line ";
3250 static char sharp_line[] = "# ";
3252 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3255 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3256 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3258 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3260 if (file_change != same_file) {
3261 cppReader_putCharQ (pfile, ' ');
3262 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3264 /* Tell cc1 if following text comes from a system header file. */
3265 if (ip->system_header_p != '\0') {
3266 cppReader_putCharQ (pfile, ' ');
3267 cppReader_putCharQ (pfile, '3');
3269 #ifndef NO_IMPLICIT_EXTERN_C
3270 /* Tell cc1plus if following text should be treated as C. */
3271 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3272 cppReader_putCharQ (pfile, ' ');
3273 cppReader_putCharQ (pfile, '4');
3276 cppReader_putCharQ (pfile, '\n');
3277 pfile->lineno = line;
3282 * Parse a macro argument and append the info on PFILE's token_buffer.
3283 * REST_ARGS means to absorb the rest of the args.
3284 * Return nonzero to indicate a syntax error.
3287 static enum cpp_token
3288 macarg (cppReader *pfile, int rest_args)
3291 enum cpp_token token;
3292 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3293 bool oldexpand = pfile->no_macro_expand;
3294 CPPOPTIONS (pfile)->put_out_comments = 1;
3296 /* Try to parse as much of the argument as exists at this
3297 input stack level. */
3299 pfile->no_macro_expand = TRUE;
3303 token = cpplib_getToken (pfile);
3310 /* If we've hit end of file, it's an error (reported by caller).
3311 Ditto if it's the end of cpp_expand_to_buffer text.
3312 If we've hit end of macro, just continue. */
3313 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3315 /*@switchbreak@*/ break;
3318 /*@switchbreak@*/ break;
3322 /*@switchbreak@*/ break;
3324 /* if we've returned to lowest level and
3325 we aren't absorbing all args */
3326 if (paren == 0 && rest_args == 0)
3328 /*@switchbreak@*/ break;
3330 /* Remove ',' or ')' from argument buffer. */
3331 cppReader_adjustWritten (pfile, -1);
3339 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3340 pfile->no_macro_expand = oldexpand;
3346 /* Turn newlines to spaces in the string of length LENGTH at START,
3347 except inside of string constants.
3348 The string is copied into itself with its beginning staying fixed. */
3351 change_newlines (char *start, int length)
3355 register char *limit;
3359 limit = start + length;
3362 while (ibp < limit) {
3363 *obp++ = c = *ibp++;
3368 /* Notice and skip strings, so that we don't delete newlines in them. */
3371 while (ibp < limit) {
3372 *obp++ = c = *ibp++;
3374 /*@innerbreak@*/ break;
3375 if (c == '\n' && quotec == '\'')
3376 /*@innerbreak@*/ break;
3379 /*@switchbreak@*/ break;
3386 static /*@observer@*/ struct tm *
3387 timestamp (/*@returned@*/ cppReader *pfile)
3389 if (pfile->timebuf == NULL)
3391 time_t t = time ((time_t *) 0);
3392 pfile->timebuf = localtime (&t);
3395 llassert (pfile->timebuf != NULL);
3397 return pfile->timebuf;
3400 static ob_mstring monthnames[] = {
3401 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3402 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3406 * expand things like __FILE__. Place the expansion into the output
3407 * buffer *without* rescanning.
3411 special_symbol (hashNode hp, cppReader *pfile)
3413 cstring buf = cstring_undefined;
3419 int paren = 0; /* For special `defined' keyword */
3421 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3423 if (ip == cppReader_nullBuffer (pfile))
3425 cppReader_errorLit (pfile,
3426 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3427 return; /* the show must go on */
3430 if (ip != NULL && ip->fname != NULL)
3442 if (hp->type == T_BASE_FILE)
3444 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3446 ip = cppBuffer_prevBuffer (ip);
3450 llassert (ip != NULL);
3451 string = cstring_toCharsSafe (ip->nominal_fname);
3458 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3459 quote_string (pfile, string);
3463 case T_INCLUDE_LEVEL:
3465 ip = cppReader_getBuffer (pfile);
3467 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3468 ip = cppBuffer_prevBuffer (ip))
3470 if (ip != NULL && ip->fname != NULL)
3476 buf = message ("%d", true_indepth - 1);
3480 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3483 #ifndef NO_BUILTIN_SIZE_TYPE
3485 buf = cstring_makeLiteral (SIZE_TYPE);
3489 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3490 case T_PTRDIFF_TYPE:
3491 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3496 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3499 case T_USER_LABEL_PREFIX_TYPE:
3500 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3503 case T_REGISTER_PREFIX_TYPE:
3504 buf = cstring_makeLiteral (REGISTER_PREFIX);
3508 buf = message ("%d", hp->value.ival);
3515 int line = ip->lineno;
3516 int col = ip->colno;
3518 llassert (ip->cur != NULL);
3519 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3521 buf = message ("%d", (int) line);
3533 char *sbuf = (char *) dmalloc (20);
3534 timebuf = timestamp (pfile);
3535 if (hp->type == T_DATE)
3537 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3538 timebuf->tm_mday, timebuf->tm_year + 1900);
3542 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3546 buf = cstring_fromCharsNew (sbuf);
3551 case T_SPEC_DEFINED:
3552 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3553 ip = cppReader_getBuffer (pfile);
3554 llassert (ip != NULL);
3555 llassert (ip->cur != NULL);
3556 SKIP_WHITE_SPACE (ip->cur);
3558 if (*ip->cur == '(')
3561 ip->cur++; /* Skip over the paren */
3562 SKIP_WHITE_SPACE (ip->cur);
3565 if (!is_idstart[(int) *ip->cur])
3567 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3570 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3573 buf = cstring_makeLiteral (" 1 ");
3576 while (is_idchar[(int) *ip->cur])
3581 SKIP_WHITE_SPACE (ip->cur);
3585 if (*ip->cur != ')')
3593 cppReader_errorLit (pfile,
3594 cstring_makeLiteralTemp ("`defined' without an identifier"));
3598 cpp_setLocation (pfile);
3599 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3602 len = cstring_length (buf);
3604 cpplib_reserve (pfile, len + 1);
3605 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3606 cppReader_nullTerminateQ (pfile);
3612 /* Write out a #define command for the special named MACRO_NAME
3613 to PFILE's token_buffer. */
3616 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3618 static char define_directive[] = "#define ";
3619 size_t macro_name_length = strlen (macro_name);
3620 output_line_command (pfile, 0, same_file);
3621 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3622 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3623 cppReader_putStrN (pfile, macro_name, macro_name_length);
3624 cppReader_putCharQ (pfile, ' ');
3625 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3626 cppReader_putChar (pfile, '\n');
3629 /* Initialize the built-in macros. */
3632 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3633 int len, enum node_type type,
3634 int ivalue, /*@null@*/ /*@only@*/ char *value,
3637 cstring sname = cstring_fromCharsNew (name);
3639 llassert (usymtab_inGlobalScope ());
3642 ** Be careful here: this is done before the ctype table has
3643 ** been initialized.
3646 if (!usymtab_exists (sname))
3648 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3650 if (ctype_equal (ctyp, ctype_string))
3652 qualList ql = qualList_new ();
3653 ql = qualList_add (ql, qual_createObserver ());
3654 uentry_reflectQualifiers (ue, ql);
3658 usymtab_addGlobalEntry (ue);
3665 (void) cpphash_install (name, len, type, ivalue, value, hash);
3666 cstring_free (sname);
3670 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3671 int len, enum node_type type,
3673 /*@only@*/ /*@null@*/ char *value, int hash)
3675 cstring sname = cstring_fromChars (name);
3676 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3678 llassert (usymtab_inGlobalScope ());
3680 if (!usymtab_existsTypeEither (sname))
3682 uentry ue = uentry_makeDatatype (sname, ctyp,
3683 NO, qual_createConcrete (),
3684 fileloc_createBuiltin ());
3685 llassert (!usymtab_existsEither (sname));
3686 usymtab_addGlobalEntry (ue);
3689 (void) cpphash_install (name, len, type, ivalue, value, hash);
3693 initialize_builtins (cppReader *pfile)
3695 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3696 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3697 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3698 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3699 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3700 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3701 #ifndef NO_BUILTIN_SIZE_TYPE
3702 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3704 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3705 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3707 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3708 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3709 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3710 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3713 ** No, don't define __STDC__
3716 if (!cppReader_isTraditional (pfile))
3718 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3725 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3729 ** This is supplied using a -D by the compiler driver
3730 ** so that it is present only when truly compiling with GNU C.
3733 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3735 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3736 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3738 /*drl 1/9/2001/ try to define the right symbol for the architecture
3739 We use autoconf to determine the target cpu
3741 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3743 /*drl 1/2/2002 set some flags based on uname
3744 I'd like to be able to do this with autoconf macro instead...
3747 /*Thanks to Nelson Beebe for suggesting possible values for these */
3749 if (! strcmp (UNAME, "Linux"))
3752 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3756 else if(! strcmp (UNAME, "Darwin"))
3758 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3759 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3761 else if(! strcmp (UNAME, "HP-UX"))
3763 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3764 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3765 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3766 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3767 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3769 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3770 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3771 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3772 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3773 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3774 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3775 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3780 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3781 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3782 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3783 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3785 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3787 else if(! strcmp (UNAME, "IRIX64"))
3789 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3791 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3792 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3793 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3794 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3795 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3796 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3797 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3798 /*_MIPS_SIM=_ABIN32*/
3799 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3800 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3801 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3802 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3803 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3805 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3806 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3807 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3808 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3809 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3810 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3811 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3812 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3813 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3814 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3815 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3816 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3817 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3818 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3819 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3820 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3821 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3822 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3824 else if(! strcmp (UNAME, "OSF1"))
3826 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3828 else if (!strcmp (UNAME, "Rhapsody"))
3830 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3831 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3834 else if (!strcmp (UNAME, "SunOS"))
3836 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3837 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3838 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3839 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3844 types which we have not explictedly handled.
3845 AIX, FreeBSD, IRIX, Mach
3850 if (CPPOPTIONS (pfile)->debug_output)
3852 dump_special_to_buffer (pfile, "__BASE_FILE__");
3853 dump_special_to_buffer (pfile, "__VERSION__");
3854 #ifndef NO_BUILTIN_SIZE_TYPE
3855 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3857 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3858 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3860 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3861 dump_special_to_buffer (pfile, "__DATE__");
3862 dump_special_to_buffer (pfile, "__TIME__");
3863 if (!cppReader_isTraditional (pfile))
3864 dump_special_to_buffer (pfile, "__STDC__");
3869 /* Return 1 iff a token ending in C1 followed directly by a token C2
3870 could cause mis-tokenization. */
3873 unsafe_chars (char c1, char c2)
3878 if (c2 == c1 || c2 == '=')
3882 case '0': case '1': case '2': case '3': case '4':
3883 case '5': case '6': case '7': case '8': case '9':
3884 case 'e': case 'E': case 'p': case 'P':
3885 if (c2 == '-' || c2 == '+')
3886 return 1; /* could extend a pre-processing number */
3889 if (c2 == '\'' || c2 == '\"')
3890 return 1; /* Could turn into L"xxx" or L'xxx'. */
3894 case 'a': case 'b': case 'c': case 'd': case 'f':
3895 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3896 case 'm': case 'n': case 'o': case 'q': case 'r':
3897 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3899 case 'A': case 'B': case 'C': case 'D': case 'F':
3900 case 'G': case 'H': case 'I': case 'J': case 'K':
3901 case 'M': case 'N': case 'O': case 'Q': case 'R':
3902 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3904 /* We're in the middle of either a name or a pre-processing number. */
3905 return (is_idchar[(int) c2] || c2 == '.');
3906 case '<': case '>': case '!': case '%': case '#': case ':':
3907 case '^': case '&': case '|': case '*': case '/': case '=':
3908 return (c2 == c1 || c2 == '=');
3913 /* Expand a macro call.
3914 HP points to the symbol that is the macro being called.
3915 Put the result of expansion onto the input stack
3916 so that subsequent input by our caller will use it.
3918 If macro wants arguments, caller has already verified that
3919 an argument list follows; arguments come from the input stack. */
3922 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3925 DEFINITION *defn = hp->value.defn;
3933 size_t old_written = cpplib_getWritten (pfile);
3937 struct argdata *args = NULL;
3939 pfile->output_escapes++;
3940 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3941 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3943 nargs = defn->nargs;
3947 enum cpp_token token = CPP_EOF;
3949 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3951 for (i = 0; i < nargs; i++)
3953 args[i].expanded = 0;
3955 args[i].raw_length = 0;
3956 args[i].expand_length = args[i].stringified_length = -1;
3957 args[i].use_count = 0;
3961 ** Parse all the macro args that are supplied. I counts them.
3962 ** The first NARGS args are stored in ARGS.
3963 ** The rest are discarded. If rest_args is set then we assume
3964 ** macarg absorbed the rest of the args.
3970 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3978 if (i < nargs || (nargs == 0 && i == 0))
3980 /* if we are working on last arg which absorbs rest of args... */
3981 if (i == nargs - 1 && defn->rest_args)
3986 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3987 token = macarg (pfile, rest_args);
3988 args[i].raw_length = size_toInt (cpplib_getWritten (pfile) - args[i].raw);
3989 args[i].newlines = FALSE; /* FIXME */
3993 token = macarg (pfile, 0);
3996 if (token == CPP_EOF || token == CPP_POP)
3998 cppReader_errorWithLine (pfile, start_line, start_column,
3999 cstring_fromCharsNew ("unterminated macro call"));
4004 } while (token == CPP_COMMA);
4006 /* If we got one arg but it was just whitespace, call that 0 args. */
4014 bp = ARG_BASE + args[0].raw;
4015 lim = bp + args[0].raw_length;
4017 /* cpp.texi says for foo ( ) we provide one argument.
4018 However, if foo wants just 0 arguments, treat this as 0. */
4022 while (bp != lim && is_space[(int) *bp])
4032 /* Don't output an error message if we have already output one for
4033 a parse error above. */
4036 if (nargs == 0 && i > 0)
4038 cppReader_error (pfile,
4039 message ("arguments given to macro `%s'", hp->name));
4043 /* traditional C allows foo() if foo wants one argument. */
4044 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4048 /* the rest args token is allowed to absorb 0 tokens */
4049 else if (i == nargs - 1 && defn->rest_args)
4052 cppReader_error (pfile,
4053 message ("macro `%s' used without args", hp->name));
4055 cppReader_error (pfile,
4056 message ("macro `%s' used with just one arg", hp->name));
4059 cppReader_error (pfile,
4060 message ("macro `%s' used with only %d args",
4066 cppReader_error (pfile,
4067 message ("macro `%s' used with too many (%d) args", hp->name, i));
4076 ** If the agrument list was multiple lines, need to insert new lines to keep line
4077 ** numbers accurate.
4080 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4081 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4083 /* If macro wants zero args, we parsed the arglist for checking only.
4084 Read directly from the macro definition. */
4088 xbuf = defn->expansion;
4089 xbuf_len = defn->length;
4093 char *exp = defn->expansion;
4094 int offset; /* offset in expansion,
4095 copied a piece at a time */
4096 size_t totlen; /* total amount of exp buffer filled so far */
4098 register struct reflist *ap, *last_ap;
4100 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4102 /* Macro really takes args. Compute the expansion of this call. */
4104 /* Compute length in characters of the macro's expansion.
4105 Also count number of times each arg is used. */
4106 xbuf_len = defn->length;
4108 llassert (args != NULL);
4110 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4114 struct argdata *arg = &args[ap->argno];
4116 /* Stringify it it hasn't already been */
4119 if (arg->stringified_length < 0)
4121 int arglen = arg->raw_length;
4122 bool escaped = FALSE;
4123 char in_string = '\0';
4126 /* Initially need_space is -1. Otherwise, 1 means the
4127 previous character was a space, but we suppressed it;
4128 0 means the previous character was a non-space. */
4129 int need_space = -1;
4132 arg->stringified = cpplib_getWritten (pfile);
4133 if (!cppReader_isTraditional (pfile))
4134 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4135 for (; i < arglen; i++)
4137 c = (ARG_BASE + arg->raw)[i];
4139 if (in_string == '\0')
4141 /* Internal sequences of whitespace are replaced by
4142 one space except within an string or char token.*/
4143 if (is_space[(int) c])
4145 if (cpplib_getWritten (pfile) > arg->stringified
4146 && (cpplib_getPWritten (pfile))[-1] == '@')
4148 /* "@ " escape markers are removed */
4149 cppReader_adjustWritten (pfile, -1);
4150 /*@innercontinue@*/ continue;
4152 if (need_space == 0)
4154 /*@innercontinue@*/ continue;
4156 else if (need_space > 0)
4157 cppReader_putChar (pfile, ' ');
4173 if (in_string != '\0')
4178 else if (c == '\"' || c == '\'')
4188 /* Escape these chars */
4189 if (c == '\"' || (in_string != '\0' && c == '\\'))
4190 cppReader_putChar (pfile, '\\');
4192 cppReader_putChar (pfile, c);
4195 cpplib_reserve (pfile, 4);
4196 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4198 cppReader_adjustWritten (pfile, 4);
4201 if (!cppReader_isTraditional (pfile))
4202 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4203 arg->stringified_length
4204 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4207 xbuf_len += args[ap->argno].stringified_length;
4209 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4211 /* Add 4 for two newline-space markers to prevent
4212 token concatenation. */
4213 assertSet (args); /*@i534 shouldn't need this */
4214 xbuf_len += args[ap->argno].raw_length + 4;
4218 /* We have an ordinary (expanded) occurrence of the arg.
4219 So compute its expansion, if we have not already. */
4221 assertSet (args); /*@i534 shouldn't need this */
4223 if (args[ap->argno].expand_length < 0)
4225 args[ap->argno].expanded = cpplib_getWritten (pfile);
4226 cpp_expand_to_buffer (pfile,
4227 ARG_BASE + args[ap->argno].raw,
4228 size_fromInt (args[ap->argno].raw_length));
4230 args[ap->argno].expand_length
4231 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4234 /* Add 4 for two newline-space markers to prevent
4235 token concatenation. */
4236 xbuf_len += args[ap->argno].expand_length + 4;
4238 if (args[ap->argno].use_count < 10)
4239 args[ap->argno].use_count++;
4242 xbuf = (char *) dmalloc (xbuf_len + 1);
4246 ** Generate in XBUF the complete expansion
4247 ** with arguments substituted in.
4248 ** TOTLEN is the total size generated so far.
4249 ** OFFSET is the index in the definition
4250 ** of where we are copying from.
4256 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4257 last_ap = ap, ap = ap->next)
4259 register struct argdata *arg = &args[ap->argno];
4260 size_t count_before = totlen;
4262 /* Add chars to XBUF. */
4263 for (i = 0; i < ap->nchars; i++, offset++)
4265 xbuf[totlen++] = exp[offset];
4268 /* If followed by an empty rest arg with concatenation,
4269 delete the last run of nonwhite chars. */
4270 if (rest_zero && totlen > count_before
4271 && ((ap->rest_args && ap->raw_before)
4272 || (last_ap != NULL && last_ap->rest_args
4273 && last_ap->raw_after)))
4275 /* Delete final whitespace. */
4276 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4281 /* Delete the nonwhites before them. */
4282 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4288 if (ap->stringify != 0)
4291 memcpy (xbuf + totlen,
4292 ARG_BASE + arg->stringified,
4293 size_fromInt (arg->stringified_length));
4294 totlen += arg->stringified_length;
4296 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4303 p1 = ARG_BASE + arg->raw;
4304 l1 = p1 + arg->raw_length;
4308 while (p1 != l1 && is_space[(int) *p1])
4313 while (p1 != l1 && is_idchar[(int) *p1])
4315 xbuf[totlen++] = *p1++;
4318 /* Delete any no-reexpansion marker that follows
4319 an identifier at the beginning of the argument
4320 if the argument is concatenated with what precedes it. */
4321 if (p1[0] == '@' && p1[1] == '-')
4326 /* Arg is concatenated after: delete trailing whitespace,
4327 whitespace markers, and no-reexpansion markers. */
4330 if (is_space[(int) l1[-1]]) l1--;
4331 else if (l1[-1] == '-')
4334 /* If a `-' is preceded by an odd number of newlines then it
4335 and the last newline are a no-reexpansion marker. */
4336 while (p2 != p1 && p2[-1] == '\n')
4341 if (((l1 - 1 - p2) & 1) != 0)
4347 /*@innerbreak@*/ break;
4352 /*@innerbreak@*/ break;
4357 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4365 expanded = ARG_BASE + arg->expanded;
4367 if (!ap->raw_before && totlen > 0
4368 && (arg->expand_length != 0)
4369 && !cppReader_isTraditional(pfile)
4370 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4372 xbuf[totlen++] = '@';
4373 xbuf[totlen++] = ' ';
4376 memcpy (xbuf + totlen, expanded,
4377 size_fromInt (arg->expand_length));
4378 totlen += arg->expand_length;
4380 if (!ap->raw_after && totlen > 0
4381 && offset < size_toInt (defn->length)
4382 && !cppReader_isTraditional(pfile)
4383 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4385 xbuf[totlen++] = '@';
4386 xbuf[totlen++] = ' ';
4389 /* If a macro argument with newlines is used multiple times,
4390 then only expand the newlines once. This avoids creating
4391 output lines which don't correspond to any input line,
4392 which confuses gdb and gcov. */
4393 if (arg->use_count > 1 && arg->newlines > 0)
4395 /* Don't bother doing change_newlines for subsequent
4399 = change_newlines (expanded, arg->expand_length);
4403 if (totlen > xbuf_len)
4407 /* if there is anything left of the definition
4408 after handling the arg list, copy that in too. */
4410 for (i = offset; i < size_toInt (defn->length); i++)
4412 /* if we've reached the end of the macro */
4415 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4416 && last_ap->raw_after))
4417 xbuf[totlen++] = exp[i];
4420 xbuf[totlen] = '\0';
4424 pfile->output_escapes--;
4426 /* Now put the expansion on the input stack
4427 so our caller will commence reading from it. */
4428 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4430 if (end_line != start_line)
4432 /* xbuf must have enough newlines */
4433 int newlines = end_line - start_line;
4434 int foundnewlines = 0;
4435 char *xbufptr = xbuf;
4437 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4442 if (*xbufptr == '\0')
4448 if (foundnewlines < newlines)
4450 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4452 while (foundnewlines < newlines)
4454 newbuf = cstring_appendChar (newbuf, '\n');
4459 xbuf = cstring_toCharsSafe (newbuf);
4460 xbuf_len = cstring_length (newbuf);
4462 } /*@=branchstate@*/
4465 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4467 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4468 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4469 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4471 /* Pop the space we've used in the token_buffer for argument expansion. */
4472 cppReader_setWritten (pfile, old_written);
4473 DPRINTF (("Done set written"));
4475 /* Recursive macro use sometimes works traditionally.
4476 #define foo(x,y) bar (x (y,0), y)
4479 if (!cppReader_isTraditional (pfile))
4480 hp->type = T_DISABLED;
4486 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4487 /*@dependent@*/ hashNode hp)
4489 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4496 mbuf->cleanup = cppReader_macroCleanup;
4498 llassert (mbuf->hnode == NULL);
4501 /* The first chars of the expansion should be a "@ " added by
4502 collect_expansion. This is to prevent accidental token-pasting
4503 between the text preceding the macro invocation, and the macro
4506 We would like to avoid adding unneeded spaces (for the sake of
4507 tools that use cpp, such as imake). In some common cases we can
4508 tell that it is safe to omit the space.
4510 The character before the macro invocation cannot have been an
4511 idchar (or else it would have been pasted with the idchars of
4512 the macro name). Therefore, if the first non-space character
4513 of the expansion is an idchar, we do not need the extra space
4514 to prevent token pasting.
4516 Also, we don't need the extra space if the first char is '(',
4517 or some other (less common) characters. */
4519 if (xbuf[0] == '@' && xbuf[1] == ' '
4520 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4521 || xbuf[2] == '\"'))
4523 llassert (mbuf->cur != NULL);
4524 DPRINTF (("Eating: %c", xbuf[2]));
4531 /* Like cpplib_getToken, except that it does not read past end-of-line.
4532 Also, horizontal space is skipped, and macros are popped. */
4534 static enum cpp_token
4535 get_directive_token (cppReader *pfile)
4539 size_t old_written = cpplib_getWritten (pfile);
4540 enum cpp_token token;
4541 cppSkipHspace (pfile);
4542 if (cppReader_peekC (pfile) == '\n')
4547 token = cpplib_getToken (pfile);
4552 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4557 cppReader_setWritten (pfile, old_written);
4558 /*@switchbreak@*/ break;
4566 /* Handle #include and #import.
4567 This function expects to see "fname" or <fname> on the input.
4569 The input is normally in part of the output_buffer following
4570 cpplib_getWritten, and will get overwritten by output_line_command.
4571 I.e. in input file specification has been popped by cppReader_handleDirective.
4575 do_include (cppReader *pfile, struct directive *keyword,
4576 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4578 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4580 char *fbeg, *fend; /* Beginning and end of fname */
4581 enum cpp_token token;
4583 /* Chain of dirs to search */
4584 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4585 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4586 struct file_name_list *searchptr = NULL;
4587 size_t old_written = cpplib_getWritten (pfile);
4590 int f; /* file number */
4591 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4592 f= -1; /* JF we iz paranoid! */
4594 pfile->parsing_include_directive++;
4595 token = get_directive_token (pfile);
4596 pfile->parsing_include_directive--;
4598 if (token == CPP_STRING)
4600 /* FIXME - check no trailing garbage */
4601 fbeg = pfile->token_buffer + old_written + 1;
4602 fend = cpplib_getPWritten (pfile) - 1;
4603 if (fbeg[-1] == '<')
4606 /* If -I-, start with the first -I dir after the -I-. */
4607 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4608 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4610 /* If -I- was specified, don't search current dir, only spec'd ones. */
4611 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4613 cppBuffer *fp = CPPBUFFER (pfile);
4614 /* We have "filename". Figure out directory this source
4615 file is coming from and put it on the front of the list. */
4617 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4622 llassert (fp != NULL);
4626 if (cstring_isDefined (fp->nominal_fname))
4628 nam = cstring_toCharsSafe (fp->nominal_fname);
4630 /* Found a named file. Figure out dir of the file,
4631 and put it in front of the search list. */
4632 dsp[0].next = search_start;
4636 ep = strrchr (nam, CONNECTCHAR);
4638 ep = strrchr (nam, ']');
4639 if (ep == NULL) ep = strrchr (nam, '>');
4640 if (ep == NULL) ep = strrchr (nam, ':');
4641 if (ep != NULL) ep++;
4651 /*@-onlytrans@*/ /* This looks like a memory leak... */
4652 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4656 if (n + INCLUDE_LEN_FUDGE > size_toInt (pfile->max_include_len))
4657 pfile->max_include_len = size_fromInt (n + INCLUDE_LEN_FUDGE);
4661 dsp[0].fname = cstring_undefined; /* Current directory */
4664 dsp[0].got_name_map = 0;
4675 else if (token == CPP_NAME)
4678 * Support '#include xyz' like VAX-C to allow for easy use of all the
4679 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4680 * code from case '<' is repeated here) and generates a warning.
4682 cppReader_warning (pfile,
4683 "VAX-C-style include specification found, use '#include <filename.h>' !");
4685 /* If -I-, start with the first -I dir after the -I-. */
4686 if (CPPOPTIONS (pfile)->first_bracket_include)
4687 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4688 fbeg = pfile->token_buffer + old_written;
4689 fend = cpplib_getPWritten (pfile);
4694 cppReader_error (pfile,
4695 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4698 cppReader_setWritten (pfile, old_written);
4699 cppReader_skipRestOfLine (pfile);
4705 token = get_directive_token (pfile);
4706 if (token != CPP_VSPACE)
4708 cppReader_errorLit (pfile,
4709 cstring_makeLiteralTemp ("Junk at end of #include"));
4711 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4713 token = get_directive_token (pfile);
4718 ** For #include_next, skip in the search path
4719 ** past the dir in which the containing file was found.
4724 cppBuffer *fp = CPPBUFFER (pfile);
4726 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4728 llassert (fp != NULL);
4730 if (fp->fname != NULL)
4732 /* fp->dir is null if the containing file was specified with
4733 an absolute file name. In that case, don't skip anything. */
4734 if (fp->dir == SELF_DIR_DUMMY)
4736 search_start = CPPOPTIONS (pfile)->include;
4738 else if (fp->dir != NULL)
4740 search_start = fp->dir->next;
4752 cppReader_setWritten (pfile, old_written);
4754 flen = size_fromInt (fend - fbeg);
4756 DPRINTF (("fbeg: %s", fbeg));
4760 cppReader_error (pfile,
4761 message ("Empty file name in #%s", keyword->name));
4766 ** Allocate this permanently, because it gets stored in the definitions
4770 fname = cstring_undefined;
4772 /* + 2 above for slash and terminating null. */
4773 /* + 2 added for '.h' on VMS (to support '#include filename') */
4775 /* If specified file name is absolute, just open it. */
4777 if (osd_isConnectChar (*fbeg)
4778 # if defined (WIN32) || defined (OS2)
4779 || (*(fbeg + 1) == ':')
4783 fname = cstring_copyLength (fbeg, flen);
4785 if (redundant_include_p (pfile, fname))
4787 cstring_free (fname);
4791 f = open_include_file (pfile, fname, NULL);
4793 if (f == IMPORT_FOUND)
4795 return 0; /* Already included this file */
4800 /* Search directory path, trying to open the file.
4801 Copy each filename tried into FNAME. */
4803 for (searchptr = search_start; searchptr != NULL;
4804 searchptr = searchptr->next)
4806 if (!cstring_isEmpty (searchptr->fname))
4808 /* The empty string in a search path is ignored.
4809 This makes it possible to turn off entirely
4810 a standard piece of the list. */
4811 if (cstring_isEmpty (searchptr->fname))
4814 fname = cstring_copy (searchptr->fname);
4815 fname = cstring_appendChar (fname, CONNECTCHAR);
4816 DPRINTF (("Here: %s", fname));
4823 fname = cstring_concatLength (fname, fbeg, flen);
4825 DPRINTF (("fname: %s", fname));
4827 /* Win32 directory fix from Kay Buschner. */
4828 #if defined (WIN32) || defined (OS2)
4829 /* Fix all unixdir slashes to win dir slashes */
4830 if (searchptr->fname && (searchptr->fname[0] != 0))
4832 cstring_replaceAll (fname, '/', '\\');
4837 /* Change this 1/2 Unix 1/2 VMS file specification into a
4838 full VMS file specification */
4839 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4840 /* Fix up the filename */
4841 hack_vms_include_specification (fname);
4843 /* This is a normal VMS filespec, so use it unchanged. */
4844 strncpy (fname, fbeg, flen);
4846 /* if it's '#include filename', add the missing .h */
4847 if (strchr (fname,'.') == NULL) {
4848 strcat (fname, ".h");
4852 /* ??? There are currently 3 separate mechanisms for avoiding processing
4853 of redundant include files: #import, #pragma once, and
4854 redundant_include_p. It would be nice if they were unified. */
4856 if (redundant_include_p (pfile, fname))
4858 cstring_free (fname);
4862 DPRINTF (("Trying: %s", fname));
4864 f = open_include_file (pfile, fname, searchptr);
4866 if (f == IMPORT_FOUND)
4868 return 0; /* Already included this file */
4871 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4873 cppReader_warning (pfile,
4874 message ("Header file %s exists, but is not readable", fname));
4887 /* A file that was not found. */
4888 fname = cstring_copyLength (fbeg, flen);
4890 if (search_start != NULL)
4892 cppReader_error (pfile,
4893 message ("Cannot find include file %s on search path: %x",
4895 searchPath_unparse (search_start)));
4899 cppReader_error (pfile,
4900 message ("No include path in which to find %s", fname));
4905 ** Check to see if this include file is a once-only include file.
4909 struct file_name_list *ptr;
4911 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4913 if (cstring_equal (ptr->fname, fname))
4915 /* This file was included before. */
4922 /* This is the first time for this file. */
4923 /* Add it to list of files included. */
4925 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4926 ptr->control_macro = NULL;
4927 ptr->c_system_include_path = NULL;
4928 ptr->next = pfile->all_include_files;
4930 ptr->got_name_map = NULL;
4932 DPRINTF (("Including file: %s", fname));
4933 pfile->all_include_files = ptr;
4934 assertSet (pfile->all_include_files);
4937 if (angle_brackets != 0)
4939 pfile->system_include_depth++;
4942 /* Actually process the file */
4943 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4945 cstring_free (fname);
4949 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4950 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4952 output_line_command (pfile, 0, enter_file);
4953 pfile->only_seen_white = 2;
4958 pfile->system_include_depth--;
4961 } /*@=branchstate@*/
4966 /* Return nonzero if there is no need to include file NAME
4967 because it has already been included and it contains a conditional
4968 to make a repeated include do nothing. */
4971 redundant_include_p (cppReader *pfile, cstring name)
4973 struct file_name_list *l = pfile->all_include_files;
4975 for (; l != NULL; l = l->next)
4977 if (cstring_equal (name, l->fname)
4978 && (l->control_macro != NULL)
4979 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4988 /* Return nonzero if the given FILENAME is an absolute pathname which
4989 designates a file within one of the known "system" include file
4990 directories. We assume here that if the given FILENAME looks like
4991 it is the name of a file which resides either directly in a "system"
4992 include file directory, or within any subdirectory thereof, then the
4993 given file must be a "system" include file. This function tells us
4994 if we should suppress pedantic errors/warnings for the given FILENAME.
4996 The value is 2 if the file is a C-language system header file
4997 for which C++ should (on most systems) assume `extern "C"'. */
5000 is_system_include (cppReader *pfile, cstring filename)
5002 struct file_name_list *searchptr;
5004 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
5006 searchptr = searchptr->next)
5008 if (!cstring_isEmpty (searchptr->fname))
5010 cstring sys_dir = searchptr->fname;
5011 size_t length = cstring_length (sys_dir);
5013 if (cstring_equalLen (sys_dir, filename, length)
5014 && osd_isConnectChar (cstring_getChar (filename, length)))
5016 if (searchptr->c_system_include_path)
5027 /* Convert a character string literal into a nul-terminated string.
5028 The input string is [IN ... LIMIT).
5029 The result is placed in RESULT. RESULT can be the same as IN.
5030 The value returned in the end of the string written to RESULT,
5031 or NULL on error. */
5033 static /*@null@*/ char *
5034 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5035 char *in, char *limit, int handle_escapes)
5055 /*@switchbreak@*/ break;
5059 char *bpc = (char *) in;
5060 int i = (char) cppReader_parseEscape (pfile, &bpc);
5063 *result++ = (char) c;
5064 /*@switchbreak@*/ break;
5078 * interpret #line command. Remembers previously seen fnames
5079 * in its very own hash table.
5082 /*@constant int FNAME_HASHSIZE@*/
5083 #define FNAME_HASHSIZE 37
5086 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5088 cppBuffer *ip = cppReader_getBuffer (pfile);
5090 size_t old_written = cpplib_getWritten (pfile);
5091 enum file_change_code file_change = same_file;
5092 enum cpp_token token;
5094 llassert (ip != NULL);
5095 token = get_directive_token (pfile);
5097 if (token != CPP_NUMBER
5098 || !isdigit(pfile->token_buffer[old_written]))
5100 cppReader_errorLit (pfile,
5101 cstring_makeLiteralTemp ("invalid format `#line' command"));
5103 goto bad_line_directive;
5106 /* The Newline at the end of this line remains to be processed.
5107 To put the next line at the specified line number,
5108 we must store a line number now that is one less. */
5109 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5110 cppReader_setWritten (pfile, old_written);
5112 /* NEW_LINENO is one less than the actual line number here. */
5113 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5114 cppReader_pedwarnLit (pfile,
5115 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5117 token = get_directive_token (pfile);
5119 if (token == CPP_STRING) {
5120 char *fname = pfile->token_buffer + old_written;
5122 static hashNode fname_table[FNAME_HASHSIZE];
5124 hashNode *hash_bucket;
5127 size_t fname_length;
5129 /* Turn the file name, which is a character string literal,
5130 into a null-terminated string. Do this in place. */
5131 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5132 if (end_name == NULL)
5134 cppReader_errorLit (pfile,
5135 cstring_makeLiteralTemp ("invalid format `#line' command"));
5136 goto bad_line_directive;
5139 fname_length = size_fromInt (end_name - fname);
5140 num_start = cpplib_getWritten (pfile);
5142 token = get_directive_token (pfile);
5143 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5144 p = pfile->token_buffer + num_start;
5145 if (cppReader_isPedantic (pfile))
5146 cppReader_pedwarnLit (pfile,
5147 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5149 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5151 cppReader_errorLit (pfile,
5152 cstring_makeLiteralTemp ("invalid format `#line' command"));
5153 goto bad_line_directive;
5156 file_change = enter_file;
5158 file_change = leave_file;
5160 ip->system_header_p = 1;
5161 else /* if (*p == 4) */
5162 ip->system_header_p = 2;
5164 cppReader_setWritten (pfile, num_start);
5165 token = get_directive_token (pfile);
5166 p = pfile->token_buffer + num_start;
5167 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5168 ip->system_header_p = *p == 3 ? 1 : 2;
5169 token = get_directive_token (pfile);
5171 if (token != CPP_VSPACE) {
5172 cppReader_errorLit (pfile,
5173 cstring_makeLiteralTemp ("invalid format `#line' command"));
5175 goto bad_line_directive;
5180 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5182 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5184 if (hp->length == fname_length)
5186 llassert (hp->value.cpval != NULL);
5188 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5190 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5197 /* Didn't find it; cons up a new one. */
5198 hp = (hashNode) dmalloc (sizeof (*hp));
5201 hp->bucket_hdr = NULL;
5203 hp->name = cstring_undefined;
5204 hp->next = *hash_bucket;
5208 hp->length = fname_length;
5209 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5210 memcpy (hp->value.cpval, fname, fname_length);
5211 hp->value.cpval[fname_length] = '\0';
5212 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5215 else if (token != CPP_VSPACE && token != CPP_EOF)
5217 cppReader_errorLit (pfile,
5218 cstring_makeLiteralTemp ("invalid format `#line' command"));
5219 goto bad_line_directive;
5226 ip->lineno = new_lineno;
5228 cppReader_skipRestOfLine (pfile);
5229 cppReader_setWritten (pfile, old_written);
5230 output_line_command (pfile, 0, file_change);
5235 * remove the definition of a symbol from the symbol table.
5236 * according to un*x /lib/cpp, it is not an error to undef
5237 * something that has no definitions, so it isn't one here either.
5241 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5246 char *orig_buf = buf;
5248 SKIP_WHITE_SPACE (buf);
5250 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5252 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5254 /* If we are generating additional info for debugging (with -g) we
5255 need to pass through all effective #undef commands. */
5256 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5258 pass_thru_directive (orig_buf, limit, pfile, keyword);
5261 if (hp->type != T_MACRO)
5263 cppReader_warning (pfile,
5264 message ("Undefining preprocessor builtin: %s",
5268 cppReader_deleteMacro (hp);
5271 if (cppReader_isPedantic (pfile)) {
5273 SKIP_WHITE_SPACE (buf);
5276 cppReader_pedwarnLit (pfile,
5277 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5286 * Report an error detected by the program we are processing.
5287 * Use the text of the line in the error message.
5288 * (We use error because it prints the filename & line#.)
5292 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5293 char *buf, char *limit)
5295 size_t length = size_fromInt (limit - buf);
5296 cstring copy = cstring_copyLength (buf, length);
5297 cstring adv = cstring_advanceWhiteSpace (copy);
5299 cppReader_error (pfile, message ("#error %s", adv));
5300 cstring_free (copy);
5305 * Report a warning detected by the program we are processing.
5306 * Use the text of the line in the warning message, then continue.
5307 * (We use error because it prints the filename & line#.)
5311 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5312 char *buf, char *limit)
5314 size_t length = size_fromInt (limit - buf);
5315 cstring copy = cstring_copyLength (buf, length);
5316 cstring adv = cstring_advanceWhiteSpace (copy);
5317 cppReader_warning (pfile, message ("#warning %s", adv));
5318 cstring_free (copy);
5323 /* #ident has already been copied to the output file, so just ignore it. */
5326 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5327 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5329 /* Allow #ident in system headers, since that's not user's fault. */
5330 if (cppReader_isPedantic (pfile)
5331 && !cppReader_getBufferSafe (pfile)->system_header_p)
5332 cppReader_pedwarnLit (pfile,
5333 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5335 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5340 /* #pragma and its argument line have already been copied to the output file.
5341 Just check for some recognized pragmas that need validation here. */
5344 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5345 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5347 while (*buf == ' ' || *buf == '\t')
5352 if (!strncmp (buf, "implementation", 14)) {
5353 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5354 been included yet. */
5355 struct file_name_list *ptr;
5356 char *p = buf + 14, *fname, *inc_fname;
5358 SKIP_WHITE_SPACE (p);
5359 if (*p == '\n' || *p != '\"')
5363 p = (char *) strchr (fname, '\"');
5364 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5366 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5368 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5369 inc_fname = (inc_fname != NULL)
5370 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5372 if ((inc_fname != NULL)
5373 && (strncmp (inc_fname, fname, fname_len) == 0))
5375 cpp_setLocation (pfile);
5377 ppllerror (message ("`#pragma implementation' for `%s' appears "
5378 "after file is included",
5379 cstring_fromChars (fname)));
5388 * handle #if command by
5389 * 1) inserting special `defined' keyword into the hash table
5390 * that gets turned into 0 or 1 by special_symbol (thus,
5391 * if the luser has a symbol called `defined' already, it won't
5392 * work inside the #if command)
5393 * 2) rescan the input into a temporary output buffer
5394 * 3) pass the output buffer to the yacc parser and collect a value
5395 * 4) clean up the mess left from steps 1 and 2.
5396 * 5) call conditional_skip to skip til the next #endif (etc.),
5397 * or not, depending on the value from step 3.
5401 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5402 char *buf, char *limit)
5404 HOST_WIDE_INT value;
5405 DPRINTF (("Do if: %s", buf));
5406 value = eval_if_expression (pfile, buf, limit - buf);
5407 conditional_skip (pfile, value == 0, T_IF, NULL);
5412 * handle a #elif directive by not changing if_stack either.
5413 * see the comment above do_else.
5416 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5417 char *buf, char *limit)
5419 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5421 cppReader_errorLit (pfile,
5422 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5427 llassert (pfile->if_stack != NULL);
5429 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5431 cppReader_errorLit (pfile,
5432 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5434 if (pfile->if_stack->fname != NULL
5435 && cppReader_getBufferSafe (pfile)->fname != NULL
5436 && !cstring_equal (pfile->if_stack->fname,
5437 cppReader_getBufferSafe (pfile)->nominal_fname))
5438 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5439 fprintf (stderr, ")\n");
5441 pfile->if_stack->type = T_ELIF;
5444 if (pfile->if_stack->if_succeeded)
5446 skip_if_group (pfile, 0);
5450 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5452 skip_if_group (pfile, 0);
5455 ++pfile->if_stack->if_succeeded; /* continue processing input */
5456 output_line_command (pfile, 1, same_file);
5464 * evaluate a #if expression in BUF, of length LENGTH,
5465 * then parse the result as a C expression and return the value as an int.
5468 static HOST_WIDE_INT
5469 eval_if_expression (cppReader *pfile,
5470 /*@unused@*/ char *buf,
5471 /*@unused@*/ int length)
5473 hashNode save_defined;
5474 HOST_WIDE_INT value;
5475 size_t old_written = cpplib_getWritten (pfile);
5477 DPRINTF (("Saving defined..."));
5478 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5479 pfile->pcp_inside_if = 1;
5481 value = cppReader_parseExpression (pfile);
5482 pfile->pcp_inside_if = 0;
5484 /* Clean up special symbol */
5485 DPRINTF (("Removing defined..."));
5486 cppReader_deleteMacro (save_defined);
5487 cppReader_setWritten (pfile, old_written); /* Pop */
5493 * routine to handle ifdef/ifndef. Try to look up the symbol,
5494 * then do or don't skip to the #endif/#else/#elif depending
5495 * on what directive is actually being processed.
5499 do_xifdef (cppReader *pfile, struct directive *keyword,
5500 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5503 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5505 size_t ident_length;
5506 enum cpp_token token;
5507 int start_of_file = 0;
5508 char *control_macro = 0;
5509 size_t old_written = cpplib_getWritten (pfile);
5511 DPRINTF (("do xifdef: %d",
5512 keyword->type == T_IFNDEF));
5514 /* Detect a #ifndef at start of file (not counting comments). */
5515 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5517 start_of_file = pfile->only_seen_white == 2;
5520 pfile->no_macro_expand++;
5521 token = get_directive_token (pfile);
5522 pfile->no_macro_expand--;
5524 ident = pfile->token_buffer + old_written;
5525 DPRINTF (("Ident: %s", ident));
5527 ident_length = cpplib_getWritten (pfile) - old_written;
5528 cppReader_setWritten (pfile, old_written); /* Pop */
5530 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5532 skip = (keyword->type == T_IFDEF);
5533 if (! cppReader_isTraditional (pfile))
5535 cppReader_pedwarn (pfile,
5536 message ("`#%s' with no argument", keyword->name));
5539 else if (token == CPP_NAME)
5541 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5543 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5545 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5547 if (start_of_file && !skip)
5549 DPRINTF (("Not skipping!"));
5550 control_macro = (char *) dmalloc (ident_length + 1);
5551 memcpy (control_macro, ident, ident_length + 1);
5556 skip = (keyword->type == T_IFDEF);
5557 if (! cppReader_isTraditional (pfile))
5559 cppReader_error (pfile,
5560 message ("`#%s' with invalid argument", keyword->name));
5564 if (!cppReader_isTraditional (pfile))
5567 cppSkipHspace (pfile);
5568 c = cppReader_peekC (pfile);
5569 if (c != EOF && c != '\n')
5571 cppReader_pedwarn (pfile,
5572 message ("garbage at end of `#%s' argument", keyword->name));
5576 cppReader_skipRestOfLine (pfile);
5578 DPRINTF (("Conditional skip: %d", skip));
5579 conditional_skip (pfile, skip, T_IF, control_macro);
5583 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5584 If this is a #ifndef starting at the beginning of a file,
5585 CONTROL_MACRO is the macro name tested by the #ifndef.
5586 Otherwise, CONTROL_MACRO is 0. */
5589 conditional_skip (cppReader *pfile, int skip,
5590 enum node_type type,
5591 /*@dependent@*/ char *control_macro)
5593 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5595 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5596 temp->next = pfile->if_stack;
5597 temp->control_macro = control_macro;
5599 temp->if_succeeded = 0;
5601 pfile->if_stack = temp;
5602 pfile->if_stack->type = type;
5606 skip_if_group (pfile, 0);
5611 ++pfile->if_stack->if_succeeded;
5612 output_line_command (pfile, 1, same_file);
5617 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5618 * leaves input ptr at the sharp sign found.
5619 * If ANY is nonzero, return at next directive of any sort.
5623 skip_if_group (cppReader *pfile, int any)
5626 struct directive *kt;
5627 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5628 register int ident_length;
5630 struct parse_marker line_start_mark;
5632 parseSetMark (&line_start_mark, pfile);
5634 if (CPPOPTIONS (pfile)->output_conditionals) {
5635 static char failed[] = "#failed\n";
5636 cppReader_puts (pfile, failed, sizeof(failed)-1);
5638 output_line_command (pfile, 1, same_file);
5642 if (CPPOPTIONS (pfile)->output_conditionals)
5644 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5647 llassert (pbuf->buf != NULL);
5649 start_line = pbuf->buf + line_start_mark.position;
5650 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5653 parseMoveMark (&line_start_mark, pfile);
5655 if (!cppReader_isTraditional (pfile))
5657 cppSkipHspace (pfile);
5660 c = cppReader_getC (pfile);
5663 size_t old_written = cpplib_getWritten (pfile);
5664 cppSkipHspace (pfile);
5666 parse_name (pfile, cppReader_getC (pfile));
5667 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5668 ident = pfile->token_buffer + old_written;
5669 pfile->limit = ident;
5671 for (kt = directive_table; kt->length >= 0; kt++)
5673 cppIfStackFrame *temp;
5674 if (ident_length == kt->length
5675 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5677 /* If we are asked to return on next directive, do so now. */
5688 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5689 temp->next = pfile->if_stack;
5690 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5691 temp->type = kt->type;
5693 temp->if_succeeded = 0;
5694 temp->control_macro = NULL;
5696 pfile->if_stack = temp;
5697 /*@switchbreak@*/ break;
5700 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5701 validate_else (pfile,
5702 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5705 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5707 cppReader_error (pfile,
5708 message ("Preprocessor command #%s is not within a conditional", kt->name));
5709 /*@switchbreak@*/ break;
5711 else if (pfile->if_stack == save_if_stack)
5713 goto done; /* found what we came for */
5720 if (kt->type != T_ENDIF)
5722 llassert (pfile->if_stack != NULL);
5724 if (pfile->if_stack->type == T_ELSE)
5726 cppReader_errorLit (pfile,
5727 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5730 pfile->if_stack->type = kt->type;
5731 /*@switchbreak@*/ break;
5734 temp = pfile->if_stack;
5735 llassert (temp != NULL);
5736 pfile->if_stack = temp->next;
5738 /*@switchbreak@*/ break;
5741 #if defined (OS2) && defined (__IBMC__)
5742 /* Dummy code to eleminate optimization problems with icc */
5751 /* Don't let erroneous code go by. */
5753 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5754 && cppReader_isPedantic (pfile))
5756 cppReader_pedwarnLit (pfile,
5757 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5761 c = cppReader_getC (pfile);
5763 /* We're in the middle of a line. Skip the rest of it. */
5771 case '/': /* possible comment */
5772 c = skip_comment (pfile, NULL);
5775 /*@switchbreak@*/ break;
5778 cppReader_forward (pfile, -1);
5779 old = cpplib_getWritten (pfile);
5780 (void) cpplib_getToken (pfile);
5781 cppReader_setWritten (pfile, old);
5782 /*@switchbreak@*/ break;
5784 /* Char after backslash loses its special meaning. */
5785 if (cppReader_peekC (pfile) == '\n')
5787 cppReader_forward (pfile, 1);
5790 /*@switchbreak@*/ break;
5794 c = cppReader_getC (pfile);
5797 if (CPPOPTIONS (pfile)->output_conditionals) {
5798 static char end_failed[] = "#endfailed\n";
5799 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5802 pfile->only_seen_white = 1;
5804 parseGotoMark (&line_start_mark, pfile);
5805 parseClearMark (&line_start_mark);
5809 * handle a #else directive. Do this by just continuing processing
5810 * without changing if_stack ; this is so that the error message
5811 * for missing #endif's etc. will point to the original #if. It
5812 * is possible that something different would be better.
5816 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5817 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5819 if (cppReader_isPedantic (pfile))
5821 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5824 cppReader_skipRestOfLine (pfile);
5826 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5827 cppReader_errorLit (pfile,
5828 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5831 /* #ifndef can't have its special treatment for containing the whole file
5832 if it has a #else clause. */
5834 llassert (pfile->if_stack != NULL);
5836 pfile->if_stack->control_macro = 0;
5838 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5840 cpp_setLocation (pfile);
5841 genppllerrorhint (FLG_PREPROC,
5842 message ("Pre-processor directive #else after #else"),
5843 message ("%q: Location of match",
5844 fileloc_unparseRaw (pfile->if_stack->fname,
5845 pfile->if_stack->lineno)));
5848 pfile->if_stack->type = T_ELSE;
5851 if (pfile->if_stack->if_succeeded)
5852 skip_if_group (pfile, 0);
5854 ++pfile->if_stack->if_succeeded; /* continue processing input */
5855 output_line_command (pfile, 1, same_file);
5862 * unstack after #endif command
5866 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5867 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5869 if (cppReader_isPedantic (pfile))
5871 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5874 cppReader_skipRestOfLine (pfile);
5876 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5878 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5882 cppIfStackFrame *temp = pfile->if_stack;
5884 llassert (temp != NULL);
5886 pfile->if_stack = temp->next;
5887 if (temp->control_macro != 0)
5889 /* This #endif matched a #ifndef at the start of the file.
5890 See if it is at the end of the file. */
5891 struct parse_marker start_mark;
5894 parseSetMark (&start_mark, pfile);
5898 cppSkipHspace (pfile);
5899 c = cppReader_getC (pfile);
5905 parseGotoMark (&start_mark, pfile);
5906 parseClearMark (&start_mark);
5910 /* If we get here, this #endif ends a #ifndef
5911 that contains all of the file (aside from whitespace).
5912 Arrange not to include the file again
5913 if the macro that was tested is defined.
5915 Do not do this for the top-level file in a -include or any
5916 file in a -imacros. */
5917 struct file_name_list *ifile = pfile->all_include_files;
5919 for ( ; ifile != NULL; ifile = ifile->next)
5921 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5923 ifile->control_macro = temp->control_macro;
5931 output_line_command (pfile, 1, same_file);
5936 /* When an #else or #endif is found while skipping failed conditional,
5937 if -pedantic was specified, this is called to warn about text after
5938 the command name. P points to the first char after the command name. */
5941 validate_else (cppReader *pfile, cstring directive)
5944 cppSkipHspace (pfile);
5945 c = cppReader_peekC (pfile);
5946 if (c != EOF && c != '\n')
5948 cppReader_pedwarn (pfile,
5949 message ("text following `%s' violates ANSI standard", directive));
5954 ** Get the next token, and add it to the text in pfile->token_buffer.
5955 ** Return the kind of token we got.
5959 cpplib_getToken (cppReader *pfile)
5961 return cpplib_getTokenAux (pfile, FALSE);
5965 cpplib_getTokenForceExpand (cppReader *pfile)
5967 return cpplib_getTokenAux (pfile, TRUE);
5971 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5974 size_t old_written = 0;
5975 int start_line, start_column;
5976 enum cpp_token token;
5977 struct cppOptions *opts = CPPOPTIONS (pfile);
5978 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5981 c = cppReader_getC (pfile);
5982 DPRINTF (("Get next token: %c", c));
5987 if (cppReader_getBufferSafe (pfile)->seen_eof)
5989 cppBuffer *buf = cppReader_popBuffer (pfile);
5991 if (buf != cppReader_nullBuffer (pfile))
6002 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6003 cppReader_getBufferSafe (pfile)->seen_eof = 1;
6005 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
6006 && next_buf != cppReader_nullBuffer (pfile))
6008 /* We're about to return from an #include file.
6009 Emit #line information now (as part of the CPP_POP) result.
6010 But the #line refers to the file we will pop to. */
6011 cppBuffer *cur_buffer = CPPBUFFER (pfile);
6012 CPPBUFFER (pfile) = next_buf;
6013 pfile->input_stack_listing_current = 0;
6014 output_line_command (pfile, 0, leave_file);
6015 CPPBUFFER (pfile) = cur_buffer;
6023 struct parse_marker start_mark;
6028 if (cppReader_peekC (pfile) == '=')
6033 if (opts->put_out_comments)
6035 parseSetMark (&start_mark, pfile);
6039 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6040 &start_line, &start_column);
6041 c = skip_comment (pfile, &newlines);
6042 DPRINTF (("c = %c", c));
6043 if (opts->put_out_comments && (c == '/' || c == EOF))
6045 assertSet (&start_mark);
6046 parseClearMark (&start_mark);
6053 cppReader_errorWithLine (pfile, start_line, start_column,
6054 cstring_makeLiteral ("Unterminated comment"));
6057 c = '/'; /* Initial letter of comment. */
6059 /* Comments are equivalent to spaces.
6060 For -traditional, a comment is equivalent to nothing. */
6062 if (opts->put_out_comments)
6066 assertSet (&start_mark);
6067 res = cpp_handleComment (pfile, &start_mark);
6068 pfile->lineno += newlines;
6071 else if (cppReader_isTraditional (pfile))
6077 cpplib_reserve(pfile, 1);
6078 cppReader_putCharQ (pfile, ' ');
6083 if (!pfile->only_seen_white)
6088 if (cppReader_handleDirective (pfile))
6090 return CPP_DIRECTIVE;
6093 pfile->only_seen_white = 0;
6098 /* A single quoted string is treated like a double -- some
6099 programs (e.g., troff) are perverse this way */
6100 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6101 &start_line, &start_column);
6102 old_written = cpplib_getWritten (pfile);
6104 DPRINTF (("Put char: %c", c));
6105 cppReader_putChar (pfile, c);
6108 int cc = cppReader_getC (pfile);
6109 DPRINTF (("cc: %c", c));
6112 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6114 /* try harder: this string crosses a macro expansion
6115 boundary. This can happen naturally if -traditional.
6116 Otherwise, only -D can make a macro with an unmatched
6119 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6120 (*cppReader_getBufferSafe (pfile)->cleanup)
6121 (cppReader_getBufferSafe (pfile), pfile);
6122 CPPBUFFER (pfile) = next_buf;
6125 if (!cppReader_isTraditional (pfile))
6127 cpp_setLocation (pfile);
6129 setLine (long_toInt (start_line));
6130 setColumn (long_toInt (start_column));
6132 if (pfile->multiline_string_line != long_toInt (start_line)
6133 && pfile->multiline_string_line != 0)
6137 message ("Unterminated string or character constant"),
6138 message ("%q: Possible real start of unterminated constant",
6140 (fileloc_filename (g_currentloc),
6141 pfile->multiline_string_line)));
6142 pfile->multiline_string_line = 0;
6148 message ("Unterminated string or character constant"));
6151 /*@loopbreak@*/ break;
6153 DPRINTF (("putting char: %c", cc));
6154 cppReader_putChar (pfile, cc);
6158 /* Traditionally, end of line ends a string constant with
6159 no error. So exit the loop and record the new line. */
6160 if (cppReader_isTraditional (pfile))
6166 if (cppReader_isPedantic (pfile)
6167 && pfile->multiline_string_line == 0)
6169 cppReader_pedwarnWithLine
6170 (pfile, long_toInt (start_line),
6171 long_toInt (start_column),
6172 cstring_makeLiteral ("String constant runs past end of line"));
6174 if (pfile->multiline_string_line == 0)
6176 pfile->multiline_string_line = start_line;
6179 /*@switchbreak@*/ break;
6182 cc = cppReader_getC (pfile);
6185 /* Backslash newline is replaced by nothing at all. */
6186 cppReader_adjustWritten (pfile, -1);
6191 /* ANSI stupidly requires that in \\ the second \
6192 is *not* prevented from combining with a newline. */
6195 cppReader_putChar (pfile, cc);
6197 /*@switchbreak@*/ break;
6203 /*@switchbreak@*/ break;
6207 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6208 cpplib_getPWritten (pfile));
6209 pfile->only_seen_white = 0;
6210 return c == '\'' ? CPP_CHAR : CPP_STRING;
6213 if (!opts->dollars_in_ident)
6218 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6226 c2 = cppReader_peekC (pfile);
6227 if (c2 == c || c2 == '=')
6237 if (cppReader_peekC (pfile) == '=')
6243 c2 = cppReader_peekC (pfile);
6244 if (c2 == '-' && opts->chill)
6246 /* Chill style comment */
6247 if (opts->put_out_comments)
6249 parseSetMark (&start_mark, pfile);
6252 cppReader_forward (pfile, 1); /* Skip second '-'. */
6256 c = cppReader_getC (pfile);
6258 /*@loopbreak@*/ break;
6261 /* Don't consider final '\n' to be part of comment. */
6262 cppReader_forward (pfile, -1);
6263 /*@loopbreak@*/ break;
6267 goto return_comment;
6269 if (c2 == '-' || c2 == '=' || c2 == '>')
6274 if (pfile->parsing_include_directive)
6278 cppReader_putChar (pfile, c);
6280 /*@loopbreak@*/ break;
6281 c = cppReader_getC (pfile);
6283 if (c == '\n' || c == EOF)
6285 cppReader_errorLit (pfile,
6286 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6287 /*@loopbreak@*/ break;
6295 c2 = cppReader_peekC (pfile);
6300 cppReader_forward (pfile, 1);
6301 cpplib_reserve (pfile, 4);
6302 cppReader_putChar (pfile, c);
6303 cppReader_putChar (pfile, c2);
6305 c3 = cppReader_peekC (pfile);
6307 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6308 cppReader_nullTerminateQ (pfile);
6309 pfile->only_seen_white = 0;
6313 DPRINTF (("Macro @!"));
6314 if (cppReader_getBufferSafe (pfile)->has_escapes)
6316 c = cppReader_getC (pfile);
6317 DPRINTF (("got c: %c", c));
6320 if (pfile->output_escapes)
6321 cppReader_puts (pfile, "@-", 2);
6322 parse_name (pfile, cppReader_getC (pfile));
6325 else if (is_space [c])
6327 cpplib_reserve (pfile, 2);
6328 if (pfile->output_escapes)
6329 cppReader_putCharQ (pfile, '@');
6330 cppReader_putCharQ (pfile, c);
6338 if (pfile->output_escapes)
6340 cppReader_puts (pfile, "@@", 2);
6346 c2 = cppReader_peekC (pfile);
6349 cpplib_reserve(pfile, 2);
6350 cppReader_putCharQ (pfile, '.');
6351 c = cppReader_getC (pfile);
6355 /* FIXME - misses the case "..\\\n." */
6356 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6358 cpplib_reserve(pfile, 4);
6359 cppReader_putCharQ (pfile, '.');
6360 cppReader_putCharQ (pfile, '.');
6361 cppReader_putCharQ (pfile, '.');
6362 cppReader_forward (pfile, 2);
6363 cppReader_nullTerminateQ (pfile);
6364 pfile->only_seen_white = 0;
6370 pfile->only_seen_white = 0;
6372 cpplib_reserve(pfile, 3);
6373 cppReader_putCharQ (pfile, c);
6374 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6375 cppReader_nullTerminateQ (pfile);
6380 c2 = cppReader_peekC (pfile);
6381 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6383 cppReader_putChar (pfile, c);
6384 c = cppReader_getC (pfile);
6389 case '0': case '1': case '2': case '3': case '4':
6390 case '5': case '6': case '7': case '8': case '9':
6395 cpplib_reserve (pfile, 2);
6396 cppReader_putCharQ (pfile, c);
6398 c = cppReader_peekC (pfile);
6400 /*@loopbreak@*/ break;
6401 if (!is_idchar[c] && c != '.'
6402 && ((c2 != 'e' && c2 != 'E'
6403 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6404 || (c != '+' && c != '-')))
6405 /*@loopbreak@*/ break;
6406 cppReader_forward (pfile, 1);
6410 cppReader_nullTerminateQ (pfile);
6411 pfile->only_seen_white = 0;
6414 case 'b': case 'c': case 'd': case 'h': case 'o':
6415 case 'B': case 'C': case 'D': case 'H': case 'O':
6416 if (opts->chill && cppReader_peekC (pfile) == '\'')
6418 pfile->only_seen_white = 0;
6419 cpplib_reserve (pfile, 2);
6420 cppReader_putCharQ (pfile, c);
6421 cppReader_putCharQ (pfile, '\'');
6422 cppReader_forward (pfile, 1);
6425 c = cppReader_getC (pfile);
6427 goto chill_number_eof;
6430 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6432 cppReader_forward (pfile, 2);
6435 /*@loopbreak@*/ break;
6437 cppReader_putChar (pfile, c);
6441 cpplib_reserve (pfile, 2);
6442 cppReader_putCharQ (pfile, c);
6443 cppReader_nullTerminateQ (pfile);
6448 cppReader_forward (pfile, -1);
6450 cppReader_nullTerminate (pfile);
6457 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6458 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6459 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6460 case 'x': case 'y': case 'z':
6461 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6462 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6463 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6469 size_t before_name_written = cpplib_getWritten (pfile);
6471 parse_name (pfile, c);
6472 pfile->only_seen_white = 0;
6474 if (pfile->no_macro_expand)
6476 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6480 ident = pfile->token_buffer + before_name_written;
6481 DPRINTF (("Ident: %s", ident));
6483 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6485 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6489 DPRINTF (("No expand: %s %d", ident, ident_len));
6493 if (hp->type == T_DISABLED)
6495 DPRINTF (("Disabled!"));
6497 if (pfile->output_escapes)
6498 { /* Return "@-IDENT", followed by '\0'. */
6500 cpplib_reserve (pfile, 3);
6501 ident = pfile->token_buffer + before_name_written;
6502 cppReader_adjustWritten (pfile, 2);
6504 for (i = size_toInt (ident_len); i >= 0; i--)
6506 ident[i+2] = ident[i];
6516 ** If macro wants an arglist, verify that a '(' follows.
6517 ** first skip all whitespace, copying it to the output
6518 ** after the macro name. Then, if there is no '(',
6519 ** decide this is not a macro call and leave things that way.
6522 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6524 struct parse_marker macro_mark;
6527 DPRINTF (("Arglist macro!"));
6530 ** evans 2002-07-03: Moved this here (from below).
6531 ** This bug caused necessary whitespace to be lost
6532 ** when parsing parameterized macros without parameters.
6535 parseSetMark (¯o_mark, pfile);
6537 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6539 cppBuffer *next_buf;
6540 cppSkipHspace (pfile);
6541 if (cppReader_peekC (pfile) != EOF)
6543 DPRINTF (("Peeking!"));
6544 /*@loopbreak@*/ break;
6547 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6548 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6549 CPPBUFFER (pfile) = next_buf;
6552 /* parseSetMark (¯o_mark, pfile); */
6556 cppSkipHspace (pfile);
6557 c = cppReader_peekC (pfile);
6558 DPRINTF (("c: %c", c));
6559 is_macro_call = c == '(';
6561 /*@loopbreak@*/ break;
6562 cppReader_forward (pfile, 1);
6567 parseGotoMark (¯o_mark, pfile);
6570 parseClearMark (¯o_mark);
6574 DPRINTF (("not macro call!"));
6579 /* This is now known to be a macro call. */
6581 /* it might not actually be a macro. */
6582 if (hp->type != T_MACRO)
6587 cppReader_setWritten (pfile, before_name_written);
6588 special_symbol (hp, pfile);
6589 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6590 xbuf = (char *) dmalloc (xbuf_len + 1);
6591 cppReader_setWritten (pfile, before_name_written);
6592 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6593 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6598 ** Expand the macro, reading arguments as needed,
6599 ** and push the expansion on the input stack.
6602 cpplib_macroExpand (pfile, hp);
6603 cppReader_setWritten (pfile, before_name_written);
6606 /* An extra "@ " is added to the end of a macro expansion
6607 to prevent accidental token pasting. We prefer to avoid
6608 unneeded extra spaces (for the sake of cpp-using tools like
6609 imake). Here we remove the space if it is safe to do so. */
6611 llassert (pfile->buffer->rlimit != NULL);
6613 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6614 && pfile->buffer->rlimit[-2] == '@'
6615 && pfile->buffer->rlimit[-1] == ' ')
6617 int c1 = pfile->buffer->rlimit[-3];
6618 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6620 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6621 pfile->buffer->rlimit -= 2;
6627 case ' ': case '\t': case '\v': case '\r':
6630 cppReader_putChar (pfile, c);
6631 c = cppReader_peekC (pfile);
6632 if (c == EOF || !is_hor_space[c])
6633 /*@loopbreak@*/ break;
6634 cppReader_forward (pfile, 1);
6639 c2 = cppReader_peekC (pfile);
6646 cppReader_putChar (pfile, c);
6647 if (pfile->only_seen_white == 0)
6648 pfile->only_seen_white = 1;
6650 output_line_command (pfile, 1, same_file);
6653 case '(': token = CPP_LPAREN; goto char1;
6654 case ')': token = CPP_RPAREN; goto char1;
6655 case '{': token = CPP_LBRACE; goto char1;
6656 case '}': token = CPP_RBRACE; goto char1;
6657 case ',': token = CPP_COMMA; goto char1;
6658 case ';': token = CPP_SEMICOLON; goto char1;
6664 pfile->only_seen_white = 0;
6665 cppReader_putChar (pfile, c);
6674 /* Parse an identifier starting with C. */
6677 parse_name (cppReader *pfile, int c)
6683 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6685 cppReader_forward (pfile, 2);
6689 cppReader_forward (pfile, -1);
6693 if (c == '$' && cppReader_isPedantic (pfile))
6695 cppReader_pedwarnLit (pfile,
6696 cstring_makeLiteralTemp ("`$' in identifier"));
6699 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6700 cppReader_putCharQ (pfile, c);
6701 c = cppReader_getC (pfile);
6707 cppReader_nullTerminateQ (pfile);
6710 /* The file_name_map structure holds a mapping of file names for a
6711 particular directory. This mapping is read from the file named
6712 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6713 map filenames on a file system with severe filename restrictions,
6714 such as DOS. The format of the file name map file is just a series
6715 of lines with two tokens on each line. The first token is the name
6716 to map, and the second token is the actual name to use. */
6718 struct file_name_map
6720 struct file_name_map *map_next;
6725 /*@constant observer char *FILE_NAME_MAP_FILE*/
6726 #define FILE_NAME_MAP_FILE "header.gcc"
6728 /* Read a space delimited string of unlimited length from a stdio
6731 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6737 set = alloc = dmalloc (len + 1);
6742 while ((ch = getc (f)) != EOF && ! is_space[ch])
6744 if (set - alloc == size_toInt (len))
6747 alloc = drealloc (alloc, len + 1);
6748 set = alloc + len / 2;
6749 /*@-branchstate@*/ }
6752 } /*@=branchstate@*/
6755 check (ungetc (ch, f) != EOF);
6757 return cstring_fromChars (alloc);
6760 /* This structure holds a linked list of file name maps, one per directory. */
6762 struct file_name_map_list
6764 /*@only@*/ struct file_name_map_list *map_list_next;
6765 /*@only@*/ cstring map_list_name;
6766 /*@null@*/ struct file_name_map *map_list_map;
6769 /* Read the file name map file for DIRNAME. */
6771 static struct file_name_map *
6772 read_name_map (cppReader *pfile, cstring dirname)
6774 struct file_name_map_list *map_list_ptr;
6778 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6779 map_list_ptr != NULL;
6780 map_list_ptr = map_list_ptr->map_list_next)
6782 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6784 return map_list_ptr->map_list_map;
6788 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6789 map_list_ptr->map_list_name = cstring_copy (dirname);
6790 map_list_ptr->map_list_map = NULL;
6792 name = cstring_copy (dirname);
6794 if (cstring_length (dirname) > 0)
6796 name = cstring_appendChar (name, CONNECTCHAR);
6799 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6801 f = fileTable_openReadFile (context_fileTable (), name);
6802 cstring_free (name);
6806 map_list_ptr->map_list_map = NULL;
6812 while ((ch = getc (f)) != EOF)
6815 struct file_name_map *ptr;
6822 from = read_filename_string (ch, f);
6823 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6828 to = read_filename_string (ch, f);
6830 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6831 ptr->map_from = from;
6833 /* Make the real filename absolute. */
6834 if (cstring_length (to) > 1
6835 && osd_isConnectChar (cstring_firstChar (to)))
6841 ptr->map_to = cstring_copy (dirname);
6842 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6843 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6846 ptr->map_next = map_list_ptr->map_list_map;
6847 map_list_ptr->map_list_map = ptr;
6849 while ((ch = getc (f)) != '\n')
6853 /*@innerbreak@*/ break;
6858 assertSet (map_list_ptr->map_list_map);
6859 check (fileTable_closeFile (context_fileTable (),f) == 0);
6862 map_list_ptr->map_list_next = pfile->opts->map_list;
6863 pfile->opts->map_list = map_list_ptr;
6865 return map_list_ptr->map_list_map;
6868 /* Try to open include file FILENAME. SEARCHPTR is the directory
6869 being tried from the include file search path. This function maps
6870 filenames on file systems based on information read by
6874 open_include_file (cppReader *pfile,
6876 struct file_name_list *searchptr)
6878 char *filename = cstring_toCharsSafe (fname);
6879 struct file_name_map *map;
6883 cstring_markOwned (fname);
6885 cpp_setLocation (pfile);
6887 if (context_getFlag (FLG_NEVERINCLUDE))
6889 if (isHeaderFile (fname))
6891 return SKIP_INCLUDE;
6895 if ((searchptr != NULL) && ! searchptr->got_name_map)
6897 searchptr->name_map = read_name_map (pfile,
6898 !cstring_isEmpty (searchptr->fname)
6899 ? searchptr->fname :
6900 cstring_makeLiteralTemp ("."));
6901 searchptr->got_name_map = 1;
6904 /* First check the mapping for the directory we are using. */
6906 if ((searchptr != NULL)
6907 && (searchptr->name_map != NULL))
6911 if (!cstring_isEmpty (searchptr->fname))
6913 from += cstring_length (searchptr->fname) + 1;
6916 for (map = searchptr->name_map;
6918 map = map->map_next)
6920 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6923 ** Found a match. Check if the file should be skipped
6926 if (cpp_skipIncludeFile (map->map_to))
6928 return SKIP_INCLUDE;
6932 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6939 ** Try to find a mapping file for the particular directory we are
6940 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6941 ** in /usr/include/header.gcc and look up types.h in
6942 ** /usr/include/sys/header.gcc.
6945 p = strrchr (filename, CONNECTCHAR);
6952 if ((searchptr != NULL)
6953 && (cstring_isDefined (searchptr->fname))
6954 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6955 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6957 size_fromInt (p - filename)))
6959 /* filename is in SEARCHPTR, which we've already checked. */
6961 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6963 return SKIP_INCLUDE;
6967 return cpp_openIncludeFile (filename);
6973 dir = mstring_copy (".");
6978 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6979 memcpy (dir, filename, size_fromInt (p - filename));
6980 dir[p - filename] = '\0';
6984 for (map = read_name_map (pfile, cstring_fromChars (dir));
6986 map = map->map_next)
6988 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6992 if (cpp_skipIncludeFile (map->map_to))
6994 return SKIP_INCLUDE;
6998 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
7005 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
7007 return SKIP_INCLUDE;
7011 return cpp_openIncludeFile (filename);
7015 /* Process the contents of include file FNAME, already open on descriptor F,
7017 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
7018 "system" include directories (as decided by the `is_system_include'
7020 DIRPTR is the link in the dir path through which this file was found,
7021 or 0 if the file name was absolute or via the current directory.
7022 Return 1 on success, 0 on failure.
7024 The caller is responsible for the cppReader_pushBuffer. */
7027 finclude (cppReader *pfile, int f,
7029 bool system_header_p,
7030 /*@dependent@*/ struct file_name_list *dirptr)
7036 cppBuffer *fp; /* For input stack frame */
7038 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7040 cppReader_perrorWithName (pfile, fname);
7041 check (close (f) == 0);
7042 (void) cppReader_popBuffer (pfile);
7048 fp = cppReader_getBufferSafe (pfile);
7050 /*@-temptrans@*/ /* fname shouldn't really be temp */
7051 fp->nominal_fname = fp->fname = fname;
7055 fp->system_header_p = system_header_p;
7058 fp->cleanup = cppReader_fileCleanup;
7060 if (S_ISREG (st_mode))
7063 fp->buf = (char *) dmalloc (st_size + 2);
7064 fp->alimit = fp->buf + st_size + 2;
7067 /* Read the file contents, knowing that st_size is an upper bound
7068 on the number of bytes we can read. */
7069 length = safe_read (f, fp->buf, size_toInt (st_size));
7070 fp->rlimit = fp->buf + length;
7071 if (length < 0) goto nope;
7073 else if (S_ISDIR (st_mode))
7075 cppReader_error (pfile,
7076 message ("Directory specified where file is expected: %s", fname));
7077 check (close (f) == 0);
7083 ** Cannot count its file size before reading.
7084 ** First read the entire file into heap and
7085 ** copy them into buffer on stack.
7088 size_t bsize = 2000;
7093 fp->buf = (char *) dmalloc (bsize + 2);
7096 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7099 goto nope; /* error! */
7102 if (st_size != bsize)
7104 break; /* End of file */
7108 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7112 length = size_toInt (st_size);
7115 if ((length > 0 && fp->buf[length - 1] != '\n')
7116 /* Backslash-newline at end is not good enough. */
7117 || (length > 1 && fp->buf[length - 2] == '\\')) {
7118 fp->buf[length++] = '\n';
7121 fp->buf[length] = '\0';
7122 fp->rlimit = fp->buf + length;
7124 /* Close descriptor now, so nesting does not use lots of descriptors. */
7125 check (close (f) == 0);
7127 /* Must do this before calling trigraph_pcp, so that the correct file name
7128 will be printed in warning messages. */
7130 pfile->input_stack_listing_current = 0;
7135 cppReader_perrorWithName (pfile, fname);
7136 check (close (f) == 0);
7142 cpplib_init (cppReader *pfile)
7144 memset ((char *) pfile, 0, sizeof (*pfile));
7146 pfile->get_token = cpplib_getToken;
7147 pfile->token_buffer_size = 200;
7148 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7149 pfile->all_include_files = NULL;
7153 cppReader_setWritten (pfile, 0);
7155 pfile->system_include_depth = 0;
7156 pfile->max_include_len = 0;
7157 pfile->timebuf = NULL;
7158 pfile->only_seen_white = 1;
7160 pfile->buffer = cppReader_nullBuffer (pfile);
7164 cppReader_finish (/*@unused@*/ cppReader *pfile)
7169 /* Free resources used by PFILE.
7170 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7173 cppCleanup (/*@special@*/ cppReader *pfile)
7175 /*@releases pfile@*/
7177 DPRINTF (("cppCleanup!"));
7179 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7181 (void) cppReader_popBuffer (pfile);
7184 if (pfile->token_buffer != NULL)
7186 sfree (pfile->token_buffer);
7187 pfile->token_buffer = NULL;
7190 while (pfile->if_stack != NULL)
7192 cppIfStackFrame *temp = pfile->if_stack;
7193 pfile->if_stack = temp->next;
7197 while (pfile->all_include_files != NULL)
7199 struct file_name_list *temp = pfile->all_include_files;
7200 pfile->all_include_files = temp->next;
7201 /*@-dependenttrans@*/
7202 cstring_free (temp->fname);
7203 /*@=dependenttrans@*/
7207 /* evans 2002-07-12 */
7208 while (pfile->opts->map_list != NULL)
7210 struct file_name_map_list *temp = pfile->opts->map_list;
7211 pfile->opts->map_list = pfile->opts->map_list->map_list_next;
7212 cstring_free (temp->map_list_name);
7216 while (pfile->opts->include != NULL)
7218 struct file_name_list *temp = pfile->opts->include;
7219 pfile->opts->include = pfile->opts->include->next;
7220 /* cstring_free (temp->fname); */
7224 sfree (pfile->opts);
7226 cppReader_hashCleanup ();
7230 ** Get the file-mode and data size of the file open on FD
7231 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7235 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7239 if (fstat (fd, &sbuf) < 0) {
7245 if (mode_pointer != NULL)
7247 *mode_pointer = sbuf.st_mode;
7250 if (size_pointer != NULL)
7252 *size_pointer = (size_t) sbuf.st_size;
7258 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7259 retrying if necessary. Return a negative value if an error occurs,
7260 otherwise return the actual number of bytes read,
7261 which must be LEN unless end-of-file was reached. */
7263 static int safe_read (int desc, char *ptr, int len)
7269 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7270 /*@-compdef@*/ /* ptr is an out parameter */
7271 int nchars = _read (desc, ptr, (unsigned) left);
7274 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7283 return (int) nchars;
7297 /* Initialize PMARK to remember the current position of PFILE. */
7300 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7302 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7304 pmark->next = pbuf->marks;
7306 pbuf->marks = pmark;
7310 pmark->position = pbuf->cur - pbuf->buf;
7311 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
7314 /* Cleanup PMARK - we no longer need it. */
7316 void parseClearMark (struct parse_marker *pmark)
7318 struct parse_marker **pp = &pmark->buf->marks;
7320 for (; ; pp = &(*pp)->next)
7322 llassert (*pp != NULL);
7323 if (*pp == pmark) break;
7329 /* Backup the current position of PFILE to that saved in PMARK. */
7332 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7334 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7336 if (pbuf != pmark->buf)
7338 cpp_setLocation (pfile);
7339 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7342 llassert (pbuf->buf != NULL);
7343 pbuf->cur = pbuf->buf + pmark->position;
7344 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
7347 /* Reset PMARK to point to the current position of PFILE. (Same
7348 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7351 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7353 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7355 if (pbuf != pmark->buf)
7357 cpp_setLocation (pfile);
7358 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7361 pmark->position = pbuf->cur - pbuf->buf;
7362 DPRINTF (("move mark: %s", pmark->position));
7365 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7367 struct cppOptions *opts = CPPOPTIONS (pfile);
7370 /* The code looks at the defaults through this pointer, rather than through
7371 the constant structure above. This pointer gets changed if an environment
7372 variable specifies other defaults. */
7374 struct default_include *include_defaults = include_defaults_array;
7376 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7377 /* There seems to be confusion about what CPATH should do,
7378 so for the moment it is not documented. */
7379 /* Some people say that CPATH should replace the standard include dirs,
7380 but that seems pointless: it comes before them, so it overrides them
7383 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7385 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7387 path_include (pfile, cstring_toCharsSafe (xp));
7390 /* Now that dollars_in_ident is known, initialize is_idchar. */
7391 initialize_char_syntax (opts);
7393 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7394 and option processing. */
7396 initialize_builtins (pfile);
7398 /* Do standard #defines and assertions
7399 that identify system and machine type. */
7401 if (!opts->inhibit_predefs) {
7402 char *p = (char *) dmalloc (strlen (predefs) + 1);
7403 strcpy (p, predefs);
7409 while (*p == ' ' || *p == '\t')
7414 /* Handle -D options. */
7415 if (p[0] == '-' && p[1] == 'D')
7419 while (*p && *p != ' ' && *p != '\t')
7429 if (opts->debug_output)
7431 output_line_command (pfile, 0, same_file);
7434 cppReader_define (pfile, q);
7436 while (*p == ' ' || *p == '\t')
7450 opts->done_initializing = 1;
7452 { /* Read the appropriate environment variable and if it exists
7453 replace include_defaults with the listed path. */
7457 int win32_buf_size = 0; /* memory we need to allocate */
7460 if (opts->cplusplus)
7462 epath = getenv ("CPLUS_INCLUDE_PATH");
7466 epath = getenv ("C_INCLUDE_PATH");
7470 ** If the environment var for this language is set,
7471 ** add to the default list of include directories.
7474 if (epath != NULL) {
7475 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7477 char *startp, *endp;
7480 /* if we have a posix path list, convert to win32 path list */
7481 if (cygwin32_posix_path_list_p (epath))
7483 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7484 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7485 cygwin32_posix_to_win32_path_list (epath, win32epath);
7489 for (num_dirs = 1, startp = epath; *startp; startp++)
7491 if (*startp == PATH_SEPARATOR)
7497 = (struct default_include *) dmalloc ((num_dirs
7498 * sizeof (struct default_include))
7499 + sizeof (include_defaults_array));
7502 startp = endp = epath;
7505 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7506 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7508 strncpy (nstore, startp, size_fromInt (endp - startp));
7511 strcpy (nstore, ".");
7515 nstore[endp-startp] = '\0';
7518 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7519 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7520 include_defaults[num_dirs].cxx_aware = 1;
7527 endp = startp = endp + 1;
7534 /* Put the usual defaults back in at the end. */
7535 memcpy ((char *) &include_defaults[num_dirs],
7536 (char *) include_defaults_array,
7537 sizeof (include_defaults_array));
7540 /*@-branchstate@*/ } /*@=branchstate@*/
7543 cppReader_appendIncludeChain (pfile, opts->before_system,
7544 opts->last_before_system);
7546 opts->first_system_include = opts->before_system;
7548 /* Unless -fnostdinc,
7549 tack on the standard include file dirs to the specified list */
7551 if (!opts->no_standard_includes) {
7552 struct default_include *p = include_defaults;
7553 char *specd_prefix = opts->include_prefix;
7554 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7555 size_t default_len = 0;
7557 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7558 if (default_prefix != NULL) {
7559 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7560 default_len = strlen (default_prefix) - 7;
7561 default_prefix[default_len] = 0;
7565 /* Search "translated" versions of GNU directories.
7566 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7567 if (specd_prefix != 0 && default_len != 0)
7568 for (p = include_defaults; p->fname != NULL; p++) {
7569 /* Some standard dirs are only for C++. */
7571 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7572 /* Does this dir start with the prefix? */
7573 llassert (default_prefix != NULL);
7575 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix, default_len))
7577 /* Yes; change prefix and add to search list. */
7578 struct file_name_list *nlist
7579 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7580 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7581 char *str = (char *) dmalloc (this_len + 1);
7582 strcpy (str, specd_prefix);
7583 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7586 nlist->fname = cstring_fromChars (str);
7587 nlist->control_macro = 0;
7588 nlist->c_system_include_path = !p->cxx_aware;
7589 nlist->got_name_map = 0;
7591 if (opts->first_system_include == 0)
7593 opts->first_system_include = nlist;
7596 cppReader_addIncludeChain (pfile, nlist);
7601 /* Search ordinary names for GNU include directories. */
7603 for (p = include_defaults; p->fname != NULL; p++)
7605 /* Some standard dirs are only for C++. */
7607 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7609 struct file_name_list *nlist
7610 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7611 nlist->control_macro = 0;
7612 nlist->c_system_include_path = !p->cxx_aware;
7613 nlist->fname = p->fname;
7614 nlist->got_name_map = 0;
7617 /*@i2523@*/ if (opts->first_system_include == NULL)
7619 opts->first_system_include = nlist;
7622 cppReader_addIncludeChain (pfile, nlist);
7625 sfree (default_prefix);
7628 /* Tack the after_include chain at the end of the include chain. */
7629 cppReader_appendIncludeChain (pfile, opts->after_include,
7630 opts->last_after_include);
7632 /*@i523@*/ if (opts->first_system_include == NULL)
7634 opts->first_system_include = opts->after_include;
7637 /* With -v, print the list of dirs to search. */
7638 if (opts->verbose) {
7639 struct file_name_list *p;
7640 fprintf (stderr, "#include \"...\" search starts here:\n");
7642 for (p = opts->include; p != NULL; p = p->next) {
7643 if (p == opts->first_bracket_include)
7644 fprintf (stderr, "#include <...> search starts here:\n");
7646 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7648 fprintf (stderr, "End of search list.\n");
7652 int cppReader_startProcess (cppReader *pfile, cstring fname)
7656 struct cppOptions *opts = CPPOPTIONS (pfile);
7658 fp = cppReader_pushBuffer (pfile, NULL, 0);
7665 if (opts->in_fname == NULL)
7667 opts->in_fname = cstring_makeLiteralTemp ("");
7670 fp->fname = opts->in_fname;
7671 fp->nominal_fname = fp->fname;
7674 /* Copy the entire contents of the main input file into
7675 the stacked input buffer previously allocated for it. */
7677 if (cstring_isEmpty (fname))
7679 fname = cstring_makeLiteralTemp ("");
7682 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7684 cppReader_error (pfile,
7685 message ("Error opening %s for reading: %s",
7686 fname, lldecodeerror (errno)));
7695 if (finclude (pfile, f, fname, 0, NULL))
7697 output_line_command (pfile, 0, same_file);
7703 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7705 return pfile->buffer;
7708 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7710 llassert (pfile->buffer != NULL);
7711 return pfile->buffer;
7714 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7716 llassert (buf->buf != NULL);
7717 return (buf->buf + buf->line_base);
7720 int cpplib_bufPeek (cppBuffer *buf)
7722 if (buf->cur == NULL || buf->rlimit == NULL) {
7726 if (buf->cur < buf->rlimit) {
7733 bool cppBuffer_isMacro (cppBuffer *buf)
7737 return (buf->cleanup == cppReader_macroCleanup);
7744 ** Returns true if the macro should be checked, false
7745 ** if it should be expanded normally.
7748 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7749 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7750 static bool expectiter = FALSE; /* preceeded by @iter@ */
7751 static bool expectenditer = FALSE; /* second after @iter@ */
7752 static bool expectfunction = FALSE; /* preceeded by @function@ */
7753 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7754 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7756 static void cpp_setLocation (cppReader *pfile)
7761 if (pfile->buffer != NULL)
7763 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7765 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7767 DPRINTF (("Looking up: %s", fname));
7769 if (fileTable_exists (context_fileTable (), fname))
7771 fid = fileTable_lookup (context_fileTable (), fname);
7775 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7777 fid = fileTable_lookup (context_fileTable (),
7778 cppReader_getBufferSafe (pfile)->fname);
7783 fid = fileTable_lookup (context_fileTable (),
7784 cppReader_getBufferSafe (pfile)->fname);
7787 line = cppReader_getBufferSafe (pfile)->lineno;
7788 fileloc_free (g_currentloc);
7790 if (fileId_isValid (fid))
7792 g_currentloc = fileloc_create (fid, line, 1);
7796 g_currentloc = fileloc_createBuiltin ();
7801 fileloc_free (g_currentloc);
7802 g_currentloc = fileloc_createBuiltin ();
7806 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7808 bool checkmacro = FALSE;
7809 bool hasParams = FALSE;
7810 bool noexpand = FALSE;
7814 cpp_setLocation (pfile);
7816 DPRINTF (("Should check macro? %s", p));
7818 if (expectiter || expectconstant || expectenditer)
7823 expectenditer = TRUE;
7828 expectconstant = FALSE;
7829 expectenditer = FALSE;
7832 if (notfunction || notparseable)
7834 notfunction = FALSE;
7835 notparseable = FALSE;
7844 llassert (*p == '#');
7847 while (*p == ' ' || *p == '\t')
7852 llassert (*p == 'd'); /* define starts */
7856 while (*p == ' ' || *p == '\t')
7861 sname = cstring_fromChars (p);
7862 DPRINTF (("Check macro: %s", sname));
7864 while (((c = *p) != ' ')
7865 && c != '\0' && c != '('
7866 && c != '\t' && c != '\\' && c != '\n'
7872 hasParams = (c == '(');
7877 notparseable = FALSE;
7879 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7881 DPRINTF (("Clear notfunction"));
7882 notfunction = FALSE;
7897 if (usymtab_existsReal (sname))
7899 uentry ue = usymtab_lookup (sname);
7901 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7903 if (fileloc_isPreproc (uentry_whereLast (ue)))
7909 if (uentry_isSpecified (ue))
7911 checkmacro = context_getFlag (FLG_SPECMACROS);
7917 checkmacro = context_getFlag (FLG_LIBMACROS)
7918 || context_getFlag (FLG_FCNMACROS);
7926 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7928 if (fileloc_isSystemFile (g_currentloc)
7929 && context_getFlag (FLG_SYSTEMDIREXPAND))
7931 ; /* don't check this macro */
7932 DPRINTF (("Don't check 1"));
7940 DPRINTF (("Has params..."));
7942 if (context_getFlag (FLG_FCNMACROS))
7944 if (usymtab_exists (sname))
7947 ** only get here is macro is redefined
7948 ** error reported elsewhere
7951 DPRINTF (("It exists!"));
7956 ** We make it a forward function, since it might be declared elsewhere.
7957 ** After all headers have been processed, we should check the forward
7961 fileloc loc = fileloc_makePreproc (g_currentloc);
7963 /* the line is off-by-one, since the newline was already read */
7968 expectfunction = FALSE;
7971 le = uentry_makeForwardFunction (sname,
7972 typeId_invalid, loc);
7978 /* Do not define here! */
7980 (void) usymtab_addEntry (le);
7984 DPRINTF (("Check: TRUE"));
7988 DPRINTF (("Flag FCN_MACROS not set!"));
7993 DPRINTF (("No params"));
7995 if (context_getFlag (FLG_CONSTMACROS))
7997 bool nocontent = FALSE;
8010 ** Check if there is nothing after the define.
8013 while ((*rest) != '\0' && isspace (*rest))
8020 nocontent = TRUE; /* empty macro, don't check */
8025 if (usymtab_exists (sname))
8031 fileloc loc = fileloc_makePreproc (g_currentloc);
8032 DPRINTF (("Make constant: %s", sname));
8033 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
8034 (void) usymtab_addEntry (le);
8037 checkmacro = !nocontent;
8042 if (checkmacro && usymtab_existsType (sname))
8044 DPRINTF (("Making false..."));
8046 ppllerror (message ("Specified type implemented as macro: %s", sname));
8056 if (usymtab_exists (sname))
8058 uentry ue = usymtab_lookupExpose (sname);
8059 fileloc tloc = fileloc_makePreproc (g_currentloc);
8061 uentry_setDefined (ue, tloc);
8062 fileloc_free (tloc);
8063 uentry_setUsed (ue, fileloc_undefined);
8067 fileloc tloc = fileloc_makePreproc (g_currentloc);
8068 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8069 DPRINTF (("Make expanded macro: %s", sname));
8070 DPRINTF (("Not in symbol table: %s", sname));
8072 (void) usymtab_addGlobalEntry (ue);
8073 fileloc_free (tloc);
8078 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8082 static enum cpp_token
8083 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8085 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8089 bool eliminateComment = FALSE;
8091 llassert (pbuf->buf != NULL);
8093 start = pbuf->buf + smark->position;
8095 llassert (pbuf->cur != NULL);
8096 len = pbuf->cur - start;
8099 && start[1] == context_getCommentMarkerChar ())
8103 char *scomment = start + 2;
8104 char savec = start[len];
8106 cpp_setLocation (pfile);
8107 loc = fileloc_copy (g_currentloc);
8109 start[0] = BEFORE_COMMENT_MARKER[0];
8110 start[1] = BEFORE_COMMENT_MARKER[1];
8112 llassert (start[len - 2] == '*');
8113 start[len - 2] = AFTER_COMMENT_MARKER[0];
8115 llassert (start[len - 1] == '/');
8116 start[len - 1] = AFTER_COMMENT_MARKER[1];
8118 cpplib_reserve(pfile, size_fromInt (1 + len));
8119 cppReader_putCharQ (pfile, c);
8121 cpp_setLocation (pfile);
8125 if (mstring_containsString (scomment, "/*"))
8127 (void) cppoptgenerror
8129 message ("Comment starts inside syntactic comment: %s",
8130 cstring_fromChars (scomment)),
8136 if (mstring_equalPrefix (scomment, "ignore"))
8138 if (!context_getFlag (FLG_NOCOMMENTS))
8140 context_enterSuppressRegion (loc);
8143 else if (mstring_equalPrefix (scomment, "end"))
8145 if (!context_getFlag (FLG_NOCOMMENTS))
8147 context_exitSuppressRegion (loc);
8150 else if (mstring_equalPrefix (scomment, "notparseable"))
8152 notparseable = TRUE;
8154 eliminateComment = TRUE;
8156 else if (mstring_equalPrefix (scomment, "notfunction"))
8160 eliminateComment = TRUE;
8162 else if (mstring_equalPrefix (scomment, "iter"))
8166 else if (mstring_equalPrefix (scomment, "function"))
8168 expectfunction = TRUE;
8170 else if (mstring_equalPrefix (scomment, "constant"))
8172 expectconstant = TRUE;
8176 char sChar = *scomment;
8182 char *rest = scomment + 1;
8184 if (mstring_equalPrefix (rest, "commentchar"))
8186 eliminateComment = TRUE;
8190 ppllerror (cstring_makeLiteral
8191 ("Cannot restore commentchar"));
8195 char *next = scomment + 12; /* strlen commentchar = 12 */
8197 if (*next != ' ' && *next != '\t' && *next != '\n')
8201 ("Syntactic commentchar comment is not followed by a "
8202 "whitespace character: %c",
8207 char cchar = *(next + 1);
8212 (cstring_makeLiteral
8213 ("Cannot set commentchar to NUL"));
8217 context_setCommentMarkerChar (cchar);
8218 /* setComment = TRUE; */
8223 else if (mstring_equalPrefix (scomment, "nestcomment"))
8225 /* fix from Mike Miller <MikeM@xata.com> */
8226 context_fileSetFlag (FLG_NESTCOMMENT,
8227 ynm_fromCodeChar (sChar),
8230 else if (mstring_equalPrefix (rest, "namechecks"))
8232 context_fileSetFlag (FLG_NAMECHECKS,
8233 ynm_fromCodeChar (sChar),
8236 else if (mstring_equalPrefix (rest, "macroredef"))
8238 context_fileSetFlag (FLG_MACROREDEF,
8239 ynm_fromCodeChar (sChar),
8242 else if (mstring_equalPrefix (rest, "usevarargs"))
8244 context_fileSetFlag (FLG_USEVARARGS,
8245 ynm_fromCodeChar (sChar),
8248 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8250 context_fileSetFlag (FLG_MACRONEXTLINE,
8251 ynm_fromCodeChar (sChar),
8254 else if (mstring_equalPrefix (rest, "allmacros")
8255 || mstring_equalPrefix (rest, "fcnmacros")
8256 || mstring_equalPrefix (rest, "constmacros"))
8260 if (mstring_equalPrefix (rest, "allmacros"))
8264 else if (mstring_equalPrefix (rest, "fcnmacros"))
8270 llassert (mstring_equalPrefix (rest, "constmacros"));
8271 fl = FLG_CONSTMACROS;
8274 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8275 notfunction = FALSE;
8288 if (eliminateComment)
8293 /* Replaces comment char's in start with spaces */
8295 for (i = 2; i < len - 2; i++)
8297 if (start[i] == BEFORE_COMMENT_MARKER[0]
8298 || start[i] == BEFORE_COMMENT_MARKER[1]
8299 || start[i] == context_getCommentMarkerChar ())
8305 cppReader_putStrN (pfile, start, size_fromInt (len));
8306 parseClearMark (smark);
8316 ** Output the comment as all spaces so line/column
8317 ** in output file is still correct.
8321 cstring lintcomment = cstring_undefined;
8323 if (context_getFlag (FLG_LINTCOMMENTS))
8325 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8327 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8329 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8331 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8333 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8335 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8337 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8339 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8341 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8343 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8347 lintcomment = cstring_undefined;
8352 lintcomment = cstring_undefined;
8355 if (cstring_isDefined (lintcomment))
8357 c = BEFORE_COMMENT_MARKER[0];
8358 start[0] = BEFORE_COMMENT_MARKER[1];
8360 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8362 for (i = 1; i < len - 2; i++)
8364 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8367 start[len - 2] = AFTER_COMMENT_MARKER[0];
8368 start[len - 1] = AFTER_COMMENT_MARKER[1];
8372 /* Replaces char's in start with spaces */
8373 for (i = 0; i < len; i++)
8377 && start[i + 1] == '*') {
8378 (void) cppoptgenerror
8380 message ("Comment starts inside comment"),
8384 if (start[i] != '\n')
8391 cpplib_reserve (pfile, size_fromInt (1 + len));
8392 cppReader_putCharQ (pfile, c);
8393 cppReader_putStrN (pfile, start, size_fromInt (len));
8394 parseClearMark (smark);
8400 static int cpp_openIncludeFile (char *filename)
8402 int res = open (filename, O_RDONLY, 0666);
8404 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8407 if (!fileTable_exists (context_fileTable (),
8408 cstring_fromChars (filename)))
8410 if (fileloc_isXHFile (g_currentloc))
8413 ** Files includes by XH files are also XH files
8416 (void) fileTable_addXHFile (context_fileTable (),
8417 cstring_fromChars (filename));
8421 (void) fileTable_addHeaderFile (context_fileTable (),
8422 cstring_fromChars (filename));
8427 DPRINTF (("File already exists: %s", filename));
8434 static bool cpp_skipIncludeFile (cstring fname)
8436 if (context_isSystemDir (fname))
8438 DPRINTF (("System dir: %s", fname));
8440 if (lcllib_isSkipHeader (fname))
8442 DPRINTF (("Skip include TRUE: %s", fname));
8446 if (context_getFlag (FLG_SKIPSYSHEADERS))
8448 DPRINTF (("Skip include TRUE: %s", fname));
8453 if (context_getFlag (FLG_SINGLEINCLUDE))
8455 fname = removePreDirs (fname);
8457 # if defined (WIN32) || defined (OS2)
8458 cstring_replaceAll (fname, '\\', '/');
8461 if (fileTable_exists (context_fileTable (), fname))
8463 DPRINTF (("Skip include TRUE: %s", fname));
8468 DPRINTF (("Skip include FALSE: %s", fname));
8472 static int cpp_peekN (cppReader *pfile, int n)
8474 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8476 llassert (buf->cur != NULL);
8478 return (buf->rlimit - buf->cur >= (n)
8483 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8488 void cppBuffer_forward (cppBuffer *buf, int n)
8490 llassert (buf->cur != NULL);
8494 /*@=bufferoverflowhigh@*/