2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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"
106 ** This is really kludgey code...
112 /* Warnings for using sprintf - suppress them all for now... */
113 /*@-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))
2759 ** Return TRUE if two parts of two macro definitions are effectively different.
2760 ** One of the parts starts at BEG1 and has LEN1 chars;
2761 ** the other has LEN2 chars at BEG2.
2762 ** Any sequence of whitespace matches any other sequence of whitespace.
2763 ** FIRST means these parts are the first of a macro definition;
2764 ** so ignore leading whitespace entirely.
2765 ** LAST means these parts are the last of a macro definition;
2766 ** so ignore trailing whitespace entirely.
2770 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2772 char *end1 = beg1 + len1;
2773 char *end2 = beg2 + len2;
2776 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2777 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2780 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2781 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2783 while (beg1 != end1 && beg2 != end2) {
2784 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2785 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2786 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2787 } else if (*beg1 == *beg2) {
2791 return (beg1 != end1) || (beg2 != end2);
2795 ** Process a #define command.
2796 ** BUF points to the contents of the #define command, as a contiguous string.
2797 ** LIMIT points to the first character past the end of the definition.
2798 ** KEYWORD is the keyword-table entry for #define,
2799 ** or NULL for a "predefined" macro.
2803 do_defineAux (cppReader *pfile, struct directive *keyword,
2804 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2810 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2815 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2817 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2821 /* Redefining a precompiled key is ok. */
2822 if (hp->type == T_PCSTRING)
2824 /* Redefining a macro is ok if the definitions are the same. */
2825 else if (hp->type == T_MACRO)
2826 ok = !compare_defs (mdef.defn, hp->value.defn);
2827 /* Redefining a constant is ok with -D. */
2828 else if (hp->type == T_CONST)
2829 ok = !CPPOPTIONS (pfile)->done_initializing;
2834 /* Print the warning if it's not ok. */
2838 ** If we are passing through #define and #undef directives, do
2839 ** that for this re-definition now.
2842 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2844 /* llassert (keyword != NULL); */
2845 pass_thru_directive (buf, limit, pfile, keyword);
2848 cpp_setLocation (pfile);
2850 if (hp->type == T_MACRO)
2852 if (hp->value.defn->noExpand)
2854 ; /* error will be reported checking macros */
2860 message ("Macro %q already defined",
2861 cstring_copyLength (mdef.symnam, mdef.symlen)),
2862 message ("%q: Previous definition of %q",
2863 fileloc_unparseRaw (hp->value.defn->file,
2864 (int) hp->value.defn->line),
2865 cstring_copyLength (mdef.symnam, mdef.symlen)));
2870 genppllerror (FLG_MACROREDEF,
2871 message ("Macro %q already defined",
2872 cstring_copyLength (mdef.symnam,
2877 /* Replace the old definition. */
2879 hp->value.defn = mdef.defn;
2884 ** If we are passing through #define and #undef directives, do
2885 ** that for this new definition now.
2890 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2892 pass_thru_directive (buf, limit, pfile, keyword);
2895 DPRINTF (("Define macro: %s / %d",
2896 mdef.symnam, mdef.defn->noExpand));
2898 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2900 } /*@=branchstate@*/
2909 do_define (cppReader *pfile, struct directive *keyword,
2910 /*@exposed@*/ char *buf, char *limit)
2912 DPRINTF (("Regular do define"));
2913 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2917 ** This structure represents one parsed argument in a macro call.
2918 ** `raw' points to the argument text as written (`raw_length' is its length).
2919 ** `expanded' points to the argument's macro-expansion
2920 ** (its length is `expand_length').
2921 ** `stringified_length' is the length the argument would have
2923 ** `use_count' is the number of times this macro arg is substituted
2924 ** into the macro. If the actual use count exceeds 10,
2925 ** the value stored is 10.
2928 /* raw and expanded are relative to ARG_BASE */
2930 #define ARG_BASE ((pfile)->token_buffer)
2933 /* Strings relative to pfile->token_buffer */
2939 int stringified_length;
2945 ** Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2946 ** If BUFFER != NULL, then use the LENGTH characters in BUFFER
2947 ** as the new input buffer.
2948 ** 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);
3005 ** Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
3006 ** Pop the buffer when done.
3010 cppReader_scanBuffer (cppReader *pfile)
3012 cppBuffer *buffer = CPPBUFFER (pfile);
3015 enum cpp_token token;
3017 token = cpplib_getToken (pfile);
3019 if (token == CPP_EOF) /* Should not happen ... */
3024 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
3026 (void) cppReader_popBuffer (pfile);
3033 * Rescan a string (which may have escape marks) into pfile's buffer.
3034 * Place the result in pfile->token_buffer.
3036 * The input is copied before it is scanned, so it is safe to pass
3037 * it something from the token_buffer that will get overwritten
3038 * (because it follows cpplib_getWritten). This is used by do_include.
3042 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3044 register cppBuffer *ip;
3045 char *limit = buf + length;
3046 char *buf1, *p1, *p2;
3048 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
3050 /* evans - 2001-08-26
3051 ** length is unsigned - this doesn't make sense
3057 /* Set up the input on the input stack. */
3059 buf1 = (char *) dmalloc (length + 1);
3069 buf1[length] = '\0';
3071 ip = cppReader_pushBuffer (pfile, buf1, length);
3076 ip->has_escapes = TRUE;
3078 /* Scan the input, create the output. */
3079 cppReader_scanBuffer (pfile);
3081 cppReader_nullTerminate (pfile);
3085 adjust_position (char *buf, char *limit, int *linep, int *colp)
3091 (*linep)++, (*colp) = 1;
3097 /* Move line_base forward, updating lineno and colno. */
3100 update_position (cppBuffer *pbuf)
3103 char *new_pos = pbuf->cur;
3104 register struct parse_marker *mark;
3106 llassert (pbuf->buf != NULL);
3107 old_pos = pbuf->buf + pbuf->line_base;
3109 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3111 if (pbuf->buf + mark->position < new_pos)
3112 new_pos = pbuf->buf + mark->position;
3114 pbuf->line_base += new_pos - old_pos;
3116 llassert (old_pos != NULL);
3117 llassert (new_pos != NULL);
3119 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3123 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3124 /*@null@*/ /*@out@*/ int *colp)
3132 } /*@=branchstate@*/
3136 *linep = pbuf->lineno;
3137 *colp = pbuf->colno;
3139 llassert (pbuf->buf != NULL);
3140 llassert (pbuf->cur != NULL);
3142 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3151 /* Return the cppBuffer that corresponds to a file (not a macro). */
3153 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3155 cppBuffer *ip = cppReader_getBuffer (pfile);
3158 ip != NULL && ip != cppReader_nullBuffer (pfile);
3159 ip = cppBuffer_prevBuffer (ip))
3161 if (ip->fname != NULL)
3171 count_newlines (char *buf, char *limit)
3173 register long count = 0;
3185 * write out a #line command, for instance, after an #include file.
3186 * If CONDITIONAL is nonzero, we can omit the #line if it would
3187 * appear to be a no-op, and we can output a few newlines instead
3188 * if we want to increase the line number by a small amount.
3189 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3193 output_line_command (cppReader *pfile, bool conditional,
3194 enum file_change_code file_change)
3197 cppBuffer *ip = CPPBUFFER (pfile);
3200 llassert (ip != NULL);
3202 if (ip->fname == NULL)
3205 update_position (ip);
3207 if (CPPOPTIONS (pfile)->no_line_commands
3208 || CPPOPTIONS (pfile)->no_output)
3211 buf = CPPBUFFER (pfile);
3213 llassert (buf != NULL);
3218 llassert (ip->cur != NULL);
3220 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3222 if (CPPOPTIONS (pfile)->no_line_commands)
3226 if (line == pfile->lineno)
3229 /* If the inherited line number is a little too small,
3230 output some newlines instead of a #line command. */
3232 if (line > pfile->lineno && line < pfile->lineno + 8)
3234 cpplib_reserve (pfile, 20);
3235 while (line > pfile->lineno)
3237 cppReader_putCharQ (pfile, '\n');
3245 cpplib_reserve (pfile, 4 * cstring_length (ip->nominal_fname) + 50);
3248 #ifdef OUTPUT_LINE_COMMANDS
3249 static char sharp_line[] = "#line ";
3251 static char sharp_line[] = "# ";
3253 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3256 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3257 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3259 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3261 if (file_change != same_file) {
3262 cppReader_putCharQ (pfile, ' ');
3263 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3265 /* Tell cc1 if following text comes from a system header file. */
3266 if (ip->system_header_p != '\0') {
3267 cppReader_putCharQ (pfile, ' ');
3268 cppReader_putCharQ (pfile, '3');
3270 #ifndef NO_IMPLICIT_EXTERN_C
3271 /* Tell cc1plus if following text should be treated as C. */
3272 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3273 cppReader_putCharQ (pfile, ' ');
3274 cppReader_putCharQ (pfile, '4');
3277 cppReader_putCharQ (pfile, '\n');
3278 pfile->lineno = line;
3283 * Parse a macro argument and append the info on PFILE's token_buffer.
3284 * REST_ARGS means to absorb the rest of the args.
3285 * Return nonzero to indicate a syntax error.
3288 static enum cpp_token
3289 macarg (cppReader *pfile, int rest_args)
3292 enum cpp_token token;
3293 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3294 bool oldexpand = pfile->no_macro_expand;
3295 CPPOPTIONS (pfile)->put_out_comments = 1;
3297 /* Try to parse as much of the argument as exists at this
3298 input stack level. */
3300 pfile->no_macro_expand = TRUE;
3304 token = cpplib_getToken (pfile);
3311 /* If we've hit end of file, it's an error (reported by caller).
3312 Ditto if it's the end of cpp_expand_to_buffer text.
3313 If we've hit end of macro, just continue. */
3314 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3316 /*@switchbreak@*/ break;
3319 /*@switchbreak@*/ break;
3323 /*@switchbreak@*/ break;
3325 /* if we've returned to lowest level and
3326 we aren't absorbing all args */
3327 if (paren == 0 && rest_args == 0)
3329 /*@switchbreak@*/ break;
3331 /* Remove ',' or ')' from argument buffer. */
3332 cppReader_adjustWritten (pfile, -1);
3340 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3341 pfile->no_macro_expand = oldexpand;
3347 /* Turn newlines to spaces in the string of length LENGTH at START,
3348 except inside of string constants.
3349 The string is copied into itself with its beginning staying fixed. */
3352 change_newlines (char *start, int length)
3356 register char *limit;
3360 limit = start + length;
3363 while (ibp < limit) {
3364 *obp++ = c = *ibp++;
3369 /* Notice and skip strings, so that we don't delete newlines in them. */
3372 while (ibp < limit) {
3373 *obp++ = c = *ibp++;
3375 /*@innerbreak@*/ break;
3376 if (c == '\n' && quotec == '\'')
3377 /*@innerbreak@*/ break;
3380 /*@switchbreak@*/ break;
3387 static /*@observer@*/ struct tm *
3388 timestamp (/*@returned@*/ cppReader *pfile)
3390 if (pfile->timebuf == NULL)
3392 time_t t = time ((time_t *) 0);
3393 pfile->timebuf = localtime (&t);
3396 llassert (pfile->timebuf != NULL);
3398 return pfile->timebuf;
3401 static ob_mstring monthnames[] = {
3402 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3403 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3407 * expand things like __FILE__. Place the expansion into the output
3408 * buffer *without* rescanning.
3412 special_symbol (hashNode hp, cppReader *pfile)
3414 cstring buf = cstring_undefined;
3420 int paren = 0; /* For special `defined' keyword */
3422 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3424 if (ip == cppReader_nullBuffer (pfile))
3426 cppReader_errorLit (pfile,
3427 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3428 return; /* the show must go on */
3431 if (ip != NULL && ip->fname != NULL)
3443 if (hp->type == T_BASE_FILE)
3445 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3447 ip = cppBuffer_prevBuffer (ip);
3451 llassert (ip != NULL);
3452 string = cstring_toCharsSafe (ip->nominal_fname);
3459 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3460 quote_string (pfile, string);
3463 case T_FUNC: /* added in ISO C99 */
3465 /* We don't know the actual name of the function, but it doesn't matter */
3466 char *string = "** function-name **";
3467 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3468 quote_string (pfile, string);
3472 case T_INCLUDE_LEVEL:
3474 ip = cppReader_getBuffer (pfile);
3476 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3477 ip = cppBuffer_prevBuffer (ip))
3479 if (ip != NULL && ip->fname != NULL)
3485 buf = message ("%d", true_indepth - 1);
3489 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3492 #ifndef NO_BUILTIN_SIZE_TYPE
3494 buf = cstring_makeLiteral (SIZE_TYPE);
3498 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3499 case T_PTRDIFF_TYPE:
3500 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3505 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3508 case T_USER_LABEL_PREFIX_TYPE:
3509 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3512 case T_REGISTER_PREFIX_TYPE:
3513 buf = cstring_makeLiteral (REGISTER_PREFIX);
3517 buf = message ("%d", hp->value.ival);
3524 int line = ip->lineno;
3525 int col = ip->colno;
3527 llassert (ip->cur != NULL);
3528 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3530 buf = message ("%d", (int) line);
3542 char *sbuf = (char *) dmalloc (20);
3543 timebuf = timestamp (pfile);
3544 if (hp->type == T_DATE)
3546 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3547 timebuf->tm_mday, timebuf->tm_year + 1900);
3551 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3555 buf = cstring_fromCharsNew (sbuf);
3560 case T_SPEC_DEFINED:
3561 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3562 ip = cppReader_getBuffer (pfile);
3563 llassert (ip != NULL);
3564 llassert (ip->cur != NULL);
3565 SKIP_WHITE_SPACE (ip->cur);
3567 if (*ip->cur == '(')
3570 ip->cur++; /* Skip over the paren */
3571 SKIP_WHITE_SPACE (ip->cur);
3574 if (!is_idstart[(int) *ip->cur])
3576 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3579 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3582 buf = cstring_makeLiteral (" 1 ");
3585 while (is_idchar[(int) *ip->cur])
3590 SKIP_WHITE_SPACE (ip->cur);
3594 if (*ip->cur != ')')
3602 cppReader_errorLit (pfile,
3603 cstring_makeLiteralTemp ("`defined' without an identifier"));
3607 cpp_setLocation (pfile);
3608 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3611 len = cstring_length (buf);
3613 cpplib_reserve (pfile, len + 1);
3614 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3615 cppReader_nullTerminateQ (pfile);
3621 /* Write out a #define command for the special named MACRO_NAME
3622 to PFILE's token_buffer. */
3625 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3627 static char define_directive[] = "#define ";
3628 size_t macro_name_length = strlen (macro_name);
3629 output_line_command (pfile, 0, same_file);
3630 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3631 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3632 cppReader_putStrN (pfile, macro_name, macro_name_length);
3633 cppReader_putCharQ (pfile, ' ');
3634 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3635 cppReader_putChar (pfile, '\n');
3638 /* Initialize the built-in macros. */
3641 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3642 int len, enum node_type type,
3643 int ivalue, /*@null@*/ /*@only@*/ char *value,
3646 cstring sname = cstring_fromCharsNew (name);
3648 llassert (usymtab_inGlobalScope ());
3651 ** Be careful here: this is done before the ctype table has
3652 ** been initialized.
3655 if (!usymtab_exists (sname))
3657 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3659 if (ctype_equal (ctyp, ctype_string))
3661 qualList ql = qualList_new ();
3662 ql = qualList_add (ql, qual_createObserver ());
3663 uentry_reflectQualifiers (ue, ql);
3667 usymtab_addGlobalEntry (ue);
3674 (void) cpphash_install (name, len, type, ivalue, value, hash);
3675 cstring_free (sname);
3679 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3680 int len, enum node_type type,
3682 /*@only@*/ /*@null@*/ char *value, int hash)
3684 cstring sname = cstring_fromChars (name);
3685 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3687 llassert (usymtab_inGlobalScope ());
3689 if (!usymtab_existsTypeEither (sname))
3691 uentry ue = uentry_makeDatatype (sname, ctyp,
3692 NO, qual_createConcrete (),
3693 fileloc_createBuiltin ());
3694 llassert (!usymtab_existsEither (sname));
3695 usymtab_addGlobalEntry (ue);
3698 (void) cpphash_install (name, len, type, ivalue, value, hash);
3702 initialize_builtins (cppReader *pfile)
3704 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3705 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3706 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3707 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3708 cpplib_installBuiltin ("__func__", ctype_string, -1, T_FUNC, 0, NULL, -1);
3709 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3710 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3711 #ifndef NO_BUILTIN_SIZE_TYPE
3712 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3714 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3715 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3717 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3718 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3719 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3720 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3723 ** No, don't define __STDC__
3726 if (!cppReader_isTraditional (pfile))
3728 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3735 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3739 ** This is supplied using a -D by the compiler driver
3740 ** so that it is present only when truly compiling with GNU C.
3743 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3745 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3746 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3748 /*drl 1/9/2001/ try to define the right symbol for the architecture
3749 We use autoconf to determine the target cpu
3751 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3753 /*drl 1/2/2002 set some flags based on uname
3754 I'd like to be able to do this with autoconf macro instead...
3757 /*Thanks to Nelson Beebe for suggesting possible values for these */
3759 if (! strcmp (UNAME, "Linux"))
3762 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3766 else if(! strcmp (UNAME, "Darwin"))
3768 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3769 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3771 else if(! strcmp (UNAME, "HP-UX"))
3773 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3774 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3775 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3780 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3781 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3782 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3783 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3785 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3786 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3787 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3788 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3789 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3791 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3792 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3793 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3794 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3795 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3797 else if(! strcmp (UNAME, "IRIX64"))
3799 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3800 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3801 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3802 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3803 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3805 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3806 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3807 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3808 /*_MIPS_SIM=_ABIN32*/
3809 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3810 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3811 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3812 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3813 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3814 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3815 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3816 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3817 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3818 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3819 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3820 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3821 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3822 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3823 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3824 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3825 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3826 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3827 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3828 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3829 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3830 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3831 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3832 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3834 else if(! strcmp (UNAME, "OSF1"))
3836 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3838 else if (!strcmp (UNAME, "Rhapsody"))
3840 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3841 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3844 else if (!strcmp (UNAME, "SunOS"))
3846 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3847 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3848 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3849 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3854 types which we have not explictedly handled.
3855 AIX, FreeBSD, IRIX, Mach
3860 if (CPPOPTIONS (pfile)->debug_output)
3862 dump_special_to_buffer (pfile, "__BASE_FILE__");
3863 dump_special_to_buffer (pfile, "__VERSION__");
3864 #ifndef NO_BUILTIN_SIZE_TYPE
3865 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3867 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3868 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3870 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3871 dump_special_to_buffer (pfile, "__DATE__");
3872 dump_special_to_buffer (pfile, "__TIME__");
3873 if (!cppReader_isTraditional (pfile))
3874 dump_special_to_buffer (pfile, "__STDC__");
3879 /* Return 1 iff a token ending in C1 followed directly by a token C2
3880 could cause mis-tokenization. */
3883 unsafe_chars (char c1, char c2)
3888 if (c2 == c1 || c2 == '=')
3892 case '0': case '1': case '2': case '3': case '4':
3893 case '5': case '6': case '7': case '8': case '9':
3894 case 'e': case 'E': case 'p': case 'P':
3895 if (c2 == '-' || c2 == '+')
3896 return 1; /* could extend a pre-processing number */
3899 if (c2 == '\'' || c2 == '\"')
3900 return 1; /* Could turn into L"xxx" or L'xxx'. */
3904 case 'a': case 'b': case 'c': case 'd': case 'f':
3905 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3906 case 'm': case 'n': case 'o': case 'q': case 'r':
3907 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3909 case 'A': case 'B': case 'C': case 'D': case 'F':
3910 case 'G': case 'H': case 'I': case 'J': case 'K':
3911 case 'M': case 'N': case 'O': case 'Q': case 'R':
3912 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3914 /* We're in the middle of either a name or a pre-processing number. */
3915 return (is_idchar[(int) c2] || c2 == '.');
3916 case '<': case '>': case '!': case '%': case '#': case ':':
3917 case '^': case '&': case '|': case '*': case '/': case '=':
3918 return (c2 == c1 || c2 == '=');
3923 /* Expand a macro call.
3924 HP points to the symbol that is the macro being called.
3925 Put the result of expansion onto the input stack
3926 so that subsequent input by our caller will use it.
3928 If macro wants arguments, caller has already verified that
3929 an argument list follows; arguments come from the input stack. */
3932 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3935 DEFINITION *defn = hp->value.defn;
3943 size_t old_written = cpplib_getWritten (pfile);
3947 struct argdata *args = NULL;
3949 pfile->output_escapes++;
3950 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3951 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3953 nargs = defn->nargs;
3957 enum cpp_token token = CPP_EOF;
3959 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3961 for (i = 0; i < nargs; i++)
3963 args[i].expanded = 0;
3965 args[i].raw_length = 0;
3966 args[i].expand_length = args[i].stringified_length = -1;
3967 args[i].use_count = 0;
3971 ** Parse all the macro args that are supplied. I counts them.
3972 ** The first NARGS args are stored in ARGS.
3973 ** The rest are discarded. If rest_args is set then we assume
3974 ** macarg absorbed the rest of the args.
3980 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3988 if (i < nargs || (nargs == 0 && i == 0))
3990 /* if we are working on last arg which absorbs rest of args... */
3991 if (i == nargs - 1 && defn->rest_args)
3996 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3997 token = macarg (pfile, rest_args);
3998 args[i].raw_length = size_toInt (cpplib_getWritten (pfile) - args[i].raw);
3999 args[i].newlines = FALSE; /* FIXME */
4003 token = macarg (pfile, 0);
4006 if (token == CPP_EOF || token == CPP_POP)
4008 cppReader_errorWithLine (pfile, start_line, start_column,
4009 cstring_fromCharsNew ("unterminated macro call"));
4014 } while (token == CPP_COMMA);
4016 /* If we got one arg but it was just whitespace, call that 0 args. */
4024 bp = ARG_BASE + args[0].raw;
4025 lim = bp + args[0].raw_length;
4027 /* cpp.texi says for foo ( ) we provide one argument.
4028 However, if foo wants just 0 arguments, treat this as 0. */
4032 while (bp != lim && is_space[(int) *bp])
4042 /* Don't output an error message if we have already output one for
4043 a parse error above. */
4046 if (nargs == 0 && i > 0)
4048 cppReader_error (pfile,
4049 message ("arguments given to macro `%s'", hp->name));
4053 /* traditional C allows foo() if foo wants one argument. */
4054 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4058 /* the rest args token is allowed to absorb 0 tokens */
4059 else if (i == nargs - 1 && defn->rest_args)
4062 cppReader_error (pfile,
4063 message ("macro `%s' used without args", hp->name));
4065 cppReader_error (pfile,
4066 message ("macro `%s' used with just one arg", hp->name));
4069 cppReader_error (pfile,
4070 message ("macro `%s' used with only %d args",
4076 cppReader_error (pfile,
4077 message ("macro `%s' used with too many (%d) args", hp->name, i));
4086 ** If the agrument list was multiple lines, need to insert new lines to keep line
4087 ** numbers accurate.
4090 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4091 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4093 /* If macro wants zero args, we parsed the arglist for checking only.
4094 Read directly from the macro definition. */
4098 xbuf = defn->expansion;
4099 xbuf_len = defn->length;
4103 char *exp = defn->expansion;
4104 int offset; /* offset in expansion,
4105 copied a piece at a time */
4106 size_t totlen; /* total amount of exp buffer filled so far */
4108 register struct reflist *ap, *last_ap;
4110 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4112 /* Macro really takes args. Compute the expansion of this call. */
4114 /* Compute length in characters of the macro's expansion.
4115 Also count number of times each arg is used. */
4116 xbuf_len = defn->length;
4118 llassert (args != NULL);
4120 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4124 struct argdata *arg = &args[ap->argno];
4126 /* Stringify it it hasn't already been */
4129 if (arg->stringified_length < 0)
4131 int arglen = arg->raw_length;
4132 bool escaped = FALSE;
4133 char in_string = '\0';
4136 /* Initially need_space is -1. Otherwise, 1 means the
4137 previous character was a space, but we suppressed it;
4138 0 means the previous character was a non-space. */
4139 int need_space = -1;
4142 arg->stringified = cpplib_getWritten (pfile);
4143 if (!cppReader_isTraditional (pfile))
4144 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4145 for (; i < arglen; i++)
4147 c = (ARG_BASE + arg->raw)[i];
4149 if (in_string == '\0')
4151 /* Internal sequences of whitespace are replaced by
4152 one space except within an string or char token.*/
4153 if (is_space[(int) c])
4155 if (cpplib_getWritten (pfile) > arg->stringified
4156 && (cpplib_getPWritten (pfile))[-1] == '@')
4158 /* "@ " escape markers are removed */
4159 cppReader_adjustWritten (pfile, -1);
4160 /*@innercontinue@*/ continue;
4162 if (need_space == 0)
4164 /*@innercontinue@*/ continue;
4166 else if (need_space > 0)
4167 cppReader_putChar (pfile, ' ');
4183 if (in_string != '\0')
4188 else if (c == '\"' || c == '\'')
4198 /* Escape these chars */
4199 if (c == '\"' || (in_string != '\0' && c == '\\'))
4200 cppReader_putChar (pfile, '\\');
4202 cppReader_putChar (pfile, c);
4205 cpplib_reserve (pfile, 4);
4206 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4208 cppReader_adjustWritten (pfile, 4);
4211 if (!cppReader_isTraditional (pfile))
4212 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4213 arg->stringified_length
4214 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4217 xbuf_len += args[ap->argno].stringified_length;
4219 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4221 /* Add 4 for two newline-space markers to prevent token concatenation. */
4222 assertSet (args); /* Splint shouldn't need this */
4223 xbuf_len += args[ap->argno].raw_length + 4;
4227 /* We have an ordinary (expanded) occurrence of the arg.
4228 So compute its expansion, if we have not already. */
4230 assertSet (args); /* shouldn't need this */
4232 if (args[ap->argno].expand_length < 0)
4234 args[ap->argno].expanded = cpplib_getWritten (pfile);
4235 cpp_expand_to_buffer (pfile,
4236 ARG_BASE + args[ap->argno].raw,
4237 size_fromInt (args[ap->argno].raw_length));
4239 args[ap->argno].expand_length
4240 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4243 /* Add 4 for two newline-space markers to prevent
4244 token concatenation. */
4245 xbuf_len += args[ap->argno].expand_length + 4;
4247 if (args[ap->argno].use_count < 10)
4248 args[ap->argno].use_count++;
4251 xbuf = (char *) dmalloc (xbuf_len + 1);
4255 ** Generate in XBUF the complete expansion
4256 ** with arguments substituted in.
4257 ** TOTLEN is the total size generated so far.
4258 ** OFFSET is the index in the definition
4259 ** of where we are copying from.
4265 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4266 last_ap = ap, ap = ap->next)
4268 register struct argdata *arg = &args[ap->argno];
4269 size_t count_before = totlen;
4271 /* Add chars to XBUF. */
4272 for (i = 0; i < ap->nchars; i++, offset++)
4274 xbuf[totlen++] = exp[offset];
4277 /* If followed by an empty rest arg with concatenation,
4278 delete the last run of nonwhite chars. */
4279 if (rest_zero && totlen > count_before
4280 && ((ap->rest_args && ap->raw_before)
4281 || (last_ap != NULL && last_ap->rest_args
4282 && last_ap->raw_after)))
4284 /* Delete final whitespace. */
4285 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4290 /* Delete the nonwhites before them. */
4291 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4297 if (ap->stringify != 0)
4300 memcpy (xbuf + totlen,
4301 ARG_BASE + arg->stringified,
4302 size_fromInt (arg->stringified_length));
4303 totlen += arg->stringified_length;
4305 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4312 p1 = ARG_BASE + arg->raw;
4313 l1 = p1 + arg->raw_length;
4317 while (p1 != l1 && is_space[(int) *p1])
4322 while (p1 != l1 && is_idchar[(int) *p1])
4324 xbuf[totlen++] = *p1++;
4327 /* Delete any no-reexpansion marker that follows
4328 an identifier at the beginning of the argument
4329 if the argument is concatenated with what precedes it. */
4330 if (p1[0] == '@' && p1[1] == '-')
4335 /* Arg is concatenated after: delete trailing whitespace,
4336 whitespace markers, and no-reexpansion markers. */
4339 if (is_space[(int) l1[-1]]) l1--;
4340 else if (l1[-1] == '-')
4343 /* If a `-' is preceded by an odd number of newlines then it
4344 and the last newline are a no-reexpansion marker. */
4345 while (p2 != p1 && p2[-1] == '\n')
4350 if (((l1 - 1 - p2) & 1) != 0)
4356 /*@innerbreak@*/ break;
4361 /*@innerbreak@*/ break;
4366 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4374 expanded = ARG_BASE + arg->expanded;
4376 if (!ap->raw_before && totlen > 0
4377 && (arg->expand_length != 0)
4378 && !cppReader_isTraditional(pfile)
4379 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4381 xbuf[totlen++] = '@';
4382 xbuf[totlen++] = ' ';
4385 memcpy (xbuf + totlen, expanded,
4386 size_fromInt (arg->expand_length));
4387 totlen += arg->expand_length;
4389 if (!ap->raw_after && totlen > 0
4390 && offset < size_toInt (defn->length)
4391 && !cppReader_isTraditional(pfile)
4392 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4394 xbuf[totlen++] = '@';
4395 xbuf[totlen++] = ' ';
4398 /* If a macro argument with newlines is used multiple times,
4399 then only expand the newlines once. This avoids creating
4400 output lines which don't correspond to any input line,
4401 which confuses gdb and gcov. */
4402 if (arg->use_count > 1 && arg->newlines > 0)
4404 /* Don't bother doing change_newlines for subsequent
4408 = change_newlines (expanded, arg->expand_length);
4412 if (totlen > xbuf_len)
4416 /* if there is anything left of the definition
4417 after handling the arg list, copy that in too. */
4419 for (i = offset; i < size_toInt (defn->length); i++)
4421 /* if we've reached the end of the macro */
4424 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4425 && last_ap->raw_after))
4426 xbuf[totlen++] = exp[i];
4429 xbuf[totlen] = '\0';
4433 pfile->output_escapes--;
4435 /* Now put the expansion on the input stack
4436 so our caller will commence reading from it. */
4437 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4439 if (end_line != start_line)
4441 /* xbuf must have enough newlines */
4442 int newlines = end_line - start_line;
4443 int foundnewlines = 0;
4444 char *xbufptr = xbuf;
4446 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4451 if (*xbufptr == '\0')
4457 if (foundnewlines < newlines)
4459 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4461 while (foundnewlines < newlines)
4463 newbuf = cstring_appendChar (newbuf, '\n');
4468 xbuf = cstring_toCharsSafe (newbuf);
4469 xbuf_len = cstring_length (newbuf);
4471 } /*@=branchstate@*/
4474 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4476 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4477 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4478 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4480 /* Pop the space we've used in the token_buffer for argument expansion. */
4481 cppReader_setWritten (pfile, old_written);
4482 DPRINTF (("Done set written"));
4484 /* Recursive macro use sometimes works traditionally.
4485 #define foo(x,y) bar (x (y,0), y)
4488 if (!cppReader_isTraditional (pfile))
4489 hp->type = T_DISABLED;
4495 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4496 /*@dependent@*/ hashNode hp)
4498 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4505 mbuf->cleanup = cppReader_macroCleanup;
4507 llassert (mbuf->hnode == NULL);
4510 /* The first chars of the expansion should be a "@ " added by
4511 collect_expansion. This is to prevent accidental token-pasting
4512 between the text preceding the macro invocation, and the macro
4515 We would like to avoid adding unneeded spaces (for the sake of
4516 tools that use cpp, such as imake). In some common cases we can
4517 tell that it is safe to omit the space.
4519 The character before the macro invocation cannot have been an
4520 idchar (or else it would have been pasted with the idchars of
4521 the macro name). Therefore, if the first non-space character
4522 of the expansion is an idchar, we do not need the extra space
4523 to prevent token pasting.
4525 Also, we don't need the extra space if the first char is '(',
4526 or some other (less common) characters. */
4528 if (xbuf[0] == '@' && xbuf[1] == ' '
4529 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4530 || xbuf[2] == '\"'))
4532 llassert (mbuf->cur != NULL);
4533 DPRINTF (("Eating: %c", xbuf[2]));
4540 /* Like cpplib_getToken, except that it does not read past end-of-line.
4541 Also, horizontal space is skipped, and macros are popped. */
4543 static enum cpp_token
4544 get_directive_token (cppReader *pfile)
4548 size_t old_written = cpplib_getWritten (pfile);
4549 enum cpp_token token;
4550 cppSkipHspace (pfile);
4551 if (cppReader_peekC (pfile) == '\n')
4556 token = cpplib_getToken (pfile);
4561 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4566 cppReader_setWritten (pfile, old_written);
4567 /*@switchbreak@*/ break;
4575 /* Handle #include and #import.
4576 This function expects to see "fname" or <fname> on the input.
4578 The input is normally in part of the output_buffer following
4579 cpplib_getWritten, and will get overwritten by output_line_command.
4580 I.e. in input file specification has been popped by cppReader_handleDirective.
4584 do_include (cppReader *pfile, struct directive *keyword,
4585 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4587 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4589 char *fbeg, *fend; /* Beginning and end of fname */
4590 enum cpp_token token;
4592 /* Chain of dirs to search */
4593 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4594 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4595 struct file_name_list *searchptr = NULL;
4596 size_t old_written = cpplib_getWritten (pfile);
4599 int f; /* file number */
4600 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4601 f= -1; /* JF we iz paranoid! */
4603 pfile->parsing_include_directive++;
4604 token = get_directive_token (pfile);
4605 pfile->parsing_include_directive--;
4607 if (token == CPP_STRING)
4609 /* FIXME - check no trailing garbage */
4610 fbeg = pfile->token_buffer + old_written + 1;
4611 fend = cpplib_getPWritten (pfile) - 1;
4612 if (fbeg[-1] == '<')
4615 /* If -I-, start with the first -I dir after the -I-. */
4616 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4617 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4619 /* If -I- was specified, don't search current dir, only spec'd ones. */
4620 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4622 cppBuffer *fp = CPPBUFFER (pfile);
4623 /* We have "filename". Figure out directory this source
4624 file is coming from and put it on the front of the list. */
4626 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4631 llassert (fp != NULL);
4635 if (cstring_isDefined (fp->nominal_fname))
4637 nam = cstring_toCharsSafe (fp->nominal_fname);
4639 /* Found a named file. Figure out dir of the file,
4640 and put it in front of the search list. */
4641 dsp[0].next = search_start;
4645 ep = strrchr (nam, CONNECTCHAR);
4647 ep = strrchr (nam, ']');
4648 if (ep == NULL) ep = strrchr (nam, '>');
4649 if (ep == NULL) ep = strrchr (nam, ':');
4650 if (ep != NULL) ep++;
4660 /*@-onlytrans@*/ /* This looks like a memory leak... */
4661 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4665 if (n + INCLUDE_LEN_FUDGE > size_toInt (pfile->max_include_len))
4666 pfile->max_include_len = size_fromInt (n + INCLUDE_LEN_FUDGE);
4670 dsp[0].fname = cstring_undefined; /* Current directory */
4673 dsp[0].got_name_map = 0;
4684 else if (token == CPP_NAME)
4687 * Support '#include xyz' like VAX-C to allow for easy use of all the
4688 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4689 * code from case '<' is repeated here) and generates a warning.
4691 cppReader_warning (pfile,
4692 "VAX-C-style include specification found, use '#include <filename.h>' !");
4694 /* If -I-, start with the first -I dir after the -I-. */
4695 if (CPPOPTIONS (pfile)->first_bracket_include)
4696 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4697 fbeg = pfile->token_buffer + old_written;
4698 fend = cpplib_getPWritten (pfile);
4703 cppReader_error (pfile,
4704 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4707 cppReader_setWritten (pfile, old_written);
4708 cppReader_skipRestOfLine (pfile);
4714 token = get_directive_token (pfile);
4715 if (token != CPP_VSPACE)
4717 cppReader_errorLit (pfile,
4718 cstring_makeLiteralTemp ("Junk at end of #include"));
4720 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4722 token = get_directive_token (pfile);
4727 ** For #include_next, skip in the search path
4728 ** past the dir in which the containing file was found.
4733 cppBuffer *fp = CPPBUFFER (pfile);
4735 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4737 llassert (fp != NULL);
4739 if (fp->fname != NULL)
4741 /* fp->dir is null if the containing file was specified with
4742 an absolute file name. In that case, don't skip anything. */
4743 if (fp->dir == SELF_DIR_DUMMY)
4745 search_start = CPPOPTIONS (pfile)->include;
4747 else if (fp->dir != NULL)
4749 search_start = fp->dir->next;
4761 cppReader_setWritten (pfile, old_written);
4763 flen = size_fromInt (fend - fbeg);
4765 DPRINTF (("fbeg: %s", fbeg));
4769 cppReader_error (pfile,
4770 message ("Empty file name in #%s", keyword->name));
4775 ** Allocate this permanently, because it gets stored in the definitions
4779 fname = cstring_undefined;
4781 /* + 2 above for slash and terminating null. */
4782 /* + 2 added for '.h' on VMS (to support '#include filename') */
4784 /* If specified file name is absolute, just open it. */
4786 if (osd_isConnectChar (*fbeg)
4787 # if defined (WIN32) || defined (OS2)
4788 || (*(fbeg + 1) == ':')
4792 fname = cstring_copyLength (fbeg, flen);
4794 if (redundant_include_p (pfile, fname))
4796 cstring_free (fname);
4800 f = open_include_file (pfile, fname, NULL);
4802 if (f == IMPORT_FOUND)
4804 return 0; /* Already included this file */
4809 /* Search directory path, trying to open the file.
4810 Copy each filename tried into FNAME. */
4812 for (searchptr = search_start; searchptr != NULL;
4813 searchptr = searchptr->next)
4815 if (!cstring_isEmpty (searchptr->fname))
4817 /* The empty string in a search path is ignored.
4818 This makes it possible to turn off entirely
4819 a standard piece of the list. */
4820 if (cstring_isEmpty (searchptr->fname))
4823 fname = cstring_copy (searchptr->fname);
4824 fname = cstring_appendChar (fname, CONNECTCHAR);
4825 DPRINTF (("Here: %s", fname));
4832 fname = cstring_concatLength (fname, fbeg, flen);
4834 DPRINTF (("fname: %s", fname));
4836 /* Win32 directory fix from Kay Buschner. */
4837 #if defined (WIN32) || defined (OS2)
4838 /* Fix all unixdir slashes to win dir slashes */
4839 if (searchptr->fname && (searchptr->fname[0] != 0))
4841 cstring_replaceAll (fname, '/', '\\');
4846 /* Change this 1/2 Unix 1/2 VMS file specification into a
4847 full VMS file specification */
4848 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4849 /* Fix up the filename */
4850 hack_vms_include_specification (fname);
4852 /* This is a normal VMS filespec, so use it unchanged. */
4853 strncpy (fname, fbeg, flen);
4855 /* if it's '#include filename', add the missing .h */
4856 if (strchr (fname,'.') == NULL) {
4857 strcat (fname, ".h");
4861 /* ??? There are currently 3 separate mechanisms for avoiding processing
4862 of redundant include files: #import, #pragma once, and
4863 redundant_include_p. It would be nice if they were unified. */
4865 if (redundant_include_p (pfile, fname))
4867 cstring_free (fname);
4871 DPRINTF (("Trying: %s", fname));
4873 f = open_include_file (pfile, fname, searchptr);
4875 if (f == IMPORT_FOUND)
4877 return 0; /* Already included this file */
4880 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4882 cppReader_warning (pfile,
4883 message ("Header file %s exists, but is not readable", fname));
4896 /* A file that was not found. */
4897 fname = cstring_copyLength (fbeg, flen);
4899 if (search_start != NULL)
4901 cppReader_error (pfile,
4902 message ("Cannot find include file %s on search path: %x",
4904 searchPath_unparse (search_start)));
4908 cppReader_error (pfile,
4909 message ("No include path in which to find %s", fname));
4914 ** Check to see if this include file is a once-only include file.
4918 struct file_name_list *ptr;
4920 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4922 if (cstring_equal (ptr->fname, fname))
4924 /* This file was included before. */
4931 /* This is the first time for this file. */
4932 /* Add it to list of files included. */
4934 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4935 ptr->control_macro = NULL;
4936 ptr->c_system_include_path = NULL;
4937 ptr->next = pfile->all_include_files;
4939 ptr->got_name_map = NULL;
4941 DPRINTF (("Including file: %s", fname));
4942 pfile->all_include_files = ptr;
4943 assertSet (pfile->all_include_files);
4946 if (angle_brackets != 0)
4948 pfile->system_include_depth++;
4951 /* Actually process the file */
4952 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4954 cstring_free (fname);
4958 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4959 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4961 output_line_command (pfile, 0, enter_file);
4962 pfile->only_seen_white = 2;
4967 pfile->system_include_depth--;
4970 } /*@=branchstate@*/
4975 /* Return nonzero if there is no need to include file NAME
4976 because it has already been included and it contains a conditional
4977 to make a repeated include do nothing. */
4980 redundant_include_p (cppReader *pfile, cstring name)
4982 struct file_name_list *l = pfile->all_include_files;
4984 for (; l != NULL; l = l->next)
4986 if (cstring_equal (name, l->fname)
4987 && (l->control_macro != NULL)
4988 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4997 /* Return nonzero if the given FILENAME is an absolute pathname which
4998 designates a file within one of the known "system" include file
4999 directories. We assume here that if the given FILENAME looks like
5000 it is the name of a file which resides either directly in a "system"
5001 include file directory, or within any subdirectory thereof, then the
5002 given file must be a "system" include file. This function tells us
5003 if we should suppress pedantic errors/warnings for the given FILENAME.
5005 The value is 2 if the file is a C-language system header file
5006 for which C++ should (on most systems) assume `extern "C"'. */
5009 is_system_include (cppReader *pfile, cstring filename)
5011 struct file_name_list *searchptr;
5013 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
5015 searchptr = searchptr->next)
5017 if (!cstring_isEmpty (searchptr->fname))
5019 cstring sys_dir = searchptr->fname;
5020 size_t length = cstring_length (sys_dir);
5022 if (cstring_equalLen (sys_dir, filename, length)
5023 && osd_isConnectChar (cstring_getChar (filename, length)))
5025 if (searchptr->c_system_include_path)
5036 /* Convert a character string literal into a nul-terminated string.
5037 The input string is [IN ... LIMIT).
5038 The result is placed in RESULT. RESULT can be the same as IN.
5039 The value returned in the end of the string written to RESULT,
5040 or NULL on error. */
5042 static /*@null@*/ char *
5043 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5044 char *in, char *limit, int handle_escapes)
5064 /*@switchbreak@*/ break;
5068 char *bpc = (char *) in;
5069 int i = (char) cppReader_parseEscape (pfile, &bpc);
5072 *result++ = (char) c;
5073 /*@switchbreak@*/ break;
5087 * interpret #line command. Remembers previously seen fnames
5088 * in its very own hash table.
5091 /*@constant int FNAME_HASHSIZE@*/
5092 #define FNAME_HASHSIZE 37
5095 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5097 cppBuffer *ip = cppReader_getBuffer (pfile);
5099 size_t old_written = cpplib_getWritten (pfile);
5100 enum file_change_code file_change = same_file;
5101 enum cpp_token token;
5103 llassert (ip != NULL);
5104 token = get_directive_token (pfile);
5106 if (token != CPP_NUMBER
5107 || !isdigit(pfile->token_buffer[old_written]))
5109 cppReader_errorLit (pfile,
5110 cstring_makeLiteralTemp ("invalid format `#line' command"));
5112 goto bad_line_directive;
5115 /* The Newline at the end of this line remains to be processed.
5116 To put the next line at the specified line number,
5117 we must store a line number now that is one less. */
5118 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5119 cppReader_setWritten (pfile, old_written);
5121 /* NEW_LINENO is one less than the actual line number here. */
5122 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5123 cppReader_pedwarnLit (pfile,
5124 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5126 token = get_directive_token (pfile);
5128 if (token == CPP_STRING) {
5129 char *fname = pfile->token_buffer + old_written;
5131 static hashNode fname_table[FNAME_HASHSIZE];
5133 hashNode *hash_bucket;
5136 size_t fname_length;
5138 /* Turn the file name, which is a character string literal,
5139 into a null-terminated string. Do this in place. */
5140 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5141 if (end_name == NULL)
5143 cppReader_errorLit (pfile,
5144 cstring_makeLiteralTemp ("invalid format `#line' command"));
5145 goto bad_line_directive;
5148 fname_length = size_fromInt (end_name - fname);
5149 num_start = cpplib_getWritten (pfile);
5151 token = get_directive_token (pfile);
5152 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5153 p = pfile->token_buffer + num_start;
5154 if (cppReader_isPedantic (pfile))
5155 cppReader_pedwarnLit (pfile,
5156 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5158 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5160 cppReader_errorLit (pfile,
5161 cstring_makeLiteralTemp ("invalid format `#line' command"));
5162 goto bad_line_directive;
5165 file_change = enter_file;
5167 file_change = leave_file;
5169 ip->system_header_p = 1;
5170 else /* if (*p == 4) */
5171 ip->system_header_p = 2;
5173 cppReader_setWritten (pfile, num_start);
5174 token = get_directive_token (pfile);
5175 p = pfile->token_buffer + num_start;
5176 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5177 ip->system_header_p = *p == 3 ? 1 : 2;
5178 token = get_directive_token (pfile);
5180 if (token != CPP_VSPACE) {
5181 cppReader_errorLit (pfile,
5182 cstring_makeLiteralTemp ("invalid format `#line' command"));
5184 goto bad_line_directive;
5189 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5191 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5193 if (hp->length == fname_length)
5195 llassert (hp->value.cpval != NULL);
5197 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5199 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5206 /* Didn't find it; cons up a new one. */
5207 hp = (hashNode) dmalloc (sizeof (*hp));
5210 hp->bucket_hdr = NULL;
5212 hp->name = cstring_undefined;
5213 hp->next = *hash_bucket;
5217 hp->length = fname_length;
5218 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5219 memcpy (hp->value.cpval, fname, fname_length);
5220 hp->value.cpval[fname_length] = '\0';
5221 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5224 else if (token != CPP_VSPACE && token != CPP_EOF)
5226 cppReader_errorLit (pfile,
5227 cstring_makeLiteralTemp ("invalid format `#line' command"));
5228 goto bad_line_directive;
5235 ip->lineno = new_lineno;
5237 cppReader_skipRestOfLine (pfile);
5238 cppReader_setWritten (pfile, old_written);
5239 output_line_command (pfile, 0, file_change);
5244 * remove the definition of a symbol from the symbol table.
5245 * according to un*x /lib/cpp, it is not an error to undef
5246 * something that has no definitions, so it isn't one here either.
5250 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5255 char *orig_buf = buf;
5257 SKIP_WHITE_SPACE (buf);
5259 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5261 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5263 /* If we are generating additional info for debugging (with -g) we
5264 need to pass through all effective #undef commands. */
5265 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5267 pass_thru_directive (orig_buf, limit, pfile, keyword);
5270 if (hp->type != T_MACRO)
5272 cppReader_warning (pfile,
5273 message ("Undefining preprocessor builtin: %s",
5277 cppReader_deleteMacro (hp);
5280 if (cppReader_isPedantic (pfile)) {
5282 SKIP_WHITE_SPACE (buf);
5285 cppReader_pedwarnLit (pfile,
5286 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5295 * Report an error detected by the program we are processing.
5296 * Use the text of the line in the error message.
5297 * (We use error because it prints the filename & line#.)
5301 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5302 char *buf, char *limit)
5304 size_t length = size_fromInt (limit - buf);
5305 cstring copy = cstring_copyLength (buf, length);
5306 cstring adv = cstring_advanceWhiteSpace (copy);
5308 cppReader_error (pfile, message ("#error %s", adv));
5309 cstring_free (copy);
5314 * Report a warning detected by the program we are processing.
5315 * Use the text of the line in the warning message, then continue.
5316 * (We use error because it prints the filename & line#.)
5320 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5321 char *buf, char *limit)
5323 size_t length = size_fromInt (limit - buf);
5324 cstring copy = cstring_copyLength (buf, length);
5325 cstring adv = cstring_advanceWhiteSpace (copy);
5326 cppReader_warning (pfile, message ("#warning %s", adv));
5327 cstring_free (copy);
5332 /* #ident has already been copied to the output file, so just ignore it. */
5335 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5336 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5338 /* Allow #ident in system headers, since that's not user's fault. */
5339 if (cppReader_isPedantic (pfile)
5340 && !cppReader_getBufferSafe (pfile)->system_header_p)
5341 cppReader_pedwarnLit (pfile,
5342 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5344 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5349 /* #pragma and its argument line have already been copied to the output file.
5350 Just check for some recognized pragmas that need validation here. */
5353 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5354 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5356 while (*buf == ' ' || *buf == '\t')
5361 if (!strncmp (buf, "implementation", 14)) {
5362 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5363 been included yet. */
5364 struct file_name_list *ptr;
5365 char *p = buf + 14, *fname, *inc_fname;
5367 SKIP_WHITE_SPACE (p);
5368 if (*p == '\n' || *p != '\"')
5372 p = (char *) strchr (fname, '\"');
5373 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5375 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5377 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5378 inc_fname = (inc_fname != NULL)
5379 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5381 if ((inc_fname != NULL)
5382 && (strncmp (inc_fname, fname, fname_len) == 0))
5384 cpp_setLocation (pfile);
5386 ppllerror (message ("`#pragma implementation' for `%s' appears "
5387 "after file is included",
5388 cstring_fromChars (fname)));
5397 * handle #if command by
5398 * 1) inserting special `defined' keyword into the hash table
5399 * that gets turned into 0 or 1 by special_symbol (thus,
5400 * if the luser has a symbol called `defined' already, it won't
5401 * work inside the #if command)
5402 * 2) rescan the input into a temporary output buffer
5403 * 3) pass the output buffer to the yacc parser and collect a value
5404 * 4) clean up the mess left from steps 1 and 2.
5405 * 5) call conditional_skip to skip til the next #endif (etc.),
5406 * or not, depending on the value from step 3.
5410 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5411 char *buf, char *limit)
5413 HOST_WIDE_INT value;
5414 DPRINTF (("Do if: %s", buf));
5415 value = eval_if_expression (pfile, buf, limit - buf);
5416 conditional_skip (pfile, value == 0, T_IF, NULL);
5421 * handle a #elif directive by not changing if_stack either.
5422 * see the comment above do_else.
5425 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5426 char *buf, char *limit)
5428 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5430 cppReader_errorLit (pfile,
5431 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5436 llassert (pfile->if_stack != NULL);
5438 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5440 cppReader_errorLit (pfile,
5441 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5443 if (pfile->if_stack->fname != NULL
5444 && cppReader_getBufferSafe (pfile)->fname != NULL
5445 && !cstring_equal (pfile->if_stack->fname,
5446 cppReader_getBufferSafe (pfile)->nominal_fname))
5447 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5448 fprintf (stderr, ")\n");
5450 pfile->if_stack->type = T_ELIF;
5453 if (pfile->if_stack->if_succeeded)
5455 skip_if_group (pfile, 0);
5459 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5461 skip_if_group (pfile, 0);
5464 ++pfile->if_stack->if_succeeded; /* continue processing input */
5465 output_line_command (pfile, 1, same_file);
5473 * evaluate a #if expression in BUF, of length LENGTH,
5474 * then parse the result as a C expression and return the value as an int.
5477 static HOST_WIDE_INT
5478 eval_if_expression (cppReader *pfile,
5479 /*@unused@*/ char *buf,
5480 /*@unused@*/ int length)
5482 hashNode save_defined;
5483 HOST_WIDE_INT value;
5484 size_t old_written = cpplib_getWritten (pfile);
5486 DPRINTF (("Saving defined..."));
5487 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5488 pfile->pcp_inside_if = 1;
5490 value = cppReader_parseExpression (pfile);
5491 pfile->pcp_inside_if = 0;
5493 /* Clean up special symbol */
5494 DPRINTF (("Removing defined..."));
5495 cppReader_deleteMacro (save_defined);
5496 cppReader_setWritten (pfile, old_written); /* Pop */
5502 * routine to handle ifdef/ifndef. Try to look up the symbol,
5503 * then do or don't skip to the #endif/#else/#elif depending
5504 * on what directive is actually being processed.
5508 do_xifdef (cppReader *pfile, struct directive *keyword,
5509 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5512 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5514 size_t ident_length;
5515 enum cpp_token token;
5516 int start_of_file = 0;
5517 char *control_macro = 0;
5518 size_t old_written = cpplib_getWritten (pfile);
5520 DPRINTF (("do xifdef: %d",
5521 keyword->type == T_IFNDEF));
5523 /* Detect a #ifndef at start of file (not counting comments). */
5524 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5526 start_of_file = pfile->only_seen_white == 2;
5529 pfile->no_macro_expand++;
5530 token = get_directive_token (pfile);
5531 pfile->no_macro_expand--;
5533 ident = pfile->token_buffer + old_written;
5534 DPRINTF (("Ident: %s", ident));
5536 ident_length = cpplib_getWritten (pfile) - old_written;
5537 cppReader_setWritten (pfile, old_written); /* Pop */
5539 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5541 skip = (keyword->type == T_IFDEF);
5542 if (! cppReader_isTraditional (pfile))
5544 cppReader_pedwarn (pfile,
5545 message ("`#%s' with no argument", keyword->name));
5548 else if (token == CPP_NAME)
5550 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5552 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5554 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5556 if (start_of_file && !skip)
5558 DPRINTF (("Not skipping!"));
5559 control_macro = (char *) dmalloc (ident_length + 1);
5560 memcpy (control_macro, ident, ident_length + 1);
5565 skip = (keyword->type == T_IFDEF);
5566 if (! cppReader_isTraditional (pfile))
5568 cppReader_error (pfile,
5569 message ("`#%s' with invalid argument", keyword->name));
5573 if (!cppReader_isTraditional (pfile))
5576 cppSkipHspace (pfile);
5577 c = cppReader_peekC (pfile);
5578 if (c != EOF && c != '\n')
5580 cppReader_pedwarn (pfile,
5581 message ("garbage at end of `#%s' argument", keyword->name));
5585 cppReader_skipRestOfLine (pfile);
5587 DPRINTF (("Conditional skip: %d", skip));
5588 conditional_skip (pfile, skip, T_IF, control_macro);
5592 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5593 If this is a #ifndef starting at the beginning of a file,
5594 CONTROL_MACRO is the macro name tested by the #ifndef.
5595 Otherwise, CONTROL_MACRO is 0. */
5598 conditional_skip (cppReader *pfile, int skip,
5599 enum node_type type,
5600 /*@dependent@*/ char *control_macro)
5602 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5604 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5605 temp->next = pfile->if_stack;
5606 temp->control_macro = control_macro;
5608 temp->if_succeeded = 0;
5610 pfile->if_stack = temp;
5611 pfile->if_stack->type = type;
5615 skip_if_group (pfile, 0);
5620 ++pfile->if_stack->if_succeeded;
5621 output_line_command (pfile, 1, same_file);
5626 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5627 * leaves input ptr at the sharp sign found.
5628 * If ANY is nonzero, return at next directive of any sort.
5632 skip_if_group (cppReader *pfile, int any)
5635 struct directive *kt;
5636 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5637 register int ident_length;
5639 struct parse_marker line_start_mark;
5641 parseSetMark (&line_start_mark, pfile);
5643 if (CPPOPTIONS (pfile)->output_conditionals) {
5644 static char failed[] = "#failed\n";
5645 cppReader_puts (pfile, failed, sizeof(failed)-1);
5647 output_line_command (pfile, 1, same_file);
5651 if (CPPOPTIONS (pfile)->output_conditionals)
5653 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5656 llassert (pbuf->buf != NULL);
5658 start_line = pbuf->buf + line_start_mark.position;
5659 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5662 parseMoveMark (&line_start_mark, pfile);
5664 if (!cppReader_isTraditional (pfile))
5666 cppSkipHspace (pfile);
5669 c = cppReader_getC (pfile);
5672 size_t old_written = cpplib_getWritten (pfile);
5673 cppSkipHspace (pfile);
5675 parse_name (pfile, cppReader_getC (pfile));
5676 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5677 ident = pfile->token_buffer + old_written;
5678 pfile->limit = ident;
5680 for (kt = directive_table; kt->length >= 0; kt++)
5682 cppIfStackFrame *temp;
5683 if (ident_length == kt->length
5684 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5686 /* If we are asked to return on next directive, do so now. */
5697 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5698 temp->next = pfile->if_stack;
5699 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5700 temp->type = kt->type;
5702 temp->if_succeeded = 0;
5703 temp->control_macro = NULL;
5705 pfile->if_stack = temp;
5706 /*@switchbreak@*/ break;
5709 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5710 validate_else (pfile,
5711 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5714 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5716 cppReader_error (pfile,
5717 message ("Preprocessor command #%s is not within a conditional", kt->name));
5718 /*@switchbreak@*/ break;
5720 else if (pfile->if_stack == save_if_stack)
5722 goto done; /* found what we came for */
5729 if (kt->type != T_ENDIF)
5731 llassert (pfile->if_stack != NULL);
5733 if (pfile->if_stack->type == T_ELSE)
5735 cppReader_errorLit (pfile,
5736 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5739 pfile->if_stack->type = kt->type;
5740 /*@switchbreak@*/ break;
5743 temp = pfile->if_stack;
5744 llassert (temp != NULL);
5745 pfile->if_stack = temp->next;
5747 /*@switchbreak@*/ break;
5750 #if defined (OS2) && defined (__IBMC__)
5751 /* Dummy code to eleminate optimization problems with icc */
5760 /* Don't let erroneous code go by. */
5762 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5763 && cppReader_isPedantic (pfile))
5765 cppReader_pedwarnLit (pfile,
5766 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5770 c = cppReader_getC (pfile);
5772 /* We're in the middle of a line. Skip the rest of it. */
5780 case '/': /* possible comment */
5781 c = skip_comment (pfile, NULL);
5784 /*@switchbreak@*/ break;
5787 cppReader_forward (pfile, -1);
5788 old = cpplib_getWritten (pfile);
5789 (void) cpplib_getToken (pfile);
5790 cppReader_setWritten (pfile, old);
5791 /*@switchbreak@*/ break;
5793 /* Char after backslash loses its special meaning. */
5794 if (cppReader_peekC (pfile) == '\n')
5796 cppReader_forward (pfile, 1);
5799 /*@switchbreak@*/ break;
5803 c = cppReader_getC (pfile);
5806 if (CPPOPTIONS (pfile)->output_conditionals) {
5807 static char end_failed[] = "#endfailed\n";
5808 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5811 pfile->only_seen_white = 1;
5813 parseGotoMark (&line_start_mark, pfile);
5814 parseClearMark (&line_start_mark);
5818 * handle a #else directive. Do this by just continuing processing
5819 * without changing if_stack ; this is so that the error message
5820 * for missing #endif's etc. will point to the original #if. It
5821 * is possible that something different would be better.
5825 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5826 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5828 if (cppReader_isPedantic (pfile))
5830 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5833 cppReader_skipRestOfLine (pfile);
5835 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5836 cppReader_errorLit (pfile,
5837 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5840 /* #ifndef can't have its special treatment for containing the whole file
5841 if it has a #else clause. */
5843 llassert (pfile->if_stack != NULL);
5845 pfile->if_stack->control_macro = 0;
5847 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5849 cpp_setLocation (pfile);
5850 genppllerrorhint (FLG_PREPROC,
5851 message ("Pre-processor directive #else after #else"),
5852 message ("%q: Location of match",
5853 fileloc_unparseRaw (pfile->if_stack->fname,
5854 pfile->if_stack->lineno)));
5857 pfile->if_stack->type = T_ELSE;
5860 if (pfile->if_stack->if_succeeded)
5861 skip_if_group (pfile, 0);
5863 ++pfile->if_stack->if_succeeded; /* continue processing input */
5864 output_line_command (pfile, 1, same_file);
5871 * unstack after #endif command
5875 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5876 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5878 if (cppReader_isPedantic (pfile))
5880 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5883 cppReader_skipRestOfLine (pfile);
5885 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5887 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5891 cppIfStackFrame *temp = pfile->if_stack;
5893 llassert (temp != NULL);
5895 pfile->if_stack = temp->next;
5896 if (temp->control_macro != 0)
5898 /* This #endif matched a #ifndef at the start of the file.
5899 See if it is at the end of the file. */
5900 struct parse_marker start_mark;
5903 parseSetMark (&start_mark, pfile);
5907 cppSkipHspace (pfile);
5908 c = cppReader_getC (pfile);
5914 parseGotoMark (&start_mark, pfile);
5915 parseClearMark (&start_mark);
5919 /* If we get here, this #endif ends a #ifndef
5920 that contains all of the file (aside from whitespace).
5921 Arrange not to include the file again
5922 if the macro that was tested is defined.
5924 Do not do this for the top-level file in a -include or any
5925 file in a -imacros. */
5926 struct file_name_list *ifile = pfile->all_include_files;
5928 for ( ; ifile != NULL; ifile = ifile->next)
5930 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5932 ifile->control_macro = temp->control_macro;
5940 output_line_command (pfile, 1, same_file);
5945 /* When an #else or #endif is found while skipping failed conditional,
5946 if -pedantic was specified, this is called to warn about text after
5947 the command name. P points to the first char after the command name. */
5950 validate_else (cppReader *pfile, cstring directive)
5953 cppSkipHspace (pfile);
5954 c = cppReader_peekC (pfile);
5955 if (c != EOF && c != '\n')
5957 cppReader_pedwarn (pfile,
5958 message ("text following `%s' violates ANSI standard", directive));
5963 ** Get the next token, and add it to the text in pfile->token_buffer.
5964 ** Return the kind of token we got.
5968 cpplib_getToken (cppReader *pfile)
5970 return cpplib_getTokenAux (pfile, FALSE);
5974 cpplib_getTokenForceExpand (cppReader *pfile)
5976 return cpplib_getTokenAux (pfile, TRUE);
5980 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5983 size_t old_written = 0;
5984 int start_line, start_column;
5985 enum cpp_token token;
5986 struct cppOptions *opts = CPPOPTIONS (pfile);
5987 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5990 c = cppReader_getC (pfile);
5991 DPRINTF (("Get next token: %c", c));
5996 if (cppReader_getBufferSafe (pfile)->seen_eof)
5998 cppBuffer *buf = cppReader_popBuffer (pfile);
6000 if (buf != cppReader_nullBuffer (pfile))
6011 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6012 cppReader_getBufferSafe (pfile)->seen_eof = 1;
6014 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
6015 && next_buf != cppReader_nullBuffer (pfile))
6017 /* We're about to return from an #include file.
6018 Emit #line information now (as part of the CPP_POP) result.
6019 But the #line refers to the file we will pop to. */
6020 cppBuffer *cur_buffer = CPPBUFFER (pfile);
6021 CPPBUFFER (pfile) = next_buf;
6022 pfile->input_stack_listing_current = 0;
6023 output_line_command (pfile, 0, leave_file);
6024 CPPBUFFER (pfile) = cur_buffer;
6032 struct parse_marker start_mark;
6037 if (cppReader_peekC (pfile) == '=')
6042 if (opts->put_out_comments)
6044 parseSetMark (&start_mark, pfile);
6048 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6049 &start_line, &start_column);
6050 c = skip_comment (pfile, &newlines);
6051 DPRINTF (("c = %c", c));
6052 if (opts->put_out_comments && (c == '/' || c == EOF))
6054 assertSet (&start_mark);
6055 parseClearMark (&start_mark);
6062 cppReader_errorWithLine (pfile, start_line, start_column,
6063 cstring_makeLiteral ("Unterminated comment"));
6066 c = '/'; /* Initial letter of comment. */
6068 /* Comments are equivalent to spaces.
6069 For -traditional, a comment is equivalent to nothing. */
6071 if (opts->put_out_comments)
6075 assertSet (&start_mark);
6076 res = cpp_handleComment (pfile, &start_mark);
6077 pfile->lineno += newlines;
6080 else if (cppReader_isTraditional (pfile))
6086 cpplib_reserve(pfile, 1);
6087 cppReader_putCharQ (pfile, ' ');
6092 if (!pfile->only_seen_white)
6097 if (cppReader_handleDirective (pfile))
6099 return CPP_DIRECTIVE;
6102 pfile->only_seen_white = 0;
6107 /* A single quoted string is treated like a double -- some
6108 programs (e.g., troff) are perverse this way */
6109 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6110 &start_line, &start_column);
6111 old_written = cpplib_getWritten (pfile);
6113 DPRINTF (("Put char: %c", c));
6114 cppReader_putChar (pfile, c);
6117 int cc = cppReader_getC (pfile);
6118 DPRINTF (("cc: %c", c));
6121 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6123 /* try harder: this string crosses a macro expansion
6124 boundary. This can happen naturally if -traditional.
6125 Otherwise, only -D can make a macro with an unmatched
6128 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6129 (*cppReader_getBufferSafe (pfile)->cleanup)
6130 (cppReader_getBufferSafe (pfile), pfile);
6131 CPPBUFFER (pfile) = next_buf;
6134 if (!cppReader_isTraditional (pfile))
6136 cpp_setLocation (pfile);
6138 setLine (long_toInt (start_line));
6139 setColumn (long_toInt (start_column));
6141 if (pfile->multiline_string_line != long_toInt (start_line)
6142 && pfile->multiline_string_line != 0)
6146 message ("Unterminated string or character constant"),
6147 message ("%q: Possible real start of unterminated constant",
6149 (fileloc_filename (g_currentloc),
6150 pfile->multiline_string_line)));
6151 pfile->multiline_string_line = 0;
6157 message ("Unterminated string or character constant"));
6160 /*@loopbreak@*/ break;
6162 DPRINTF (("putting char: %c", cc));
6163 cppReader_putChar (pfile, cc);
6167 /* Traditionally, end of line ends a string constant with
6168 no error. So exit the loop and record the new line. */
6169 if (cppReader_isTraditional (pfile))
6175 if (cppReader_isPedantic (pfile)
6176 && pfile->multiline_string_line == 0)
6178 cppReader_pedwarnWithLine
6179 (pfile, long_toInt (start_line),
6180 long_toInt (start_column),
6181 cstring_makeLiteral ("String constant runs past end of line"));
6183 if (pfile->multiline_string_line == 0)
6185 pfile->multiline_string_line = start_line;
6188 /*@switchbreak@*/ break;
6191 cc = cppReader_getC (pfile);
6194 /* Backslash newline is replaced by nothing at all. */
6195 cppReader_adjustWritten (pfile, -1);
6200 /* ANSI stupidly requires that in \\ the second \
6201 is *not* prevented from combining with a newline. */
6204 cppReader_putChar (pfile, cc);
6206 /*@switchbreak@*/ break;
6212 /*@switchbreak@*/ break;
6216 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6217 cpplib_getPWritten (pfile));
6218 pfile->only_seen_white = 0;
6219 return c == '\'' ? CPP_CHAR : CPP_STRING;
6222 if (!opts->dollars_in_ident)
6227 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6235 c2 = cppReader_peekC (pfile);
6236 if (c2 == c || c2 == '=')
6246 if (cppReader_peekC (pfile) == '=')
6252 c2 = cppReader_peekC (pfile);
6253 if (c2 == '-' && opts->chill)
6255 /* Chill style comment */
6256 if (opts->put_out_comments)
6258 parseSetMark (&start_mark, pfile);
6261 cppReader_forward (pfile, 1); /* Skip second '-'. */
6265 c = cppReader_getC (pfile);
6267 /*@loopbreak@*/ break;
6270 /* Don't consider final '\n' to be part of comment. */
6271 cppReader_forward (pfile, -1);
6272 /*@loopbreak@*/ break;
6276 goto return_comment;
6278 if (c2 == '-' || c2 == '=' || c2 == '>')
6283 if (pfile->parsing_include_directive)
6287 cppReader_putChar (pfile, c);
6289 /*@loopbreak@*/ break;
6290 c = cppReader_getC (pfile);
6292 if (c == '\n' || c == EOF)
6294 cppReader_errorLit (pfile,
6295 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6296 /*@loopbreak@*/ break;
6304 c2 = cppReader_peekC (pfile);
6309 cppReader_forward (pfile, 1);
6310 cpplib_reserve (pfile, 4);
6311 cppReader_putChar (pfile, c);
6312 cppReader_putChar (pfile, c2);
6314 c3 = cppReader_peekC (pfile);
6316 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6317 cppReader_nullTerminateQ (pfile);
6318 pfile->only_seen_white = 0;
6322 DPRINTF (("Macro @!"));
6323 if (cppReader_getBufferSafe (pfile)->has_escapes)
6325 c = cppReader_getC (pfile);
6326 DPRINTF (("got c: %c", c));
6329 if (pfile->output_escapes)
6330 cppReader_puts (pfile, "@-", 2);
6331 parse_name (pfile, cppReader_getC (pfile));
6334 else if (is_space [c])
6336 cpplib_reserve (pfile, 2);
6337 if (pfile->output_escapes)
6338 cppReader_putCharQ (pfile, '@');
6339 cppReader_putCharQ (pfile, c);
6347 if (pfile->output_escapes)
6349 cppReader_puts (pfile, "@@", 2);
6355 c2 = cppReader_peekC (pfile);
6358 cpplib_reserve(pfile, 2);
6359 cppReader_putCharQ (pfile, '.');
6360 c = cppReader_getC (pfile);
6364 /* FIXME - misses the case "..\\\n." */
6365 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6367 cpplib_reserve(pfile, 4);
6368 cppReader_putCharQ (pfile, '.');
6369 cppReader_putCharQ (pfile, '.');
6370 cppReader_putCharQ (pfile, '.');
6371 cppReader_forward (pfile, 2);
6372 cppReader_nullTerminateQ (pfile);
6373 pfile->only_seen_white = 0;
6379 pfile->only_seen_white = 0;
6381 cpplib_reserve(pfile, 3);
6382 cppReader_putCharQ (pfile, c);
6383 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6384 cppReader_nullTerminateQ (pfile);
6389 c2 = cppReader_peekC (pfile);
6390 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6392 cppReader_putChar (pfile, c);
6393 c = cppReader_getC (pfile);
6398 case '0': case '1': case '2': case '3': case '4':
6399 case '5': case '6': case '7': case '8': case '9':
6404 cpplib_reserve (pfile, 2);
6405 cppReader_putCharQ (pfile, c);
6407 c = cppReader_peekC (pfile);
6409 /*@loopbreak@*/ break;
6410 if (!is_idchar[c] && c != '.'
6411 && ((c2 != 'e' && c2 != 'E'
6412 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6413 || (c != '+' && c != '-')))
6414 /*@loopbreak@*/ break;
6415 cppReader_forward (pfile, 1);
6419 cppReader_nullTerminateQ (pfile);
6420 pfile->only_seen_white = 0;
6423 case 'b': case 'c': case 'd': case 'h': case 'o':
6424 case 'B': case 'C': case 'D': case 'H': case 'O':
6425 if (opts->chill && cppReader_peekC (pfile) == '\'')
6427 pfile->only_seen_white = 0;
6428 cpplib_reserve (pfile, 2);
6429 cppReader_putCharQ (pfile, c);
6430 cppReader_putCharQ (pfile, '\'');
6431 cppReader_forward (pfile, 1);
6434 c = cppReader_getC (pfile);
6436 goto chill_number_eof;
6439 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6441 cppReader_forward (pfile, 2);
6444 /*@loopbreak@*/ break;
6446 cppReader_putChar (pfile, c);
6450 cpplib_reserve (pfile, 2);
6451 cppReader_putCharQ (pfile, c);
6452 cppReader_nullTerminateQ (pfile);
6457 cppReader_forward (pfile, -1);
6459 cppReader_nullTerminate (pfile);
6466 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6467 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6468 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6469 case 'x': case 'y': case 'z':
6470 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6471 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6472 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6478 size_t before_name_written = cpplib_getWritten (pfile);
6480 parse_name (pfile, c);
6481 pfile->only_seen_white = 0;
6483 if (pfile->no_macro_expand)
6485 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6489 ident = pfile->token_buffer + before_name_written;
6490 DPRINTF (("Ident: %s", ident));
6492 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6494 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6498 DPRINTF (("No expand: %s %d", ident, ident_len));
6502 if (hp->type == T_DISABLED)
6504 DPRINTF (("Disabled!"));
6506 if (pfile->output_escapes)
6507 { /* Return "@-IDENT", followed by '\0'. */
6509 cpplib_reserve (pfile, 3);
6510 ident = pfile->token_buffer + before_name_written;
6511 cppReader_adjustWritten (pfile, 2);
6513 for (i = size_toInt (ident_len); i >= 0; i--)
6515 ident[i+2] = ident[i];
6525 ** If macro wants an arglist, verify that a '(' follows.
6526 ** first skip all whitespace, copying it to the output
6527 ** after the macro name. Then, if there is no '(',
6528 ** decide this is not a macro call and leave things that way.
6531 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6533 struct parse_marker macro_mark;
6536 DPRINTF (("Arglist macro!"));
6539 ** evans 2002-07-03: Moved this here (from below).
6540 ** This bug caused necessary whitespace to be lost
6541 ** when parsing parameterized macros without parameters.
6544 parseSetMark (¯o_mark, pfile);
6546 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6548 cppBuffer *next_buf;
6549 cppSkipHspace (pfile);
6550 if (cppReader_peekC (pfile) != EOF)
6552 DPRINTF (("Peeking!"));
6553 /*@loopbreak@*/ break;
6556 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6557 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6558 CPPBUFFER (pfile) = next_buf;
6561 /* parseSetMark (¯o_mark, pfile); */
6565 cppSkipHspace (pfile);
6566 c = cppReader_peekC (pfile);
6567 DPRINTF (("c: %c", c));
6568 is_macro_call = c == '(';
6570 /*@loopbreak@*/ break;
6571 cppReader_forward (pfile, 1);
6576 parseGotoMark (¯o_mark, pfile);
6579 parseClearMark (¯o_mark);
6583 DPRINTF (("not macro call!"));
6588 /* This is now known to be a macro call. */
6590 /* it might not actually be a macro. */
6591 if (hp->type != T_MACRO)
6596 cppReader_setWritten (pfile, before_name_written);
6597 special_symbol (hp, pfile);
6598 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6599 xbuf = (char *) dmalloc (xbuf_len + 1);
6600 cppReader_setWritten (pfile, before_name_written);
6601 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6602 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6607 ** Expand the macro, reading arguments as needed,
6608 ** and push the expansion on the input stack.
6611 cpplib_macroExpand (pfile, hp);
6612 cppReader_setWritten (pfile, before_name_written);
6615 /* An extra "@ " is added to the end of a macro expansion
6616 to prevent accidental token pasting. We prefer to avoid
6617 unneeded extra spaces (for the sake of cpp-using tools like
6618 imake). Here we remove the space if it is safe to do so. */
6620 llassert (pfile->buffer->rlimit != NULL);
6622 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6623 && pfile->buffer->rlimit[-2] == '@'
6624 && pfile->buffer->rlimit[-1] == ' ')
6626 int c1 = pfile->buffer->rlimit[-3];
6627 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6629 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6630 pfile->buffer->rlimit -= 2;
6636 case ' ': case '\t': case '\v': case '\r':
6639 cppReader_putChar (pfile, c);
6640 c = cppReader_peekC (pfile);
6641 if (c == EOF || !is_hor_space[c])
6642 /*@loopbreak@*/ break;
6643 cppReader_forward (pfile, 1);
6648 c2 = cppReader_peekC (pfile);
6655 cppReader_putChar (pfile, c);
6656 if (pfile->only_seen_white == 0)
6657 pfile->only_seen_white = 1;
6659 output_line_command (pfile, 1, same_file);
6662 case '(': token = CPP_LPAREN; goto char1;
6663 case ')': token = CPP_RPAREN; goto char1;
6664 case '{': token = CPP_LBRACE; goto char1;
6665 case '}': token = CPP_RBRACE; goto char1;
6666 case ',': token = CPP_COMMA; goto char1;
6667 case ';': token = CPP_SEMICOLON; goto char1;
6673 pfile->only_seen_white = 0;
6674 cppReader_putChar (pfile, c);
6683 /* Parse an identifier starting with C. */
6686 parse_name (cppReader *pfile, int c)
6692 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6694 cppReader_forward (pfile, 2);
6698 cppReader_forward (pfile, -1);
6702 if (c == '$' && cppReader_isPedantic (pfile))
6704 cppReader_pedwarnLit (pfile,
6705 cstring_makeLiteralTemp ("`$' in identifier"));
6708 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6709 cppReader_putCharQ (pfile, c);
6710 c = cppReader_getC (pfile);
6716 cppReader_nullTerminateQ (pfile);
6719 /* The file_name_map structure holds a mapping of file names for a
6720 particular directory. This mapping is read from the file named
6721 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6722 map filenames on a file system with severe filename restrictions,
6723 such as DOS. The format of the file name map file is just a series
6724 of lines with two tokens on each line. The first token is the name
6725 to map, and the second token is the actual name to use. */
6727 struct file_name_map
6729 struct file_name_map *map_next;
6734 /*@constant observer char *FILE_NAME_MAP_FILE*/
6735 #define FILE_NAME_MAP_FILE "header.gcc"
6737 /* Read a space delimited string of unlimited length from a stdio
6740 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6746 set = alloc = dmalloc (len + 1);
6751 while ((ch = getc (f)) != EOF && ! is_space[ch])
6753 if (set - alloc == size_toInt (len))
6756 alloc = drealloc (alloc, len + 1);
6757 set = alloc + len / 2;
6758 /*@-branchstate@*/ }
6761 } /*@=branchstate@*/
6764 check (ungetc (ch, f) != EOF);
6766 return cstring_fromChars (alloc);
6769 /* This structure holds a linked list of file name maps, one per directory. */
6771 struct file_name_map_list
6773 /*@only@*/ struct file_name_map_list *map_list_next;
6774 /*@only@*/ cstring map_list_name;
6775 /*@null@*/ struct file_name_map *map_list_map;
6778 /* Read the file name map file for DIRNAME. */
6780 static struct file_name_map *
6781 read_name_map (cppReader *pfile, cstring dirname)
6783 struct file_name_map_list *map_list_ptr;
6787 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6788 map_list_ptr != NULL;
6789 map_list_ptr = map_list_ptr->map_list_next)
6791 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6793 return map_list_ptr->map_list_map;
6797 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6798 map_list_ptr->map_list_name = cstring_copy (dirname);
6799 map_list_ptr->map_list_map = NULL;
6801 name = cstring_copy (dirname);
6803 if (cstring_length (dirname) > 0)
6805 name = cstring_appendChar (name, CONNECTCHAR);
6808 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6810 f = fileTable_openReadFile (context_fileTable (), name);
6811 cstring_free (name);
6815 map_list_ptr->map_list_map = NULL;
6821 while ((ch = getc (f)) != EOF)
6824 struct file_name_map *ptr;
6831 from = read_filename_string (ch, f);
6832 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6837 to = read_filename_string (ch, f);
6839 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6840 ptr->map_from = from;
6842 /* Make the real filename absolute. */
6843 if (cstring_length (to) > 1
6844 && osd_isConnectChar (cstring_firstChar (to)))
6850 ptr->map_to = cstring_copy (dirname);
6851 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6852 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6855 ptr->map_next = map_list_ptr->map_list_map;
6856 map_list_ptr->map_list_map = ptr;
6858 while ((ch = getc (f)) != '\n')
6862 /*@innerbreak@*/ break;
6867 assertSet (map_list_ptr->map_list_map);
6868 check (fileTable_closeFile (context_fileTable (),f) == 0);
6871 map_list_ptr->map_list_next = pfile->opts->map_list;
6872 pfile->opts->map_list = map_list_ptr;
6874 return map_list_ptr->map_list_map;
6877 /* Try to open include file FILENAME. SEARCHPTR is the directory
6878 being tried from the include file search path. This function maps
6879 filenames on file systems based on information read by
6883 open_include_file (cppReader *pfile,
6885 struct file_name_list *searchptr)
6887 char *filename = cstring_toCharsSafe (fname);
6888 struct file_name_map *map;
6892 cstring_markOwned (fname);
6894 cpp_setLocation (pfile);
6896 if (context_getFlag (FLG_NEVERINCLUDE))
6898 if (isHeaderFile (fname))
6900 return SKIP_INCLUDE;
6904 if ((searchptr != NULL) && ! searchptr->got_name_map)
6906 searchptr->name_map = read_name_map (pfile,
6907 !cstring_isEmpty (searchptr->fname)
6908 ? searchptr->fname :
6909 cstring_makeLiteralTemp ("."));
6910 searchptr->got_name_map = 1;
6913 /* First check the mapping for the directory we are using. */
6915 if ((searchptr != NULL)
6916 && (searchptr->name_map != NULL))
6920 if (!cstring_isEmpty (searchptr->fname))
6922 from += cstring_length (searchptr->fname) + 1;
6925 for (map = searchptr->name_map;
6927 map = map->map_next)
6929 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6932 ** Found a match. Check if the file should be skipped
6935 if (cpp_skipIncludeFile (map->map_to))
6937 return SKIP_INCLUDE;
6941 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6948 ** Try to find a mapping file for the particular directory we are
6949 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6950 ** in /usr/include/header.gcc and look up types.h in
6951 ** /usr/include/sys/header.gcc.
6954 p = strrchr (filename, CONNECTCHAR);
6961 if ((searchptr != NULL)
6962 && (cstring_isDefined (searchptr->fname))
6963 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6964 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6966 size_fromInt (p - filename)))
6968 /* filename is in SEARCHPTR, which we've already checked. */
6970 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6972 return SKIP_INCLUDE;
6976 return cpp_openIncludeFile (filename);
6982 dir = mstring_copy (".");
6987 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6988 memcpy (dir, filename, size_fromInt (p - filename));
6989 dir[p - filename] = '\0';
6993 for (map = read_name_map (pfile, cstring_fromChars (dir));
6995 map = map->map_next)
6997 if (cstring_equal (map->map_from, cstring_fromChars (from)))
7001 if (cpp_skipIncludeFile (map->map_to))
7003 return SKIP_INCLUDE;
7007 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
7014 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
7016 return SKIP_INCLUDE;
7020 return cpp_openIncludeFile (filename);
7024 /* Process the contents of include file FNAME, already open on descriptor F,
7026 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
7027 "system" include directories (as decided by the `is_system_include'
7029 DIRPTR is the link in the dir path through which this file was found,
7030 or 0 if the file name was absolute or via the current directory.
7031 Return 1 on success, 0 on failure.
7033 The caller is responsible for the cppReader_pushBuffer. */
7036 finclude (cppReader *pfile, int f,
7038 bool system_header_p,
7039 /*@dependent@*/ struct file_name_list *dirptr)
7045 cppBuffer *fp; /* For input stack frame */
7047 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7049 cppReader_perrorWithName (pfile, fname);
7050 check (close (f) == 0);
7051 (void) cppReader_popBuffer (pfile);
7057 fp = cppReader_getBufferSafe (pfile);
7059 /*@-temptrans@*/ /* fname shouldn't really be temp */
7060 fp->nominal_fname = fp->fname = fname;
7064 fp->system_header_p = system_header_p;
7067 fp->cleanup = cppReader_fileCleanup;
7069 if (S_ISREG (st_mode))
7072 fp->buf = (char *) dmalloc (st_size + 2);
7073 fp->alimit = fp->buf + st_size + 2;
7076 /* Read the file contents, knowing that st_size is an upper bound
7077 on the number of bytes we can read. */
7078 length = safe_read (f, fp->buf, size_toInt (st_size));
7079 fp->rlimit = fp->buf + length;
7080 if (length < 0) goto nope;
7082 else if (S_ISDIR (st_mode))
7084 cppReader_error (pfile,
7085 message ("Directory specified where file is expected: %s", fname));
7086 check (close (f) == 0);
7092 ** Cannot count its file size before reading.
7093 ** First read the entire file into heap and
7094 ** copy them into buffer on stack.
7097 size_t bsize = 2000;
7102 fp->buf = (char *) dmalloc (bsize + 2);
7105 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7108 goto nope; /* error! */
7111 if (st_size != bsize)
7113 break; /* End of file */
7117 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7121 length = size_toInt (st_size);
7124 if ((length > 0 && fp->buf[length - 1] != '\n')
7125 /* Backslash-newline at end is not good enough. */
7126 || (length > 1 && fp->buf[length - 2] == '\\')) {
7127 fp->buf[length++] = '\n';
7130 fp->buf[length] = '\0';
7131 fp->rlimit = fp->buf + length;
7133 /* Close descriptor now, so nesting does not use lots of descriptors. */
7134 check (close (f) == 0);
7136 /* Must do this before calling trigraph_pcp, so that the correct file name
7137 will be printed in warning messages. */
7139 pfile->input_stack_listing_current = 0;
7144 cppReader_perrorWithName (pfile, fname);
7145 check (close (f) == 0);
7151 cpplib_init (cppReader *pfile)
7153 memset ((char *) pfile, 0, sizeof (*pfile));
7155 pfile->get_token = cpplib_getToken;
7156 pfile->token_buffer_size = 200;
7157 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7158 pfile->all_include_files = NULL;
7162 cppReader_setWritten (pfile, 0);
7164 pfile->system_include_depth = 0;
7165 pfile->max_include_len = 0;
7166 pfile->timebuf = NULL;
7167 pfile->only_seen_white = 1;
7169 pfile->buffer = cppReader_nullBuffer (pfile);
7173 cppReader_finish (/*@unused@*/ cppReader *pfile)
7178 /* Free resources used by PFILE.
7179 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7182 cppCleanup (/*@special@*/ cppReader *pfile)
7184 /*@releases pfile@*/
7186 DPRINTF (("cppCleanup!"));
7188 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7190 (void) cppReader_popBuffer (pfile);
7193 if (pfile->token_buffer != NULL)
7195 sfree (pfile->token_buffer);
7196 pfile->token_buffer = NULL;
7199 while (pfile->if_stack != NULL)
7201 cppIfStackFrame *temp = pfile->if_stack;
7202 pfile->if_stack = temp->next;
7206 while (pfile->all_include_files != NULL)
7208 struct file_name_list *temp = pfile->all_include_files;
7209 pfile->all_include_files = temp->next;
7210 /*@-dependenttrans@*/
7211 cstring_free (temp->fname);
7212 /*@=dependenttrans@*/
7216 /* evans 2002-07-12 */
7217 while (pfile->opts->map_list != NULL)
7219 struct file_name_map_list *temp = pfile->opts->map_list;
7220 pfile->opts->map_list = pfile->opts->map_list->map_list_next;
7221 cstring_free (temp->map_list_name);
7225 while (pfile->opts->include != NULL)
7227 struct file_name_list *temp = pfile->opts->include;
7228 pfile->opts->include = pfile->opts->include->next;
7229 /* cstring_free (temp->fname); */
7233 sfree (pfile->opts);
7235 cppReader_hashCleanup ();
7239 ** Get the file-mode and data size of the file open on FD
7240 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7244 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7248 if (fstat (fd, &sbuf) < 0) {
7254 if (mode_pointer != NULL)
7256 *mode_pointer = sbuf.st_mode;
7259 if (size_pointer != NULL)
7261 *size_pointer = (size_t) sbuf.st_size;
7267 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7268 retrying if necessary. Return a negative value if an error occurs,
7269 otherwise return the actual number of bytes read,
7270 which must be LEN unless end-of-file was reached. */
7272 static int safe_read (int desc, char *ptr, int len)
7278 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7279 /*@-compdef@*/ /* ptr is an out parameter */
7280 int nchars = _read (desc, ptr, (unsigned) left);
7283 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7292 return (int) nchars;
7306 /* Initialize PMARK to remember the current position of PFILE. */
7309 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7311 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7313 pmark->next = pbuf->marks;
7315 pbuf->marks = pmark;
7319 pmark->position = pbuf->cur - pbuf->buf;
7320 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
7323 /* Cleanup PMARK - we no longer need it. */
7325 void parseClearMark (struct parse_marker *pmark)
7327 struct parse_marker **pp = &pmark->buf->marks;
7329 for (; ; pp = &(*pp)->next)
7331 llassert (*pp != NULL);
7332 if (*pp == pmark) break;
7338 /* Backup the current position of PFILE to that saved in PMARK. */
7341 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7343 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7345 if (pbuf != pmark->buf)
7347 cpp_setLocation (pfile);
7348 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7351 llassert (pbuf->buf != NULL);
7352 pbuf->cur = pbuf->buf + pmark->position;
7353 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
7356 /* Reset PMARK to point to the current position of PFILE. (Same
7357 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7360 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7362 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7364 if (pbuf != pmark->buf)
7366 cpp_setLocation (pfile);
7367 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7370 pmark->position = pbuf->cur - pbuf->buf;
7371 DPRINTF (("move mark: %s", pmark->position));
7374 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7376 struct cppOptions *opts = CPPOPTIONS (pfile);
7379 /* The code looks at the defaults through this pointer, rather than through
7380 the constant structure above. This pointer gets changed if an environment
7381 variable specifies other defaults. */
7383 struct default_include *include_defaults = include_defaults_array;
7385 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7386 /* There seems to be confusion about what CPATH should do,
7387 so for the moment it is not documented. */
7388 /* Some people say that CPATH should replace the standard include dirs,
7389 but that seems pointless: it comes before them, so it overrides them
7392 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7394 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7396 path_include (pfile, cstring_toCharsSafe (xp));
7399 /* Now that dollars_in_ident is known, initialize is_idchar. */
7400 initialize_char_syntax (opts);
7402 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7403 and option processing. */
7405 initialize_builtins (pfile);
7407 /* Do standard #defines and assertions
7408 that identify system and machine type. */
7410 if (!opts->inhibit_predefs) {
7411 char *p = (char *) dmalloc (strlen (predefs) + 1);
7412 strcpy (p, predefs);
7418 while (*p == ' ' || *p == '\t')
7423 /* Handle -D options. */
7424 if (p[0] == '-' && p[1] == 'D')
7428 while (*p && *p != ' ' && *p != '\t')
7438 if (opts->debug_output)
7440 output_line_command (pfile, 0, same_file);
7443 cppReader_define (pfile, q);
7445 while (*p == ' ' || *p == '\t')
7459 opts->done_initializing = 1;
7461 { /* Read the appropriate environment variable and if it exists
7462 replace include_defaults with the listed path. */
7466 int win32_buf_size = 0; /* memory we need to allocate */
7469 if (opts->cplusplus)
7471 epath = getenv ("CPLUS_INCLUDE_PATH");
7475 epath = getenv ("C_INCLUDE_PATH");
7479 ** If the environment var for this language is set,
7480 ** add to the default list of include directories.
7483 if (epath != NULL) {
7484 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7486 char *startp, *endp;
7489 /* if we have a posix path list, convert to win32 path list */
7490 if (cygwin32_posix_path_list_p (epath))
7492 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7493 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7494 cygwin32_posix_to_win32_path_list (epath, win32epath);
7498 for (num_dirs = 1, startp = epath; *startp; startp++)
7500 if (*startp == PATH_SEPARATOR)
7506 = (struct default_include *) dmalloc ((num_dirs
7507 * sizeof (struct default_include))
7508 + sizeof (include_defaults_array));
7511 startp = endp = epath;
7514 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7515 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7517 strncpy (nstore, startp, size_fromInt (endp - startp));
7520 strcpy (nstore, ".");
7524 nstore[endp-startp] = '\0';
7527 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7528 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7529 include_defaults[num_dirs].cxx_aware = 1;
7536 endp = startp = endp + 1;
7543 /* Put the usual defaults back in at the end. */
7544 memcpy ((char *) &include_defaults[num_dirs],
7545 (char *) include_defaults_array,
7546 sizeof (include_defaults_array));
7549 /*@-branchstate@*/ } /*@=branchstate@*/
7552 cppReader_appendIncludeChain (pfile, opts->before_system,
7553 opts->last_before_system);
7555 opts->first_system_include = opts->before_system;
7557 /* Unless -fnostdinc,
7558 tack on the standard include file dirs to the specified list */
7560 if (!opts->no_standard_includes) {
7561 struct default_include *p = include_defaults;
7562 char *specd_prefix = opts->include_prefix;
7563 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7564 size_t default_len = 0;
7566 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7567 if (default_prefix != NULL) {
7568 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7569 default_len = strlen (default_prefix) - 7;
7570 default_prefix[default_len] = 0;
7574 /* Search "translated" versions of GNU directories.
7575 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7576 if (specd_prefix != 0 && default_len != 0)
7577 for (p = include_defaults; p->fname != NULL; p++) {
7578 /* Some standard dirs are only for C++. */
7580 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7581 /* Does this dir start with the prefix? */
7582 llassert (default_prefix != NULL);
7584 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix, default_len))
7586 /* Yes; change prefix and add to search list. */
7587 struct file_name_list *nlist
7588 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7589 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7590 char *str = (char *) dmalloc (this_len + 1);
7591 strcpy (str, specd_prefix);
7592 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7595 nlist->fname = cstring_fromChars (str);
7596 nlist->control_macro = 0;
7597 nlist->c_system_include_path = !p->cxx_aware;
7598 nlist->got_name_map = 0;
7600 if (opts->first_system_include == 0)
7602 opts->first_system_include = nlist;
7605 cppReader_addIncludeChain (pfile, nlist);
7610 /* Search ordinary names for GNU include directories. */
7612 for (p = include_defaults; p->fname != NULL; p++)
7614 /* Some standard dirs are only for C++. */
7616 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7618 struct file_name_list *nlist
7619 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7620 nlist->control_macro = 0;
7621 nlist->c_system_include_path = !p->cxx_aware;
7622 nlist->fname = p->fname;
7623 nlist->got_name_map = 0;
7626 /* Spurious warning reported for opts->first_system_include */
7627 /*@-usereleased@*/ if (opts->first_system_include == NULL)
7629 opts->first_system_include = nlist;
7633 cppReader_addIncludeChain (pfile, nlist);
7636 sfree (default_prefix);
7639 /* Tack the after_include chain at the end of the include chain. */
7640 cppReader_appendIncludeChain (pfile, opts->after_include,
7641 opts->last_after_include);
7643 /* Spurious warnings for opts->first_system_include */
7645 if (opts->first_system_include == NULL)
7647 opts->first_system_include = opts->after_include;
7651 /* With -v, print the list of dirs to search. */
7652 if (opts->verbose) {
7653 struct file_name_list *p;
7654 fprintf (stderr, "#include \"...\" search starts here:\n");
7656 for (p = opts->include; p != NULL; p = p->next) {
7657 if (p == opts->first_bracket_include)
7658 fprintf (stderr, "#include <...> search starts here:\n");
7660 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7662 fprintf (stderr, "End of search list.\n");
7666 int cppReader_startProcess (cppReader *pfile, cstring fname)
7670 struct cppOptions *opts = CPPOPTIONS (pfile);
7672 fp = cppReader_pushBuffer (pfile, NULL, 0);
7679 if (opts->in_fname == NULL)
7681 opts->in_fname = cstring_makeLiteralTemp ("");
7684 fp->fname = opts->in_fname;
7685 fp->nominal_fname = fp->fname;
7688 /* Copy the entire contents of the main input file into
7689 the stacked input buffer previously allocated for it. */
7691 if (cstring_isEmpty (fname))
7693 fname = cstring_makeLiteralTemp ("");
7696 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7698 cppReader_error (pfile,
7699 message ("Error opening %s for reading: %s",
7700 fname, lldecodeerror (errno)));
7709 if (finclude (pfile, f, fname, 0, NULL))
7711 output_line_command (pfile, 0, same_file);
7717 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7719 return pfile->buffer;
7722 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7724 llassert (pfile->buffer != NULL);
7725 return pfile->buffer;
7728 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7730 llassert (buf->buf != NULL);
7731 return (buf->buf + buf->line_base);
7734 int cpplib_bufPeek (cppBuffer *buf)
7736 if (buf->cur == NULL || buf->rlimit == NULL) {
7740 if (buf->cur < buf->rlimit) {
7747 bool cppBuffer_isMacro (cppBuffer *buf)
7751 return (buf->cleanup == cppReader_macroCleanup);
7758 ** Returns true if the macro should be checked, false
7759 ** if it should be expanded normally.
7762 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7763 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7764 static bool expectiter = FALSE; /* preceeded by @iter@ */
7765 static bool expectenditer = FALSE; /* second after @iter@ */
7766 static bool expectfunction = FALSE; /* preceeded by @function@ */
7767 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7768 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7770 static void cpp_setLocation (cppReader *pfile)
7775 if (pfile->buffer != NULL)
7777 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7779 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7781 DPRINTF (("Looking up: %s", fname));
7783 if (fileTable_exists (context_fileTable (), fname))
7785 fid = fileTable_lookup (context_fileTable (), fname);
7789 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7791 fid = fileTable_lookup (context_fileTable (),
7792 cppReader_getBufferSafe (pfile)->fname);
7797 fid = fileTable_lookup (context_fileTable (),
7798 cppReader_getBufferSafe (pfile)->fname);
7801 line = cppReader_getBufferSafe (pfile)->lineno;
7802 fileloc_free (g_currentloc);
7804 if (fileId_isValid (fid))
7806 g_currentloc = fileloc_create (fid, line, 1);
7810 g_currentloc = fileloc_createBuiltin ();
7815 fileloc_free (g_currentloc);
7816 g_currentloc = fileloc_createBuiltin ();
7820 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7822 bool checkmacro = FALSE;
7823 bool hasParams = FALSE;
7824 bool noexpand = FALSE;
7828 cpp_setLocation (pfile);
7830 DPRINTF (("Should check macro? %s", p));
7832 if (expectiter || expectconstant || expectenditer)
7837 expectenditer = TRUE;
7842 expectconstant = FALSE;
7843 expectenditer = FALSE;
7846 if (notfunction || notparseable)
7848 notfunction = FALSE;
7849 notparseable = FALSE;
7858 llassert (*p == '#');
7861 while (*p == ' ' || *p == '\t')
7866 llassert (*p == 'd'); /* define starts */
7870 while (*p == ' ' || *p == '\t')
7875 sname = cstring_fromChars (p);
7876 DPRINTF (("Check macro: %s", sname));
7878 while (((c = *p) != ' ')
7879 && c != '\0' && c != '('
7880 && c != '\t' && c != '\\' && c != '\n'
7886 hasParams = (c == '(');
7891 notparseable = FALSE;
7893 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7895 DPRINTF (("Clear notfunction"));
7896 notfunction = FALSE;
7911 if (usymtab_existsReal (sname))
7913 uentry ue = usymtab_lookup (sname);
7915 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7917 if (fileloc_isPreproc (uentry_whereLast (ue)))
7923 if (uentry_isSpecified (ue))
7925 checkmacro = context_getFlag (FLG_SPECMACROS);
7931 checkmacro = context_getFlag (FLG_LIBMACROS)
7932 || context_getFlag (FLG_FCNMACROS);
7940 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7942 if (fileloc_isSystemFile (g_currentloc)
7943 && context_getFlag (FLG_SYSTEMDIREXPAND))
7945 ; /* don't check this macro */
7946 DPRINTF (("Don't check 1"));
7954 DPRINTF (("Has params..."));
7956 if (context_getFlag (FLG_FCNMACROS))
7958 if (usymtab_exists (sname))
7961 ** only get here is macro is redefined
7962 ** error reported elsewhere
7965 DPRINTF (("It exists!"));
7970 ** We make it a forward function, since it might be declared elsewhere.
7971 ** After all headers have been processed, we should check the forward
7975 fileloc loc = fileloc_makePreproc (g_currentloc);
7977 /* the line is off-by-one, since the newline was already read */
7982 expectfunction = FALSE;
7985 le = uentry_makeForwardFunction (sname,
7986 typeId_invalid, loc);
7992 /* Do not define here! */
7994 (void) usymtab_addEntry (le);
7998 DPRINTF (("Check: TRUE"));
8002 DPRINTF (("Flag FCN_MACROS not set!"));
8007 DPRINTF (("No params"));
8009 if (context_getFlag (FLG_CONSTMACROS))
8011 bool nocontent = FALSE;
8024 ** Check if there is nothing after the define.
8027 while ((*rest) != '\0' && isspace (*rest))
8034 nocontent = TRUE; /* empty macro, don't check */
8039 if (usymtab_exists (sname))
8045 fileloc loc = fileloc_makePreproc (g_currentloc);
8046 DPRINTF (("Make constant: %s", sname));
8047 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
8048 (void) usymtab_addEntry (le);
8051 checkmacro = !nocontent;
8056 if (checkmacro && usymtab_existsType (sname))
8058 DPRINTF (("Making false..."));
8060 ppllerror (message ("Specified type implemented as macro: %s", sname));
8070 if (usymtab_exists (sname))
8072 uentry ue = usymtab_lookupExpose (sname);
8073 fileloc tloc = fileloc_makePreproc (g_currentloc);
8075 uentry_setDefined (ue, tloc);
8076 fileloc_free (tloc);
8077 uentry_setUsed (ue, fileloc_undefined);
8081 fileloc tloc = fileloc_makePreproc (g_currentloc);
8082 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8083 DPRINTF (("Make expanded macro: %s", sname));
8084 DPRINTF (("Not in symbol table: %s", sname));
8086 (void) usymtab_addGlobalEntry (ue);
8087 fileloc_free (tloc);
8092 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8096 static enum cpp_token
8097 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8099 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8103 bool eliminateComment = FALSE;
8105 llassert (pbuf->buf != NULL);
8107 start = pbuf->buf + smark->position;
8109 llassert (pbuf->cur != NULL);
8110 len = pbuf->cur - start;
8113 && start[1] == context_getCommentMarkerChar ())
8117 char *scomment = start + 2;
8118 char savec = start[len];
8120 cpp_setLocation (pfile);
8121 loc = fileloc_copy (g_currentloc);
8123 start[0] = BEFORE_COMMENT_MARKER[0];
8124 start[1] = BEFORE_COMMENT_MARKER[1];
8126 llassert (start[len - 2] == '*');
8127 start[len - 2] = AFTER_COMMENT_MARKER[0];
8129 llassert (start[len - 1] == '/');
8130 start[len - 1] = AFTER_COMMENT_MARKER[1];
8132 cpplib_reserve(pfile, size_fromInt (1 + len));
8133 cppReader_putCharQ (pfile, c);
8135 cpp_setLocation (pfile);
8139 if (mstring_containsString (scomment, "/*"))
8141 (void) cppoptgenerror
8143 message ("Comment starts inside syntactic comment: %s",
8144 cstring_fromChars (scomment)),
8150 if (mstring_equalPrefix (scomment, "ignore"))
8152 if (!context_getFlag (FLG_NOCOMMENTS))
8154 context_enterSuppressRegion (loc);
8157 else if (mstring_equalPrefix (scomment, "end"))
8159 if (!context_getFlag (FLG_NOCOMMENTS))
8161 context_exitSuppressRegion (loc);
8164 else if (mstring_equalPrefix (scomment, "notparseable"))
8166 notparseable = TRUE;
8168 eliminateComment = TRUE;
8170 else if (mstring_equalPrefix (scomment, "notfunction"))
8174 eliminateComment = TRUE;
8176 else if (mstring_equalPrefix (scomment, "iter"))
8180 else if (mstring_equalPrefix (scomment, "function"))
8182 expectfunction = TRUE;
8184 else if (mstring_equalPrefix (scomment, "constant"))
8186 expectconstant = TRUE;
8190 char sChar = *scomment;
8196 char *rest = scomment + 1;
8198 if (mstring_equalPrefix (rest, "commentchar"))
8200 eliminateComment = TRUE;
8204 ppllerror (cstring_makeLiteral
8205 ("Cannot restore commentchar"));
8209 char *next = scomment + 12; /* strlen commentchar = 12 */
8211 if (*next != ' ' && *next != '\t' && *next != '\n')
8215 ("Syntactic commentchar comment is not followed by a "
8216 "whitespace character: %c",
8221 char cchar = *(next + 1);
8226 (cstring_makeLiteral
8227 ("Cannot set commentchar to NUL"));
8231 context_setCommentMarkerChar (cchar);
8232 /* setComment = TRUE; */
8237 else if (mstring_equalPrefix (scomment, "nestcomment"))
8239 /* fix from Mike Miller <MikeM@xata.com> */
8240 context_fileSetFlag (FLG_NESTCOMMENT,
8241 ynm_fromCodeChar (sChar),
8244 else if (mstring_equalPrefix (rest, "namechecks"))
8246 context_fileSetFlag (FLG_NAMECHECKS,
8247 ynm_fromCodeChar (sChar),
8250 else if (mstring_equalPrefix (rest, "macroredef"))
8252 context_fileSetFlag (FLG_MACROREDEF,
8253 ynm_fromCodeChar (sChar),
8256 else if (mstring_equalPrefix (rest, "usevarargs"))
8258 context_fileSetFlag (FLG_USEVARARGS,
8259 ynm_fromCodeChar (sChar),
8262 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8264 context_fileSetFlag (FLG_MACRONEXTLINE,
8265 ynm_fromCodeChar (sChar),
8268 else if (mstring_equalPrefix (rest, "allmacros")
8269 || mstring_equalPrefix (rest, "fcnmacros")
8270 || mstring_equalPrefix (rest, "constmacros"))
8274 if (mstring_equalPrefix (rest, "allmacros"))
8278 else if (mstring_equalPrefix (rest, "fcnmacros"))
8284 llassert (mstring_equalPrefix (rest, "constmacros"));
8285 fl = FLG_CONSTMACROS;
8288 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8289 notfunction = FALSE;
8302 if (eliminateComment)
8307 /* Replaces comment char's in start with spaces */
8309 for (i = 2; i < len - 2; i++)
8311 if (start[i] == BEFORE_COMMENT_MARKER[0]
8312 || start[i] == BEFORE_COMMENT_MARKER[1]
8313 || start[i] == context_getCommentMarkerChar ())
8319 cppReader_putStrN (pfile, start, size_fromInt (len));
8320 parseClearMark (smark);
8330 ** Output the comment as all spaces so line/column
8331 ** in output file is still correct.
8335 cstring lintcomment = cstring_undefined;
8337 if (context_getFlag (FLG_LINTCOMMENTS))
8339 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8341 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8343 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8345 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8347 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8349 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8351 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8353 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8355 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8357 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8361 lintcomment = cstring_undefined;
8366 lintcomment = cstring_undefined;
8369 if (cstring_isDefined (lintcomment))
8371 c = BEFORE_COMMENT_MARKER[0];
8372 start[0] = BEFORE_COMMENT_MARKER[1];
8374 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8376 for (i = 1; i < len - 2; i++)
8378 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8381 start[len - 2] = AFTER_COMMENT_MARKER[0];
8382 start[len - 1] = AFTER_COMMENT_MARKER[1];
8386 /* Replaces char's in start with spaces */
8387 for (i = 0; i < len; i++)
8391 && start[i + 1] == '*') {
8392 (void) cppoptgenerror
8394 message ("Comment starts inside comment"),
8398 if (start[i] != '\n')
8405 cpplib_reserve (pfile, size_fromInt (1 + len));
8406 cppReader_putCharQ (pfile, c);
8407 cppReader_putStrN (pfile, start, size_fromInt (len));
8408 parseClearMark (smark);
8414 static int cpp_openIncludeFile (char *filename)
8416 int res = open (filename, O_RDONLY, 0666);
8418 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8421 if (!fileTable_exists (context_fileTable (),
8422 cstring_fromChars (filename)))
8424 if (fileloc_isXHFile (g_currentloc))
8427 ** Files includes by XH files are also XH files
8430 (void) fileTable_addXHFile (context_fileTable (),
8431 cstring_fromChars (filename));
8435 (void) fileTable_addHeaderFile (context_fileTable (),
8436 cstring_fromChars (filename));
8441 DPRINTF (("File already exists: %s", filename));
8448 static bool cpp_skipIncludeFile (cstring fname)
8450 if (context_isSystemDir (fname))
8452 DPRINTF (("System dir: %s", fname));
8454 if (lcllib_isSkipHeader (fname))
8456 DPRINTF (("Skip include TRUE: %s", fname));
8460 if (context_getFlag (FLG_SKIPSYSHEADERS))
8463 ** 2003-04-18: Patch from Randal Parsons
8467 ** Don't skip include file unless the file actually exists.
8468 ** It may be in a different directory.
8471 int f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666);
8475 check (close (f) == 0);
8476 DPRINTF (("Skip include TRUE: %s", fname));
8481 /* Keep looking... */
8486 if (context_getFlag (FLG_SINGLEINCLUDE))
8488 fname = removePreDirs (fname);
8490 # if defined (WIN32) || defined (OS2)
8491 cstring_replaceAll (fname, '\\', '/');
8494 if (fileTable_exists (context_fileTable (), fname))
8496 DPRINTF (("Skip include TRUE: %s", fname));
8501 DPRINTF (("Skip include FALSE: %s", fname));
8505 static int cpp_peekN (cppReader *pfile, int n)
8507 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8509 llassert (buf->cur != NULL);
8511 return (buf->rlimit - buf->cur >= (n)
8516 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8521 void cppBuffer_forward (cppBuffer *buf, int n)
8523 llassert (buf->cur != NULL);
8527 /*@=bufferoverflowhigh@*/