2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
29 Contributed by Per Bothner, 1994-95.
30 Based on CCCP program by Paul Rubin, June 1986
31 Adapted to ANSI C, Richard Stallman, Jan 1987
33 This program is free software; you can redistribute it and/or modify it
34 under the terms of the GNU General Public License as published by the
35 Free Software Foundation; either version 2, or (at your option) any
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 In other words, you are welcome to use, share and improve this program.
48 You are forbidden to forbid anyone else to use, share and improve
49 what you give them. Help stamp out software-hoarding! */
53 * - OS2 drive specs like WIN32
54 * - Includes for IBMs OS/2 compiler
65 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
68 # include <sys/types.h>
69 # include <sys/stat.h>
71 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
73 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
78 ** evans 2002-07-03: exception for WATCOM 10.6 compiler suggest by Adam Clarke
80 # if !defined (USG) && !defined (__WATCOMC__)
81 # include <time.h> /* Reported by Paul Smith */
82 # include <sys/time.h>
83 # include <sys/resource.h>
85 # include <sys/times.h>
90 # endif /* not WIN32 */
92 /* This defines "errno" properly for VMS, and gives us EACCES. */
95 # include "splintMacros.nf"
99 # include "cpperror.h"
100 # include "cpphash.h"
102 # include "version.h"
107 ** This is really kludgey code...
113 /* Warnings for using sprintf - suppress them all for now... */
114 /*@-bufferoverflowhigh@*/
116 #define NO_SHORTNAMES
124 /*@constant int IMPORT_FOUND@*/
125 # define IMPORT_FOUND -2
127 /*@constant int SKIP_INCLUDE@*/
128 # define SKIP_INCLUDE IMPORT_FOUND
130 /*@constant unused int IMPORT_NOT_FOUND@*/
131 # define IMPORT_NOT_FOUND -1
134 /*@constant unused int STDC_VALUE@*/
138 /* By default, colon separates directories in a path. */
139 #ifndef PATH_SEPARATOR
140 /*@constant char PATH_SEPARATOR@*/
141 #define PATH_SEPARATOR ':'
144 static void parse_name (cppReader *, int);
146 static int cpp_openIncludeFile (char *p_filename)
147 /*@modifies fileSystem @*/ ;
149 static void cpp_setLocation (cppReader *p_pfile)
150 /*@modifies g_currentloc@*/ ;
152 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
153 struct parse_marker *p_smark)
154 /*@modifies p_pfile, p_smark@*/;
156 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
158 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
160 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
166 /* Symbols to predefine. */
168 #ifdef CPP_PREDEFINES
169 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
171 static /*@observer@*/ char *predefs = "";
174 /* We let tm.h override the types used here, to handle trivial differences
175 such as the choice of unsigned int or long unsigned int for size_t.
176 When machines start needing nontrivial differences in the size type,
177 it would be best to do something here to figure out automatically
178 from other information what type to use. */
180 /* The string value for __SIZE_TYPE__. */
183 /*@constant observer char *SIZE_TYPE@*/
184 #define SIZE_TYPE "long unsigned int"
187 /* The string value for __PTRDIFF_TYPE__. */
190 /*@constant observer char *PTRDIFF_TYPE@*/
191 #define PTRDIFF_TYPE "long int"
194 /* The string value for __WCHAR_TYPE__. */
197 /*@constant observer char *WCHAR_TYPE@*/
198 #define WCHAR_TYPE "int"
201 /* The string value for __USER_LABEL_PREFIX__ */
203 #ifndef USER_LABEL_PREFIX
204 /*@constant observer char *USER_LABEL_PREFIX@*/
205 #define USER_LABEL_PREFIX ""
208 /* The string value for __REGISTER_PREFIX__ */
210 #ifndef REGISTER_PREFIX
211 /*@constant observer char *REGISTER_PREFIX@*/
212 #define REGISTER_PREFIX ""
215 /* table to tell if char can be part of a C identifier. */
216 static bool is_idchar[256];
217 /* table to tell if char can be first char of a c identifier. */
218 static bool is_idstart[256];
219 /* table to tell if c is horizontal space. */
220 static bool is_hor_space[256];
221 /* table to tell if c is horizontal or vertical space. */
222 static bool is_space[256];
224 static /*@exposed@*/ /*@null@*/ cppBuffer *
225 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
226 /*@uses p_pfile->buffer@*/
227 /*@modifies nothing@*/ ;
230 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
233 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
235 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
237 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
238 # define cppBuffer_get(BUFFER) \
239 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
241 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
242 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
243 modifies *p_file; @*/
244 # define cppReader_puts(PFILE, STR, N) \
245 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
247 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
249 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
250 modifies *p_file; @*/
251 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
253 static void cppReader_putCharQ (cppReader *p_file, char p_ch)
255 fprintf (stderr, "put char: %c\n", p_ch);
256 (*(p_file)->limit++ = (p_ch));
259 /* Append character CH to PFILE's output buffer. Make space if need be. */
261 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
262 modifies *p_file; @*/
263 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
265 /* Make sure PFILE->limit is followed by '\0'. */
266 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
267 modifies *p_file; @*/
269 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
271 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
272 modifies *p_file; @*/
273 # define cppReader_nullTerminate(PFILE) \
274 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
276 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
277 modifies *p_file; @*/
278 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
280 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
281 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
283 /*@function static observer char *cppReader_wcharType (cppReader *)
284 modifies nothing; @*/
286 # define cppReader_wcharType(PFILE) \
287 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
289 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
291 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
292 # define cppReader_forward(pfile, N) \
293 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
295 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
296 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
298 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
299 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
301 /* Move all backslash-newline pairs out of embarrassing places.
302 Exchange all such pairs following BP
303 with any potentially-embarrassing characters that follow them.
304 Potentially-embarrassing characters are / and *
305 (because a backslash-newline inside a comment delimiter
306 would cause it not to be recognized). */
309 # define NEWLINE_FIX \
310 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
312 /* Same, but assume we've already read the potential '\\' into C. */
314 # define NEWLINE_FIX1(C) do { \
315 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
318 static void parseSetMark (/*@out@*/ struct parse_marker *,
320 static void parseClearMark (struct parse_marker *);
321 static void parseGotoMark (struct parse_marker *, cppReader *);
322 static void parseMoveMark (struct parse_marker *, cppReader *);
324 /* If we have a huge buffer, may need to cache more recent counts */
325 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
327 static /*@exposed@*/ /*@null@*/ cppBuffer *
328 cppReader_pushBuffer (cppReader *p_pfile,
329 /*@owned@*/ /*@null@*/ char *, size_t)
330 /*@modifies p_pfile@*/ ;
332 static void cppReader_appendIncludeChain
334 /*@keep@*/ struct file_name_list *p_first,
335 /*@dependent@*/ struct file_name_list *p_last);
337 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
338 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
340 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
341 /*@unused@*/ cppReader *p_pfile);
343 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
344 /*@unused@*/ cppReader *p_pfile);
346 static int cppReader_handleDirective (cppReader *p_pfile);
348 static void cppReader_scanBuffer (cppReader *p_pfile);
350 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
353 ** WIN32 (at least the VC++ include files) does not define mode_t.
356 /*@-incondefs@*/ /*@-czechtypes@*/
357 typedef unsigned int mode_t;
358 /*@=incondefs@*/ /*@=czechtypes@*/
362 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
363 /*@out@*/ size_t *p_size_pointer);
364 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
368 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
369 ** (Note that it is false while we're expanding marco *arguments*.)
372 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
374 static void path_include (cppReader *p_pfile, char *p_path)
375 /*@modifies p_pfile@*/ ;
377 static void initialize_builtins (cppReader *p_pfile)
378 /*@modifies p_pfile@*/ ;
380 static void initialize_char_syntax (struct cppOptions *p_opts) ;
382 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
384 bool p_system_header_p,
385 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
387 static void validate_else (cppReader *p_pfile, cstring p_directive);
389 static void conditional_skip (cppReader *p_pfile, int p_skip,
390 enum node_type p_type,
391 /*@dependent@*/ /*@null@*/ char *p_control_macro);
393 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
397 static void skip_if_group (cppReader *p_pfile, int p_any);
399 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
400 char *p_beg2, int p_len2, bool p_last);
403 extern void fancy_abort ();
406 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
407 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
409 static /*@observer@*/ /*@null@*/ struct file_name_map *
410 read_name_map (cppReader *p_pfile, cstring p_dirname);
412 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
414 static int open_include_file (cppReader *p_pfile,
415 /*@owned@*/ cstring p_fname,
416 /*@null@*/ struct file_name_list *p_searchptr);
418 static void push_macro_expansion (cppReader *,
419 /*@owned@*/ char *, size_t,
420 /*@dependent@*/ hashNode);
422 /* Last arg to output_line_command. */
423 enum file_change_code {
424 same_file, enter_file, leave_file
427 /* `struct directive' defines one #-directive, including how to handle it. */
430 int length; /* Length of name */
431 /*@null@*/ int (*func)(); /* Function to handle directive */
432 /*@observer@*/ cstring name; /* Name of directive */
433 enum node_type type; /* Code which describes which directive. */
434 bool command_reads_line; /* One if rest of line is read by func. */
435 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
436 bool pass_thru; /* Copy preprocessed directive to output file.*/
439 /* These functions are declared to return int instead of void since they
440 are going to be placed in a table and some old compilers have trouble with
441 pointers to functions returning void. */
443 static int do_define (cppReader *, /*@null@*/ struct directive *,
444 /*@exposed@*/ char *, char *);
445 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
446 /*@exposed@*/ char *, char *, bool);
448 static int do_line (cppReader *, /*@null@*/ struct directive *);
449 static int do_include (cppReader *, struct directive *, char *, char *);
450 static int do_undef (cppReader *, struct directive *, char *, char *);
451 static int do_error (cppReader *, struct directive *, char *, char *);
452 static int do_pragma (cppReader *, struct directive *, char *, char *);
453 static int do_ident (cppReader *, struct directive *, char *, char *);
454 static int do_if (cppReader *, struct directive *, char *, char *);
455 static int do_xifdef (cppReader *, struct directive *, char *, char *);
456 static int do_else (cppReader *, struct directive *, char *, char *);
457 static int do_elif (cppReader *, struct directive *, char *, char *);
458 static int do_endif (cppReader *, struct directive *, char *, char *);
459 static int do_warning (cppReader *, struct directive *, char *, char *);
461 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
462 via the same directory as the file that #included it. */
464 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
465 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
467 /* #include "file" looks in source file dir, then stack. */
468 /* #include <file> just looks in the stack. */
469 /* -I directories are added to the end, then the defaults are added. */
473 static struct default_include {
474 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
475 int cplusplus; /* Only look here if we're compiling C++. */
476 int cxx_aware; /* Includes in this directory don't need to
477 be wrapped in extern "C" when compiling
479 } include_defaults_array[]
481 /* This is the dir for fixincludes. Put it just before
482 the files that we fix. */
483 { GCC_INCLUDE_DIR, 0, 0 },
484 { GCC_INCLUDE_DIR2, 0, 0 },
485 { cstring_undefined, 0, 0 }
488 /*@noaccess cstring@*/
490 /* Here is the actual list of #-directives, most-often-used first.
491 The initialize_builtins function assumes #define is the very first. */
495 static struct directive directive_table[] = {
496 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
497 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
498 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
499 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
500 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
501 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
502 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
503 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
504 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
505 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
506 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
507 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
508 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
509 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
510 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
511 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
513 /*@noaccess cstring@*/
515 static cstring searchPath_unparse (struct file_name_list *search_start)
517 cstring res = cstring_newEmpty ();
518 struct file_name_list *searchptr = NULL;
520 for (searchptr = search_start; searchptr != NULL;
521 searchptr = searchptr->next)
523 if (!cstring_isEmpty (searchptr->fname)) {
524 res = cstring_concatFree1 (res, searchptr->fname);
525 if (searchptr->next != NULL) {
526 res = cstring_appendChar (res, ';');
536 initialize_char_syntax (struct cppOptions *opts)
541 * Set up is_idchar and is_idstart tables. These should be
542 * faster than saying (is_alpha (c) || c == '_'), etc.
543 * Set up these things before calling any routines tthat
547 for (i = 'a'; i <= 'z'; i++) {
548 is_idchar[i - 'a' + 'A'] = TRUE;
549 is_idchar[(int) i] = TRUE;
550 is_idstart[i - 'a' + 'A'] = TRUE;
551 is_idstart[(int) i] = TRUE;
554 for (i = '0'; i <= '9'; i++)
556 is_idchar[(int) i] = TRUE;
559 is_idchar['_'] = TRUE;
560 is_idstart['_'] = TRUE;
561 is_idchar['$'] = opts->dollars_in_ident;
562 is_idstart['$'] = opts->dollars_in_ident;
564 /* horizontal space table */
565 is_hor_space[' '] = TRUE;
566 is_hor_space['\t'] = TRUE;
567 is_hor_space['\v'] = TRUE;
568 is_hor_space['\f'] = TRUE;
569 is_hor_space['\r'] = TRUE;
571 is_space[' '] = TRUE;
572 is_space['\t'] = TRUE;
573 is_space['\v'] = TRUE;
574 is_space['\f'] = TRUE;
575 is_space['\n'] = TRUE;
576 is_space['\r'] = TRUE;
579 bool isIdentifierChar (char c)
581 return is_idchar[(int) c];
584 /* Place into P_PFILE a quoted string representing the string SRC.
585 Caller must reserve enough space in pfile->token_buffer. */
588 quote_string (cppReader *pfile, char *src)
592 cppReader_putCharQ (pfile, '\"');
595 switch ((c = *src++))
599 cppReader_putCharQ (pfile, c);
602 sprintf (cpplib_getPWritten (pfile), "\\%03o",
604 cppReader_adjustWritten (pfile, (size_t) 4);
606 /*@switchbreak@*/ break;
610 cppReader_putCharQ (pfile, '\\');
611 cppReader_putCharQ (pfile, c);
612 /*@switchbreak@*/ break;
615 cppReader_putCharQ (pfile, '\"');
616 cppReader_nullTerminateQ (pfile);
622 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
625 cppReader_growBuffer (cppReader *pfile, size_t n)
627 size_t old_written = cpplib_getWritten (pfile);
628 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
629 pfile->token_buffer = (char *)
630 drealloc (pfile->token_buffer, pfile->token_buffer_size);
631 cppReader_setWritten (pfile, old_written);
635 * process a given definition string, for initialization
636 * If STR is just an identifier, define it with value 1.
637 * If STR has anything after the identifier, then it should
638 * be identifier=definition.
642 cppReader_define (cppReader *pfile, char *str)
647 DPRINTF (("Cpp reader define: %s", str));
649 if (!is_idstart[(int) *p])
651 DPRINTF (("ERROR 1"));
652 cppReader_error (pfile,
653 message ("Malformed option `-D%s'",
654 cstring_fromChars (str)));
661 DPRINTF (("Here 2"));
663 while (is_idchar[(int) *p])
670 while (*p != ')' && *p != '\0') {
679 message ("Malformed option: -D%s (no closing parenthesis)",
680 cstring_fromChars (str)));
684 DPRINTF (("Here 2"));
688 buf = (char *) dmalloc (size_fromInt (p - str + 4));
689 strcpy ((char *) buf, str);
690 strcat ((char *) buf, " 1");
694 DPRINTF (("ERROR 2"));
695 cppReader_error (pfile,
696 message ("Malformed option: -D%s (expected '=', found '%c')",
697 cstring_fromChars (str),
704 /* Copy the entire option so we can modify it. */
705 DPRINTF (("Copying..."));
706 buf = (char *) dmalloc (2 * strlen (str) + 1);
707 strncpy (buf, str, size_fromInt (p - str));
709 /* Change the = to a space. */
711 /* Scan for any backslash-newline and remove it. */
717 if (*p == '\\' && p[1] == '\n')
723 DPRINTF (("Here we are..."));
727 llassert (buf != NULL);
728 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
729 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
733 /* Append a chain of `struct file_name_list's
734 to the end of the main include chain.
735 FIRST is gthe beginning of the chain to append, and LAST is the end. */
738 cppReader_appendIncludeChain (cppReader *pfile,
739 struct file_name_list *first,
740 struct file_name_list *last)
742 struct cppOptions *opts = CPPOPTIONS (pfile);
743 struct file_name_list *dir;
745 if (first == NULL || last == NULL)
750 if (opts->include == 0)
752 opts->include = first;
756 llassert (opts->last_include->next == NULL);
757 opts->last_include->next = first;
760 if (opts->first_bracket_include == 0)
762 opts->first_bracket_include = first;
764 for (dir = first; ; dir = dir->next) {
765 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
766 if (len > pfile->max_include_len)
767 pfile->max_include_len = len;
773 llassert (last->next == NULL);
774 /* last->next = NULL; */
775 opts->last_include = last;
779 static /*@unused@*/ void
780 cppReader_showIncludeChain (cppReader *pfile)
782 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
788 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
792 fprintf (stderr, "\n");
796 fprintf (stderr, "No includes\n");
802 cppReader_getIncludePath ()
804 cppReader *pfile = &g_cppState;
805 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
806 cstring res = cstring_undefined;
812 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
818 res = cstring_makeLiteral ("<no include path>");
825 cppReader_addIncludeChain (cppReader *pfile, /*@only@*/ struct file_name_list *dir)
827 struct cppOptions *opts = CPPOPTIONS (pfile);
834 if (opts->include == 0)
840 llassert (opts->last_include->next == NULL);
841 opts->last_include->next = dir;
844 if (opts->first_bracket_include == 0)
846 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
847 opts->first_bracket_include = dir;
848 if (len > pfile->max_include_len)
850 pfile->max_include_len = len;
855 opts->last_include = dir;
856 /* cppReader_showIncludeChain (pfile); */
859 /* Given a colon-separated list of file names PATH,
860 add all the names to the search path for include files. */
863 path_include (cppReader *pfile, char *path)
870 /* if we have a posix path list, convert to win32 path list */
871 win32temp = (char *) dmalloc /*@i4@*/
872 (cygwin32_posix_to_win32_path_list_buf_size (path));
873 cygwin32_posix_to_win32_path_list (path, win32temp);
883 struct file_name_list *dirtmp;
885 /* Find the end of this name. */
886 while (*q != '\0' && *q != PATH_SEPARATOR)
893 /* An empty name in the path stands for the current directory. */
894 name = (char *) dmalloc ((size_t) 2);
900 /* Otherwise use the directory that is named. */
901 name = (char *) dmalloc (size_fromInt (q - p + 1));
902 memcpy (name, p, size_fromInt (q - p));
906 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
907 dirtmp->next = 0; /* New one goes on the end */
908 dirtmp->control_macro = 0;
909 dirtmp->c_system_include_path = 0;
910 dirtmp->fname = cstring_fromChars (name);
911 dirtmp->got_name_map = 0;
912 cppReader_addIncludeChain (pfile, dirtmp);
914 /* Advance past this name. */
918 /* Skip the colon. */
924 cppOptions_init (cppOptions *opts)
926 memset ((char *) opts, 0, sizeof *opts);
929 opts->in_fname = NULL;
930 opts->out_fname = NULL;
932 /* Initialize is_idchar to allow $. */
933 opts->dollars_in_ident = TRUE;
935 opts->no_line_commands = 0;
936 opts->no_trigraphs = TRUE;
937 opts->put_out_comments = 1;
938 opts->print_include_names = 0;
939 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
943 opts->cplusplus_comments = 1;
948 opts->pedantic_errors = 0;
949 opts->warn_comments = 0;
950 opts->warnings_are_errors = 0;
952 initialize_char_syntax (opts);
956 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
962 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
963 /*@unused@*/ cppReader *pfile)
969 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
971 hashNode macro = pbuf->hnode;
973 if (macro->type == T_DISABLED)
975 macro->type = T_MACRO;
978 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
986 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
988 if (pbuf->buf != NULL)
995 /* Assuming we have read '/'.
996 If this is the start of a comment (followed by '*' or '/'),
997 skip to the end of the comment, and return ' '.
998 Return EOF if we reached the end of file before the end of the comment.
999 If not the start of a comment, return '/'. */
1002 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1006 llassert (pfile->buffer != NULL);
1007 llassert (pfile->buffer->cur != NULL);
1009 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1016 cppReader_forward (pfile, 2);
1019 if (cppReader_peekC (pfile) == '*')
1021 cppReader_forward (pfile, 1);
1026 c = cppReader_getC (pfile);
1033 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1040 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1043 if (prev_c == (int) '*' && c == (int) '/')
1048 if (c == (int) '\n' && (linep != NULL))
1054 else if (cppReader_peekC (pfile) == '/'
1055 && CPPOPTIONS (pfile)->cplusplus_comments)
1058 (void) cppoptgenerror
1059 (FLG_SLASHSLASHCOMMENT,
1060 message ("C++ style // comment"
1064 cppReader_forward (pfile, 1);
1068 c = cppReader_getC (pfile);
1072 /* Allow hash comment to be terminated by EOF. */
1076 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1078 cppReader_forward (pfile, 1);
1079 c = cppReader_getC (pfile);
1087 if (c == (int) '\n')
1089 /* Don't consider final '\n' to be part of comment. */
1090 cppReader_forward (pfile, -1);
1101 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1103 cppSkipHspace (cppReader *pfile)
1111 llassert (pfile->buffer != NULL);
1113 c = cppReader_peekC (pfile);
1117 return 0; /* FIXME */
1120 if (is_hor_space[c])
1122 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1123 cppReader_pedwarn (pfile,
1124 message ("%s in preprocessing directive",
1126 ? cstring_makeLiteralTemp ("formfeed")
1127 : cstring_makeLiteralTemp ("vertical tab")));
1130 cppReader_forward (pfile, 1);
1134 cppReader_forward (pfile, 1);
1135 c = skip_comment (pfile, NULL);
1139 cppReader_forward (pfile, -1);
1142 if (c == EOF || c == '/')
1147 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1149 cppReader_forward (pfile, 2);
1151 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1152 && is_hor_space [cpp_peekN (pfile, 1)])
1154 cppReader_forward (pfile, 2);
1163 /* Read the rest of the current line.
1164 The line is appended to PFILE's output buffer. */
1167 copy_rest_of_line (cppReader *pfile)
1169 struct cppOptions *opts = CPPOPTIONS (pfile);
1176 llassert (pfile->buffer != NULL);
1178 c = cppReader_getC (pfile);
1186 ** Patch from Brian St. Pierre for handling MS-DOS files.
1189 DPRINTF (("Reading directive: %d", (int) c));
1191 if (cppReader_peekC (pfile) == '\n'
1192 || cppReader_peekC (pfile) == '\r')
1194 DPRINTF (("Reading directive..."));
1195 if (cppReader_peekC (pfile) == '\r')
1197 DPRINTF (("Reading directive..."));
1198 cppReader_forward (pfile, 1);
1201 DPRINTF (("Reading directive..."));
1202 cppReader_forward (pfile, 1);
1206 DPRINTF (("Falling..."));
1207 /*@fallthrough@*/ case '\'': case '\"':
1208 goto scan_directive_token;
1211 nextc = cppReader_peekC (pfile);
1214 ** was (opts->cplusplus_comments && nextc == '*')
1219 || (opts->cplusplus_comments && nextc == '/'))
1221 goto scan_directive_token;
1223 /*@switchbreak@*/ break;
1226 if (cppReader_isPedantic (pfile))
1227 cppReader_pedwarn (pfile,
1228 message ("%s in preprocessing directive",
1230 ? cstring_makeLiteralTemp ("formfeed")
1231 : cstring_makeLiteralTemp ("vertical tab")));
1232 /*@switchbreak@*/ break;
1235 cppReader_forward (pfile, -1);
1237 scan_directive_token:
1238 cppReader_forward (pfile, -1);
1239 (void) cpplib_getToken (pfile);
1242 cppReader_putChar (pfile, c);
1245 cppReader_nullTerminate (pfile);
1249 cppReader_skipRestOfLine (cppReader *pfile)
1251 size_t old = cpplib_getWritten (pfile);
1252 copy_rest_of_line (pfile);
1253 cppReader_setWritten (pfile, old);
1256 /* Handle a possible # directive.
1257 '#' has already been read. */
1260 cppReader_handleDirective (cppReader *pfile)
1263 struct directive *kt = NULL;
1265 size_t after_ident = 0;
1267 char *line_end = NULL;
1268 size_t old_written = cpplib_getWritten (pfile);
1269 int nspaces = cppSkipHspace (pfile);
1271 c = cppReader_peekC (pfile);
1273 if (c >= '0' && c <= '9')
1275 /* Handle # followed by a line number. */
1276 if (cppReader_isPedantic (pfile))
1278 cppReader_pedwarnLit
1280 cstring_makeLiteralTemp ("`#' followed by integer"));
1283 (void) do_line (pfile, NULL);
1284 goto done_a_directive;
1288 /* Now find the directive name. */
1290 cppReader_putChar (pfile, '#');
1292 parse_name (pfile, cppReader_getC (pfile));
1294 llassert (pfile->token_buffer != NULL);
1295 ident = pfile->token_buffer + old_written + 1;
1297 ident_length = cpplib_getPWritten (pfile) - ident;
1299 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1301 /* A line of just `#' becomes blank. */
1305 for (kt = directive_table; ; kt++)
1307 if (kt->length <= 0)
1309 return 0; /* goto not_a_directive; */
1312 if (kt->length == ident_length
1313 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1319 if (kt->command_reads_line)
1325 /* Nonzero means do not delete comments within the directive.
1326 #define needs this when -traditional. */
1327 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1328 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1329 CPPOPTIONS (pfile)->put_out_comments = comments;
1330 after_ident = cpplib_getWritten (pfile);
1331 copy_rest_of_line (pfile);
1332 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1336 /* For #pragma and #define, we may want to pass through the directive.
1337 Other directives may create output, but we don't want the directive
1338 itself out, so we pop it now. For example #include may write a #line
1339 command (see comment in do_include), and conditionals may emit
1340 #failed ... #endfailed stuff. But note that popping the buffer
1341 means the parameters to kt->func may point after pfile->limit
1342 so these parameters are invalid as soon as something gets appended
1343 to the token_buffer. */
1345 line_end = cpplib_getPWritten (pfile);
1348 if (!kt->pass_thru && kt->type != T_DEFINE)
1350 cppReader_setWritten (pfile, old_written);
1353 llassert (pfile->token_buffer != NULL);
1355 /* was kt->pass_thru || */
1357 if (kt->type == T_DEFINE
1358 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1360 char *p = pfile->token_buffer + old_written;
1363 ** Still need to record value for preprocessing, so
1364 ** #ifdef's, etc. using the value behave correctly.
1367 (void) do_defineAux (pfile, kt,
1368 pfile->token_buffer + after_ident,
1377 SKIP_WHITE_SPACE (p);
1379 llassert (*p == 'd');
1382 llassert (*p == 'e');
1385 llassert (*p == 'f');
1388 llassert (*p == 'i');
1391 llassert (*p == 'n');
1394 llassert (*p == 'e');
1397 ** This is way-bogus. We use the last char to record the number of
1398 ** spaces. Its too hard to get them back into the input stream.
1401 if (nspaces > 9) nspaces = 9;
1403 *p++ = '0' + nspaces;
1405 return 0; /* not_a_directive */
1407 else if (kt->pass_thru)
1409 /* Just leave the entire #define in the output stack. */
1410 return 0; /* not_a_directive */
1413 else if (kt->type == T_DEFINE
1414 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1416 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1417 SKIP_WHITE_SPACE (p);
1419 while (is_idchar[(int) *p])
1425 cppReader_putChar (pfile, '\n');
1427 else if (kt->type == T_DEFINE)
1429 cppReader_setWritten (pfile, old_written);
1440 llassert (kt->func != NULL);
1441 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1446 /* Pass a directive through to the output file.
1447 BUF points to the contents of the directive, as a contiguous string.
1448 LIMIT points to the first character past the end of the directive.
1449 KEYWORD is the keyword-table entry for the directive. */
1452 pass_thru_directive (char *buf, char *limit,
1454 struct directive *keyword)
1456 int keyword_length = keyword->length;
1458 cpplib_reserve (pfile,
1459 size_fromInt (2 + keyword_length + (limit - buf)));
1460 cppReader_putCharQ (pfile, '#');
1461 /*@-observertrans@*/
1462 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1463 size_fromInt (keyword_length));
1464 /*:=observertrans@*/
1466 if (limit != buf && buf[0] != ' ')
1468 /* Was a bug, since reserve only used 1 + ... */
1469 cppReader_putCharQ (pfile, ' ');
1472 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1475 /* Read a replacement list for a macro with parameters.
1476 Build the DEFINITION structure.
1477 Reads characters of text starting at BUF until END.
1478 ARGLIST specifies the formal parameters to look for
1479 in the text of the definition; NARGS is the number of args
1480 in that list, or -1 for a macro name that wants no argument list.
1481 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1482 and NAMELEN is its length in characters.
1484 Note that comments, backslash-newlines, and leading white space
1485 have already been deleted from the argument. */
1488 collect_expansion (cppReader *pfile, char *buf, char *limit,
1489 int nargs, /*@null@*/ struct arglist *arglist)
1492 char *p, *lastp, *exp_p;
1493 struct reflist *endpat = NULL;
1494 /* Pointer to first nonspace after last ## seen. */
1496 /* Pointer to first nonspace after last single-# seen. */
1497 char *stringify = 0;
1499 char expected_delimiter = '\0';
1502 /* Scan thru the replacement list, ignoring comments and quoted
1503 strings, picking up on the macro calls. It does a linear search
1504 thru the arg list on every potential symbol. Profiling might say
1505 that something smarter should happen. */
1510 /* Find the beginning of the trailing whitespace. */
1513 while (p < limit && is_space[(int) limit[-1]])
1518 /* Allocate space for the text in the macro definition.
1519 Leading and trailing whitespace chars need 2 bytes each.
1520 Each other input char may or may not need 1 byte,
1521 so this is an upper bound. The extra 5 are for invented
1522 leading and trailing newline-marker and final null. */
1523 maxsize = (sizeof (*defn) + (limit - p) + 5);
1525 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1534 defn = (DEFINITION *) dmalloc (maxsize);
1535 defn->noExpand = FALSE;
1537 defn->pattern = NULL;
1538 defn->nargs = nargs;
1539 defn->predefined = NULL;
1541 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1544 defn->rest_args = NULL;
1545 defn->args.argnames = NULL;
1551 /* Add one initial space escape-marker to prevent accidental
1552 token-pasting (often removed by cpplib_macroExpand). */
1556 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1557 cppReader_errorLit (pfile,
1558 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1562 /* Process the main body of the definition. */
1564 int skipped_arg = 0;
1565 register char c = *p++;
1569 if (!cppReader_isTraditional (pfile)) {
1573 if (expected_delimiter != '\0')
1575 if (c == expected_delimiter)
1576 expected_delimiter = '\0';
1580 expected_delimiter = c;
1582 /*@switchbreak@*/ break;
1585 if (p < limit && (expected_delimiter != '\0'))
1587 /* In a string, backslash goes through
1588 and makes next char ordinary. */
1591 /*@switchbreak@*/ break;
1594 /* An '@' in a string or character constant stands for itself,
1595 and does not need to be escaped. */
1596 if (expected_delimiter == '\0')
1601 /*@switchbreak@*/ break;
1604 /* # is ordinary inside a string. */
1605 if (expected_delimiter != '\0')
1607 /*@switchbreak@*/ break;
1610 if (p < limit && *p == '#') {
1611 /* ##: concatenate preceding and following tokens. */
1612 /* Take out the first #, discard preceding whitespace. */
1616 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1622 /* Skip the second #. */
1624 /* Discard following whitespace. */
1625 SKIP_WHITE_SPACE (p);
1629 cppReader_errorLit (pfile,
1630 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1632 } else if (nargs >= 0) {
1633 /* Single #: stringify following argument ref.
1634 Don't leave the # in the expansion. */
1636 SKIP_WHITE_SPACE (p);
1637 if (p == limit || ! is_idstart[(int) *p]
1638 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1639 cppReader_errorLit (pfile,
1640 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1647 /*@switchbreak@*/ break;
1650 /* In -traditional mode, recognize arguments inside strings and
1651 and character constants, and ignore special properties of #.
1652 Arguments inside strings are considered "stringified", but no
1653 extra quote marks are supplied. */
1657 if (expected_delimiter != '\0') {
1658 if (c == expected_delimiter)
1659 expected_delimiter = '\0';
1661 expected_delimiter = c;
1662 /*@switchbreak@*/ break;
1665 /* Backslash quotes delimiters and itself, but not macro args. */
1666 if (expected_delimiter != '\0' && p < limit
1667 && (*p == expected_delimiter || *p == '\\')) {
1671 /*@switchbreak@*/ break;
1674 if (expected_delimiter != '\0') /* No comments inside strings. */
1675 /*@switchbreak@*/ break;
1677 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1678 this must be -traditional. So replace the comment with
1682 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1687 /*@switchbreak@*/ break;
1691 /* Handle the start of a symbol. */
1692 if (is_idchar[(int) c] && nargs > 0) {
1693 char *id_beg = p - 1;
1697 while (p != limit && is_idchar[(int) *p])
1702 id_len = size_fromInt (p - id_beg);
1704 if (is_idstart[(int) c]
1705 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1706 register struct arglist *arg;
1708 for (arg = arglist; arg != NULL; arg = arg->next) {
1709 struct reflist *tpat;
1711 if (arg->name[0] == c
1712 && arg->length == id_len
1713 && strncmp (arg->name, id_beg, id_len) == 0) {
1716 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1717 if (cppReader_isTraditional (pfile)) {
1718 cppReader_warning (pfile,
1719 message ("macro argument `%x' is stringified.",
1720 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1722 cppReader_warning (pfile,
1723 message ("macro arg `%x' would be stringified with -traditional.",
1724 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1727 /* If ANSI, don't actually substitute inside a string. */
1728 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1729 /*@innerbreak@*/ break;
1730 /* make a pat node for this arg and append it to the end of
1732 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1734 tpat->raw_before = (concat == id_beg);
1735 tpat->raw_after = 0;
1736 tpat->rest_args = arg->rest_args;
1737 tpat->stringify = (cppReader_isTraditional (pfile)
1738 ? expected_delimiter != '\0'
1739 : stringify == id_beg);
1743 defn->pattern = tpat;
1747 endpat->next = tpat;
1749 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1753 tpat->argno = arg->argno;
1754 tpat->nchars = exp_p - lastp;
1758 SKIP_WHITE_SPACE (p1);
1760 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1762 tpat->raw_after = 1;
1765 lastp = exp_p; /* place to start copying from next time */
1768 /*@innerbreak@*/ break;
1773 /* If this was not a macro arg, copy it into the expansion. */
1774 if (skipped_arg == 0) {
1775 register char *lim1 = p;
1783 if (stringify == id_beg)
1784 cppReader_errorLit (pfile,
1785 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1790 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1792 /* If ANSI, put in a "@ " marker to prevent token pasting.
1793 But not if "inside a string" (which in ANSI mode
1794 happens only for -D option). */
1801 defn->length = size_fromInt (exp_p - defn->expansion);
1803 /* Crash now if we overrun the allocated size. */
1804 if (defn->length + 1 > maxsize)
1806 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1814 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1818 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1819 int nargs, /*@null@*/ struct arglist *arglist)
1822 char *p, *lastp, *exp_p;
1823 struct reflist *endpat = NULL;
1824 /* Pointer to first nonspace after last ## seen. */
1826 /* Pointer to first nonspace after last single-# seen. */
1827 char *stringify = 0;
1829 char expected_delimiter = '\0';
1832 /* Scan thru the replacement list, ignoring comments and quoted
1833 strings, picking up on the macro calls. It does a linear search
1834 thru the arg list on every potential symbol. Profiling might say
1835 that something smarter should happen. */
1839 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1840 fileloc_unparse (loc)));
1843 /* Find the beginning of the trailing whitespace. */
1846 while (p < limit && is_space[(int) limit[-1]])
1851 /* Allocate space for the text in the macro definition.
1852 Leading and trailing whitespace chars need 2 bytes each.
1853 Each other input char may or may not need 1 byte,
1854 so this is an upper bound. The extra 5 are for invented
1855 leading and trailing newline-marker and final null. */
1856 maxsize = (sizeof (*defn) + (limit - p) + 5);
1858 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1867 defn = (DEFINITION *) dmalloc (maxsize);
1868 defn->noExpand = FALSE;
1870 defn->pattern = NULL;
1871 defn->nargs = nargs;
1872 defn->predefined = NULL;
1874 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1877 defn->rest_args = NULL;
1878 defn->args.argnames = NULL;
1884 /* Add one initial space escape-marker to prevent accidental
1885 token-pasting (often removed by cpplib_macroExpand). */
1889 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1890 voptgenerror (FLG_PREPROC,
1891 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1896 /* Process the main body of the definition. */
1898 int skipped_arg = 0;
1899 register char c = *p++;
1903 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1907 if (expected_delimiter != '\0')
1909 if (c == expected_delimiter)
1910 expected_delimiter = '\0';
1914 expected_delimiter = c;
1916 /*@switchbreak@*/ break;
1919 if (p < limit && (expected_delimiter != '\0'))
1921 /* In a string, backslash goes through
1922 and makes next char ordinary. */
1925 /*@switchbreak@*/ break;
1928 /* An '@' in a string or character constant stands for itself,
1929 and does not need to be escaped. */
1930 if (expected_delimiter == '\0')
1935 /*@switchbreak@*/ break;
1938 /* # is ordinary inside a string. */
1939 if (expected_delimiter != '\0')
1941 /*@switchbreak@*/ break;
1944 if (p < limit && *p == '#') {
1945 /* ##: concatenate preceding and following tokens. */
1946 /* Take out the first #, discard preceding whitespace. */
1950 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1956 /* Skip the second #. */
1958 /* Discard following whitespace. */
1959 SKIP_WHITE_SPACE (p);
1963 voptgenerror (FLG_PREPROC,
1964 cstring_makeLiteral ("`##' at end of macro definition"),
1967 } else if (nargs >= 0) {
1968 /* Single #: stringify following argument ref.
1969 Don't leave the # in the expansion. */
1971 SKIP_WHITE_SPACE (p);
1972 if (p == limit || ! is_idstart[(int) *p]
1973 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1977 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1986 /*@switchbreak@*/ break;
1989 /* In -traditional mode, recognize arguments inside strings and
1990 and character constants, and ignore special properties of #.
1991 Arguments inside strings are considered "stringified", but no
1992 extra quote marks are supplied. */
1996 if (expected_delimiter != '\0') {
1997 if (c == expected_delimiter)
1998 expected_delimiter = '\0';
2000 expected_delimiter = c;
2001 /*@switchbreak@*/ break;
2004 /* Backslash quotes delimiters and itself, but not macro args. */
2005 if (expected_delimiter != '\0' && p < limit
2006 && (*p == expected_delimiter || *p == '\\')) {
2010 /*@switchbreak@*/ break;
2013 if (expected_delimiter != '\0') /* No comments inside strings. */
2014 /*@switchbreak@*/ break;
2016 /* If we find a comment that wasn't removed by cppReader_handleDirective,
2017 this must be -traditional. So replace the comment with
2021 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2026 /*@switchbreak@*/ break;
2030 /* Handle the start of a symbol. */
2031 if (is_idchar[(int) c] && nargs > 0) {
2032 char *id_beg = p - 1;
2036 while (p != limit && is_idchar[(int) *p])
2041 id_len = size_fromInt (p - id_beg);
2043 if (is_idstart[(int) c]
2044 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2045 register struct arglist *arg;
2047 for (arg = arglist; arg != NULL; arg = arg->next) {
2048 struct reflist *tpat;
2050 if (arg->name[0] == c
2051 && arg->length == id_len
2052 && strncmp (arg->name, id_beg, id_len) == 0) {
2055 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2056 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2057 voptgenerror (FLG_PREPROC,
2058 message ("macro argument `%x' is stringified.",
2059 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2063 voptgenerror (FLG_PREPROC,
2064 message ("Macro arg `%x' would be stringified with -traditional.",
2065 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2070 /* If ANSI, don't actually substitute inside a string. */
2071 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2072 /*@innerbreak@*/ break;
2073 /* make a pat node for this arg and append it to the end of
2075 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2077 tpat->raw_before = (concat == id_beg);
2078 tpat->raw_after = 0;
2079 tpat->rest_args = arg->rest_args;
2080 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2081 ? expected_delimiter != '\0'
2082 : stringify == id_beg);
2086 defn->pattern = tpat;
2090 endpat->next = tpat;
2092 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2096 tpat->argno = arg->argno;
2097 tpat->nchars = exp_p - lastp;
2101 SKIP_WHITE_SPACE (p1);
2103 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2105 tpat->raw_after = 1;
2108 lastp = exp_p; /* place to start copying from next time */
2111 /*@innerbreak@*/ break;
2116 /* If this was not a macro arg, copy it into the expansion. */
2117 if (skipped_arg == 0) {
2118 register char *lim1 = p;
2126 if (stringify == id_beg)
2130 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2137 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2139 /* If ANSI, put in a "@ " marker to prevent token pasting.
2140 But not if "inside a string" (which in ANSI mode
2141 happens only for -D option). */
2148 defn->length = size_fromInt (exp_p - defn->expansion);
2150 /* Crash now if we overrun the allocated size. */
2151 if (defn->length + 1 > maxsize)
2153 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2160 * special extension string that can be added to the last macro argument to
2161 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2162 * #define wow(a, b...) process (b, a, b)
2163 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2164 * { wow (one, two); } -> { process (two, one, two); }
2165 * if this "rest_arg" is used with the concat token '##' and if it is not
2166 * supplied then the token attached to with ## will not be outputted. Ex:
2167 * #define wow (a, b...) process (b ## , a, ## b)
2168 * { wow (1, 2); } -> { process (2, 1, 2); }
2169 * { wow (one); } -> { process (one); {
2172 /*@-readonlytrans@*/
2173 static char rest_extension[] = "...";
2174 /*:=readonlytrans@*/
2177 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2179 /* Create a DEFINITION node from a #define directive. Arguments are
2180 as for do_define. */
2183 static /*@null@*/ macroDef
2184 create_definition (/*@exposed@*/ char *buf, char *limit,
2185 cppReader *pfile, bool predefinition,
2188 char *bp; /* temp ptr into input buffer */
2189 char *symname; /* remember where symbol name starts */
2190 size_t sym_length; /* and how long it is */
2191 int rest_args = 0; /* really int! */
2194 cstring file = (CPPBUFFER (pfile) != NULL)
2195 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2197 int arglengths = 0; /* Accumulate lengths of arg names
2198 plus number of args. */
2202 DPRINTF (("Create definition: %s", buf));
2205 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2209 while (is_hor_space[(int) *bp])
2214 symname = bp; /* remember where it starts */
2216 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2220 /* Lossage will occur if identifiers or control keywords are broken
2221 across lines using backslash. This is not the right place to take
2225 struct arglist *arg_ptrs = NULL;
2228 bp++; /* skip '(' */
2229 SKIP_WHITE_SPACE (bp);
2231 /* Loop over macro argument names. */
2234 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2236 temp->next = arg_ptrs;
2237 temp->argno = argno++;
2238 temp->rest_args = 0;
2244 cppReader_pedwarn (pfile,
2245 message ("another parameter follows `%s'",
2246 cstring_fromChars (rest_extension)));
2249 if (!is_idstart[(int) *bp])
2251 cppReader_pedwarnLit (pfile,
2252 cstring_makeLiteralTemp ("Invalid character in macro parameter name"));
2255 /* Find the end of the arg name. */
2256 while (is_idchar[(int) *bp])
2259 /* do we have a "special" rest-args extension here? */
2260 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2261 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2264 temp->rest_args = 1;
2265 /*@innerbreak@*/ break;
2269 temp->length = size_fromInt (bp - temp->name);
2273 bp += REST_EXTENSION_LENGTH;
2276 arglengths += temp->length + 2;
2277 SKIP_WHITE_SPACE (bp);
2279 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2280 cppReader_errorLit (pfile,
2281 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2287 SKIP_WHITE_SPACE (bp);
2290 cppReader_errorLit (pfile,
2291 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2295 struct arglist *otemp;
2297 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2299 if (temp->length == otemp->length &&
2300 strncmp (temp->name, otemp->name, temp->length) == 0) {
2301 cstring name = cstring_copyLength (temp->name, temp->length);
2302 cppReader_error (pfile,
2303 message ("duplicate argument name `%x' in `#define'", name));
2310 ++bp; /* skip paren */
2311 SKIP_WHITE_SPACE (bp);
2312 /* now everything from bp before limit is the definition. */
2313 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2314 defn->rest_args = rest_args;
2316 /* Now set defn->args.argnames to the result of concatenating
2317 the argument names in reverse order
2318 with comma-space between them. */
2319 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2322 struct arglist *temp;
2324 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2326 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2328 if (temp->next != 0)
2330 defn->args.argnames[i++] = ',';
2331 defn->args.argnames[i++] = ' ';
2335 defn->args.argnames[i] = '\0';
2340 /* Simple expansion or empty definition. */
2344 if (is_hor_space[(int) *bp]) {
2346 SKIP_WHITE_SPACE (bp);
2349 case '!': case '\"': case '#': case '%': case '&': case '\'':
2350 case ')': case '*': case '+': case ',': case '-': case '.':
2351 case '/': case ':': case ';': case '<': case '=': case '>':
2352 case '?': case '[': case '\\': case ']': case '^': case '{':
2353 case '|': case '}': case '~':
2354 cppReader_warning (pfile,
2355 message ("Missing white space after #define %x",
2356 cstring_prefix (cstring_fromChars (symname),
2361 cppReader_pedwarn (pfile,
2362 message ("Missing white space after #define %x",
2363 cstring_prefix (cstring_fromChars (symname),
2369 /* now everything from bp before limit is the definition. */
2370 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2371 defn->args.argnames = mstring_createEmpty ();
2374 defn->noExpand = noExpand;
2375 DPRINTF (("No expand: %d", noExpand));
2379 /* not: llassert (cstring_isUndefined (defn->file)); */
2382 /* OP is null if this is a predefinition */
2383 defn->predefined = predefinition;
2385 mdef.symnam = symname;
2386 mdef.symlen = sym_length;
2397 cpplib_createDefinition (cstring def,
2402 char *buf = cstring_toCharsSafe (def);
2403 char *limit = buf + cstring_length (def);
2404 char *bp; /* temp ptr into input buffer */
2405 char *symname; /* remember where symbol name starts */
2406 size_t sym_length; /* and how long it is */
2407 int rest_args = 0; /* really int! */
2408 int line = fileloc_lineno (loc);
2409 cstring file = fileloc_filename (loc);
2411 int arglengths = 0; /* Accumulate lengths of arg names
2412 plus number of args. */
2417 DPRINTF (("Creating definition: %s", buf));
2419 while (is_hor_space[(int) *bp])
2424 symname = bp; /* remember where it starts */
2426 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2428 DPRINTF (("length: %d", sym_length));
2432 DPRINTF (("Here: %s", bp));
2434 /* Lossage will occur if identifiers or control keywords are broken
2435 across lines using backslash. This is not the right place to take
2439 struct arglist *arg_ptrs = NULL;
2442 bp++; /* skip '(' */
2443 SKIP_WHITE_SPACE (bp);
2445 /* Loop over macro argument names. */
2448 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2450 temp->next = arg_ptrs;
2451 temp->argno = argno++;
2452 temp->rest_args = 0;
2458 voptgenerror (FLG_PREPROC,
2459 message ("Another parameter follows %s",
2460 cstring_fromChars (rest_extension)),
2464 if (!is_idstart[(int) *bp])
2466 voptgenerror (FLG_PREPROC,
2467 message ("Invalid character in macro parameter name: %c", *bp),
2471 /* Find the end of the arg name. */
2472 while (is_idchar[(int) *bp])
2475 /* do we have a "special" rest-args extension here? */
2476 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2477 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2480 temp->rest_args = 1;
2481 /*@innerbreak@*/ break;
2485 temp->length = size_fromInt (bp - temp->name);
2489 bp += REST_EXTENSION_LENGTH;
2492 arglengths += temp->length + 2;
2493 SKIP_WHITE_SPACE (bp);
2495 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2496 voptgenerror (FLG_PREPROC,
2497 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2504 SKIP_WHITE_SPACE (bp);
2507 voptgenerror (FLG_PREPROC,
2508 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2513 struct arglist *otemp;
2515 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2517 if (temp->length == otemp->length &&
2518 strncmp (temp->name, otemp->name, temp->length) == 0) {
2519 cstring name = cstring_copyLength (temp->name, temp->length);
2521 voptgenerror (FLG_PREPROC,
2522 message ("Duplicate argument name in #define: %s", name),
2530 ++bp; /* skip paren */
2531 SKIP_WHITE_SPACE (bp);
2532 /* now everything from bp before limit is the definition. */
2533 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2534 defn->rest_args = rest_args;
2536 /* Now set defn->args.argnames to the result of concatenating
2537 the argument names in reverse order
2538 with comma-space between them. */
2539 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2542 struct arglist *temp;
2544 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2545 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2547 if (temp->next != 0) {
2548 defn->args.argnames[i++] = ',';
2549 defn->args.argnames[i++] = ' ';
2553 defn->args.argnames[i] = '\0';
2558 /* Simple expansion or empty definition. */
2562 if (is_hor_space[(int) *bp]) {
2564 SKIP_WHITE_SPACE (bp);
2567 case '!': case '\"': case '#': case '%': case '&': case '\'':
2568 case ')': case '*': case '+': case ',': case '-': case '.':
2569 case '/': case ':': case ';': case '<': case '=': case '>':
2570 case '?': case '[': case '\\': case ']': case '^': case '{':
2571 case '|': case '}': case '~':
2572 voptgenerror (FLG_PREPROC,
2573 message ("Missing white space after #define %x",
2574 cstring_prefix (cstring_fromChars (symname),
2580 voptgenerror (FLG_PREPROC,
2581 message ("Missing white space after #define %x",
2582 cstring_prefix (cstring_fromChars (symname),
2590 /* now everything from bp before limit is the definition. */
2591 llassert (limit > bp);
2592 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2593 defn->args.argnames = mstring_createEmpty ();
2596 defn->noExpand = noExpand;
2597 DPRINTF (("No expand: %d", noExpand));
2601 /* not: llassert (cstring_isUndefined (defn->file)); */
2604 /* OP is null if this is a predefinition */
2605 defn->predefined = predefinition;
2608 mdef.symnam = symname;
2609 mdef.symlen = sym_length;
2619 /* Check a purported macro name SYMNAME, and yield its length.
2620 USAGE is the kind of name this is intended for. */
2622 size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2627 for (p = symname; is_idchar[(int) *p]; p++)
2632 sym_length = size_fromInt (p - symname);
2635 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2637 cppReader_error (pfile, message ("invalid %s name", usage));
2639 else if (!is_idstart[(int) *symname])
2641 char *msg = (char *) dmalloc (sym_length + 1);
2642 memcpy (msg, symname, sym_length);
2643 msg[sym_length] = '\0';
2644 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2645 cstring_fromChars (msg)));
2650 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2652 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2661 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2664 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2669 for (p = symname; is_idchar[(int) *p]; p++)
2674 sym_length = size_fromInt (p - symname);
2677 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2679 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2680 cstring_fromChars (symname)), loc);
2682 else if (!is_idstart[(int) *symname])
2684 char *msg = (char *) dmalloc (sym_length + 1);
2685 memcpy (msg, symname, sym_length);
2686 msg[sym_length] = '\0';
2687 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2688 cstring_fromChars (msg)),
2694 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2696 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2703 /* Return zero if two DEFINITIONs are isomorphic. */
2706 compare_defs (DEFINITION *d1, DEFINITION *d2)
2708 register struct reflist *a1, *a2;
2709 register char *p1 = d1->expansion;
2710 register char *p2 = d2->expansion;
2713 if (d1->nargs != d2->nargs)
2718 llassert (d1->args.argnames != NULL);
2719 llassert (d2->args.argnames != NULL);
2721 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2726 for (a1 = d1->pattern, a2 = d2->pattern;
2727 (a1 != NULL) && (a2 != NULL);
2728 a1 = a1->next, a2 = a2->next) {
2729 if (!((a1->nchars == a2->nchars
2730 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2731 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2732 || a1->argno != a2->argno
2733 || a1->stringify != a2->stringify
2734 || a1->raw_before != a2->raw_before
2735 || a1->raw_after != a2->raw_after)
2744 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2745 p2, d2->length - (p2 - d2->expansion), 1))
2751 /* Return TRUE if two parts of two macro definitions are effectively different.
2752 One of the parts starts at BEG1 and has LEN1 chars;
2753 the other has LEN2 chars at BEG2.
2754 Any sequence of whitespace matches any other sequence of whitespace.
2755 FIRST means these parts are the first of a macro definition;
2756 so ignore leading whitespace entirely.
2757 LAST means these parts are the last of a macro definition;
2758 so ignore trailing whitespace entirely. */
2761 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2763 char *end1 = beg1 + len1;
2764 char *end2 = beg2 + len2;
2767 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2768 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2771 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2772 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2774 while (beg1 != end1 && beg2 != end2) {
2775 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2776 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2777 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2778 } else if (*beg1 == *beg2) {
2782 return (beg1 != end1) || (beg2 != end2);
2785 /* Process a #define command.
2786 BUF points to the contents of the #define command, as a contiguous string.
2787 LIMIT points to the first character past the end of the definition.
2788 KEYWORD is the keyword-table entry for #define,
2789 or NULL for a "predefined" macro. */
2792 do_defineAux (cppReader *pfile, struct directive *keyword,
2793 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2799 DPRINTF (("Define aux: %d", noExpand));
2801 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2806 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2808 DPRINTF (("Macro: %s / %s",
2809 cstring_copyLength (mdef.symnam, mdef.symlen),
2810 bool_unparse (noExpand)));
2812 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2816 /* Redefining a precompiled key is ok. */
2817 if (hp->type == T_PCSTRING)
2819 /* Redefining a macro is ok if the definitions are the same. */
2820 else if (hp->type == T_MACRO)
2821 ok = !compare_defs (mdef.defn, hp->value.defn);
2822 /* Redefining a constant is ok with -D. */
2823 else if (hp->type == T_CONST)
2824 ok = !CPPOPTIONS (pfile)->done_initializing;
2829 /* Print the warning if it's not ok. */
2833 ** If we are passing through #define and #undef directives, do
2834 ** that for this re-definition now.
2837 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2839 /* llassert (keyword != NULL); */
2840 pass_thru_directive (buf, limit, pfile, keyword);
2843 cpp_setLocation (pfile);
2845 if (hp->type == T_MACRO)
2847 if (hp->value.defn->noExpand)
2849 ; /* error will be reported checking macros */
2855 message ("Macro %q already defined",
2856 cstring_copyLength (mdef.symnam, mdef.symlen)),
2857 message ("%q: Previous definition of %q",
2858 fileloc_unparseRaw (hp->value.defn->file,
2859 (int) hp->value.defn->line),
2860 cstring_copyLength (mdef.symnam, mdef.symlen)));
2865 genppllerror (FLG_MACROREDEF,
2866 message ("Macro %q already defined",
2867 cstring_copyLength (mdef.symnam,
2873 /* Replace the old definition. */
2875 hp->value.defn = mdef.defn;
2880 ** If we are passing through #define and #undef directives, do
2881 ** that for this new definition now.
2886 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2888 pass_thru_directive (buf, limit, pfile, keyword);
2891 DPRINTF (("Define macro: %s / %d",
2892 mdef.symnam, mdef.defn->noExpand));
2894 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2896 } /*@=branchstate@*/
2906 do_define (cppReader *pfile, struct directive *keyword,
2907 /*@exposed@*/ char *buf, char *limit)
2909 DPRINTF (("Regular do define"));
2910 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2913 /* This structure represents one parsed argument in a macro call.
2914 `raw' points to the argument text as written (`raw_length' is its length).
2915 `expanded' points to the argument's macro-expansion
2916 (its length is `expand_length').
2917 `stringified_length' is the length the argument would have
2919 `use_count' is the number of times this macro arg is substituted
2920 into the macro. If the actual use count exceeds 10,
2921 the value stored is 10. */
2923 /* raw and expanded are relative to ARG_BASE */
2925 #define ARG_BASE ((pfile)->token_buffer)
2928 /* Strings relative to pfile->token_buffer */
2934 int stringified_length;
2939 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2940 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2941 as the new input buffer.
2942 Return the new buffer, or NULL on failure. */
2944 /*@null@*/ /*@exposed@*/ cppBuffer *
2945 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2947 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2949 if (buf == pfile->buffer_stack)
2951 cppReader_fatalError
2953 message ("%s: macro or `#include' recursion too deep",
2954 (buf->fname != NULL)
2956 : cstring_makeLiteral ("<no name>")));
2957 sfreeEventually (buffer);
2961 llassert (buf != NULL);
2964 memset ((char *) buf, 0, sizeof (*buf));
2965 DPRINTF (("Pushing buffer: %s", cstring_copyLength (buffer, length)));
2966 CPPBUFFER (pfile) = buf;
2968 buf->if_stack = pfile->if_stack;
2969 buf->cleanup = cppReader_nullCleanup;
2970 buf->underflow = cppReader_nullUnderflow;
2972 buf->cur = buf->buf;
2976 buf->alimit = buf->rlimit = buffer + length;
2980 buf->alimit = buf->rlimit = NULL;
2987 cppReader_popBuffer (cppReader *pfile)
2989 cppBuffer *buf = CPPBUFFER (pfile);
2991 llassert (buf != NULL);
2993 (void) (*buf->cleanup) (buf, pfile);
2994 return ++CPPBUFFER (pfile);
2997 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2998 Pop the buffer when done. */
3001 cppReader_scanBuffer (cppReader *pfile)
3003 cppBuffer *buffer = CPPBUFFER (pfile);
3006 enum cpp_token token;
3008 token = cpplib_getToken (pfile);
3010 if (token == CPP_EOF) /* Should not happen ... */
3015 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
3017 (void) cppReader_popBuffer (pfile);
3025 * Rescan a string (which may have escape marks) into pfile's buffer.
3026 * Place the result in pfile->token_buffer.
3028 * The input is copied before it is scanned, so it is safe to pass
3029 * it something from the token_buffer that will get overwritten
3030 * (because it follows cpplib_getWritten). This is used by do_include.
3034 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3036 register cppBuffer *ip;
3037 char *limit = buf + length;
3038 char *buf1, *p1, *p2;
3040 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
3042 /* evans - 2001-08-26
3043 ** length is unsigned - this doesn't make sense
3049 /* Set up the input on the input stack. */
3051 buf1 = (char *) dmalloc (length + 1);
3061 buf1[length] = '\0';
3063 ip = cppReader_pushBuffer (pfile, buf1, length);
3068 ip->has_escapes = TRUE;
3070 /* Scan the input, create the output. */
3071 cppReader_scanBuffer (pfile);
3073 cppReader_nullTerminate (pfile);
3077 adjust_position (char *buf, char *limit, int *linep, int *colp)
3083 (*linep)++, (*colp) = 1;
3089 /* Move line_base forward, updating lineno and colno. */
3092 update_position (cppBuffer *pbuf)
3095 char *new_pos = pbuf->cur;
3096 register struct parse_marker *mark;
3098 llassert (pbuf->buf != NULL);
3099 old_pos = pbuf->buf + pbuf->line_base;
3101 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3103 if (pbuf->buf + mark->position < new_pos)
3104 new_pos = pbuf->buf + mark->position;
3106 pbuf->line_base += new_pos - old_pos;
3108 llassert (old_pos != NULL);
3109 llassert (new_pos != NULL);
3111 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3115 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3116 /*@null@*/ /*@out@*/ int *colp)
3124 } /*@=branchstate@*/
3128 *linep = pbuf->lineno;
3129 *colp = pbuf->colno;
3131 llassert (pbuf->buf != NULL);
3132 llassert (pbuf->cur != NULL);
3134 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3143 /* Return the cppBuffer that corresponds to a file (not a macro). */
3145 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3147 cppBuffer *ip = cppReader_getBuffer (pfile);
3150 ip != NULL && ip != cppReader_nullBuffer (pfile);
3151 ip = cppBuffer_prevBuffer (ip))
3153 if (ip->fname != NULL)
3163 count_newlines (char *buf, char *limit)
3165 register long count = 0;
3177 * write out a #line command, for instance, after an #include file.
3178 * If CONDITIONAL is nonzero, we can omit the #line if it would
3179 * appear to be a no-op, and we can output a few newlines instead
3180 * if we want to increase the line number by a small amount.
3181 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3185 output_line_command (cppReader *pfile, bool conditional,
3186 enum file_change_code file_change)
3189 cppBuffer *ip = CPPBUFFER (pfile);
3192 llassert (ip != NULL);
3194 if (ip->fname == NULL)
3197 update_position (ip);
3199 if (CPPOPTIONS (pfile)->no_line_commands
3200 || CPPOPTIONS (pfile)->no_output)
3203 buf = CPPBUFFER (pfile);
3205 llassert (buf != NULL);
3210 llassert (ip->cur != NULL);
3212 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3214 if (CPPOPTIONS (pfile)->no_line_commands)
3218 if (line == pfile->lineno)
3221 /* If the inherited line number is a little too small,
3222 output some newlines instead of a #line command. */
3224 if (line > pfile->lineno && line < pfile->lineno + 8)
3226 cpplib_reserve (pfile, 20);
3227 while (line > pfile->lineno)
3229 cppReader_putCharQ (pfile, '\n');
3237 cpplib_reserve (pfile,
3238 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
3241 #ifdef OUTPUT_LINE_COMMANDS
3242 static char sharp_line[] = "#line ";
3244 static char sharp_line[] = "# ";
3246 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3249 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3250 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3252 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3254 if (file_change != same_file) {
3255 cppReader_putCharQ (pfile, ' ');
3256 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3258 /* Tell cc1 if following text comes from a system header file. */
3259 if (ip->system_header_p != '\0') {
3260 cppReader_putCharQ (pfile, ' ');
3261 cppReader_putCharQ (pfile, '3');
3263 #ifndef NO_IMPLICIT_EXTERN_C
3264 /* Tell cc1plus if following text should be treated as C. */
3265 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3266 cppReader_putCharQ (pfile, ' ');
3267 cppReader_putCharQ (pfile, '4');
3270 cppReader_putCharQ (pfile, '\n');
3271 pfile->lineno = line;
3276 * Parse a macro argument and append the info on PFILE's token_buffer.
3277 * REST_ARGS means to absorb the rest of the args.
3278 * Return nonzero to indicate a syntax error.
3281 static enum cpp_token
3282 macarg (cppReader *pfile, int rest_args)
3285 enum cpp_token token;
3286 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3287 bool oldexpand = pfile->no_macro_expand;
3288 CPPOPTIONS (pfile)->put_out_comments = 1;
3290 /* Try to parse as much of the argument as exists at this
3291 input stack level. */
3293 pfile->no_macro_expand = TRUE;
3297 token = cpplib_getToken (pfile);
3304 /* If we've hit end of file, it's an error (reported by caller).
3305 Ditto if it's the end of cpp_expand_to_buffer text.
3306 If we've hit end of macro, just continue. */
3307 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3309 /*@switchbreak@*/ break;
3312 /*@switchbreak@*/ break;
3316 /*@switchbreak@*/ break;
3318 /* if we've returned to lowest level and
3319 we aren't absorbing all args */
3320 if (paren == 0 && rest_args == 0)
3322 /*@switchbreak@*/ break;
3324 /* Remove ',' or ')' from argument buffer. */
3325 cppReader_adjustWritten (pfile, -1);
3333 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3334 pfile->no_macro_expand = oldexpand;
3340 /* Turn newlines to spaces in the string of length LENGTH at START,
3341 except inside of string constants.
3342 The string is copied into itself with its beginning staying fixed. */
3345 change_newlines (char *start, int length)
3349 register char *limit;
3353 limit = start + length;
3356 while (ibp < limit) {
3357 *obp++ = c = *ibp++;
3362 /* Notice and skip strings, so that we don't delete newlines in them. */
3365 while (ibp < limit) {
3366 *obp++ = c = *ibp++;
3368 /*@innerbreak@*/ break;
3369 if (c == '\n' && quotec == '\'')
3370 /*@innerbreak@*/ break;
3373 /*@switchbreak@*/ break;
3380 static /*@observer@*/ struct tm *
3381 timestamp (/*@returned@*/ cppReader *pfile)
3383 if (pfile->timebuf == NULL)
3385 time_t t = time ((time_t *) 0);
3386 pfile->timebuf = localtime (&t);
3389 llassert (pfile->timebuf != NULL);
3391 return pfile->timebuf;
3394 static ob_mstring monthnames[] = {
3395 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3396 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3400 * expand things like __FILE__. Place the expansion into the output
3401 * buffer *without* rescanning.
3405 special_symbol (hashNode hp, cppReader *pfile)
3407 cstring buf = cstring_undefined;
3413 int paren = 0; /* For special `defined' keyword */
3415 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3417 if (ip == cppReader_nullBuffer (pfile))
3419 cppReader_errorLit (pfile,
3420 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3421 return; /* the show must go on */
3424 if (ip != NULL && ip->fname != NULL)
3436 if (hp->type == T_BASE_FILE)
3438 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3440 ip = cppBuffer_prevBuffer (ip);
3444 llassert (ip != NULL);
3445 string = cstring_toCharsSafe (ip->nominal_fname);
3452 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3453 quote_string (pfile, string);
3457 case T_INCLUDE_LEVEL:
3459 ip = cppReader_getBuffer (pfile);
3461 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3462 ip = cppBuffer_prevBuffer (ip))
3464 if (ip != NULL && ip->fname != NULL)
3470 buf = message ("%d", true_indepth - 1);
3474 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3477 #ifndef NO_BUILTIN_SIZE_TYPE
3479 buf = cstring_makeLiteral (SIZE_TYPE);
3483 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3484 case T_PTRDIFF_TYPE:
3485 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3490 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3493 case T_USER_LABEL_PREFIX_TYPE:
3494 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3497 case T_REGISTER_PREFIX_TYPE:
3498 buf = cstring_makeLiteral (REGISTER_PREFIX);
3502 buf = message ("%d", hp->value.ival);
3509 int line = ip->lineno;
3510 int col = ip->colno;
3512 llassert (ip->cur != NULL);
3513 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3515 buf = message ("%d", (int) line);
3527 char *sbuf = (char *) dmalloc (20);
3528 timebuf = timestamp (pfile);
3529 if (hp->type == T_DATE)
3531 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3532 timebuf->tm_mday, timebuf->tm_year + 1900);
3536 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3540 buf = cstring_fromCharsNew (sbuf);
3545 case T_SPEC_DEFINED:
3546 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3547 ip = cppReader_getBuffer (pfile);
3548 llassert (ip != NULL);
3549 llassert (ip->cur != NULL);
3550 SKIP_WHITE_SPACE (ip->cur);
3552 if (*ip->cur == '(')
3555 ip->cur++; /* Skip over the paren */
3556 SKIP_WHITE_SPACE (ip->cur);
3559 if (!is_idstart[(int) *ip->cur])
3561 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3564 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3567 buf = cstring_makeLiteral (" 1 ");
3570 while (is_idchar[(int) *ip->cur])
3575 SKIP_WHITE_SPACE (ip->cur);
3579 if (*ip->cur != ')')
3587 cppReader_errorLit (pfile,
3588 cstring_makeLiteralTemp ("`defined' without an identifier"));
3592 cpp_setLocation (pfile);
3593 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3596 len = cstring_length (buf);
3598 cpplib_reserve (pfile, len + 1);
3599 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3600 cppReader_nullTerminateQ (pfile);
3606 /* Write out a #define command for the special named MACRO_NAME
3607 to PFILE's token_buffer. */
3610 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3612 static char define_directive[] = "#define ";
3613 size_t macro_name_length = strlen (macro_name);
3614 output_line_command (pfile, 0, same_file);
3615 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3616 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3617 cppReader_putStrN (pfile, macro_name, macro_name_length);
3618 cppReader_putCharQ (pfile, ' ');
3619 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3620 cppReader_putChar (pfile, '\n');
3623 /* Initialize the built-in macros. */
3626 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3627 int len, enum node_type type,
3628 int ivalue, /*@null@*/ /*@only@*/ char *value,
3631 cstring sname = cstring_fromCharsNew (name);
3633 llassert (usymtab_inGlobalScope ());
3636 ** Be careful here: this is done before the ctype table has
3637 ** been initialized.
3640 if (!usymtab_exists (sname))
3642 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3644 if (ctype_equal (ctyp, ctype_string))
3646 qualList ql = qualList_new ();
3647 ql = qualList_add (ql, qual_createObserver ());
3648 uentry_reflectQualifiers (ue, ql);
3652 usymtab_addGlobalEntry (ue);
3659 (void) cpphash_install (name, len, type, ivalue, value, hash);
3660 cstring_free (sname);
3664 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3665 int len, enum node_type type,
3667 /*@only@*/ /*@null@*/ char *value, int hash)
3669 cstring sname = cstring_fromChars (name);
3670 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3672 llassert (usymtab_inGlobalScope ());
3674 if (!usymtab_existsTypeEither (sname))
3676 uentry ue = uentry_makeDatatype (sname, ctyp,
3678 fileloc_createBuiltin ());
3679 llassert (!usymtab_existsEither (sname));
3680 usymtab_addGlobalEntry (ue);
3683 (void) cpphash_install (name, len, type, ivalue, value, hash);
3687 initialize_builtins (cppReader *pfile)
3689 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3690 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3691 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3692 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3693 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3694 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3695 #ifndef NO_BUILTIN_SIZE_TYPE
3696 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3698 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3699 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3701 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3702 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3703 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3704 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3707 ** No, don't define __STDC__
3710 if (!cppReader_isTraditional (pfile))
3712 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3719 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3723 ** This is supplied using a -D by the compiler driver
3724 ** so that it is present only when truly compiling with GNU C.
3727 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3729 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3730 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3732 /*drl 1/9/2001/ try to define the right symbol for the architecture
3733 We use autoconf to determine the target cpu
3735 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3737 /*drl 1/2/2002 set some flags based on uname
3738 I'd like to be able to do this with autoconf macro instead...
3741 /*Thanks to Nelson Beebe for suggesting possible values for these */
3743 if (! strcmp (UNAME, "Linux"))
3746 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3750 else if(! strcmp (UNAME, "Darwin"))
3752 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3753 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3755 else if(! strcmp (UNAME, "HP-UX"))
3757 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3758 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3759 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3760 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3761 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3762 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3763 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3764 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3765 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3766 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3767 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3769 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3770 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3771 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3772 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3773 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3774 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3775 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3781 else if(! strcmp (UNAME, "IRIX64"))
3783 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3785 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3786 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3787 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3788 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3789 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3791 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3792 /*_MIPS_SIM=_ABIN32*/
3793 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3794 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3795 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3796 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3797 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3798 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3799 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3800 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3801 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3802 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3803 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3805 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3806 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3807 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3808 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3809 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3810 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3811 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3812 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3813 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3814 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3815 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3816 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3818 else if(! strcmp (UNAME, "OSF1"))
3820 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3822 else if (!strcmp (UNAME, "Rhapsody"))
3824 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3825 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3828 else if (!strcmp (UNAME, "SunOS"))
3830 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3831 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3832 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3833 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3838 types which we have not explictedly handled.
3839 AIX, FreeBSD, IRIX, Mach
3844 if (CPPOPTIONS (pfile)->debug_output)
3846 dump_special_to_buffer (pfile, "__BASE_FILE__");
3847 dump_special_to_buffer (pfile, "__VERSION__");
3848 #ifndef NO_BUILTIN_SIZE_TYPE
3849 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3851 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3852 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3854 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3855 dump_special_to_buffer (pfile, "__DATE__");
3856 dump_special_to_buffer (pfile, "__TIME__");
3857 if (!cppReader_isTraditional (pfile))
3858 dump_special_to_buffer (pfile, "__STDC__");
3863 /* Return 1 iff a token ending in C1 followed directly by a token C2
3864 could cause mis-tokenization. */
3867 unsafe_chars (char c1, char c2)
3872 if (c2 == c1 || c2 == '=')
3876 case '0': case '1': case '2': case '3': case '4':
3877 case '5': case '6': case '7': case '8': case '9':
3878 case 'e': case 'E': case 'p': case 'P':
3879 if (c2 == '-' || c2 == '+')
3880 return 1; /* could extend a pre-processing number */
3883 if (c2 == '\'' || c2 == '\"')
3884 return 1; /* Could turn into L"xxx" or L'xxx'. */
3888 case 'a': case 'b': case 'c': case 'd': case 'f':
3889 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3890 case 'm': case 'n': case 'o': case 'q': case 'r':
3891 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3893 case 'A': case 'B': case 'C': case 'D': case 'F':
3894 case 'G': case 'H': case 'I': case 'J': case 'K':
3895 case 'M': case 'N': case 'O': case 'Q': case 'R':
3896 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3898 /* We're in the middle of either a name or a pre-processing number. */
3899 return (is_idchar[(int) c2] || c2 == '.');
3900 case '<': case '>': case '!': case '%': case '#': case ':':
3901 case '^': case '&': case '|': case '*': case '/': case '=':
3902 return (c2 == c1 || c2 == '=');
3907 /* Expand a macro call.
3908 HP points to the symbol that is the macro being called.
3909 Put the result of expansion onto the input stack
3910 so that subsequent input by our caller will use it.
3912 If macro wants arguments, caller has already verified that
3913 an argument list follows; arguments come from the input stack. */
3916 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3919 DEFINITION *defn = hp->value.defn;
3927 size_t old_written = cpplib_getWritten (pfile);
3931 struct argdata *args = NULL;
3933 pfile->output_escapes++;
3934 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3935 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3937 nargs = defn->nargs;
3941 enum cpp_token token = CPP_EOF;
3943 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3945 for (i = 0; i < nargs; i++)
3947 args[i].expanded = 0;
3949 args[i].raw_length = 0;
3950 args[i].expand_length = args[i].stringified_length = -1;
3951 args[i].use_count = 0;
3955 ** Parse all the macro args that are supplied. I counts them.
3956 ** The first NARGS args are stored in ARGS.
3957 ** The rest are discarded. If rest_args is set then we assume
3958 ** macarg absorbed the rest of the args.
3964 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3972 if (i < nargs || (nargs == 0 && i == 0))
3974 /* if we are working on last arg which absorbs rest of args... */
3975 if (i == nargs - 1 && defn->rest_args)
3980 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3981 token = macarg (pfile, rest_args);
3982 args[i].raw_length = cpplib_getWritten (pfile) - args[i].raw;
3983 args[i].newlines = FALSE; /* FIXME */
3987 token = macarg (pfile, 0);
3990 if (token == CPP_EOF || token == CPP_POP)
3992 cppReader_errorWithLine (pfile, start_line, start_column,
3993 cstring_fromCharsNew ("unterminated macro call"));
3998 } while (token == CPP_COMMA);
4000 /* If we got one arg but it was just whitespace, call that 0 args. */
4008 bp = ARG_BASE + args[0].raw;
4009 lim = bp + args[0].raw_length;
4011 /* cpp.texi says for foo ( ) we provide one argument.
4012 However, if foo wants just 0 arguments, treat this as 0. */
4016 while (bp != lim && is_space[(int) *bp])
4026 /* Don't output an error message if we have already output one for
4027 a parse error above. */
4030 if (nargs == 0 && i > 0)
4032 cppReader_error (pfile,
4033 message ("arguments given to macro `%s'", hp->name));
4037 /* traditional C allows foo() if foo wants one argument. */
4038 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4042 /* the rest args token is allowed to absorb 0 tokens */
4043 else if (i == nargs - 1 && defn->rest_args)
4046 cppReader_error (pfile,
4047 message ("macro `%s' used without args", hp->name));
4049 cppReader_error (pfile,
4050 message ("macro `%s' used with just one arg", hp->name));
4053 cppReader_error (pfile,
4054 message ("macro `%s' used with only %d args",
4060 cppReader_error (pfile,
4061 message ("macro `%s' used with too many (%d) args", hp->name, i));
4070 ** If the agrument list was multiple lines, need to insert new lines to keep line
4071 ** numbers accurate.
4074 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4075 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4077 /* If macro wants zero args, we parsed the arglist for checking only.
4078 Read directly from the macro definition. */
4082 xbuf = defn->expansion;
4083 xbuf_len = defn->length;
4087 char *exp = defn->expansion;
4088 int offset; /* offset in expansion,
4089 copied a piece at a time */
4090 size_t totlen; /* total amount of exp buffer filled so far */
4092 register struct reflist *ap, *last_ap;
4094 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4096 /* Macro really takes args. Compute the expansion of this call. */
4098 /* Compute length in characters of the macro's expansion.
4099 Also count number of times each arg is used. */
4100 xbuf_len = defn->length;
4102 llassert (args != NULL);
4104 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4108 struct argdata *arg = &args[ap->argno];
4110 /* Stringify it it hasn't already been */
4113 if (arg->stringified_length < 0)
4115 int arglen = arg->raw_length;
4116 bool escaped = FALSE;
4117 char in_string = '\0';
4120 /* Initially need_space is -1. Otherwise, 1 means the
4121 previous character was a space, but we suppressed it;
4122 0 means the previous character was a non-space. */
4123 int need_space = -1;
4126 arg->stringified = cpplib_getWritten (pfile);
4127 if (!cppReader_isTraditional (pfile))
4128 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4129 for (; i < arglen; i++)
4131 c = (ARG_BASE + arg->raw)[i];
4133 if (in_string == '\0')
4135 /* Internal sequences of whitespace are replaced by
4136 one space except within an string or char token.*/
4137 if (is_space[(int) c])
4139 if (cpplib_getWritten (pfile) > arg->stringified
4140 && (cpplib_getPWritten (pfile))[-1] == '@')
4142 /* "@ " escape markers are removed */
4143 cppReader_adjustWritten (pfile, -1);
4144 /*@innercontinue@*/ continue;
4146 if (need_space == 0)
4148 /*@innercontinue@*/ continue;
4150 else if (need_space > 0)
4151 cppReader_putChar (pfile, ' ');
4167 if (in_string != '\0')
4172 else if (c == '\"' || c == '\'')
4182 /* Escape these chars */
4183 if (c == '\"' || (in_string != '\0' && c == '\\'))
4184 cppReader_putChar (pfile, '\\');
4186 cppReader_putChar (pfile, c);
4189 cpplib_reserve (pfile, 4);
4190 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4192 cppReader_adjustWritten (pfile, 4);
4195 if (!cppReader_isTraditional (pfile))
4196 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4197 arg->stringified_length
4198 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4201 xbuf_len += args[ap->argno].stringified_length;
4203 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4205 /* Add 4 for two newline-space markers to prevent
4206 token concatenation. */
4207 assertSet (args); /*@i534 shouldn't need this */
4208 xbuf_len += args[ap->argno].raw_length + 4;
4212 /* We have an ordinary (expanded) occurrence of the arg.
4213 So compute its expansion, if we have not already. */
4215 assertSet (args); /*@i534 shouldn't need this */
4217 if (args[ap->argno].expand_length < 0)
4219 args[ap->argno].expanded = cpplib_getWritten (pfile);
4220 cpp_expand_to_buffer (pfile,
4221 ARG_BASE + args[ap->argno].raw,
4222 size_fromInt (args[ap->argno].raw_length));
4224 args[ap->argno].expand_length
4225 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4228 /* Add 4 for two newline-space markers to prevent
4229 token concatenation. */
4230 xbuf_len += args[ap->argno].expand_length + 4;
4232 if (args[ap->argno].use_count < 10)
4233 args[ap->argno].use_count++;
4236 xbuf = (char *) dmalloc (xbuf_len + 1);
4240 ** Generate in XBUF the complete expansion
4241 ** with arguments substituted in.
4242 ** TOTLEN is the total size generated so far.
4243 ** OFFSET is the index in the definition
4244 ** of where we are copying from.
4250 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4251 last_ap = ap, ap = ap->next)
4253 register struct argdata *arg = &args[ap->argno];
4254 size_t count_before = totlen;
4256 /* Add chars to XBUF. */
4257 for (i = 0; i < ap->nchars; i++, offset++)
4259 xbuf[totlen++] = exp[offset];
4262 /* If followed by an empty rest arg with concatenation,
4263 delete the last run of nonwhite chars. */
4264 if (rest_zero && totlen > count_before
4265 && ((ap->rest_args && ap->raw_before)
4266 || (last_ap != NULL && last_ap->rest_args
4267 && last_ap->raw_after)))
4269 /* Delete final whitespace. */
4270 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4275 /* Delete the nonwhites before them. */
4276 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4282 if (ap->stringify != 0)
4285 memcpy (xbuf + totlen,
4286 ARG_BASE + arg->stringified,
4287 size_fromInt (arg->stringified_length));
4288 totlen += arg->stringified_length;
4290 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4297 p1 = ARG_BASE + arg->raw;
4298 l1 = p1 + arg->raw_length;
4302 while (p1 != l1 && is_space[(int) *p1])
4307 while (p1 != l1 && is_idchar[(int) *p1])
4309 xbuf[totlen++] = *p1++;
4312 /* Delete any no-reexpansion marker that follows
4313 an identifier at the beginning of the argument
4314 if the argument is concatenated with what precedes it. */
4315 if (p1[0] == '@' && p1[1] == '-')
4320 /* Arg is concatenated after: delete trailing whitespace,
4321 whitespace markers, and no-reexpansion markers. */
4324 if (is_space[(int) l1[-1]]) l1--;
4325 else if (l1[-1] == '-')
4328 /* If a `-' is preceded by an odd number of newlines then it
4329 and the last newline are a no-reexpansion marker. */
4330 while (p2 != p1 && p2[-1] == '\n')
4335 if (((l1 - 1 - p2) & 1) != 0)
4341 /*@innerbreak@*/ break;
4346 /*@innerbreak@*/ break;
4351 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4359 expanded = ARG_BASE + arg->expanded;
4361 if (!ap->raw_before && totlen > 0
4362 && (arg->expand_length != 0)
4363 && !cppReader_isTraditional(pfile)
4364 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4366 xbuf[totlen++] = '@';
4367 xbuf[totlen++] = ' ';
4370 memcpy (xbuf + totlen, expanded,
4371 size_fromInt (arg->expand_length));
4372 totlen += arg->expand_length;
4374 if (!ap->raw_after && totlen > 0
4375 && offset < size_toInt (defn->length)
4376 && !cppReader_isTraditional(pfile)
4377 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4379 xbuf[totlen++] = '@';
4380 xbuf[totlen++] = ' ';
4383 /* If a macro argument with newlines is used multiple times,
4384 then only expand the newlines once. This avoids creating
4385 output lines which don't correspond to any input line,
4386 which confuses gdb and gcov. */
4387 if (arg->use_count > 1 && arg->newlines > 0)
4389 /* Don't bother doing change_newlines for subsequent
4393 = change_newlines (expanded, arg->expand_length);
4397 if (totlen > xbuf_len)
4401 /* if there is anything left of the definition
4402 after handling the arg list, copy that in too. */
4404 for (i = offset; i < size_toInt (defn->length); i++)
4406 /* if we've reached the end of the macro */
4409 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4410 && last_ap->raw_after))
4411 xbuf[totlen++] = exp[i];
4414 xbuf[totlen] = '\0';
4418 pfile->output_escapes--;
4420 /* Now put the expansion on the input stack
4421 so our caller will commence reading from it. */
4422 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4424 if (end_line != start_line)
4426 /* xbuf must have enough newlines */
4427 int newlines = end_line - start_line;
4428 int foundnewlines = 0;
4429 char *xbufptr = xbuf;
4431 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4436 if (*xbufptr == '\0')
4442 if (foundnewlines < newlines)
4444 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4446 while (foundnewlines < newlines)
4448 newbuf = cstring_appendChar (newbuf, '\n');
4453 xbuf = cstring_toCharsSafe (newbuf);
4454 xbuf_len = cstring_length (newbuf);
4456 } /*@=branchstate@*/
4459 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4461 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4462 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4463 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4465 /* Pop the space we've used in the token_buffer for argument expansion. */
4466 cppReader_setWritten (pfile, old_written);
4467 DPRINTF (("Done set written"));
4469 /* Recursive macro use sometimes works traditionally.
4470 #define foo(x,y) bar (x (y,0), y)
4473 if (!cppReader_isTraditional (pfile))
4474 hp->type = T_DISABLED;
4480 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4481 /*@dependent@*/ hashNode hp)
4483 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4490 mbuf->cleanup = cppReader_macroCleanup;
4492 llassert (mbuf->hnode == NULL);
4495 /* The first chars of the expansion should be a "@ " added by
4496 collect_expansion. This is to prevent accidental token-pasting
4497 between the text preceding the macro invocation, and the macro
4500 We would like to avoid adding unneeded spaces (for the sake of
4501 tools that use cpp, such as imake). In some common cases we can
4502 tell that it is safe to omit the space.
4504 The character before the macro invocation cannot have been an
4505 idchar (or else it would have been pasted with the idchars of
4506 the macro name). Therefore, if the first non-space character
4507 of the expansion is an idchar, we do not need the extra space
4508 to prevent token pasting.
4510 Also, we don't need the extra space if the first char is '(',
4511 or some other (less common) characters. */
4513 if (xbuf[0] == '@' && xbuf[1] == ' '
4514 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4515 || xbuf[2] == '\"'))
4517 llassert (mbuf->cur != NULL);
4518 DPRINTF (("Eating: %c", xbuf[2]));
4525 /* Like cpplib_getToken, except that it does not read past end-of-line.
4526 Also, horizontal space is skipped, and macros are popped. */
4528 static enum cpp_token
4529 get_directive_token (cppReader *pfile)
4533 size_t old_written = cpplib_getWritten (pfile);
4534 enum cpp_token token;
4535 cppSkipHspace (pfile);
4536 if (cppReader_peekC (pfile) == '\n')
4541 token = cpplib_getToken (pfile);
4546 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4551 cppReader_setWritten (pfile, old_written);
4552 /*@switchbreak@*/ break;
4560 /* Handle #include and #import.
4561 This function expects to see "fname" or <fname> on the input.
4563 The input is normally in part of the output_buffer following
4564 cpplib_getWritten, and will get overwritten by output_line_command.
4565 I.e. in input file specification has been popped by cppReader_handleDirective.
4569 do_include (cppReader *pfile, struct directive *keyword,
4570 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4572 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4574 char *fbeg, *fend; /* Beginning and end of fname */
4575 enum cpp_token token;
4577 /* Chain of dirs to search */
4578 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4579 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4580 struct file_name_list *searchptr = NULL;
4581 size_t old_written = cpplib_getWritten (pfile);
4584 int f; /* file number */
4585 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4586 f= -1; /* JF we iz paranoid! */
4588 pfile->parsing_include_directive++;
4589 token = get_directive_token (pfile);
4590 pfile->parsing_include_directive--;
4592 if (token == CPP_STRING)
4594 /* FIXME - check no trailing garbage */
4595 fbeg = pfile->token_buffer + old_written + 1;
4596 fend = cpplib_getPWritten (pfile) - 1;
4597 if (fbeg[-1] == '<')
4600 /* If -I-, start with the first -I dir after the -I-. */
4601 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4602 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4604 /* If -I- was specified, don't search current dir, only spec'd ones. */
4605 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4607 cppBuffer *fp = CPPBUFFER (pfile);
4608 /* We have "filename". Figure out directory this source
4609 file is coming from and put it on the front of the list. */
4611 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4616 llassert (fp != NULL);
4620 if (cstring_isDefined (fp->nominal_fname))
4622 nam = cstring_toCharsSafe (fp->nominal_fname);
4624 /* Found a named file. Figure out dir of the file,
4625 and put it in front of the search list. */
4626 dsp[0].next = search_start;
4630 ep = strrchr (nam, CONNECTCHAR);
4632 ep = strrchr (nam, ']');
4633 if (ep == NULL) ep = strrchr (nam, '>');
4634 if (ep == NULL) ep = strrchr (nam, ':');
4635 if (ep != NULL) ep++;
4645 /*@-onlytrans@*/ /* This looks like a memory leak... */
4646 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4650 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
4651 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
4655 dsp[0].fname = cstring_undefined; /* Current directory */
4658 dsp[0].got_name_map = 0;
4669 else if (token == CPP_NAME)
4672 * Support '#include xyz' like VAX-C to allow for easy use of all the
4673 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4674 * code from case '<' is repeated here) and generates a warning.
4676 cppReader_warning (pfile,
4677 "VAX-C-style include specification found, use '#include <filename.h>' !");
4679 /* If -I-, start with the first -I dir after the -I-. */
4680 if (CPPOPTIONS (pfile)->first_bracket_include)
4681 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4682 fbeg = pfile->token_buffer + old_written;
4683 fend = cpplib_getPWritten (pfile);
4688 cppReader_error (pfile,
4689 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4692 cppReader_setWritten (pfile, old_written);
4693 cppReader_skipRestOfLine (pfile);
4699 token = get_directive_token (pfile);
4700 if (token != CPP_VSPACE)
4702 cppReader_errorLit (pfile,
4703 cstring_makeLiteralTemp ("Junk at end of #include"));
4705 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4707 token = get_directive_token (pfile);
4712 ** For #include_next, skip in the search path
4713 ** past the dir in which the containing file was found.
4718 cppBuffer *fp = CPPBUFFER (pfile);
4720 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4722 llassert (fp != NULL);
4724 if (fp->fname != NULL)
4726 /* fp->dir is null if the containing file was specified with
4727 an absolute file name. In that case, don't skip anything. */
4728 if (fp->dir == SELF_DIR_DUMMY)
4730 search_start = CPPOPTIONS (pfile)->include;
4732 else if (fp->dir != NULL)
4734 search_start = fp->dir->next;
4746 cppReader_setWritten (pfile, old_written);
4748 flen = size_fromInt (fend - fbeg);
4750 DPRINTF (("fbeg: %s", fbeg));
4754 cppReader_error (pfile,
4755 message ("Empty file name in #%s", keyword->name));
4760 ** Allocate this permanently, because it gets stored in the definitions
4764 fname = cstring_undefined;
4766 /* + 2 above for slash and terminating null. */
4767 /* + 2 added for '.h' on VMS (to support '#include filename') */
4769 /* If specified file name is absolute, just open it. */
4771 if (osd_isConnectChar (*fbeg)
4772 # if defined (WIN32) || defined (OS2)
4773 || (*(fbeg + 1) == ':')
4777 fname = cstring_copyLength (fbeg, flen);
4779 if (redundant_include_p (pfile, fname))
4781 cstring_free (fname);
4785 f = open_include_file (pfile, fname, NULL);
4787 if (f == IMPORT_FOUND)
4789 return 0; /* Already included this file */
4794 /* Search directory path, trying to open the file.
4795 Copy each filename tried into FNAME. */
4797 for (searchptr = search_start; searchptr != NULL;
4798 searchptr = searchptr->next)
4800 if (!cstring_isEmpty (searchptr->fname))
4802 /* The empty string in a search path is ignored.
4803 This makes it possible to turn off entirely
4804 a standard piece of the list. */
4805 if (cstring_isEmpty (searchptr->fname))
4808 fname = cstring_copy (searchptr->fname);
4809 fname = cstring_appendChar (fname, CONNECTCHAR);
4810 DPRINTF (("Here: %s", fname));
4817 fname = cstring_concatLength (fname, fbeg, flen);
4819 DPRINTF (("fname: %s", fname));
4821 /* Win32 directory fix from Kay Buschner. */
4822 #if defined (WIN32) || defined (OS2)
4823 /* Fix all unixdir slashes to win dir slashes */
4824 if (searchptr->fname && (searchptr->fname[0] != 0))
4826 cstring_replaceAll (fname, '/', '\\');
4831 /* Change this 1/2 Unix 1/2 VMS file specification into a
4832 full VMS file specification */
4833 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4834 /* Fix up the filename */
4835 hack_vms_include_specification (fname);
4837 /* This is a normal VMS filespec, so use it unchanged. */
4838 strncpy (fname, fbeg, flen);
4840 /* if it's '#include filename', add the missing .h */
4841 if (strchr (fname,'.') == NULL) {
4842 strcat (fname, ".h");
4846 /* ??? There are currently 3 separate mechanisms for avoiding processing
4847 of redundant include files: #import, #pragma once, and
4848 redundant_include_p. It would be nice if they were unified. */
4850 if (redundant_include_p (pfile, fname))
4852 cstring_free (fname);
4856 DPRINTF (("Trying: %s", fname));
4858 f = open_include_file (pfile, fname, searchptr);
4860 if (f == IMPORT_FOUND)
4862 return 0; /* Already included this file */
4865 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4867 cppReader_warning (pfile,
4868 message ("Header file %s exists, but is not readable", fname));
4881 /* A file that was not found. */
4882 fname = cstring_copyLength (fbeg, flen);
4884 if (search_start != NULL)
4886 cppReader_error (pfile,
4887 message ("Cannot find include file %s on search path: %x",
4889 searchPath_unparse (search_start)));
4893 cppReader_error (pfile,
4894 message ("No include path in which to find %s", fname));
4899 ** Check to see if this include file is a once-only include file.
4903 struct file_name_list *ptr;
4905 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4907 if (cstring_equal (ptr->fname, fname))
4909 /* This file was included before. */
4916 /* This is the first time for this file. */
4917 /* Add it to list of files included. */
4919 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4920 ptr->control_macro = NULL;
4921 ptr->c_system_include_path = NULL;
4922 ptr->next = pfile->all_include_files;
4924 ptr->got_name_map = NULL;
4926 DPRINTF (("Including file: %s", fname));
4927 pfile->all_include_files = ptr;
4928 assertSet (pfile->all_include_files);
4931 if (angle_brackets != 0)
4933 pfile->system_include_depth++;
4936 /* Actually process the file */
4937 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4939 cstring_free (fname);
4943 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4944 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4946 output_line_command (pfile, 0, enter_file);
4947 pfile->only_seen_white = 2;
4952 pfile->system_include_depth--;
4955 } /*@=branchstate@*/
4960 /* Return nonzero if there is no need to include file NAME
4961 because it has already been included and it contains a conditional
4962 to make a repeated include do nothing. */
4965 redundant_include_p (cppReader *pfile, cstring name)
4967 struct file_name_list *l = pfile->all_include_files;
4969 for (; l != NULL; l = l->next)
4971 if (cstring_equal (name, l->fname)
4972 && (l->control_macro != NULL)
4973 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4982 /* Return nonzero if the given FILENAME is an absolute pathname which
4983 designates a file within one of the known "system" include file
4984 directories. We assume here that if the given FILENAME looks like
4985 it is the name of a file which resides either directly in a "system"
4986 include file directory, or within any subdirectory thereof, then the
4987 given file must be a "system" include file. This function tells us
4988 if we should suppress pedantic errors/warnings for the given FILENAME.
4990 The value is 2 if the file is a C-language system header file
4991 for which C++ should (on most systems) assume `extern "C"'. */
4994 is_system_include (cppReader *pfile, cstring filename)
4996 struct file_name_list *searchptr;
4998 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
5000 searchptr = searchptr->next)
5002 if (!cstring_isEmpty (searchptr->fname))
5004 cstring sys_dir = searchptr->fname;
5005 size_t length = cstring_length (sys_dir);
5007 if (cstring_equalLen (sys_dir, filename, length)
5008 && osd_isConnectChar (cstring_getChar (filename, length)))
5010 if (searchptr->c_system_include_path)
5021 /* Convert a character string literal into a nul-terminated string.
5022 The input string is [IN ... LIMIT).
5023 The result is placed in RESULT. RESULT can be the same as IN.
5024 The value returned in the end of the string written to RESULT,
5025 or NULL on error. */
5027 static /*@null@*/ char *
5028 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5029 char *in, char *limit, int handle_escapes)
5049 /*@switchbreak@*/ break;
5053 char *bpc = (char *) in;
5054 int i = (char) cppReader_parseEscape (pfile, &bpc);
5057 *result++ = (char) c;
5058 /*@switchbreak@*/ break;
5072 * interpret #line command. Remembers previously seen fnames
5073 * in its very own hash table.
5076 /*@constant int FNAME_HASHSIZE@*/
5077 #define FNAME_HASHSIZE 37
5080 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5082 cppBuffer *ip = cppReader_getBuffer (pfile);
5084 size_t old_written = cpplib_getWritten (pfile);
5085 enum file_change_code file_change = same_file;
5086 enum cpp_token token;
5088 llassert (ip != NULL);
5089 token = get_directive_token (pfile);
5091 if (token != CPP_NUMBER
5092 || !isdigit(pfile->token_buffer[old_written]))
5094 cppReader_errorLit (pfile,
5095 cstring_makeLiteralTemp ("invalid format `#line' command"));
5097 goto bad_line_directive;
5100 /* The Newline at the end of this line remains to be processed.
5101 To put the next line at the specified line number,
5102 we must store a line number now that is one less. */
5103 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5104 cppReader_setWritten (pfile, old_written);
5106 /* NEW_LINENO is one less than the actual line number here. */
5107 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5108 cppReader_pedwarnLit (pfile,
5109 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5111 token = get_directive_token (pfile);
5113 if (token == CPP_STRING) {
5114 char *fname = pfile->token_buffer + old_written;
5116 static hashNode fname_table[FNAME_HASHSIZE];
5118 hashNode *hash_bucket;
5121 size_t fname_length;
5123 /* Turn the file name, which is a character string literal,
5124 into a null-terminated string. Do this in place. */
5125 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5126 if (end_name == NULL)
5128 cppReader_errorLit (pfile,
5129 cstring_makeLiteralTemp ("invalid format `#line' command"));
5130 goto bad_line_directive;
5133 fname_length = size_fromInt (end_name - fname);
5134 num_start = cpplib_getWritten (pfile);
5136 token = get_directive_token (pfile);
5137 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5138 p = pfile->token_buffer + num_start;
5139 if (cppReader_isPedantic (pfile))
5140 cppReader_pedwarnLit (pfile,
5141 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5143 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5145 cppReader_errorLit (pfile,
5146 cstring_makeLiteralTemp ("invalid format `#line' command"));
5147 goto bad_line_directive;
5150 file_change = enter_file;
5152 file_change = leave_file;
5154 ip->system_header_p = 1;
5155 else /* if (*p == 4) */
5156 ip->system_header_p = 2;
5158 cppReader_setWritten (pfile, num_start);
5159 token = get_directive_token (pfile);
5160 p = pfile->token_buffer + num_start;
5161 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5162 ip->system_header_p = *p == 3 ? 1 : 2;
5163 token = get_directive_token (pfile);
5165 if (token != CPP_VSPACE) {
5166 cppReader_errorLit (pfile,
5167 cstring_makeLiteralTemp ("invalid format `#line' command"));
5169 goto bad_line_directive;
5174 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5176 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5178 if (hp->length == fname_length)
5180 llassert (hp->value.cpval != NULL);
5182 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5184 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5191 /* Didn't find it; cons up a new one. */
5192 hp = (hashNode) dmalloc (sizeof (*hp));
5195 hp->bucket_hdr = NULL;
5197 hp->name = cstring_undefined;
5198 hp->next = *hash_bucket;
5202 hp->length = fname_length;
5203 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5204 memcpy (hp->value.cpval, fname, fname_length);
5205 hp->value.cpval[fname_length] = '\0';
5206 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5209 else if (token != CPP_VSPACE && token != CPP_EOF)
5211 cppReader_errorLit (pfile,
5212 cstring_makeLiteralTemp ("invalid format `#line' command"));
5213 goto bad_line_directive;
5220 ip->lineno = new_lineno;
5222 cppReader_skipRestOfLine (pfile);
5223 cppReader_setWritten (pfile, old_written);
5224 output_line_command (pfile, 0, file_change);
5229 * remove the definition of a symbol from the symbol table.
5230 * according to un*x /lib/cpp, it is not an error to undef
5231 * something that has no definitions, so it isn't one here either.
5235 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5240 char *orig_buf = buf;
5242 SKIP_WHITE_SPACE (buf);
5244 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5246 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5248 /* If we are generating additional info for debugging (with -g) we
5249 need to pass through all effective #undef commands. */
5250 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5252 pass_thru_directive (orig_buf, limit, pfile, keyword);
5255 if (hp->type != T_MACRO)
5257 cppReader_warning (pfile,
5258 message ("Undefining preprocessor builtin: %s",
5262 cppReader_deleteMacro (hp);
5265 if (cppReader_isPedantic (pfile)) {
5267 SKIP_WHITE_SPACE (buf);
5270 cppReader_pedwarnLit (pfile,
5271 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5280 * Report an error detected by the program we are processing.
5281 * Use the text of the line in the error message.
5282 * (We use error because it prints the filename & line#.)
5286 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5287 char *buf, char *limit)
5289 size_t length = size_fromInt (limit - buf);
5290 cstring copy = cstring_copyLength (buf, length);
5291 cstring adv = cstring_advanceWhiteSpace (copy);
5293 cppReader_error (pfile, message ("#error %s", adv));
5294 cstring_free (copy);
5299 * Report a warning detected by the program we are processing.
5300 * Use the text of the line in the warning message, then continue.
5301 * (We use error because it prints the filename & line#.)
5305 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5306 char *buf, char *limit)
5308 size_t length = size_fromInt (limit - buf);
5309 cstring copy = cstring_copyLength (buf, length);
5310 cstring adv = cstring_advanceWhiteSpace (copy);
5311 cppReader_warning (pfile, message ("#warning %s", adv));
5312 cstring_free (copy);
5317 /* #ident has already been copied to the output file, so just ignore it. */
5320 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5321 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5323 /* Allow #ident in system headers, since that's not user's fault. */
5324 if (cppReader_isPedantic (pfile)
5325 && !cppReader_getBufferSafe (pfile)->system_header_p)
5326 cppReader_pedwarnLit (pfile,
5327 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5329 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5334 /* #pragma and its argument line have already been copied to the output file.
5335 Just check for some recognized pragmas that need validation here. */
5338 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5339 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5341 while (*buf == ' ' || *buf == '\t')
5346 if (!strncmp (buf, "implementation", 14)) {
5347 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5348 been included yet. */
5349 struct file_name_list *ptr;
5350 char *p = buf + 14, *fname, *inc_fname;
5352 SKIP_WHITE_SPACE (p);
5353 if (*p == '\n' || *p != '\"')
5357 p = (char *) strchr (fname, '\"');
5358 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5360 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5362 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5363 inc_fname = (inc_fname != NULL)
5364 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5366 if ((inc_fname != NULL)
5367 && (strncmp (inc_fname, fname, fname_len) == 0))
5369 cpp_setLocation (pfile);
5371 ppllerror (message ("`#pragma implementation' for `%s' appears "
5372 "after file is included",
5373 cstring_fromChars (fname)));
5382 * handle #if command by
5383 * 1) inserting special `defined' keyword into the hash table
5384 * that gets turned into 0 or 1 by special_symbol (thus,
5385 * if the luser has a symbol called `defined' already, it won't
5386 * work inside the #if command)
5387 * 2) rescan the input into a temporary output buffer
5388 * 3) pass the output buffer to the yacc parser and collect a value
5389 * 4) clean up the mess left from steps 1 and 2.
5390 * 5) call conditional_skip to skip til the next #endif (etc.),
5391 * or not, depending on the value from step 3.
5395 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5396 char *buf, char *limit)
5398 HOST_WIDE_INT value;
5399 DPRINTF (("Do if: %s", buf));
5400 value = eval_if_expression (pfile, buf, limit - buf);
5401 conditional_skip (pfile, value == 0, T_IF, NULL);
5406 * handle a #elif directive by not changing if_stack either.
5407 * see the comment above do_else.
5410 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5411 char *buf, char *limit)
5413 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5415 cppReader_errorLit (pfile,
5416 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5421 llassert (pfile->if_stack != NULL);
5423 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5425 cppReader_errorLit (pfile,
5426 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5428 if (pfile->if_stack->fname != NULL
5429 && cppReader_getBufferSafe (pfile)->fname != NULL
5430 && !cstring_equal (pfile->if_stack->fname,
5431 cppReader_getBufferSafe (pfile)->nominal_fname))
5432 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5433 fprintf (stderr, ")\n");
5435 pfile->if_stack->type = T_ELIF;
5438 if (pfile->if_stack->if_succeeded)
5440 skip_if_group (pfile, 0);
5444 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5446 skip_if_group (pfile, 0);
5449 ++pfile->if_stack->if_succeeded; /* continue processing input */
5450 output_line_command (pfile, 1, same_file);
5458 * evaluate a #if expression in BUF, of length LENGTH,
5459 * then parse the result as a C expression and return the value as an int.
5462 static HOST_WIDE_INT
5463 eval_if_expression (cppReader *pfile,
5464 /*@unused@*/ char *buf,
5465 /*@unused@*/ int length)
5467 hashNode save_defined;
5468 HOST_WIDE_INT value;
5469 size_t old_written = cpplib_getWritten (pfile);
5471 DPRINTF (("Saving defined..."));
5472 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5473 pfile->pcp_inside_if = 1;
5475 value = cppReader_parseExpression (pfile);
5476 pfile->pcp_inside_if = 0;
5478 /* Clean up special symbol */
5479 DPRINTF (("Removing defined..."));
5480 cppReader_deleteMacro (save_defined);
5481 cppReader_setWritten (pfile, old_written); /* Pop */
5487 * routine to handle ifdef/ifndef. Try to look up the symbol,
5488 * then do or don't skip to the #endif/#else/#elif depending
5489 * on what directive is actually being processed.
5493 do_xifdef (cppReader *pfile, struct directive *keyword,
5494 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5497 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5499 size_t ident_length;
5500 enum cpp_token token;
5501 int start_of_file = 0;
5502 char *control_macro = 0;
5503 size_t old_written = cpplib_getWritten (pfile);
5505 DPRINTF (("do xifdef: %d",
5506 keyword->type == T_IFNDEF));
5508 /* Detect a #ifndef at start of file (not counting comments). */
5509 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5511 start_of_file = pfile->only_seen_white == 2;
5514 pfile->no_macro_expand++;
5515 token = get_directive_token (pfile);
5516 pfile->no_macro_expand--;
5518 ident = pfile->token_buffer + old_written;
5519 DPRINTF (("Ident: %s", ident));
5521 ident_length = cpplib_getWritten (pfile) - old_written;
5522 cppReader_setWritten (pfile, old_written); /* Pop */
5524 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5526 skip = (keyword->type == T_IFDEF);
5527 if (! cppReader_isTraditional (pfile))
5529 cppReader_pedwarn (pfile,
5530 message ("`#%s' with no argument", keyword->name));
5533 else if (token == CPP_NAME)
5535 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5537 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5539 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5541 if (start_of_file && !skip)
5543 DPRINTF (("Not skipping!"));
5544 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
5545 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
5550 skip = (keyword->type == T_IFDEF);
5551 if (! cppReader_isTraditional (pfile))
5553 cppReader_error (pfile,
5554 message ("`#%s' with invalid argument", keyword->name));
5558 if (!cppReader_isTraditional (pfile))
5561 cppSkipHspace (pfile);
5562 c = cppReader_peekC (pfile);
5563 if (c != EOF && c != '\n')
5565 cppReader_pedwarn (pfile,
5566 message ("garbage at end of `#%s' argument", keyword->name));
5570 cppReader_skipRestOfLine (pfile);
5572 DPRINTF (("Conditional skip: %d", skip));
5573 conditional_skip (pfile, skip, T_IF, control_macro);
5577 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5578 If this is a #ifndef starting at the beginning of a file,
5579 CONTROL_MACRO is the macro name tested by the #ifndef.
5580 Otherwise, CONTROL_MACRO is 0. */
5583 conditional_skip (cppReader *pfile, int skip,
5584 enum node_type type,
5585 /*@dependent@*/ char *control_macro)
5587 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5589 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5590 temp->next = pfile->if_stack;
5591 temp->control_macro = control_macro;
5593 temp->if_succeeded = 0;
5595 pfile->if_stack = temp;
5596 pfile->if_stack->type = type;
5600 skip_if_group (pfile, 0);
5605 ++pfile->if_stack->if_succeeded;
5606 output_line_command (pfile, 1, same_file);
5611 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5612 * leaves input ptr at the sharp sign found.
5613 * If ANY is nonzero, return at next directive of any sort.
5617 skip_if_group (cppReader *pfile, int any)
5620 struct directive *kt;
5621 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5622 register int ident_length;
5624 struct parse_marker line_start_mark;
5626 parseSetMark (&line_start_mark, pfile);
5628 if (CPPOPTIONS (pfile)->output_conditionals) {
5629 static char failed[] = "#failed\n";
5630 cppReader_puts (pfile, failed, sizeof(failed)-1);
5632 output_line_command (pfile, 1, same_file);
5636 if (CPPOPTIONS (pfile)->output_conditionals)
5638 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5641 llassert (pbuf->buf != NULL);
5643 start_line = pbuf->buf + line_start_mark.position;
5644 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5647 parseMoveMark (&line_start_mark, pfile);
5649 if (!cppReader_isTraditional (pfile))
5651 cppSkipHspace (pfile);
5654 c = cppReader_getC (pfile);
5657 size_t old_written = cpplib_getWritten (pfile);
5658 cppSkipHspace (pfile);
5660 parse_name (pfile, cppReader_getC (pfile));
5661 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5662 ident = pfile->token_buffer + old_written;
5663 pfile->limit = ident;
5665 for (kt = directive_table; kt->length >= 0; kt++)
5667 cppIfStackFrame *temp;
5668 if (ident_length == kt->length
5669 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5671 /* If we are asked to return on next directive, do so now. */
5682 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5683 temp->next = pfile->if_stack;
5684 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5685 temp->type = kt->type;
5687 temp->if_succeeded = 0;
5688 temp->control_macro = NULL;
5690 pfile->if_stack = temp;
5691 /*@switchbreak@*/ break;
5694 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5695 validate_else (pfile,
5696 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5699 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5701 cppReader_error (pfile,
5702 message ("Preprocessor command #%s is not within a conditional", kt->name));
5703 /*@switchbreak@*/ break;
5705 else if (pfile->if_stack == save_if_stack)
5707 goto done; /* found what we came for */
5714 if (kt->type != T_ENDIF)
5716 llassert (pfile->if_stack != NULL);
5718 if (pfile->if_stack->type == T_ELSE)
5720 cppReader_errorLit (pfile,
5721 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5724 pfile->if_stack->type = kt->type;
5725 /*@switchbreak@*/ break;
5728 temp = pfile->if_stack;
5729 llassert (temp != NULL);
5730 pfile->if_stack = temp->next;
5732 /*@switchbreak@*/ break;
5735 #if defined (OS2) && defined (__IBMC__)
5736 /* Dummy code to eleminate optimization problems with icc */
5745 /* Don't let erroneous code go by. */
5747 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5748 && cppReader_isPedantic (pfile))
5750 cppReader_pedwarnLit (pfile,
5751 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5755 c = cppReader_getC (pfile);
5757 /* We're in the middle of a line. Skip the rest of it. */
5765 case '/': /* possible comment */
5766 c = skip_comment (pfile, NULL);
5769 /*@switchbreak@*/ break;
5772 cppReader_forward (pfile, -1);
5773 old = cpplib_getWritten (pfile);
5774 (void) cpplib_getToken (pfile);
5775 cppReader_setWritten (pfile, old);
5776 /*@switchbreak@*/ break;
5778 /* Char after backslash loses its special meaning. */
5779 if (cppReader_peekC (pfile) == '\n')
5781 cppReader_forward (pfile, 1);
5784 /*@switchbreak@*/ break;
5788 c = cppReader_getC (pfile);
5791 if (CPPOPTIONS (pfile)->output_conditionals) {
5792 static char end_failed[] = "#endfailed\n";
5793 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5796 pfile->only_seen_white = 1;
5798 parseGotoMark (&line_start_mark, pfile);
5799 parseClearMark (&line_start_mark);
5803 * handle a #else directive. Do this by just continuing processing
5804 * without changing if_stack ; this is so that the error message
5805 * for missing #endif's etc. will point to the original #if. It
5806 * is possible that something different would be better.
5810 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5811 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5813 if (cppReader_isPedantic (pfile))
5815 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5818 cppReader_skipRestOfLine (pfile);
5820 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5821 cppReader_errorLit (pfile,
5822 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5825 /* #ifndef can't have its special treatment for containing the whole file
5826 if it has a #else clause. */
5828 llassert (pfile->if_stack != NULL);
5830 pfile->if_stack->control_macro = 0;
5832 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5834 cpp_setLocation (pfile);
5835 genppllerrorhint (FLG_PREPROC,
5836 message ("Pre-processor directive #else after #else"),
5837 message ("%q: Location of match",
5838 fileloc_unparseRaw (pfile->if_stack->fname,
5839 pfile->if_stack->lineno)));
5842 pfile->if_stack->type = T_ELSE;
5845 if (pfile->if_stack->if_succeeded)
5846 skip_if_group (pfile, 0);
5848 ++pfile->if_stack->if_succeeded; /* continue processing input */
5849 output_line_command (pfile, 1, same_file);
5856 * unstack after #endif command
5860 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5861 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5863 if (cppReader_isPedantic (pfile))
5865 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5868 cppReader_skipRestOfLine (pfile);
5870 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5872 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5876 cppIfStackFrame *temp = pfile->if_stack;
5878 llassert (temp != NULL);
5880 pfile->if_stack = temp->next;
5881 if (temp->control_macro != 0)
5883 /* This #endif matched a #ifndef at the start of the file.
5884 See if it is at the end of the file. */
5885 struct parse_marker start_mark;
5888 parseSetMark (&start_mark, pfile);
5892 cppSkipHspace (pfile);
5893 c = cppReader_getC (pfile);
5899 parseGotoMark (&start_mark, pfile);
5900 parseClearMark (&start_mark);
5904 /* If we get here, this #endif ends a #ifndef
5905 that contains all of the file (aside from whitespace).
5906 Arrange not to include the file again
5907 if the macro that was tested is defined.
5909 Do not do this for the top-level file in a -include or any
5910 file in a -imacros. */
5911 struct file_name_list *ifile = pfile->all_include_files;
5913 for ( ; ifile != NULL; ifile = ifile->next)
5915 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5917 ifile->control_macro = temp->control_macro;
5925 output_line_command (pfile, 1, same_file);
5930 /* When an #else or #endif is found while skipping failed conditional,
5931 if -pedantic was specified, this is called to warn about text after
5932 the command name. P points to the first char after the command name. */
5935 validate_else (cppReader *pfile, cstring directive)
5938 cppSkipHspace (pfile);
5939 c = cppReader_peekC (pfile);
5940 if (c != EOF && c != '\n')
5942 cppReader_pedwarn (pfile,
5943 message ("text following `%s' violates ANSI standard", directive));
5948 ** Get the next token, and add it to the text in pfile->token_buffer.
5949 ** Return the kind of token we got.
5953 cpplib_getToken (cppReader *pfile)
5955 return cpplib_getTokenAux (pfile, FALSE);
5959 cpplib_getTokenForceExpand (cppReader *pfile)
5961 return cpplib_getTokenAux (pfile, TRUE);
5965 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5968 size_t old_written = 0;
5969 int start_line, start_column;
5970 enum cpp_token token;
5971 struct cppOptions *opts = CPPOPTIONS (pfile);
5972 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5975 c = cppReader_getC (pfile);
5976 DPRINTF (("Get next token: %c", c));
5981 if (cppReader_getBufferSafe (pfile)->seen_eof)
5983 cppBuffer *buf = cppReader_popBuffer (pfile);
5985 if (buf != cppReader_nullBuffer (pfile))
5996 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
5997 cppReader_getBufferSafe (pfile)->seen_eof = 1;
5999 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
6000 && next_buf != cppReader_nullBuffer (pfile))
6002 /* We're about to return from an #include file.
6003 Emit #line information now (as part of the CPP_POP) result.
6004 But the #line refers to the file we will pop to. */
6005 cppBuffer *cur_buffer = CPPBUFFER (pfile);
6006 CPPBUFFER (pfile) = next_buf;
6007 pfile->input_stack_listing_current = 0;
6008 output_line_command (pfile, 0, leave_file);
6009 CPPBUFFER (pfile) = cur_buffer;
6017 struct parse_marker start_mark;
6022 if (cppReader_peekC (pfile) == '=')
6027 if (opts->put_out_comments)
6029 parseSetMark (&start_mark, pfile);
6033 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6034 &start_line, &start_column);
6035 c = skip_comment (pfile, &newlines);
6036 DPRINTF (("c = %c", c));
6037 if (opts->put_out_comments && (c == '/' || c == EOF))
6039 assertSet (&start_mark);
6040 parseClearMark (&start_mark);
6047 cppReader_errorWithLine (pfile, start_line, start_column,
6048 cstring_makeLiteral ("Unterminated comment"));
6051 c = '/'; /* Initial letter of comment. */
6053 /* Comments are equivalent to spaces.
6054 For -traditional, a comment is equivalent to nothing. */
6056 if (opts->put_out_comments)
6060 assertSet (&start_mark);
6061 res = cpp_handleComment (pfile, &start_mark);
6062 pfile->lineno += newlines;
6065 else if (cppReader_isTraditional (pfile))
6071 cpplib_reserve(pfile, 1);
6072 cppReader_putCharQ (pfile, ' ');
6077 if (!pfile->only_seen_white)
6082 if (cppReader_handleDirective (pfile))
6084 return CPP_DIRECTIVE;
6087 pfile->only_seen_white = 0;
6092 /* A single quoted string is treated like a double -- some
6093 programs (e.g., troff) are perverse this way */
6094 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6095 &start_line, &start_column);
6096 old_written = cpplib_getWritten (pfile);
6098 DPRINTF (("Put char: %c", c));
6099 cppReader_putChar (pfile, c);
6102 int cc = cppReader_getC (pfile);
6103 DPRINTF (("cc: %c", c));
6106 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6108 /* try harder: this string crosses a macro expansion
6109 boundary. This can happen naturally if -traditional.
6110 Otherwise, only -D can make a macro with an unmatched
6113 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6114 (*cppReader_getBufferSafe (pfile)->cleanup)
6115 (cppReader_getBufferSafe (pfile), pfile);
6116 CPPBUFFER (pfile) = next_buf;
6119 if (!cppReader_isTraditional (pfile))
6121 cpp_setLocation (pfile);
6123 setLine (long_toInt (start_line));
6124 setColumn (long_toInt (start_column));
6126 if (pfile->multiline_string_line != long_toInt (start_line)
6127 && pfile->multiline_string_line != 0)
6131 message ("Unterminated string or character constant"),
6132 message ("%q: Possible real start of unterminated constant",
6134 (fileloc_filename (g_currentloc),
6135 pfile->multiline_string_line)));
6136 pfile->multiline_string_line = 0;
6142 message ("Unterminated string or character constant"));
6145 /*@loopbreak@*/ break;
6147 DPRINTF (("putting char: %c", cc));
6148 cppReader_putChar (pfile, cc);
6152 /* Traditionally, end of line ends a string constant with
6153 no error. So exit the loop and record the new line. */
6154 if (cppReader_isTraditional (pfile))
6160 if (cppReader_isPedantic (pfile)
6161 && pfile->multiline_string_line == 0)
6163 cppReader_pedwarnWithLine
6164 (pfile, long_toInt (start_line),
6165 long_toInt (start_column),
6166 cstring_makeLiteral ("String constant runs past end of line"));
6168 if (pfile->multiline_string_line == 0)
6170 pfile->multiline_string_line = start_line;
6173 /*@switchbreak@*/ break;
6176 cc = cppReader_getC (pfile);
6179 /* Backslash newline is replaced by nothing at all. */
6180 cppReader_adjustWritten (pfile, -1);
6185 /* ANSI stupidly requires that in \\ the second \
6186 is *not* prevented from combining with a newline. */
6189 cppReader_putChar (pfile, cc);
6191 /*@switchbreak@*/ break;
6197 /*@switchbreak@*/ break;
6201 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6202 cpplib_getPWritten (pfile));
6203 pfile->only_seen_white = 0;
6204 return c == '\'' ? CPP_CHAR : CPP_STRING;
6207 if (!opts->dollars_in_ident)
6212 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6220 c2 = cppReader_peekC (pfile);
6221 if (c2 == c || c2 == '=')
6231 if (cppReader_peekC (pfile) == '=')
6237 c2 = cppReader_peekC (pfile);
6238 if (c2 == '-' && opts->chill)
6240 /* Chill style comment */
6241 if (opts->put_out_comments)
6243 parseSetMark (&start_mark, pfile);
6246 cppReader_forward (pfile, 1); /* Skip second '-'. */
6250 c = cppReader_getC (pfile);
6252 /*@loopbreak@*/ break;
6255 /* Don't consider final '\n' to be part of comment. */
6256 cppReader_forward (pfile, -1);
6257 /*@loopbreak@*/ break;
6261 goto return_comment;
6263 if (c2 == '-' || c2 == '=' || c2 == '>')
6268 if (pfile->parsing_include_directive)
6272 cppReader_putChar (pfile, c);
6274 /*@loopbreak@*/ break;
6275 c = cppReader_getC (pfile);
6277 if (c == '\n' || c == EOF)
6279 cppReader_errorLit (pfile,
6280 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6281 /*@loopbreak@*/ break;
6289 c2 = cppReader_peekC (pfile);
6294 cppReader_forward (pfile, 1);
6295 cpplib_reserve (pfile, 4);
6296 cppReader_putChar (pfile, c);
6297 cppReader_putChar (pfile, c2);
6299 c3 = cppReader_peekC (pfile);
6301 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6302 cppReader_nullTerminateQ (pfile);
6303 pfile->only_seen_white = 0;
6307 DPRINTF (("Macro @!"));
6308 if (cppReader_getBufferSafe (pfile)->has_escapes)
6310 c = cppReader_getC (pfile);
6311 DPRINTF (("got c: %c", c));
6314 if (pfile->output_escapes)
6315 cppReader_puts (pfile, "@-", 2);
6316 parse_name (pfile, cppReader_getC (pfile));
6319 else if (is_space [c])
6321 cpplib_reserve (pfile, 2);
6322 if (pfile->output_escapes)
6323 cppReader_putCharQ (pfile, '@');
6324 cppReader_putCharQ (pfile, c);
6332 if (pfile->output_escapes)
6334 cppReader_puts (pfile, "@@", 2);
6340 c2 = cppReader_peekC (pfile);
6343 cpplib_reserve(pfile, 2);
6344 cppReader_putCharQ (pfile, '.');
6345 c = cppReader_getC (pfile);
6349 /* FIXME - misses the case "..\\\n." */
6350 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6352 cpplib_reserve(pfile, 4);
6353 cppReader_putCharQ (pfile, '.');
6354 cppReader_putCharQ (pfile, '.');
6355 cppReader_putCharQ (pfile, '.');
6356 cppReader_forward (pfile, 2);
6357 cppReader_nullTerminateQ (pfile);
6358 pfile->only_seen_white = 0;
6364 pfile->only_seen_white = 0;
6366 cpplib_reserve(pfile, 3);
6367 cppReader_putCharQ (pfile, c);
6368 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6369 cppReader_nullTerminateQ (pfile);
6374 c2 = cppReader_peekC (pfile);
6375 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6377 cppReader_putChar (pfile, c);
6378 c = cppReader_getC (pfile);
6383 case '0': case '1': case '2': case '3': case '4':
6384 case '5': case '6': case '7': case '8': case '9':
6389 cpplib_reserve (pfile, 2);
6390 cppReader_putCharQ (pfile, c);
6392 c = cppReader_peekC (pfile);
6394 /*@loopbreak@*/ break;
6395 if (!is_idchar[c] && c != '.'
6396 && ((c2 != 'e' && c2 != 'E'
6397 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6398 || (c != '+' && c != '-')))
6399 /*@loopbreak@*/ break;
6400 cppReader_forward (pfile, 1);
6404 cppReader_nullTerminateQ (pfile);
6405 pfile->only_seen_white = 0;
6408 case 'b': case 'c': case 'd': case 'h': case 'o':
6409 case 'B': case 'C': case 'D': case 'H': case 'O':
6410 if (opts->chill && cppReader_peekC (pfile) == '\'')
6412 pfile->only_seen_white = 0;
6413 cpplib_reserve (pfile, 2);
6414 cppReader_putCharQ (pfile, c);
6415 cppReader_putCharQ (pfile, '\'');
6416 cppReader_forward (pfile, 1);
6419 c = cppReader_getC (pfile);
6421 goto chill_number_eof;
6424 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6426 cppReader_forward (pfile, 2);
6429 /*@loopbreak@*/ break;
6431 cppReader_putChar (pfile, c);
6435 cpplib_reserve (pfile, 2);
6436 cppReader_putCharQ (pfile, c);
6437 cppReader_nullTerminateQ (pfile);
6442 cppReader_forward (pfile, -1);
6444 cppReader_nullTerminate (pfile);
6451 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6452 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6453 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6454 case 'x': case 'y': case 'z':
6455 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6456 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6457 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6463 size_t before_name_written = cpplib_getWritten (pfile);
6465 parse_name (pfile, c);
6466 pfile->only_seen_white = 0;
6468 if (pfile->no_macro_expand)
6470 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6474 ident = pfile->token_buffer + before_name_written;
6475 DPRINTF (("Ident: %s", ident));
6477 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6479 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6483 DPRINTF (("No expand: %s %d", ident, ident_len));
6487 if (hp->type == T_DISABLED)
6489 DPRINTF (("Disabled!"));
6491 if (pfile->output_escapes)
6492 { /* Return "@-IDENT", followed by '\0'. */
6494 cpplib_reserve (pfile, 3);
6495 ident = pfile->token_buffer + before_name_written;
6496 cppReader_adjustWritten (pfile, 2);
6498 for (i = size_toInt (ident_len); i >= 0; i--)
6500 ident[i+2] = ident[i];
6510 ** If macro wants an arglist, verify that a '(' follows.
6511 ** first skip all whitespace, copying it to the output
6512 ** after the macro name. Then, if there is no '(',
6513 ** decide this is not a macro call and leave things that way.
6516 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6518 struct parse_marker macro_mark;
6521 DPRINTF (("Arglist macro!"));
6524 ** evans 2002-07-03: Moved this here (from below).
6525 ** This bug caused necessary whitespace to be lost
6526 ** when parsing parameterized macros without parameters.
6529 parseSetMark (¯o_mark, pfile);
6531 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6533 cppBuffer *next_buf;
6534 cppSkipHspace (pfile);
6535 if (cppReader_peekC (pfile) != EOF)
6537 DPRINTF (("Peeking!"));
6538 /*@loopbreak@*/ break;
6541 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6542 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6543 CPPBUFFER (pfile) = next_buf;
6546 /* parseSetMark (¯o_mark, pfile); */
6550 cppSkipHspace (pfile);
6551 c = cppReader_peekC (pfile);
6552 DPRINTF (("c: %c", c));
6553 is_macro_call = c == '(';
6555 /*@loopbreak@*/ break;
6556 cppReader_forward (pfile, 1);
6561 parseGotoMark (¯o_mark, pfile);
6564 parseClearMark (¯o_mark);
6568 DPRINTF (("not macro call!"));
6573 /* This is now known to be a macro call. */
6575 /* it might not actually be a macro. */
6576 if (hp->type != T_MACRO)
6581 cppReader_setWritten (pfile, before_name_written);
6582 special_symbol (hp, pfile);
6583 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6584 xbuf = (char *) dmalloc (xbuf_len + 1);
6585 cppReader_setWritten (pfile, before_name_written);
6586 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6587 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6592 ** Expand the macro, reading arguments as needed,
6593 ** and push the expansion on the input stack.
6596 cpplib_macroExpand (pfile, hp);
6597 cppReader_setWritten (pfile, before_name_written);
6600 /* An extra "@ " is added to the end of a macro expansion
6601 to prevent accidental token pasting. We prefer to avoid
6602 unneeded extra spaces (for the sake of cpp-using tools like
6603 imake). Here we remove the space if it is safe to do so. */
6605 llassert (pfile->buffer->rlimit != NULL);
6607 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6608 && pfile->buffer->rlimit[-2] == '@'
6609 && pfile->buffer->rlimit[-1] == ' ')
6611 int c1 = pfile->buffer->rlimit[-3];
6612 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6614 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6615 pfile->buffer->rlimit -= 2;
6621 case ' ': case '\t': case '\v': case '\r':
6624 cppReader_putChar (pfile, c);
6625 c = cppReader_peekC (pfile);
6626 if (c == EOF || !is_hor_space[c])
6627 /*@loopbreak@*/ break;
6628 cppReader_forward (pfile, 1);
6633 c2 = cppReader_peekC (pfile);
6640 cppReader_putChar (pfile, c);
6641 if (pfile->only_seen_white == 0)
6642 pfile->only_seen_white = 1;
6644 output_line_command (pfile, 1, same_file);
6647 case '(': token = CPP_LPAREN; goto char1;
6648 case ')': token = CPP_RPAREN; goto char1;
6649 case '{': token = CPP_LBRACE; goto char1;
6650 case '}': token = CPP_RBRACE; goto char1;
6651 case ',': token = CPP_COMMA; goto char1;
6652 case ';': token = CPP_SEMICOLON; goto char1;
6658 pfile->only_seen_white = 0;
6659 cppReader_putChar (pfile, c);
6668 /* Parse an identifier starting with C. */
6671 parse_name (cppReader *pfile, int c)
6677 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6679 cppReader_forward (pfile, 2);
6683 cppReader_forward (pfile, -1);
6687 if (c == '$' && cppReader_isPedantic (pfile))
6689 cppReader_pedwarnLit (pfile,
6690 cstring_makeLiteralTemp ("`$' in identifier"));
6693 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6694 cppReader_putCharQ (pfile, c);
6695 c = cppReader_getC (pfile);
6701 cppReader_nullTerminateQ (pfile);
6704 /* The file_name_map structure holds a mapping of file names for a
6705 particular directory. This mapping is read from the file named
6706 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6707 map filenames on a file system with severe filename restrictions,
6708 such as DOS. The format of the file name map file is just a series
6709 of lines with two tokens on each line. The first token is the name
6710 to map, and the second token is the actual name to use. */
6712 struct file_name_map
6714 struct file_name_map *map_next;
6719 /*@constant observer char *FILE_NAME_MAP_FILE*/
6720 #define FILE_NAME_MAP_FILE "header.gcc"
6722 /* Read a space delimited string of unlimited length from a stdio
6725 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6731 set = alloc = dmalloc (len + 1);
6736 while ((ch = getc (f)) != EOF && ! is_space[ch])
6738 if (set - alloc == size_toInt (len))
6741 alloc = drealloc (alloc, len + 1);
6742 set = alloc + len / 2;
6743 /*@-branchstate@*/ }
6746 } /*@=branchstate@*/
6749 check (ungetc (ch, f) != EOF);
6751 return cstring_fromChars (alloc);
6754 /* This structure holds a linked list of file name maps, one per directory. */
6756 struct file_name_map_list
6758 /*@only@*/ struct file_name_map_list *map_list_next;
6759 /*@only@*/ cstring map_list_name;
6760 /*@null@*/ struct file_name_map *map_list_map;
6763 /* Read the file name map file for DIRNAME. */
6765 static struct file_name_map *
6766 read_name_map (cppReader *pfile, cstring dirname)
6768 struct file_name_map_list *map_list_ptr;
6772 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6773 map_list_ptr != NULL;
6774 map_list_ptr = map_list_ptr->map_list_next)
6776 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6778 return map_list_ptr->map_list_map;
6782 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6783 map_list_ptr->map_list_name = cstring_copy (dirname);
6784 map_list_ptr->map_list_map = NULL;
6786 name = cstring_copy (dirname);
6788 if (cstring_length (dirname) > 0)
6790 name = cstring_appendChar (name, CONNECTCHAR);
6793 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6795 f = fileTable_openReadFile (context_fileTable (), name);
6796 cstring_free (name);
6800 map_list_ptr->map_list_map = NULL;
6806 while ((ch = getc (f)) != EOF)
6809 struct file_name_map *ptr;
6816 from = read_filename_string (ch, f);
6817 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6822 to = read_filename_string (ch, f);
6824 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6825 ptr->map_from = from;
6827 /* Make the real filename absolute. */
6828 if (cstring_length (to) > 1
6829 && osd_isConnectChar (cstring_firstChar (to)))
6835 ptr->map_to = cstring_copy (dirname);
6836 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6837 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6840 ptr->map_next = map_list_ptr->map_list_map;
6841 map_list_ptr->map_list_map = ptr;
6843 while ((ch = getc (f)) != '\n')
6847 /*@innerbreak@*/ break;
6852 assertSet (map_list_ptr->map_list_map);
6853 check (fileTable_closeFile (context_fileTable (),f) == 0);
6856 map_list_ptr->map_list_next = pfile->opts->map_list;
6857 pfile->opts->map_list = map_list_ptr;
6859 return map_list_ptr->map_list_map;
6862 /* Try to open include file FILENAME. SEARCHPTR is the directory
6863 being tried from the include file search path. This function maps
6864 filenames on file systems based on information read by
6868 open_include_file (cppReader *pfile,
6870 struct file_name_list *searchptr)
6872 char *filename = cstring_toCharsSafe (fname);
6873 struct file_name_map *map;
6877 cstring_markOwned (fname);
6879 cpp_setLocation (pfile);
6881 if (context_getFlag (FLG_NEVERINCLUDE))
6883 if (isHeaderFile (fname))
6885 return SKIP_INCLUDE;
6889 if ((searchptr != NULL) && ! searchptr->got_name_map)
6891 searchptr->name_map = read_name_map (pfile,
6892 !cstring_isEmpty (searchptr->fname)
6893 ? searchptr->fname :
6894 cstring_makeLiteralTemp ("."));
6895 searchptr->got_name_map = 1;
6898 /* First check the mapping for the directory we are using. */
6900 if ((searchptr != NULL)
6901 && (searchptr->name_map != NULL))
6905 if (!cstring_isEmpty (searchptr->fname))
6907 from += cstring_length (searchptr->fname) + 1;
6910 for (map = searchptr->name_map;
6912 map = map->map_next)
6914 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6917 ** Found a match. Check if the file should be skipped
6920 if (cpp_skipIncludeFile (map->map_to))
6922 return SKIP_INCLUDE;
6926 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6933 ** Try to find a mapping file for the particular directory we are
6934 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6935 ** in /usr/include/header.gcc and look up types.h in
6936 ** /usr/include/sys/header.gcc.
6939 p = strrchr (filename, CONNECTCHAR);
6946 if ((searchptr != NULL)
6947 && (cstring_isDefined (searchptr->fname))
6948 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6949 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6951 size_fromInt (p - filename)))
6953 /* filename is in SEARCHPTR, which we've already checked. */
6955 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6957 return SKIP_INCLUDE;
6961 return cpp_openIncludeFile (filename);
6967 dir = mstring_copy (".");
6972 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6973 memcpy (dir, filename, size_fromInt (p - filename));
6974 dir[p - filename] = '\0';
6978 for (map = read_name_map (pfile, cstring_fromChars (dir));
6980 map = map->map_next)
6982 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6986 if (cpp_skipIncludeFile (map->map_to))
6988 return SKIP_INCLUDE;
6992 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6999 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
7001 return SKIP_INCLUDE;
7005 return cpp_openIncludeFile (filename);
7009 /* Process the contents of include file FNAME, already open on descriptor F,
7011 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
7012 "system" include directories (as decided by the `is_system_include'
7014 DIRPTR is the link in the dir path through which this file was found,
7015 or 0 if the file name was absolute or via the current directory.
7016 Return 1 on success, 0 on failure.
7018 The caller is responsible for the cppReader_pushBuffer. */
7021 finclude (cppReader *pfile, int f,
7023 bool system_header_p,
7024 /*@dependent@*/ struct file_name_list *dirptr)
7030 cppBuffer *fp; /* For input stack frame */
7032 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7034 cppReader_perrorWithName (pfile, fname);
7035 check (close (f) == 0);
7036 (void) cppReader_popBuffer (pfile);
7042 fp = cppReader_getBufferSafe (pfile);
7044 /*@-temptrans@*/ /* fname shouldn't really be temp */
7045 fp->nominal_fname = fp->fname = fname;
7049 fp->system_header_p = system_header_p;
7052 fp->cleanup = cppReader_fileCleanup;
7054 if (S_ISREG (st_mode))
7057 fp->buf = (char *) dmalloc (st_size + 2);
7058 fp->alimit = fp->buf + st_size + 2;
7061 /* Read the file contents, knowing that st_size is an upper bound
7062 on the number of bytes we can read. */
7063 length = safe_read (f, fp->buf, size_toInt (st_size));
7064 fp->rlimit = fp->buf + length;
7065 if (length < 0) goto nope;
7067 else if (S_ISDIR (st_mode))
7069 cppReader_error (pfile,
7070 message ("Directory specified where file is expected: %s", fname));
7071 check (close (f) == 0);
7077 ** Cannot count its file size before reading.
7078 ** First read the entire file into heap and
7079 ** copy them into buffer on stack.
7082 size_t bsize = 2000;
7087 fp->buf = (char *) dmalloc (bsize + 2);
7090 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7093 goto nope; /* error! */
7096 if (st_size != bsize)
7098 break; /* End of file */
7102 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7106 length = size_toInt (st_size);
7109 if ((length > 0 && fp->buf[length - 1] != '\n')
7110 /* Backslash-newline at end is not good enough. */
7111 || (length > 1 && fp->buf[length - 2] == '\\')) {
7112 fp->buf[length++] = '\n';
7115 fp->buf[length] = '\0';
7116 fp->rlimit = fp->buf + length;
7118 /* Close descriptor now, so nesting does not use lots of descriptors. */
7119 check (close (f) == 0);
7121 /* Must do this before calling trigraph_pcp, so that the correct file name
7122 will be printed in warning messages. */
7124 pfile->input_stack_listing_current = 0;
7129 cppReader_perrorWithName (pfile, fname);
7130 check (close (f) == 0);
7136 cpplib_init (cppReader *pfile)
7138 memset ((char *) pfile, 0, sizeof (*pfile));
7140 pfile->get_token = cpplib_getToken;
7141 pfile->token_buffer_size = 200;
7142 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7143 pfile->all_include_files = NULL;
7147 cppReader_setWritten (pfile, 0);
7149 pfile->system_include_depth = 0;
7150 pfile->max_include_len = 0;
7151 pfile->timebuf = NULL;
7152 pfile->only_seen_white = 1;
7154 pfile->buffer = cppReader_nullBuffer (pfile);
7158 cppReader_finish (/*@unused@*/ cppReader *pfile)
7163 /* Free resources used by PFILE.
7164 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7167 cppCleanup (/*@special@*/ cppReader *pfile)
7169 /*@releases pfile@*/
7171 DPRINTF (("cppCleanup!"));
7173 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7175 (void) cppReader_popBuffer (pfile);
7178 if (pfile->token_buffer != NULL)
7180 sfree (pfile->token_buffer);
7181 pfile->token_buffer = NULL;
7184 while (pfile->if_stack != NULL)
7186 cppIfStackFrame *temp = pfile->if_stack;
7187 pfile->if_stack = temp->next;
7191 while (pfile->all_include_files != NULL)
7193 struct file_name_list *temp = pfile->all_include_files;
7194 pfile->all_include_files = temp->next;
7195 /*@-dependenttrans@*/
7196 cstring_free (temp->fname);
7197 /*@=dependenttrans@*/
7201 /* evans 2002-07-12 */
7202 while (pfile->opts->map_list != NULL)
7204 struct file_name_map_list *temp = pfile->opts->map_list;
7205 pfile->opts->map_list = pfile->opts->map_list->map_list_next;
7206 cstring_free (temp->map_list_name);
7210 while (pfile->opts->include != NULL)
7212 struct file_name_list *temp = pfile->opts->include;
7213 pfile->opts->include = pfile->opts->include->next;
7214 /* cstring_free (temp->fname); */
7218 sfree (pfile->opts);
7220 cppReader_hashCleanup ();
7224 ** Get the file-mode and data size of the file open on FD
7225 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7229 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7233 if (fstat (fd, &sbuf) < 0) {
7239 if (mode_pointer != NULL)
7241 *mode_pointer = sbuf.st_mode;
7244 if (size_pointer != NULL)
7246 *size_pointer = (size_t) sbuf.st_size;
7252 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7253 retrying if necessary. Return a negative value if an error occurs,
7254 otherwise return the actual number of bytes read,
7255 which must be LEN unless end-of-file was reached. */
7257 static int safe_read (int desc, char *ptr, int len)
7263 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7264 /*@-compdef@*/ /* ptr is an out parameter */
7265 int nchars = _read (desc, ptr, (unsigned) left);
7268 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7277 return (int) nchars;
7291 /* Initialize PMARK to remember the current position of PFILE. */
7294 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7296 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7298 pmark->next = pbuf->marks;
7300 pbuf->marks = pmark;
7304 pmark->position = pbuf->cur - pbuf->buf;
7305 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
7308 /* Cleanup PMARK - we no longer need it. */
7310 void parseClearMark (struct parse_marker *pmark)
7312 struct parse_marker **pp = &pmark->buf->marks;
7314 for (; ; pp = &(*pp)->next)
7316 llassert (*pp != NULL);
7317 if (*pp == pmark) break;
7323 /* Backup the current position of PFILE to that saved in PMARK. */
7326 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7328 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7330 if (pbuf != pmark->buf)
7332 cpp_setLocation (pfile);
7333 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7336 llassert (pbuf->buf != NULL);
7337 pbuf->cur = pbuf->buf + pmark->position;
7338 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
7341 /* Reset PMARK to point to the current position of PFILE. (Same
7342 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7345 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7347 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7349 if (pbuf != pmark->buf)
7351 cpp_setLocation (pfile);
7352 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7355 pmark->position = pbuf->cur - pbuf->buf;
7356 DPRINTF (("move mark: %s", pmark->position));
7359 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7361 struct cppOptions *opts = CPPOPTIONS (pfile);
7364 /* The code looks at the defaults through this pointer, rather than through
7365 the constant structure above. This pointer gets changed if an environment
7366 variable specifies other defaults. */
7368 struct default_include *include_defaults = include_defaults_array;
7370 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7371 /* There seems to be confusion about what CPATH should do,
7372 so for the moment it is not documented. */
7373 /* Some people say that CPATH should replace the standard include dirs,
7374 but that seems pointless: it comes before them, so it overrides them
7377 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7379 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7381 path_include (pfile, cstring_toCharsSafe (xp));
7384 /* Now that dollars_in_ident is known, initialize is_idchar. */
7385 initialize_char_syntax (opts);
7387 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7388 and option processing. */
7390 initialize_builtins (pfile);
7392 /* Do standard #defines and assertions
7393 that identify system and machine type. */
7395 if (!opts->inhibit_predefs) {
7396 char *p = (char *) dmalloc (strlen (predefs) + 1);
7397 strcpy (p, predefs);
7403 while (*p == ' ' || *p == '\t')
7408 /* Handle -D options. */
7409 if (p[0] == '-' && p[1] == 'D')
7413 while (*p && *p != ' ' && *p != '\t')
7423 if (opts->debug_output)
7425 output_line_command (pfile, 0, same_file);
7428 cppReader_define (pfile, q);
7430 while (*p == ' ' || *p == '\t')
7444 opts->done_initializing = 1;
7446 { /* Read the appropriate environment variable and if it exists
7447 replace include_defaults with the listed path. */
7451 int win32_buf_size = 0; /* memory we need to allocate */
7454 if (opts->cplusplus)
7456 epath = getenv ("CPLUS_INCLUDE_PATH");
7460 epath = getenv ("C_INCLUDE_PATH");
7464 ** If the environment var for this language is set,
7465 ** add to the default list of include directories.
7468 if (epath != NULL) {
7469 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7471 char *startp, *endp;
7474 /* if we have a posix path list, convert to win32 path list */
7475 if (cygwin32_posix_path_list_p (epath))
7477 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7478 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7479 cygwin32_posix_to_win32_path_list (epath, win32epath);
7483 for (num_dirs = 1, startp = epath; *startp; startp++)
7485 if (*startp == PATH_SEPARATOR)
7491 = (struct default_include *) dmalloc ((num_dirs
7492 * sizeof (struct default_include))
7493 + sizeof (include_defaults_array));
7496 startp = endp = epath;
7499 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7500 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7502 strncpy (nstore, startp, size_fromInt (endp - startp));
7505 strcpy (nstore, ".");
7509 nstore[endp-startp] = '\0';
7512 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7513 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7514 include_defaults[num_dirs].cxx_aware = 1;
7521 endp = startp = endp + 1;
7528 /* Put the usual defaults back in at the end. */
7529 memcpy ((char *) &include_defaults[num_dirs],
7530 (char *) include_defaults_array,
7531 sizeof (include_defaults_array));
7534 /*@-branchstate@*/ } /*@=branchstate@*/
7537 cppReader_appendIncludeChain (pfile, opts->before_system,
7538 opts->last_before_system);
7540 opts->first_system_include = opts->before_system;
7542 /* Unless -fnostdinc,
7543 tack on the standard include file dirs to the specified list */
7545 if (!opts->no_standard_includes) {
7546 struct default_include *p = include_defaults;
7547 char *specd_prefix = opts->include_prefix;
7548 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7549 int default_len = 0;
7551 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7552 if (default_prefix != NULL) {
7553 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7554 default_len = strlen (default_prefix) - 7;
7555 default_prefix[default_len] = 0;
7559 /* Search "translated" versions of GNU directories.
7560 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7561 if (specd_prefix != 0 && default_len != 0)
7562 for (p = include_defaults; p->fname != NULL; p++) {
7563 /* Some standard dirs are only for C++. */
7565 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7566 /* Does this dir start with the prefix? */
7567 llassert (default_prefix != NULL);
7569 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
7570 size_fromInt (default_len)))
7572 /* Yes; change prefix and add to search list. */
7573 struct file_name_list *nlist
7574 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7575 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7576 char *str = (char *) dmalloc (this_len + 1);
7577 strcpy (str, specd_prefix);
7578 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7581 nlist->fname = cstring_fromChars (str);
7582 nlist->control_macro = 0;
7583 nlist->c_system_include_path = !p->cxx_aware;
7584 nlist->got_name_map = 0;
7586 if (opts->first_system_include == 0)
7588 opts->first_system_include = nlist;
7591 cppReader_addIncludeChain (pfile, nlist);
7596 /* Search ordinary names for GNU include directories. */
7598 for (p = include_defaults; p->fname != NULL; p++)
7600 /* Some standard dirs are only for C++. */
7602 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7604 struct file_name_list *nlist
7605 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7606 nlist->control_macro = 0;
7607 nlist->c_system_include_path = !p->cxx_aware;
7608 nlist->fname = p->fname;
7609 nlist->got_name_map = 0;
7612 if (opts->first_system_include == NULL)
7614 opts->first_system_include = nlist;
7617 cppReader_addIncludeChain (pfile, nlist);
7620 sfree (default_prefix);
7623 /* Tack the after_include chain at the end of the include chain. */
7624 cppReader_appendIncludeChain (pfile, opts->after_include,
7625 opts->last_after_include);
7627 if (opts->first_system_include == NULL)
7629 opts->first_system_include = opts->after_include;
7632 /* With -v, print the list of dirs to search. */
7633 if (opts->verbose) {
7634 struct file_name_list *p;
7635 fprintf (stderr, "#include \"...\" search starts here:\n");
7637 for (p = opts->include; p != NULL; p = p->next) {
7638 if (p == opts->first_bracket_include)
7639 fprintf (stderr, "#include <...> search starts here:\n");
7641 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7643 fprintf (stderr, "End of search list.\n");
7647 int cppReader_startProcess (cppReader *pfile, cstring fname)
7651 struct cppOptions *opts = CPPOPTIONS (pfile);
7653 fp = cppReader_pushBuffer (pfile, NULL, 0);
7660 if (opts->in_fname == NULL)
7662 opts->in_fname = cstring_makeLiteralTemp ("");
7665 fp->fname = opts->in_fname;
7666 fp->nominal_fname = fp->fname;
7669 /* Copy the entire contents of the main input file into
7670 the stacked input buffer previously allocated for it. */
7672 if (cstring_isEmpty (fname))
7674 fname = cstring_makeLiteralTemp ("");
7677 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7679 cppReader_error (pfile,
7680 message ("Error opening %s for reading: %s",
7681 fname, lldecodeerror (errno)));
7690 if (finclude (pfile, f, fname, 0, NULL))
7692 output_line_command (pfile, 0, same_file);
7698 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7700 return pfile->buffer;
7703 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7705 llassert (pfile->buffer != NULL);
7706 return pfile->buffer;
7709 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7711 llassert (buf->buf != NULL);
7712 return (buf->buf + buf->line_base);
7715 int cpplib_bufPeek (cppBuffer *buf)
7717 if (buf->cur == NULL || buf->rlimit == NULL) {
7721 if (buf->cur < buf->rlimit) {
7728 bool cppBuffer_isMacro (cppBuffer *buf)
7732 return (buf->cleanup == cppReader_macroCleanup);
7739 ** Returns true if the macro should be checked, false
7740 ** if it should be expanded normally.
7743 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7744 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7745 static bool expectiter = FALSE; /* preceeded by @iter@ */
7746 static bool expectenditer = FALSE; /* second after @iter@ */
7747 static bool expectfunction = FALSE; /* preceeded by @function@ */
7748 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7749 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7751 static void cpp_setLocation (cppReader *pfile)
7756 if (pfile->buffer != NULL)
7758 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7760 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7762 DPRINTF (("Looking up: %s", fname));
7764 if (fileTable_exists (context_fileTable (), fname))
7766 fid = fileTable_lookup (context_fileTable (), fname);
7770 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7772 fid = fileTable_lookup (context_fileTable (),
7773 cppReader_getBufferSafe (pfile)->fname);
7778 fid = fileTable_lookup (context_fileTable (),
7779 cppReader_getBufferSafe (pfile)->fname);
7782 line = cppReader_getBufferSafe (pfile)->lineno;
7783 fileloc_free (g_currentloc);
7785 if (fileId_isValid (fid))
7787 g_currentloc = fileloc_create (fid, line, 1);
7791 g_currentloc = fileloc_createBuiltin ();
7796 fileloc_free (g_currentloc);
7797 g_currentloc = fileloc_createBuiltin ();
7801 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7803 bool checkmacro = FALSE;
7804 bool hasParams = FALSE;
7805 bool noexpand = FALSE;
7809 cpp_setLocation (pfile);
7811 DPRINTF (("Should check macro? %s", p));
7813 if (expectiter || expectconstant || expectenditer)
7818 expectenditer = TRUE;
7823 expectconstant = FALSE;
7824 expectenditer = FALSE;
7827 if (notfunction || notparseable)
7829 notfunction = FALSE;
7830 notparseable = FALSE;
7839 llassert (*p == '#');
7842 while (*p == ' ' || *p == '\t')
7847 llassert (*p == 'd'); /* define starts */
7851 while (*p == ' ' || *p == '\t')
7856 sname = cstring_fromChars (p);
7857 DPRINTF (("Check macro: %s", sname));
7859 while (((c = *p) != ' ')
7860 && c != '\0' && c != '('
7861 && c != '\t' && c != '\\' && c != '\n'
7867 hasParams = (c == '(');
7872 notparseable = FALSE;
7874 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7876 DPRINTF (("Clear notfunction"));
7877 notfunction = FALSE;
7892 if (usymtab_existsReal (sname))
7894 uentry ue = usymtab_lookup (sname);
7896 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7898 if (fileloc_isPreproc (uentry_whereLast (ue)))
7904 if (uentry_isSpecified (ue))
7906 checkmacro = context_getFlag (FLG_SPECMACROS);
7912 checkmacro = context_getFlag (FLG_LIBMACROS)
7913 || context_getFlag (FLG_FCNMACROS);
7921 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7923 if (fileloc_isSystemFile (g_currentloc)
7924 && context_getFlag (FLG_SYSTEMDIREXPAND))
7926 ; /* don't check this macro */
7927 DPRINTF (("Don't check 1"));
7935 DPRINTF (("Has params..."));
7937 if (context_getFlag (FLG_FCNMACROS))
7939 if (usymtab_exists (sname))
7942 ** only get here is macro is redefined
7943 ** error reported elsewhere
7946 DPRINTF (("It exists!"));
7951 ** We make it a forward function, since it might be declared elsewhere.
7952 ** After all headers have been processed, we should check the forward
7956 fileloc loc = fileloc_makePreproc (g_currentloc);
7958 /* the line is off-by-one, since the newline was already read */
7963 expectfunction = FALSE;
7966 le = uentry_makeForwardFunction (sname,
7967 typeId_invalid, loc);
7973 /* Do not define here! */
7975 (void) usymtab_addEntry (le);
7979 DPRINTF (("Check: TRUE"));
7983 DPRINTF (("Flag FCN_MACROS not set!"));
7988 DPRINTF (("No params"));
7990 if (context_getFlag (FLG_CONSTMACROS))
7992 bool nocontent = FALSE;
8005 ** Check if there is nothing after the define.
8008 while ((*rest) != '\0' && isspace (*rest))
8015 nocontent = TRUE; /* empty macro, don't check */
8020 if (usymtab_exists (sname))
8026 fileloc loc = fileloc_makePreproc (g_currentloc);
8027 DPRINTF (("Make constant: %s", sname));
8028 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
8029 (void) usymtab_addEntry (le);
8032 checkmacro = !nocontent;
8037 if (checkmacro && usymtab_existsType (sname))
8039 DPRINTF (("Making false..."));
8041 ppllerror (message ("Specified type implemented as macro: %s", sname));
8051 if (usymtab_exists (sname))
8053 uentry ue = usymtab_lookupExpose (sname);
8054 fileloc tloc = fileloc_makePreproc (g_currentloc);
8056 uentry_setDefined (ue, tloc);
8057 fileloc_free (tloc);
8058 uentry_setUsed (ue, fileloc_undefined);
8062 fileloc tloc = fileloc_makePreproc (g_currentloc);
8063 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8064 DPRINTF (("Make expanded macro: %s", sname));
8065 DPRINTF (("Not in symbol table: %s", sname));
8067 (void) usymtab_addGlobalEntry (ue);
8068 fileloc_free (tloc);
8073 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8077 static enum cpp_token
8078 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8080 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8084 bool eliminateComment = FALSE;
8086 llassert (pbuf->buf != NULL);
8088 start = pbuf->buf + smark->position;
8090 llassert (pbuf->cur != NULL);
8091 len = pbuf->cur - start;
8094 && start[1] == context_getCommentMarkerChar ())
8098 char *scomment = start + 2;
8099 char savec = start[len];
8101 cpp_setLocation (pfile);
8102 loc = fileloc_copy (g_currentloc);
8104 start[0] = BEFORE_COMMENT_MARKER[0];
8105 start[1] = BEFORE_COMMENT_MARKER[1];
8107 llassert (start[len - 2] == '*');
8108 start[len - 2] = AFTER_COMMENT_MARKER[0];
8110 llassert (start[len - 1] == '/');
8111 start[len - 1] = AFTER_COMMENT_MARKER[1];
8113 cpplib_reserve(pfile, size_fromInt (1 + len));
8114 cppReader_putCharQ (pfile, c);
8116 cpp_setLocation (pfile);
8120 if (mstring_containsString (scomment, "/*"))
8122 (void) cppoptgenerror
8124 message ("Comment starts inside syntactic comment: %s",
8125 cstring_fromChars (scomment)),
8131 if (mstring_equalPrefix (scomment, "ignore"))
8133 if (!context_getFlag (FLG_NOCOMMENTS))
8135 context_enterSuppressRegion (loc);
8138 else if (mstring_equalPrefix (scomment, "end"))
8140 if (!context_getFlag (FLG_NOCOMMENTS))
8142 context_exitSuppressRegion (loc);
8145 else if (mstring_equalPrefix (scomment, "notparseable"))
8147 notparseable = TRUE;
8149 eliminateComment = TRUE;
8151 else if (mstring_equalPrefix (scomment, "notfunction"))
8155 eliminateComment = TRUE;
8157 else if (mstring_equalPrefix (scomment, "iter"))
8161 else if (mstring_equalPrefix (scomment, "function"))
8163 expectfunction = TRUE;
8165 else if (mstring_equalPrefix (scomment, "constant"))
8167 expectconstant = TRUE;
8171 char sChar = *scomment;
8177 char *rest = scomment + 1;
8179 if (mstring_equalPrefix (rest, "commentchar"))
8181 eliminateComment = TRUE;
8185 ppllerror (cstring_makeLiteral
8186 ("Cannot restore commentchar"));
8190 char *next = scomment + 12; /* strlen commentchar = 12 */
8192 if (*next != ' ' && *next != '\t' && *next != '\n')
8196 ("Syntactic commentchar comment is not followed by a "
8197 "whitespace character: %c",
8202 char cchar = *(next + 1);
8207 (cstring_makeLiteral
8208 ("Cannot set commentchar to NUL"));
8212 context_setCommentMarkerChar (cchar);
8213 /* setComment = TRUE; */
8218 else if (mstring_equalPrefix (scomment, "nestcomment"))
8220 /* fix from Mike Miller <MikeM@xata.com> */
8221 context_fileSetFlag (FLG_NESTCOMMENT,
8222 ynm_fromCodeChar (sChar),
8225 else if (mstring_equalPrefix (rest, "namechecks"))
8227 context_fileSetFlag (FLG_NAMECHECKS,
8228 ynm_fromCodeChar (sChar),
8231 else if (mstring_equalPrefix (rest, "macroredef"))
8233 context_fileSetFlag (FLG_MACROREDEF,
8234 ynm_fromCodeChar (sChar),
8237 else if (mstring_equalPrefix (rest, "usevarargs"))
8239 context_fileSetFlag (FLG_USEVARARGS,
8240 ynm_fromCodeChar (sChar),
8243 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8245 context_fileSetFlag (FLG_MACRONEXTLINE,
8246 ynm_fromCodeChar (sChar),
8249 else if (mstring_equalPrefix (rest, "allmacros")
8250 || mstring_equalPrefix (rest, "fcnmacros")
8251 || mstring_equalPrefix (rest, "constmacros"))
8255 if (mstring_equalPrefix (rest, "allmacros"))
8259 else if (mstring_equalPrefix (rest, "fcnmacros"))
8265 llassert (mstring_equalPrefix (rest, "constmacros"));
8266 fl = FLG_CONSTMACROS;
8269 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8270 notfunction = FALSE;
8283 if (eliminateComment)
8288 /* Replaces comment char's in start with spaces */
8290 for (i = 2; i < len - 2; i++)
8292 if (start[i] == BEFORE_COMMENT_MARKER[0]
8293 || start[i] == BEFORE_COMMENT_MARKER[1]
8294 || start[i] == context_getCommentMarkerChar ())
8300 cppReader_putStrN (pfile, start, size_fromInt (len));
8301 parseClearMark (smark);
8311 ** Output the comment as all spaces so line/column
8312 ** in output file is still correct.
8316 cstring lintcomment = cstring_undefined;
8318 if (context_getFlag (FLG_LINTCOMMENTS))
8320 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8322 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8324 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8326 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8328 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8330 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8332 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8334 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8336 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8338 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8342 lintcomment = cstring_undefined;
8347 lintcomment = cstring_undefined;
8350 if (cstring_isDefined (lintcomment))
8352 c = BEFORE_COMMENT_MARKER[0];
8353 start[0] = BEFORE_COMMENT_MARKER[1];
8355 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8357 for (i = 1; i < len - 2; i++)
8359 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8362 start[len - 2] = AFTER_COMMENT_MARKER[0];
8363 start[len - 1] = AFTER_COMMENT_MARKER[1];
8367 /* Replaces char's in start with spaces */
8368 for (i = 0; i < len; i++)
8372 && start[i + 1] == '*') {
8373 (void) cppoptgenerror
8375 message ("Comment starts inside comment"),
8379 if (start[i] != '\n')
8386 cpplib_reserve (pfile, size_fromInt (1 + len));
8387 cppReader_putCharQ (pfile, c);
8388 cppReader_putStrN (pfile, start, size_fromInt (len));
8389 parseClearMark (smark);
8395 static int cpp_openIncludeFile (char *filename)
8397 int res = open (filename, O_RDONLY, 0666);
8399 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8402 if (!fileTable_exists (context_fileTable (),
8403 cstring_fromChars (filename)))
8405 if (fileloc_isXHFile (g_currentloc))
8408 ** Files includes by XH files are also XH files
8411 (void) fileTable_addXHFile (context_fileTable (),
8412 cstring_fromChars (filename));
8416 (void) fileTable_addHeaderFile (context_fileTable (),
8417 cstring_fromChars (filename));
8422 DPRINTF (("File already exists: %s", filename));
8429 static bool cpp_skipIncludeFile (cstring fname)
8431 if (context_isSystemDir (fname))
8433 DPRINTF (("System dir: %s", fname));
8435 if (lcllib_isSkipHeader (fname))
8437 DPRINTF (("Skip include TRUE: %s", fname));
8441 if (context_getFlag (FLG_SKIPSYSHEADERS))
8443 DPRINTF (("Skip include TRUE: %s", fname));
8448 if (context_getFlag (FLG_SINGLEINCLUDE))
8450 fname = removePreDirs (fname);
8452 # if defined (WIN32) || defined (OS2)
8453 cstring_replaceAll (fname, '\\', '/');
8456 if (fileTable_exists (context_fileTable (), fname))
8458 DPRINTF (("Skip include TRUE: %s", fname));
8463 DPRINTF (("Skip include FALSE: %s", fname));
8467 static int cpp_peekN (cppReader *pfile, int n)
8469 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8471 llassert (buf->cur != NULL);
8473 return (buf->rlimit - buf->cur >= (n)
8478 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8483 void cppBuffer_forward (cppBuffer *buf, int n)
8485 llassert (buf->cur != NULL);
8489 /*@=bufferoverflowhigh@*/