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 #define NO_SHORTNAMES
121 /*@constant int IMPORT_FOUND@*/
122 # define IMPORT_FOUND -2
124 /*@constant int SKIP_INCLUDE@*/
125 # define SKIP_INCLUDE IMPORT_FOUND
127 /*@constant unused int IMPORT_NOT_FOUND@*/
128 # define IMPORT_NOT_FOUND -1
131 /*@constant unused int STDC_VALUE@*/
135 /* By default, colon separates directories in a path. */
136 #ifndef PATH_SEPARATOR
137 /*@constant char PATH_SEPARATOR@*/
138 #define PATH_SEPARATOR ':'
141 static void parse_name (cppReader *, int);
143 static int cpp_openIncludeFile (char *p_filename)
144 /*@modifies fileSystem @*/ ;
146 static void cpp_setLocation (cppReader *p_pfile)
147 /*@modifies g_currentloc@*/ ;
149 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
150 struct parse_marker *p_smark)
151 /*@modifies p_pfile, p_smark@*/;
153 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
155 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
157 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
163 /* Symbols to predefine. */
165 #ifdef CPP_PREDEFINES
166 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
168 static /*@observer@*/ char *predefs = "";
171 /* We let tm.h override the types used here, to handle trivial differences
172 such as the choice of unsigned int or long unsigned int for size_t.
173 When machines start needing nontrivial differences in the size type,
174 it would be best to do something here to figure out automatically
175 from other information what type to use. */
177 /* The string value for __SIZE_TYPE__. */
180 /*@constant observer char *SIZE_TYPE@*/
181 #define SIZE_TYPE "long unsigned int"
184 /* The string value for __PTRDIFF_TYPE__. */
187 /*@constant observer char *PTRDIFF_TYPE@*/
188 #define PTRDIFF_TYPE "long int"
191 /* The string value for __WCHAR_TYPE__. */
194 /*@constant observer char *WCHAR_TYPE@*/
195 #define WCHAR_TYPE "int"
198 /* The string value for __USER_LABEL_PREFIX__ */
200 #ifndef USER_LABEL_PREFIX
201 /*@constant observer char *USER_LABEL_PREFIX@*/
202 #define USER_LABEL_PREFIX ""
205 /* The string value for __REGISTER_PREFIX__ */
207 #ifndef REGISTER_PREFIX
208 /*@constant observer char *REGISTER_PREFIX@*/
209 #define REGISTER_PREFIX ""
212 /* table to tell if char can be part of a C identifier. */
213 static bool is_idchar[256];
214 /* table to tell if char can be first char of a c identifier. */
215 static bool is_idstart[256];
216 /* table to tell if c is horizontal space. */
217 static bool is_hor_space[256];
218 /* table to tell if c is horizontal or vertical space. */
219 static bool is_space[256];
221 static /*@exposed@*/ /*@null@*/ cppBuffer *
222 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
223 /*@uses p_pfile->buffer@*/
224 /*@modifies nothing@*/ ;
227 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
230 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
232 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
234 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
235 # define cppBuffer_get(BUFFER) \
236 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
238 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
239 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
240 modifies *p_file; @*/
241 # define cppReader_puts(PFILE, STR, N) \
242 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
244 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
246 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
247 modifies *p_file; @*/
248 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
250 static void cppReader_putCharQ (cppReader *p_file, char p_ch)
252 fprintf (stderr, "put char: %c\n", p_ch);
253 (*(p_file)->limit++ = (p_ch));
256 /* Append character CH to PFILE's output buffer. Make space if need be. */
258 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
259 modifies *p_file; @*/
260 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
262 /* Make sure PFILE->limit is followed by '\0'. */
263 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
264 modifies *p_file; @*/
266 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
268 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
269 modifies *p_file; @*/
270 # define cppReader_nullTerminate(PFILE) \
271 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
273 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
274 modifies *p_file; @*/
275 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
277 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
278 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
280 /*@function static observer char *cppReader_wcharType (cppReader *)
281 modifies nothing; @*/
283 # define cppReader_wcharType(PFILE) \
284 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
286 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
288 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
289 # define cppReader_forward(pfile, N) \
290 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
292 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
293 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
295 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
296 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
298 /* Move all backslash-newline pairs out of embarrassing places.
299 Exchange all such pairs following BP
300 with any potentially-embarrassing characters that follow them.
301 Potentially-embarrassing characters are / and *
302 (because a backslash-newline inside a comment delimiter
303 would cause it not to be recognized). */
306 # define NEWLINE_FIX \
307 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
309 /* Same, but assume we've already read the potential '\\' into C. */
311 # define NEWLINE_FIX1(C) do { \
312 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
315 static void parseSetMark (/*@out@*/ struct parse_marker *,
317 static void parseClearMark (struct parse_marker *);
318 static void parseGotoMark (struct parse_marker *, cppReader *);
319 static void parseMoveMark (struct parse_marker *, cppReader *);
321 /* If we have a huge buffer, may need to cache more recent counts */
322 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
324 static /*@exposed@*/ /*@null@*/ cppBuffer *
325 cppReader_pushBuffer (cppReader *p_pfile,
326 /*@owned@*/ /*@null@*/ char *, size_t)
327 /*@modifies p_pfile@*/ ;
329 static void cppReader_appendIncludeChain
331 /*@keep@*/ struct file_name_list *p_first,
332 /*@dependent@*/ struct file_name_list *p_last);
334 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
335 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
337 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
338 /*@unused@*/ cppReader *p_pfile);
340 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
341 /*@unused@*/ cppReader *p_pfile);
343 static int cppReader_handleDirective (cppReader *p_pfile);
345 static void cppReader_scanBuffer (cppReader *p_pfile);
347 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
350 ** WIN32 (at least the VC++ include files) does not define mode_t.
353 /*@-incondefs@*/ /*@-czechtypes@*/
354 typedef unsigned int mode_t;
355 /*@=incondefs@*/ /*@=czechtypes@*/
359 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
360 /*@out@*/ size_t *p_size_pointer);
361 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
365 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
366 ** (Note that it is false while we're expanding marco *arguments*.)
369 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
371 static void path_include (cppReader *p_pfile, char *p_path)
372 /*@modifies p_pfile@*/ ;
374 static void initialize_builtins (cppReader *p_pfile)
375 /*@modifies p_pfile@*/ ;
377 static void initialize_char_syntax (struct cppOptions *p_opts) ;
379 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
381 bool p_system_header_p,
382 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
384 static void validate_else (cppReader *p_pfile, cstring p_directive);
386 static void conditional_skip (cppReader *p_pfile, int p_skip,
387 enum node_type p_type,
388 /*@dependent@*/ /*@null@*/ char *p_control_macro);
390 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
394 static void skip_if_group (cppReader *p_pfile, int p_any);
396 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
397 char *p_beg2, int p_len2, bool p_last);
400 extern void fancy_abort ();
403 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
404 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
406 static /*@observer@*/ /*@null@*/ struct file_name_map *
407 read_name_map (cppReader *p_pfile, cstring p_dirname);
409 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
411 static int open_include_file (cppReader *p_pfile,
412 /*@owned@*/ cstring p_fname,
413 /*@null@*/ struct file_name_list *p_searchptr);
415 static void push_macro_expansion (cppReader *,
416 /*@owned@*/ char *, size_t,
417 /*@dependent@*/ hashNode);
419 /* Last arg to output_line_command. */
420 enum file_change_code {
421 same_file, enter_file, leave_file
424 /* `struct directive' defines one #-directive, including how to handle it. */
427 int length; /* Length of name */
428 /*@null@*/ int (*func)(); /* Function to handle directive */
429 /*@observer@*/ cstring name; /* Name of directive */
430 enum node_type type; /* Code which describes which directive. */
431 bool command_reads_line; /* One if rest of line is read by func. */
432 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
433 bool pass_thru; /* Copy preprocessed directive to output file.*/
436 /* These functions are declared to return int instead of void since they
437 are going to be placed in a table and some old compilers have trouble with
438 pointers to functions returning void. */
440 static int do_define (cppReader *, /*@null@*/ struct directive *,
441 /*@exposed@*/ char *, char *);
442 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
443 /*@exposed@*/ char *, char *, bool);
445 static int do_line (cppReader *, /*@null@*/ struct directive *);
446 static int do_include (cppReader *, struct directive *, char *, char *);
447 static int do_undef (cppReader *, struct directive *, char *, char *);
448 static int do_error (cppReader *, struct directive *, char *, char *);
449 static int do_pragma (cppReader *, struct directive *, char *, char *);
450 static int do_ident (cppReader *, struct directive *, char *, char *);
451 static int do_if (cppReader *, struct directive *, char *, char *);
452 static int do_xifdef (cppReader *, struct directive *, char *, char *);
453 static int do_else (cppReader *, struct directive *, char *, char *);
454 static int do_elif (cppReader *, struct directive *, char *, char *);
455 static int do_endif (cppReader *, struct directive *, char *, char *);
456 static int do_warning (cppReader *, struct directive *, char *, char *);
458 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
459 via the same directory as the file that #included it. */
461 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
462 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
464 /* #include "file" looks in source file dir, then stack. */
465 /* #include <file> just looks in the stack. */
466 /* -I directories are added to the end, then the defaults are added. */
470 static struct default_include {
471 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
472 int cplusplus; /* Only look here if we're compiling C++. */
473 int cxx_aware; /* Includes in this directory don't need to
474 be wrapped in extern "C" when compiling
476 } include_defaults_array[]
478 /* This is the dir for fixincludes. Put it just before
479 the files that we fix. */
480 { GCC_INCLUDE_DIR, 0, 0 },
481 { GCC_INCLUDE_DIR2, 0, 0 },
482 { cstring_undefined, 0, 0 }
485 /*@noaccess cstring@*/
487 /* Here is the actual list of #-directives, most-often-used first.
488 The initialize_builtins function assumes #define is the very first. */
492 static struct directive directive_table[] = {
493 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
494 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
495 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
496 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
497 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
498 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
499 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
500 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
501 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
502 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
503 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
504 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
505 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
506 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
507 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
508 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
510 /*@noaccess cstring@*/
512 static cstring searchPath_unparse (struct file_name_list *search_start)
514 cstring res = cstring_newEmpty ();
515 struct file_name_list *searchptr = NULL;
517 for (searchptr = search_start; searchptr != NULL;
518 searchptr = searchptr->next)
520 if (!cstring_isEmpty (searchptr->fname)) {
521 res = cstring_concatFree1 (res, searchptr->fname);
522 if (searchptr->next != NULL) {
523 res = cstring_appendChar (res, ';');
533 initialize_char_syntax (struct cppOptions *opts)
538 * Set up is_idchar and is_idstart tables. These should be
539 * faster than saying (is_alpha (c) || c == '_'), etc.
540 * Set up these things before calling any routines tthat
544 for (i = 'a'; i <= 'z'; i++) {
545 is_idchar[i - 'a' + 'A'] = TRUE;
546 is_idchar[(int) i] = TRUE;
547 is_idstart[i - 'a' + 'A'] = TRUE;
548 is_idstart[(int) i] = TRUE;
551 for (i = '0'; i <= '9'; i++)
553 is_idchar[(int) i] = TRUE;
556 is_idchar['_'] = TRUE;
557 is_idstart['_'] = TRUE;
558 is_idchar['$'] = opts->dollars_in_ident;
559 is_idstart['$'] = opts->dollars_in_ident;
561 /* horizontal space table */
562 is_hor_space[' '] = TRUE;
563 is_hor_space['\t'] = TRUE;
564 is_hor_space['\v'] = TRUE;
565 is_hor_space['\f'] = TRUE;
566 is_hor_space['\r'] = TRUE;
568 is_space[' '] = TRUE;
569 is_space['\t'] = TRUE;
570 is_space['\v'] = TRUE;
571 is_space['\f'] = TRUE;
572 is_space['\n'] = TRUE;
573 is_space['\r'] = TRUE;
576 bool isIdentifierChar (char c)
578 return is_idchar[(int) c];
581 /* Place into P_PFILE a quoted string representing the string SRC.
582 Caller must reserve enough space in pfile->token_buffer. */
585 quote_string (cppReader *pfile, char *src)
589 cppReader_putCharQ (pfile, '\"');
592 switch ((c = *src++))
596 cppReader_putCharQ (pfile, c);
599 sprintf (cpplib_getPWritten (pfile), "\\%03o",
601 cppReader_adjustWritten (pfile, (size_t) 4);
603 /*@switchbreak@*/ break;
607 cppReader_putCharQ (pfile, '\\');
608 cppReader_putCharQ (pfile, c);
609 /*@switchbreak@*/ break;
612 cppReader_putCharQ (pfile, '\"');
613 cppReader_nullTerminateQ (pfile);
619 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
622 cppReader_growBuffer (cppReader *pfile, size_t n)
624 size_t old_written = cpplib_getWritten (pfile);
625 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
626 pfile->token_buffer = (char *)
627 drealloc (pfile->token_buffer, pfile->token_buffer_size);
628 cppReader_setWritten (pfile, old_written);
632 * process a given definition string, for initialization
633 * If STR is just an identifier, define it with value 1.
634 * If STR has anything after the identifier, then it should
635 * be identifier=definition.
639 cppReader_define (cppReader *pfile, char *str)
644 DPRINTF (("Cpp reader define: %s", str));
646 if (!is_idstart[(int) *p])
648 DPRINTF (("ERROR 1"));
649 cppReader_error (pfile,
650 message ("Malformed option `-D%s'",
651 cstring_fromChars (str)));
658 DPRINTF (("Here 2"));
660 while (is_idchar[(int) *p])
667 while (*p != ')' && *p != '\0') {
676 message ("Malformed option: -D%s (no closing parenthesis)",
677 cstring_fromChars (str)));
681 DPRINTF (("Here 2"));
685 buf = (char *) dmalloc (size_fromInt (p - str + 4));
686 strcpy ((char *) buf, str);
687 strcat ((char *) buf, " 1");
691 DPRINTF (("ERROR 2"));
692 cppReader_error (pfile,
693 message ("Malformed option: -D%s (expected '=', found '%c')",
694 cstring_fromChars (str),
701 /* Copy the entire option so we can modify it. */
702 DPRINTF (("Copying..."));
703 buf = (char *) dmalloc (2 * strlen (str) + 1);
704 strncpy (buf, str, size_fromInt (p - str));
706 /* Change the = to a space. */
708 /* Scan for any backslash-newline and remove it. */
714 if (*p == '\\' && p[1] == '\n')
720 DPRINTF (("Here we are..."));
724 llassert (buf != NULL);
725 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
726 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
730 /* Append a chain of `struct file_name_list's
731 to the end of the main include chain.
732 FIRST is gthe beginning of the chain to append, and LAST is the end. */
735 cppReader_appendIncludeChain (cppReader *pfile,
736 struct file_name_list *first,
737 struct file_name_list *last)
739 struct cppOptions *opts = CPPOPTIONS (pfile);
740 struct file_name_list *dir;
742 if (first == NULL || last == NULL)
747 if (opts->include == 0)
749 opts->include = first;
753 llassert (opts->last_include->next == NULL);
754 opts->last_include->next = first;
757 if (opts->first_bracket_include == 0)
759 opts->first_bracket_include = first;
761 for (dir = first; ; dir = dir->next) {
762 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
763 if (len > pfile->max_include_len)
764 pfile->max_include_len = len;
770 llassert (last->next == NULL);
771 /* last->next = NULL; */
772 opts->last_include = last;
776 static /*@unused@*/ void
777 cppReader_showIncludeChain (cppReader *pfile)
779 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
785 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
789 fprintf (stderr, "\n");
793 fprintf (stderr, "No includes\n");
799 cppReader_getIncludePath ()
801 cppReader *pfile = &g_cppState;
802 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
803 cstring res = cstring_undefined;
809 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
815 res = cstring_makeLiteral ("<no include path>");
822 cppReader_addIncludeChain (cppReader *pfile, struct file_name_list *dir)
824 struct cppOptions *opts = CPPOPTIONS (pfile);
831 if (opts->include == 0)
837 llassert (opts->last_include->next == NULL);
838 opts->last_include->next = dir;
841 if (opts->first_bracket_include == 0)
843 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
844 opts->first_bracket_include = dir;
845 if (len > pfile->max_include_len)
847 pfile->max_include_len = len;
852 opts->last_include = dir;
853 /* cppReader_showIncludeChain (pfile); */
856 /* Given a colon-separated list of file names PATH,
857 add all the names to the search path for include files. */
860 path_include (cppReader *pfile, char *path)
867 /* if we have a posix path list, convert to win32 path list */
868 win32temp = (char *) dmalloc /*@i4@*/
869 (cygwin32_posix_to_win32_path_list_buf_size (path));
870 cygwin32_posix_to_win32_path_list (path, win32temp);
880 struct file_name_list *dirtmp;
882 /* Find the end of this name. */
883 while (*q != '\0' && *q != PATH_SEPARATOR)
890 /* An empty name in the path stands for the current directory. */
891 name = (char *) dmalloc ((size_t) 2);
897 /* Otherwise use the directory that is named. */
898 name = (char *) dmalloc (size_fromInt (q - p + 1));
899 memcpy (name, p, size_fromInt (q - p));
903 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
904 dirtmp->next = 0; /* New one goes on the end */
905 dirtmp->control_macro = 0;
906 dirtmp->c_system_include_path = 0;
907 dirtmp->fname = cstring_fromChars (name);
908 dirtmp->got_name_map = 0;
909 cppReader_addIncludeChain (pfile, dirtmp);
911 /* Advance past this name. */
915 /* Skip the colon. */
921 cppOptions_init (cppOptions *opts)
923 memset ((char *) opts, 0, sizeof *opts);
926 opts->in_fname = NULL;
927 opts->out_fname = NULL;
929 /* Initialize is_idchar to allow $. */
930 opts->dollars_in_ident = TRUE;
932 opts->no_line_commands = 0;
933 opts->no_trigraphs = TRUE;
934 opts->put_out_comments = 1;
935 opts->print_include_names = 0;
936 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
940 opts->cplusplus_comments = 1;
945 opts->pedantic_errors = 0;
946 opts->warn_comments = 0;
947 opts->warnings_are_errors = 0;
949 initialize_char_syntax (opts);
953 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
959 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
960 /*@unused@*/ cppReader *pfile)
966 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
968 hashNode macro = pbuf->hnode;
970 if (macro->type == T_DISABLED)
972 macro->type = T_MACRO;
975 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
983 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
985 if (pbuf->buf != NULL)
992 /* Assuming we have read '/'.
993 If this is the start of a comment (followed by '*' or '/'),
994 skip to the end of the comment, and return ' '.
995 Return EOF if we reached the end of file before the end of the comment.
996 If not the start of a comment, return '/'. */
999 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1003 llassert (pfile->buffer != NULL);
1004 llassert (pfile->buffer->cur != NULL);
1006 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1013 cppReader_forward (pfile, 2);
1016 if (cppReader_peekC (pfile) == '*')
1018 cppReader_forward (pfile, 1);
1023 c = cppReader_getC (pfile);
1030 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1037 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1040 if (prev_c == (int) '*' && c == (int) '/')
1045 if (c == (int) '\n' && (linep != NULL))
1051 else if (cppReader_peekC (pfile) == '/'
1052 && CPPOPTIONS (pfile)->cplusplus_comments)
1055 (void) cppoptgenerror
1056 (FLG_SLASHSLASHCOMMENT,
1057 message ("C++ style // comment"
1061 cppReader_forward (pfile, 1);
1065 c = cppReader_getC (pfile);
1069 /* Allow hash comment to be terminated by EOF. */
1073 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1075 cppReader_forward (pfile, 1);
1076 c = cppReader_getC (pfile);
1084 if (c == (int) '\n')
1086 /* Don't consider final '\n' to be part of comment. */
1087 cppReader_forward (pfile, -1);
1098 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1100 cppSkipHspace (cppReader *pfile)
1108 llassert (pfile->buffer != NULL);
1110 c = cppReader_peekC (pfile);
1114 return 0; /* FIXME */
1117 if (is_hor_space[c])
1119 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1120 cppReader_pedwarn (pfile,
1121 message ("%s in preprocessing directive",
1123 ? cstring_makeLiteralTemp ("formfeed")
1124 : cstring_makeLiteralTemp ("vertical tab")));
1127 cppReader_forward (pfile, 1);
1131 cppReader_forward (pfile, 1);
1132 c = skip_comment (pfile, NULL);
1136 cppReader_forward (pfile, -1);
1139 if (c == EOF || c == '/')
1144 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1146 cppReader_forward (pfile, 2);
1148 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1149 && is_hor_space [cpp_peekN (pfile, 1)])
1151 cppReader_forward (pfile, 2);
1160 /* Read the rest of the current line.
1161 The line is appended to PFILE's output buffer. */
1164 copy_rest_of_line (cppReader *pfile)
1166 struct cppOptions *opts = CPPOPTIONS (pfile);
1173 llassert (pfile->buffer != NULL);
1175 c = cppReader_getC (pfile);
1182 ** Patch from Brian St. Pierre for handling MS-DOS files.
1185 if (cppReader_peekC (pfile) == '\n'
1186 || cppReader_peekC (pfile) == '\r')
1188 if (cppReader_peekC (pfile) == '\r')
1190 cppReader_forward (pfile, 1);
1193 cppReader_forward (pfile, 1);
1197 /*@fallthrough@*/ case '\'': case '\"':
1198 goto scan_directive_token;
1201 nextc = cppReader_peekC (pfile);
1204 ** was (opts->cplusplus_comments && nextc == '*')
1209 || (opts->cplusplus_comments && nextc == '/'))
1211 goto scan_directive_token;
1213 /*@switchbreak@*/ break;
1216 if (cppReader_isPedantic (pfile))
1217 cppReader_pedwarn (pfile,
1218 message ("%s in preprocessing directive",
1220 ? cstring_makeLiteralTemp ("formfeed")
1221 : cstring_makeLiteralTemp ("vertical tab")));
1222 /*@switchbreak@*/ break;
1225 cppReader_forward (pfile, -1);
1227 scan_directive_token:
1228 cppReader_forward (pfile, -1);
1229 (void) cpplib_getToken (pfile);
1232 cppReader_putChar (pfile, c);
1235 cppReader_nullTerminate (pfile);
1239 cppReader_skipRestOfLine (cppReader *pfile)
1241 size_t old = cpplib_getWritten (pfile);
1242 copy_rest_of_line (pfile);
1243 cppReader_setWritten (pfile, old);
1246 /* Handle a possible # directive.
1247 '#' has already been read. */
1250 cppReader_handleDirective (cppReader *pfile)
1253 struct directive *kt = NULL;
1255 size_t after_ident = 0;
1257 char *line_end = NULL;
1258 size_t old_written = cpplib_getWritten (pfile);
1259 int nspaces = cppSkipHspace (pfile);
1261 c = cppReader_peekC (pfile);
1263 if (c >= '0' && c <= '9')
1265 /* Handle # followed by a line number. */
1266 if (cppReader_isPedantic (pfile))
1268 cppReader_pedwarnLit
1270 cstring_makeLiteralTemp ("`#' followed by integer"));
1273 (void) do_line (pfile, NULL);
1274 goto done_a_directive;
1278 /* Now find the directive name. */
1280 cppReader_putChar (pfile, '#');
1282 parse_name (pfile, cppReader_getC (pfile));
1284 llassert (pfile->token_buffer != NULL);
1285 ident = pfile->token_buffer + old_written + 1;
1287 ident_length = cpplib_getPWritten (pfile) - ident;
1289 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1291 /* A line of just `#' becomes blank. */
1295 for (kt = directive_table; ; kt++)
1297 if (kt->length <= 0)
1299 return 0; /* goto not_a_directive; */
1302 if (kt->length == ident_length
1303 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1309 if (kt->command_reads_line)
1315 /* Nonzero means do not delete comments within the directive.
1316 #define needs this when -traditional. */
1317 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1318 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1319 CPPOPTIONS (pfile)->put_out_comments = comments;
1320 after_ident = cpplib_getWritten (pfile);
1321 copy_rest_of_line (pfile);
1322 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1326 /* For #pragma and #define, we may want to pass through the directive.
1327 Other directives may create output, but we don't want the directive
1328 itself out, so we pop it now. For example #include may write a #line
1329 command (see comment in do_include), and conditionals may emit
1330 #failed ... #endfailed stuff. But note that popping the buffer
1331 means the parameters to kt->func may point after pfile->limit
1332 so these parameters are invalid as soon as something gets appended
1333 to the token_buffer. */
1335 line_end = cpplib_getPWritten (pfile);
1338 if (!kt->pass_thru && kt->type != T_DEFINE)
1340 cppReader_setWritten (pfile, old_written);
1343 llassert (pfile->token_buffer != NULL);
1345 /* was kt->pass_thru || */
1347 if (kt->type == T_DEFINE
1348 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1350 char *p = pfile->token_buffer + old_written;
1353 ** Still need to record value for preprocessing, so
1354 ** #ifdef's, etc. using the value behave correctly.
1357 (void) do_defineAux (pfile, kt,
1358 pfile->token_buffer + after_ident,
1367 SKIP_WHITE_SPACE (p);
1369 llassert (*p == 'd');
1372 llassert (*p == 'e');
1375 llassert (*p == 'f');
1378 llassert (*p == 'i');
1381 llassert (*p == 'n');
1384 llassert (*p == 'e');
1387 ** This is way-bogus. We use the last char to record the number of
1388 ** spaces. Its too hard to get them back into the input stream.
1391 if (nspaces > 9) nspaces = 9;
1393 *p++ = '0' + nspaces;
1395 return 0; /* not_a_directive */
1397 else if (kt->pass_thru)
1399 /* Just leave the entire #define in the output stack. */
1400 return 0; /* not_a_directive */
1403 else if (kt->type == T_DEFINE
1404 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1406 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1407 SKIP_WHITE_SPACE (p);
1409 while (is_idchar[(int) *p])
1415 cppReader_putChar (pfile, '\n');
1417 else if (kt->type == T_DEFINE)
1419 cppReader_setWritten (pfile, old_written);
1430 llassert (kt->func != NULL);
1431 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1436 /* Pass a directive through to the output file.
1437 BUF points to the contents of the directive, as a contiguous string.
1438 LIMIT points to the first character past the end of the directive.
1439 KEYWORD is the keyword-table entry for the directive. */
1442 pass_thru_directive (char *buf, char *limit,
1444 struct directive *keyword)
1446 int keyword_length = keyword->length;
1448 cpplib_reserve (pfile,
1449 size_fromInt (2 + keyword_length + (limit - buf)));
1450 cppReader_putCharQ (pfile, '#');
1451 /*@-observertrans@*/
1452 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1453 size_fromInt (keyword_length));
1454 /*:=observertrans@*/
1456 if (limit != buf && buf[0] != ' ')
1458 /* Was a bug, since reserve only used 1 + ... */
1459 cppReader_putCharQ (pfile, ' ');
1462 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1465 /* Read a replacement list for a macro with parameters.
1466 Build the DEFINITION structure.
1467 Reads characters of text starting at BUF until END.
1468 ARGLIST specifies the formal parameters to look for
1469 in the text of the definition; NARGS is the number of args
1470 in that list, or -1 for a macro name that wants no argument list.
1471 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1472 and NAMELEN is its length in characters.
1474 Note that comments, backslash-newlines, and leading white space
1475 have already been deleted from the argument. */
1478 collect_expansion (cppReader *pfile, char *buf, char *limit,
1479 int nargs, /*@null@*/ struct arglist *arglist)
1482 char *p, *lastp, *exp_p;
1483 struct reflist *endpat = NULL;
1484 /* Pointer to first nonspace after last ## seen. */
1486 /* Pointer to first nonspace after last single-# seen. */
1487 char *stringify = 0;
1489 char expected_delimiter = '\0';
1492 /* Scan thru the replacement list, ignoring comments and quoted
1493 strings, picking up on the macro calls. It does a linear search
1494 thru the arg list on every potential symbol. Profiling might say
1495 that something smarter should happen. */
1500 /* Find the beginning of the trailing whitespace. */
1503 while (p < limit && is_space[(int) limit[-1]])
1508 /* Allocate space for the text in the macro definition.
1509 Leading and trailing whitespace chars need 2 bytes each.
1510 Each other input char may or may not need 1 byte,
1511 so this is an upper bound. The extra 5 are for invented
1512 leading and trailing newline-marker and final null. */
1513 maxsize = (sizeof (*defn) + (limit - p) + 5);
1515 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1524 defn = (DEFINITION *) dmalloc (maxsize);
1525 defn->noExpand = FALSE;
1527 defn->pattern = NULL;
1528 defn->nargs = nargs;
1529 defn->predefined = NULL;
1531 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1534 defn->rest_args = NULL;
1535 defn->args.argnames = NULL;
1541 /* Add one initial space escape-marker to prevent accidental
1542 token-pasting (often removed by cpplib_macroExpand). */
1546 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1547 cppReader_errorLit (pfile,
1548 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1552 /* Process the main body of the definition. */
1554 int skipped_arg = 0;
1555 register char c = *p++;
1559 if (!cppReader_isTraditional (pfile)) {
1563 if (expected_delimiter != '\0')
1565 if (c == expected_delimiter)
1566 expected_delimiter = '\0';
1570 expected_delimiter = c;
1572 /*@switchbreak@*/ break;
1575 if (p < limit && (expected_delimiter != '\0'))
1577 /* In a string, backslash goes through
1578 and makes next char ordinary. */
1581 /*@switchbreak@*/ break;
1584 /* An '@' in a string or character constant stands for itself,
1585 and does not need to be escaped. */
1586 if (expected_delimiter == '\0')
1591 /*@switchbreak@*/ break;
1594 /* # is ordinary inside a string. */
1595 if (expected_delimiter != '\0')
1597 /*@switchbreak@*/ break;
1600 if (p < limit && *p == '#') {
1601 /* ##: concatenate preceding and following tokens. */
1602 /* Take out the first #, discard preceding whitespace. */
1606 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1612 /* Skip the second #. */
1614 /* Discard following whitespace. */
1615 SKIP_WHITE_SPACE (p);
1619 cppReader_errorLit (pfile,
1620 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1622 } else if (nargs >= 0) {
1623 /* Single #: stringify following argument ref.
1624 Don't leave the # in the expansion. */
1626 SKIP_WHITE_SPACE (p);
1627 if (p == limit || ! is_idstart[(int) *p]
1628 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1629 cppReader_errorLit (pfile,
1630 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1637 /*@switchbreak@*/ break;
1640 /* In -traditional mode, recognize arguments inside strings and
1641 and character constants, and ignore special properties of #.
1642 Arguments inside strings are considered "stringified", but no
1643 extra quote marks are supplied. */
1647 if (expected_delimiter != '\0') {
1648 if (c == expected_delimiter)
1649 expected_delimiter = '\0';
1651 expected_delimiter = c;
1652 /*@switchbreak@*/ break;
1655 /* Backslash quotes delimiters and itself, but not macro args. */
1656 if (expected_delimiter != '\0' && p < limit
1657 && (*p == expected_delimiter || *p == '\\')) {
1661 /*@switchbreak@*/ break;
1664 if (expected_delimiter != '\0') /* No comments inside strings. */
1665 /*@switchbreak@*/ break;
1667 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1668 this must be -traditional. So replace the comment with
1672 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1677 /*@switchbreak@*/ break;
1681 /* Handle the start of a symbol. */
1682 if (is_idchar[(int) c] && nargs > 0) {
1683 char *id_beg = p - 1;
1687 while (p != limit && is_idchar[(int) *p])
1692 id_len = size_fromInt (p - id_beg);
1694 if (is_idstart[(int) c]
1695 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1696 register struct arglist *arg;
1698 for (arg = arglist; arg != NULL; arg = arg->next) {
1699 struct reflist *tpat;
1701 if (arg->name[0] == c
1702 && arg->length == id_len
1703 && strncmp (arg->name, id_beg, id_len) == 0) {
1706 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1707 if (cppReader_isTraditional (pfile)) {
1708 cppReader_warning (pfile,
1709 message ("macro argument `%x' is stringified.",
1710 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1712 cppReader_warning (pfile,
1713 message ("macro arg `%x' would be stringified with -traditional.",
1714 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1717 /* If ANSI, don't actually substitute inside a string. */
1718 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1719 /*@innerbreak@*/ break;
1720 /* make a pat node for this arg and append it to the end of
1722 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1724 tpat->raw_before = (concat == id_beg);
1725 tpat->raw_after = 0;
1726 tpat->rest_args = arg->rest_args;
1727 tpat->stringify = (cppReader_isTraditional (pfile)
1728 ? expected_delimiter != '\0'
1729 : stringify == id_beg);
1733 defn->pattern = tpat;
1737 endpat->next = tpat;
1739 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1743 tpat->argno = arg->argno;
1744 tpat->nchars = exp_p - lastp;
1748 SKIP_WHITE_SPACE (p1);
1750 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1752 tpat->raw_after = 1;
1755 lastp = exp_p; /* place to start copying from next time */
1758 /*@innerbreak@*/ break;
1763 /* If this was not a macro arg, copy it into the expansion. */
1764 if (skipped_arg == 0) {
1765 register char *lim1 = p;
1773 if (stringify == id_beg)
1774 cppReader_errorLit (pfile,
1775 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1780 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1782 /* If ANSI, put in a "@ " marker to prevent token pasting.
1783 But not if "inside a string" (which in ANSI mode
1784 happens only for -D option). */
1791 defn->length = size_fromInt (exp_p - defn->expansion);
1793 /* Crash now if we overrun the allocated size. */
1794 if (defn->length + 1 > maxsize)
1796 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1804 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1808 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1809 int nargs, /*@null@*/ struct arglist *arglist)
1812 char *p, *lastp, *exp_p;
1813 struct reflist *endpat = NULL;
1814 /* Pointer to first nonspace after last ## seen. */
1816 /* Pointer to first nonspace after last single-# seen. */
1817 char *stringify = 0;
1819 char expected_delimiter = '\0';
1822 /* Scan thru the replacement list, ignoring comments and quoted
1823 strings, picking up on the macro calls. It does a linear search
1824 thru the arg list on every potential symbol. Profiling might say
1825 that something smarter should happen. */
1829 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1830 fileloc_unparse (loc)));
1833 /* Find the beginning of the trailing whitespace. */
1836 while (p < limit && is_space[(int) limit[-1]])
1841 /* Allocate space for the text in the macro definition.
1842 Leading and trailing whitespace chars need 2 bytes each.
1843 Each other input char may or may not need 1 byte,
1844 so this is an upper bound. The extra 5 are for invented
1845 leading and trailing newline-marker and final null. */
1846 maxsize = (sizeof (*defn) + (limit - p) + 5);
1848 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1857 defn = (DEFINITION *) dmalloc (maxsize);
1858 defn->noExpand = FALSE;
1860 defn->pattern = NULL;
1861 defn->nargs = nargs;
1862 defn->predefined = NULL;
1864 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1867 defn->rest_args = NULL;
1868 defn->args.argnames = NULL;
1874 /* Add one initial space escape-marker to prevent accidental
1875 token-pasting (often removed by cpplib_macroExpand). */
1879 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1880 voptgenerror (FLG_PREPROC,
1881 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1886 /* Process the main body of the definition. */
1888 int skipped_arg = 0;
1889 register char c = *p++;
1893 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1897 if (expected_delimiter != '\0')
1899 if (c == expected_delimiter)
1900 expected_delimiter = '\0';
1904 expected_delimiter = c;
1906 /*@switchbreak@*/ break;
1909 if (p < limit && (expected_delimiter != '\0'))
1911 /* In a string, backslash goes through
1912 and makes next char ordinary. */
1915 /*@switchbreak@*/ break;
1918 /* An '@' in a string or character constant stands for itself,
1919 and does not need to be escaped. */
1920 if (expected_delimiter == '\0')
1925 /*@switchbreak@*/ break;
1928 /* # is ordinary inside a string. */
1929 if (expected_delimiter != '\0')
1931 /*@switchbreak@*/ break;
1934 if (p < limit && *p == '#') {
1935 /* ##: concatenate preceding and following tokens. */
1936 /* Take out the first #, discard preceding whitespace. */
1940 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1946 /* Skip the second #. */
1948 /* Discard following whitespace. */
1949 SKIP_WHITE_SPACE (p);
1953 voptgenerror (FLG_PREPROC,
1954 cstring_makeLiteral ("`##' at end of macro definition"),
1957 } else if (nargs >= 0) {
1958 /* Single #: stringify following argument ref.
1959 Don't leave the # in the expansion. */
1961 SKIP_WHITE_SPACE (p);
1962 if (p == limit || ! is_idstart[(int) *p]
1963 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1967 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1976 /*@switchbreak@*/ break;
1979 /* In -traditional mode, recognize arguments inside strings and
1980 and character constants, and ignore special properties of #.
1981 Arguments inside strings are considered "stringified", but no
1982 extra quote marks are supplied. */
1986 if (expected_delimiter != '\0') {
1987 if (c == expected_delimiter)
1988 expected_delimiter = '\0';
1990 expected_delimiter = c;
1991 /*@switchbreak@*/ break;
1994 /* Backslash quotes delimiters and itself, but not macro args. */
1995 if (expected_delimiter != '\0' && p < limit
1996 && (*p == expected_delimiter || *p == '\\')) {
2000 /*@switchbreak@*/ break;
2003 if (expected_delimiter != '\0') /* No comments inside strings. */
2004 /*@switchbreak@*/ break;
2006 /* If we find a comment that wasn't removed by cppReader_handleDirective,
2007 this must be -traditional. So replace the comment with
2011 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2016 /*@switchbreak@*/ break;
2020 /* Handle the start of a symbol. */
2021 if (is_idchar[(int) c] && nargs > 0) {
2022 char *id_beg = p - 1;
2026 while (p != limit && is_idchar[(int) *p])
2031 id_len = size_fromInt (p - id_beg);
2033 if (is_idstart[(int) c]
2034 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2035 register struct arglist *arg;
2037 for (arg = arglist; arg != NULL; arg = arg->next) {
2038 struct reflist *tpat;
2040 if (arg->name[0] == c
2041 && arg->length == id_len
2042 && strncmp (arg->name, id_beg, id_len) == 0) {
2045 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2046 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2047 voptgenerror (FLG_PREPROC,
2048 message ("macro argument `%x' is stringified.",
2049 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2053 voptgenerror (FLG_PREPROC,
2054 message ("Macro arg `%x' would be stringified with -traditional.",
2055 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2060 /* If ANSI, don't actually substitute inside a string. */
2061 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2062 /*@innerbreak@*/ break;
2063 /* make a pat node for this arg and append it to the end of
2065 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2067 tpat->raw_before = (concat == id_beg);
2068 tpat->raw_after = 0;
2069 tpat->rest_args = arg->rest_args;
2070 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2071 ? expected_delimiter != '\0'
2072 : stringify == id_beg);
2076 defn->pattern = tpat;
2080 endpat->next = tpat;
2082 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2086 tpat->argno = arg->argno;
2087 tpat->nchars = exp_p - lastp;
2091 SKIP_WHITE_SPACE (p1);
2093 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2095 tpat->raw_after = 1;
2098 lastp = exp_p; /* place to start copying from next time */
2101 /*@innerbreak@*/ break;
2106 /* If this was not a macro arg, copy it into the expansion. */
2107 if (skipped_arg == 0) {
2108 register char *lim1 = p;
2116 if (stringify == id_beg)
2120 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2127 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2129 /* If ANSI, put in a "@ " marker to prevent token pasting.
2130 But not if "inside a string" (which in ANSI mode
2131 happens only for -D option). */
2138 defn->length = size_fromInt (exp_p - defn->expansion);
2140 /* Crash now if we overrun the allocated size. */
2141 if (defn->length + 1 > maxsize)
2143 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2150 * special extension string that can be added to the last macro argument to
2151 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2152 * #define wow(a, b...) process (b, a, b)
2153 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2154 * { wow (one, two); } -> { process (two, one, two); }
2155 * if this "rest_arg" is used with the concat token '##' and if it is not
2156 * supplied then the token attached to with ## will not be outputted. Ex:
2157 * #define wow (a, b...) process (b ## , a, ## b)
2158 * { wow (1, 2); } -> { process (2, 1, 2); }
2159 * { wow (one); } -> { process (one); {
2162 /*@-readonlytrans@*/
2163 static char rest_extension[] = "...";
2164 /*:=readonlytrans@*/
2167 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2169 /* Create a DEFINITION node from a #define directive. Arguments are
2170 as for do_define. */
2173 static /*@null@*/ macroDef
2174 create_definition (/*@exposed@*/ char *buf, char *limit,
2175 cppReader *pfile, bool predefinition,
2178 char *bp; /* temp ptr into input buffer */
2179 char *symname; /* remember where symbol name starts */
2180 size_t sym_length; /* and how long it is */
2181 int rest_args = 0; /* really int! */
2184 cstring file = (CPPBUFFER (pfile) != NULL)
2185 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2187 int arglengths = 0; /* Accumulate lengths of arg names
2188 plus number of args. */
2192 DPRINTF (("Create definition: %s", buf));
2195 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2199 while (is_hor_space[(int) *bp])
2204 symname = bp; /* remember where it starts */
2206 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2210 /* Lossage will occur if identifiers or control keywords are broken
2211 across lines using backslash. This is not the right place to take
2215 struct arglist *arg_ptrs = NULL;
2218 bp++; /* skip '(' */
2219 SKIP_WHITE_SPACE (bp);
2221 /* Loop over macro argument names. */
2224 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2226 temp->next = arg_ptrs;
2227 temp->argno = argno++;
2228 temp->rest_args = 0;
2234 cppReader_pedwarn (pfile,
2235 message ("another parameter follows `%s'",
2236 cstring_fromChars (rest_extension)));
2239 if (!is_idstart[(int) *bp])
2241 cppReader_pedwarnLit (pfile,
2242 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
2245 /* Find the end of the arg name. */
2246 while (is_idchar[(int) *bp])
2249 /* do we have a "special" rest-args extension here? */
2250 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2251 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2254 temp->rest_args = 1;
2255 /*@innerbreak@*/ break;
2259 temp->length = size_fromInt (bp - temp->name);
2263 bp += REST_EXTENSION_LENGTH;
2266 arglengths += temp->length + 2;
2267 SKIP_WHITE_SPACE (bp);
2269 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2270 cppReader_errorLit (pfile,
2271 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2277 SKIP_WHITE_SPACE (bp);
2280 cppReader_errorLit (pfile,
2281 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2285 struct arglist *otemp;
2287 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2289 if (temp->length == otemp->length &&
2290 strncmp (temp->name, otemp->name, temp->length) == 0) {
2291 cstring name = cstring_copyLength (temp->name, temp->length);
2292 cppReader_error (pfile,
2293 message ("duplicate argument name `%x' in `#define'", name));
2300 ++bp; /* skip paren */
2301 SKIP_WHITE_SPACE (bp);
2302 /* now everything from bp before limit is the definition. */
2303 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2304 defn->rest_args = rest_args;
2306 /* Now set defn->args.argnames to the result of concatenating
2307 the argument names in reverse order
2308 with comma-space between them. */
2309 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2312 struct arglist *temp;
2314 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2316 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2318 if (temp->next != 0)
2320 defn->args.argnames[i++] = ',';
2321 defn->args.argnames[i++] = ' ';
2325 defn->args.argnames[i] = '\0';
2330 /* Simple expansion or empty definition. */
2334 if (is_hor_space[(int) *bp]) {
2336 SKIP_WHITE_SPACE (bp);
2339 case '!': case '\"': case '#': case '%': case '&': case '\'':
2340 case ')': case '*': case '+': case ',': case '-': case '.':
2341 case '/': case ':': case ';': case '<': case '=': case '>':
2342 case '?': case '[': case '\\': case ']': case '^': case '{':
2343 case '|': case '}': case '~':
2344 cppReader_warning (pfile,
2345 message ("Missing white space after #define %x",
2346 cstring_prefix (cstring_fromChars (symname),
2351 cppReader_pedwarn (pfile,
2352 message ("Missing white space after #define %x",
2353 cstring_prefix (cstring_fromChars (symname),
2359 /* now everything from bp before limit is the definition. */
2360 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2361 defn->args.argnames = mstring_createEmpty ();
2364 defn->noExpand = noExpand;
2365 DPRINTF (("No expand: %d", noExpand));
2369 /* not: llassert (cstring_isUndefined (defn->file)); */
2372 /* OP is null if this is a predefinition */
2373 defn->predefined = predefinition;
2375 mdef.symnam = symname;
2376 mdef.symlen = sym_length;
2387 cpplib_createDefinition (cstring def,
2392 char *buf = cstring_toCharsSafe (def);
2393 char *limit = buf + cstring_length (def);
2394 char *bp; /* temp ptr into input buffer */
2395 char *symname; /* remember where symbol name starts */
2396 size_t sym_length; /* and how long it is */
2397 int rest_args = 0; /* really int! */
2398 int line = fileloc_lineno (loc);
2399 cstring file = fileloc_filename (loc);
2401 int arglengths = 0; /* Accumulate lengths of arg names
2402 plus number of args. */
2407 DPRINTF (("Creating definition: %s", buf));
2409 while (is_hor_space[(int) *bp])
2414 symname = bp; /* remember where it starts */
2416 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2418 DPRINTF (("length: %d", sym_length));
2422 DPRINTF (("Here: %s", bp));
2424 /* Lossage will occur if identifiers or control keywords are broken
2425 across lines using backslash. This is not the right place to take
2429 struct arglist *arg_ptrs = NULL;
2432 bp++; /* skip '(' */
2433 SKIP_WHITE_SPACE (bp);
2435 /* Loop over macro argument names. */
2438 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2440 temp->next = arg_ptrs;
2441 temp->argno = argno++;
2442 temp->rest_args = 0;
2448 voptgenerror (FLG_PREPROC,
2449 message ("Another parameter follows %s",
2450 cstring_fromChars (rest_extension)),
2454 if (!is_idstart[(int) *bp])
2456 voptgenerror (FLG_PREPROC,
2457 message ("Invalid character in macro parameter name: %c", *bp),
2461 /* Find the end of the arg name. */
2462 while (is_idchar[(int) *bp])
2465 /* do we have a "special" rest-args extension here? */
2466 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2467 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2470 temp->rest_args = 1;
2471 /*@innerbreak@*/ break;
2475 temp->length = size_fromInt (bp - temp->name);
2479 bp += REST_EXTENSION_LENGTH;
2482 arglengths += temp->length + 2;
2483 SKIP_WHITE_SPACE (bp);
2485 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2486 voptgenerror (FLG_PREPROC,
2487 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2494 SKIP_WHITE_SPACE (bp);
2497 voptgenerror (FLG_PREPROC,
2498 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2503 struct arglist *otemp;
2505 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2507 if (temp->length == otemp->length &&
2508 strncmp (temp->name, otemp->name, temp->length) == 0) {
2509 cstring name = cstring_copyLength (temp->name, temp->length);
2511 voptgenerror (FLG_PREPROC,
2512 message ("Duplicate argument name in #define: %s", name),
2520 ++bp; /* skip paren */
2521 SKIP_WHITE_SPACE (bp);
2522 /* now everything from bp before limit is the definition. */
2523 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2524 defn->rest_args = rest_args;
2526 /* Now set defn->args.argnames to the result of concatenating
2527 the argument names in reverse order
2528 with comma-space between them. */
2529 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2532 struct arglist *temp;
2534 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2535 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2537 if (temp->next != 0) {
2538 defn->args.argnames[i++] = ',';
2539 defn->args.argnames[i++] = ' ';
2543 defn->args.argnames[i] = '\0';
2548 /* Simple expansion or empty definition. */
2552 if (is_hor_space[(int) *bp]) {
2554 SKIP_WHITE_SPACE (bp);
2557 case '!': case '\"': case '#': case '%': case '&': case '\'':
2558 case ')': case '*': case '+': case ',': case '-': case '.':
2559 case '/': case ':': case ';': case '<': case '=': case '>':
2560 case '?': case '[': case '\\': case ']': case '^': case '{':
2561 case '|': case '}': case '~':
2562 voptgenerror (FLG_PREPROC,
2563 message ("Missing white space after #define %x",
2564 cstring_prefix (cstring_fromChars (symname),
2570 voptgenerror (FLG_PREPROC,
2571 message ("Missing white space after #define %x",
2572 cstring_prefix (cstring_fromChars (symname),
2580 /* now everything from bp before limit is the definition. */
2581 llassert (limit > bp);
2582 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2583 defn->args.argnames = mstring_createEmpty ();
2586 defn->noExpand = noExpand;
2587 DPRINTF (("No expand: %d", noExpand));
2591 /* not: llassert (cstring_isUndefined (defn->file)); */
2594 /* OP is null if this is a predefinition */
2595 defn->predefined = predefinition;
2598 mdef.symnam = symname;
2599 mdef.symlen = sym_length;
2609 /* Check a purported macro name SYMNAME, and yield its length.
2610 USAGE is the kind of name this is intended for. */
2612 size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2617 for (p = symname; is_idchar[(int) *p]; p++)
2622 sym_length = size_fromInt (p - symname);
2625 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2627 cppReader_error (pfile, message ("invalid %s name", usage));
2629 else if (!is_idstart[(int) *symname])
2631 char *msg = (char *) dmalloc (sym_length + 1);
2632 memcpy (msg, symname, sym_length);
2633 msg[sym_length] = '\0';
2634 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2635 cstring_fromChars (msg)));
2640 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2642 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2651 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2654 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2659 for (p = symname; is_idchar[(int) *p]; p++)
2664 sym_length = size_fromInt (p - symname);
2667 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2669 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2670 cstring_fromChars (symname)), loc);
2672 else if (!is_idstart[(int) *symname])
2674 char *msg = (char *) dmalloc (sym_length + 1);
2675 memcpy (msg, symname, sym_length);
2676 msg[sym_length] = '\0';
2677 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2678 cstring_fromChars (msg)),
2684 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2686 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2693 /* Return zero if two DEFINITIONs are isomorphic. */
2696 compare_defs (DEFINITION *d1, DEFINITION *d2)
2698 register struct reflist *a1, *a2;
2699 register char *p1 = d1->expansion;
2700 register char *p2 = d2->expansion;
2703 if (d1->nargs != d2->nargs)
2708 llassert (d1->args.argnames != NULL);
2709 llassert (d2->args.argnames != NULL);
2711 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2716 for (a1 = d1->pattern, a2 = d2->pattern;
2717 (a1 != NULL) && (a2 != NULL);
2718 a1 = a1->next, a2 = a2->next) {
2719 if (!((a1->nchars == a2->nchars
2720 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2721 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2722 || a1->argno != a2->argno
2723 || a1->stringify != a2->stringify
2724 || a1->raw_before != a2->raw_before
2725 || a1->raw_after != a2->raw_after)
2734 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2735 p2, d2->length - (p2 - d2->expansion), 1))
2741 /* Return TRUE if two parts of two macro definitions are effectively different.
2742 One of the parts starts at BEG1 and has LEN1 chars;
2743 the other has LEN2 chars at BEG2.
2744 Any sequence of whitespace matches any other sequence of whitespace.
2745 FIRST means these parts are the first of a macro definition;
2746 so ignore leading whitespace entirely.
2747 LAST means these parts are the last of a macro definition;
2748 so ignore trailing whitespace entirely. */
2751 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2753 char *end1 = beg1 + len1;
2754 char *end2 = beg2 + len2;
2757 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2758 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2761 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2762 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2764 while (beg1 != end1 && beg2 != end2) {
2765 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2766 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2767 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2768 } else if (*beg1 == *beg2) {
2772 return (beg1 != end1) || (beg2 != end2);
2775 /* Process a #define command.
2776 BUF points to the contents of the #define command, as a contiguous string.
2777 LIMIT points to the first character past the end of the definition.
2778 KEYWORD is the keyword-table entry for #define,
2779 or NULL for a "predefined" macro. */
2782 do_defineAux (cppReader *pfile, struct directive *keyword,
2783 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2789 DPRINTF (("Define aux: %d", noExpand));
2791 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2796 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2798 DPRINTF (("Macro: %s / %s",
2799 cstring_copyLength (mdef.symnam, mdef.symlen),
2800 bool_unparse (noExpand)));
2802 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2806 /* Redefining a precompiled key is ok. */
2807 if (hp->type == T_PCSTRING)
2809 /* Redefining a macro is ok if the definitions are the same. */
2810 else if (hp->type == T_MACRO)
2811 ok = !compare_defs (mdef.defn, hp->value.defn);
2812 /* Redefining a constant is ok with -D. */
2813 else if (hp->type == T_CONST)
2814 ok = !CPPOPTIONS (pfile)->done_initializing;
2819 /* Print the warning if it's not ok. */
2823 ** If we are passing through #define and #undef directives, do
2824 ** that for this re-definition now.
2827 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2829 /* llassert (keyword != NULL); */
2830 pass_thru_directive (buf, limit, pfile, keyword);
2833 cpp_setLocation (pfile);
2835 if (hp->type == T_MACRO)
2837 if (hp->value.defn->noExpand)
2839 ; /* error will be reported checking macros */
2845 message ("Macro %q already defined",
2846 cstring_copyLength (mdef.symnam, mdef.symlen)),
2847 message ("%q: Previous definition of %q",
2848 fileloc_unparseRaw (hp->value.defn->file,
2849 (int) hp->value.defn->line),
2850 cstring_copyLength (mdef.symnam, mdef.symlen)));
2855 genppllerror (FLG_MACROREDEF,
2856 message ("Macro %q already defined",
2857 cstring_copyLength (mdef.symnam,
2863 /* Replace the old definition. */
2865 hp->value.defn = mdef.defn;
2870 ** If we are passing through #define and #undef directives, do
2871 ** that for this new definition now.
2876 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2878 pass_thru_directive (buf, limit, pfile, keyword);
2881 DPRINTF (("Define macro: %s / %d",
2882 mdef.symnam, mdef.defn->noExpand));
2884 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2886 } /*@=branchstate@*/
2896 do_define (cppReader *pfile, struct directive *keyword,
2897 /*@exposed@*/ char *buf, char *limit)
2899 DPRINTF (("Regular do define"));
2900 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2903 /* This structure represents one parsed argument in a macro call.
2904 `raw' points to the argument text as written (`raw_length' is its length).
2905 `expanded' points to the argument's macro-expansion
2906 (its length is `expand_length').
2907 `stringified_length' is the length the argument would have
2909 `use_count' is the number of times this macro arg is substituted
2910 into the macro. If the actual use count exceeds 10,
2911 the value stored is 10. */
2913 /* raw and expanded are relative to ARG_BASE */
2915 #define ARG_BASE ((pfile)->token_buffer)
2918 /* Strings relative to pfile->token_buffer */
2924 int stringified_length;
2929 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2930 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2931 as the new input buffer.
2932 Return the new buffer, or NULL on failure. */
2934 /*@null@*/ /*@exposed@*/ cppBuffer *
2935 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2937 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2939 if (buf == pfile->buffer_stack)
2941 cppReader_fatalError
2943 message ("%s: macro or `#include' recursion too deep",
2944 (buf->fname != NULL)
2946 : cstring_makeLiteral ("<no name>")));
2947 sfreeEventually (buffer);
2951 llassert (buf != NULL);
2954 memset ((char *) buf, 0, sizeof (*buf));
2955 DPRINTF (("Pushing buffer: %s", cstring_copyLength (buffer, length)));
2956 CPPBUFFER (pfile) = buf;
2958 buf->if_stack = pfile->if_stack;
2959 buf->cleanup = cppReader_nullCleanup;
2960 buf->underflow = cppReader_nullUnderflow;
2962 buf->cur = buf->buf;
2966 buf->alimit = buf->rlimit = buffer + length;
2970 buf->alimit = buf->rlimit = NULL;
2977 cppReader_popBuffer (cppReader *pfile)
2979 cppBuffer *buf = CPPBUFFER (pfile);
2981 llassert (buf != NULL);
2983 (void) (*buf->cleanup) (buf, pfile);
2984 return ++CPPBUFFER (pfile);
2987 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2988 Pop the buffer when done. */
2991 cppReader_scanBuffer (cppReader *pfile)
2993 cppBuffer *buffer = CPPBUFFER (pfile);
2996 enum cpp_token token;
2998 token = cpplib_getToken (pfile);
3000 if (token == CPP_EOF) /* Should not happen ... */
3005 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
3007 (void) cppReader_popBuffer (pfile);
3015 * Rescan a string (which may have escape marks) into pfile's buffer.
3016 * Place the result in pfile->token_buffer.
3018 * The input is copied before it is scanned, so it is safe to pass
3019 * it something from the token_buffer that will get overwritten
3020 * (because it follows cpplib_getWritten). This is used by do_include.
3024 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3026 register cppBuffer *ip;
3027 char *limit = buf + length;
3028 char *buf1, *p1, *p2;
3030 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
3032 /* evans - 2001-08-26
3033 ** length is unsigned - this doesn't make sense
3039 /* Set up the input on the input stack. */
3041 buf1 = (char *) dmalloc (length + 1);
3051 buf1[length] = '\0';
3053 ip = cppReader_pushBuffer (pfile, buf1, length);
3058 ip->has_escapes = TRUE;
3060 /* Scan the input, create the output. */
3061 cppReader_scanBuffer (pfile);
3063 cppReader_nullTerminate (pfile);
3067 adjust_position (char *buf, char *limit, int *linep, int *colp)
3073 (*linep)++, (*colp) = 1;
3079 /* Move line_base forward, updating lineno and colno. */
3082 update_position (cppBuffer *pbuf)
3085 char *new_pos = pbuf->cur;
3086 register struct parse_marker *mark;
3088 llassert (pbuf->buf != NULL);
3089 old_pos = pbuf->buf + pbuf->line_base;
3091 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3093 if (pbuf->buf + mark->position < new_pos)
3094 new_pos = pbuf->buf + mark->position;
3096 pbuf->line_base += new_pos - old_pos;
3098 llassert (old_pos != NULL);
3099 llassert (new_pos != NULL);
3101 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3105 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3106 /*@null@*/ /*@out@*/ int *colp)
3114 } /*@=branchstate@*/
3118 *linep = pbuf->lineno;
3119 *colp = pbuf->colno;
3121 llassert (pbuf->buf != NULL);
3122 llassert (pbuf->cur != NULL);
3124 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3133 /* Return the cppBuffer that corresponds to a file (not a macro). */
3135 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3137 cppBuffer *ip = cppReader_getBuffer (pfile);
3140 ip != NULL && ip != cppReader_nullBuffer (pfile);
3141 ip = cppBuffer_prevBuffer (ip))
3143 if (ip->fname != NULL)
3153 count_newlines (char *buf, char *limit)
3155 register long count = 0;
3167 * write out a #line command, for instance, after an #include file.
3168 * If CONDITIONAL is nonzero, we can omit the #line if it would
3169 * appear to be a no-op, and we can output a few newlines instead
3170 * if we want to increase the line number by a small amount.
3171 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3175 output_line_command (cppReader *pfile, bool conditional,
3176 enum file_change_code file_change)
3179 cppBuffer *ip = CPPBUFFER (pfile);
3182 llassert (ip != NULL);
3184 if (ip->fname == NULL)
3187 update_position (ip);
3189 if (CPPOPTIONS (pfile)->no_line_commands
3190 || CPPOPTIONS (pfile)->no_output)
3193 buf = CPPBUFFER (pfile);
3195 llassert (buf != NULL);
3200 llassert (ip->cur != NULL);
3202 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3204 if (CPPOPTIONS (pfile)->no_line_commands)
3208 if (line == pfile->lineno)
3211 /* If the inherited line number is a little too small,
3212 output some newlines instead of a #line command. */
3214 if (line > pfile->lineno && line < pfile->lineno + 8)
3216 cpplib_reserve (pfile, 20);
3217 while (line > pfile->lineno)
3219 cppReader_putCharQ (pfile, '\n');
3227 cpplib_reserve (pfile,
3228 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
3231 #ifdef OUTPUT_LINE_COMMANDS
3232 static char sharp_line[] = "#line ";
3234 static char sharp_line[] = "# ";
3236 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3239 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3240 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3242 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3244 if (file_change != same_file) {
3245 cppReader_putCharQ (pfile, ' ');
3246 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3248 /* Tell cc1 if following text comes from a system header file. */
3249 if (ip->system_header_p != '\0') {
3250 cppReader_putCharQ (pfile, ' ');
3251 cppReader_putCharQ (pfile, '3');
3253 #ifndef NO_IMPLICIT_EXTERN_C
3254 /* Tell cc1plus if following text should be treated as C. */
3255 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3256 cppReader_putCharQ (pfile, ' ');
3257 cppReader_putCharQ (pfile, '4');
3260 cppReader_putCharQ (pfile, '\n');
3261 pfile->lineno = line;
3266 * Parse a macro argument and append the info on PFILE's token_buffer.
3267 * REST_ARGS means to absorb the rest of the args.
3268 * Return nonzero to indicate a syntax error.
3271 static enum cpp_token
3272 macarg (cppReader *pfile, int rest_args)
3275 enum cpp_token token;
3276 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3277 bool oldexpand = pfile->no_macro_expand;
3278 CPPOPTIONS (pfile)->put_out_comments = 1;
3280 /* Try to parse as much of the argument as exists at this
3281 input stack level. */
3283 pfile->no_macro_expand = TRUE;
3287 token = cpplib_getToken (pfile);
3294 /* If we've hit end of file, it's an error (reported by caller).
3295 Ditto if it's the end of cpp_expand_to_buffer text.
3296 If we've hit end of macro, just continue. */
3297 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3299 /*@switchbreak@*/ break;
3302 /*@switchbreak@*/ break;
3306 /*@switchbreak@*/ break;
3308 /* if we've returned to lowest level and
3309 we aren't absorbing all args */
3310 if (paren == 0 && rest_args == 0)
3312 /*@switchbreak@*/ break;
3314 /* Remove ',' or ')' from argument buffer. */
3315 cppReader_adjustWritten (pfile, -1);
3323 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3324 pfile->no_macro_expand = oldexpand;
3330 /* Turn newlines to spaces in the string of length LENGTH at START,
3331 except inside of string constants.
3332 The string is copied into itself with its beginning staying fixed. */
3335 change_newlines (char *start, int length)
3339 register char *limit;
3343 limit = start + length;
3346 while (ibp < limit) {
3347 *obp++ = c = *ibp++;
3352 /* Notice and skip strings, so that we don't delete newlines in them. */
3355 while (ibp < limit) {
3356 *obp++ = c = *ibp++;
3358 /*@innerbreak@*/ break;
3359 if (c == '\n' && quotec == '\'')
3360 /*@innerbreak@*/ break;
3363 /*@switchbreak@*/ break;
3370 static /*@observer@*/ struct tm *
3371 timestamp (/*@returned@*/ cppReader *pfile)
3373 if (pfile->timebuf == NULL)
3375 time_t t = time ((time_t *) 0);
3376 pfile->timebuf = localtime (&t);
3379 llassert (pfile->timebuf != NULL);
3381 return pfile->timebuf;
3384 static ob_mstring monthnames[] = {
3385 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3386 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3390 * expand things like __FILE__. Place the expansion into the output
3391 * buffer *without* rescanning.
3395 special_symbol (hashNode hp, cppReader *pfile)
3397 cstring buf = cstring_undefined;
3403 int paren = 0; /* For special `defined' keyword */
3405 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3407 if (ip == cppReader_nullBuffer (pfile))
3409 cppReader_errorLit (pfile,
3410 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3411 return; /* the show must go on */
3414 if (ip != NULL && ip->fname != NULL)
3426 if (hp->type == T_BASE_FILE)
3428 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3430 ip = cppBuffer_prevBuffer (ip);
3434 llassert (ip != NULL);
3435 string = cstring_toCharsSafe (ip->nominal_fname);
3442 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3443 quote_string (pfile, string);
3447 case T_INCLUDE_LEVEL:
3449 ip = cppReader_getBuffer (pfile);
3451 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3452 ip = cppBuffer_prevBuffer (ip))
3454 if (ip != NULL && ip->fname != NULL)
3460 buf = message ("%d", true_indepth - 1);
3464 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3467 #ifndef NO_BUILTIN_SIZE_TYPE
3469 buf = cstring_makeLiteral (SIZE_TYPE);
3473 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3474 case T_PTRDIFF_TYPE:
3475 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3480 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3483 case T_USER_LABEL_PREFIX_TYPE:
3484 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3487 case T_REGISTER_PREFIX_TYPE:
3488 buf = cstring_makeLiteral (REGISTER_PREFIX);
3492 buf = message ("%d", hp->value.ival);
3499 int line = ip->lineno;
3500 int col = ip->colno;
3502 llassert (ip->cur != NULL);
3503 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3505 buf = message ("%d", (int) line);
3517 char *sbuf = (char *) dmalloc (20);
3518 timebuf = timestamp (pfile);
3519 if (hp->type == T_DATE)
3521 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3522 timebuf->tm_mday, timebuf->tm_year + 1900);
3526 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3530 buf = cstring_fromCharsNew (sbuf);
3535 case T_SPEC_DEFINED:
3536 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3537 ip = cppReader_getBuffer (pfile);
3538 llassert (ip != NULL);
3539 llassert (ip->cur != NULL);
3540 SKIP_WHITE_SPACE (ip->cur);
3542 if (*ip->cur == '(')
3545 ip->cur++; /* Skip over the paren */
3546 SKIP_WHITE_SPACE (ip->cur);
3549 if (!is_idstart[(int) *ip->cur])
3551 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3554 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3557 buf = cstring_makeLiteral (" 1 ");
3560 while (is_idchar[(int) *ip->cur])
3565 SKIP_WHITE_SPACE (ip->cur);
3569 if (*ip->cur != ')')
3577 cppReader_errorLit (pfile,
3578 cstring_makeLiteralTemp ("`defined' without an identifier"));
3582 cpp_setLocation (pfile);
3583 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3586 len = cstring_length (buf);
3588 cpplib_reserve (pfile, len + 1);
3589 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3590 cppReader_nullTerminateQ (pfile);
3596 /* Write out a #define command for the special named MACRO_NAME
3597 to PFILE's token_buffer. */
3600 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3602 static char define_directive[] = "#define ";
3603 size_t macro_name_length = strlen (macro_name);
3604 output_line_command (pfile, 0, same_file);
3605 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3606 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3607 cppReader_putStrN (pfile, macro_name, macro_name_length);
3608 cppReader_putCharQ (pfile, ' ');
3609 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3610 cppReader_putChar (pfile, '\n');
3613 /* Initialize the built-in macros. */
3616 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3617 int len, enum node_type type,
3618 int ivalue, /*@null@*/ /*@only@*/ char *value,
3621 cstring sname = cstring_fromCharsNew (name);
3623 llassert (usymtab_inGlobalScope ());
3626 ** Be careful here: this is done before the ctype table has
3627 ** been initialized.
3630 if (!usymtab_exists (sname))
3632 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3634 if (ctype_equal (ctyp, ctype_string))
3636 qualList ql = qualList_new ();
3637 ql = qualList_add (ql, qual_createObserver ());
3638 uentry_reflectQualifiers (ue, ql);
3642 usymtab_addGlobalEntry (ue);
3649 (void) cpphash_install (name, len, type, ivalue, value, hash);
3650 cstring_free (sname);
3654 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3655 int len, enum node_type type,
3657 /*@only@*/ /*@null@*/ char *value, int hash)
3659 cstring sname = cstring_fromChars (name);
3660 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3662 llassert (usymtab_inGlobalScope ());
3664 if (!usymtab_existsTypeEither (sname))
3666 uentry ue = uentry_makeDatatype (sname, ctyp,
3668 fileloc_createBuiltin ());
3669 llassert (!usymtab_existsEither (sname));
3670 usymtab_addGlobalEntry (ue);
3673 (void) cpphash_install (name, len, type, ivalue, value, hash);
3677 initialize_builtins (cppReader *pfile)
3679 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3680 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3681 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3682 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3683 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3684 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3685 #ifndef NO_BUILTIN_SIZE_TYPE
3686 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3688 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3689 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3691 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3692 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3693 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3694 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3697 ** No, don't define __STDC__
3700 if (!cppReader_isTraditional (pfile))
3702 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3709 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3713 ** This is supplied using a -D by the compiler driver
3714 ** so that it is present only when truly compiling with GNU C.
3717 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3719 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3720 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3722 /*drl 1/9/2001/ try to define the right symbol for the architecture
3723 We use autoconf to determine the target cpu
3725 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3727 /*drl 1/2/2002 set some flags based on uname
3728 I'd like to be able to do this with autoconf macro instead...
3731 /*Thanks to Nelson Beebe for suggesting possible values for these */
3733 if (! strcmp (UNAME, "Linux"))
3736 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3740 else if(! strcmp (UNAME, "Darwin"))
3742 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3743 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3745 else if(! strcmp (UNAME, "HP-UX"))
3747 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3748 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3749 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3750 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3751 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3752 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3753 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3754 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3755 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3756 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3757 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3758 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3759 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3760 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3761 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3762 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3763 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3764 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3765 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3766 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3767 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3769 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3771 else if(! strcmp (UNAME, "IRIX64"))
3773 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3774 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3775 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3777 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3780 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3781 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3782 /*_MIPS_SIM=_ABIN32*/
3783 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3784 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3785 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3786 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3787 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3788 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3789 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3791 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3792 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3793 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3794 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3795 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3796 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3797 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3798 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3799 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3800 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3801 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3802 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3803 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3805 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3806 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3808 else if(! strcmp (UNAME, "OSF1"))
3810 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3812 else if (!strcmp (UNAME, "Rhapsody"))
3814 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3815 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3818 else if (!strcmp (UNAME, "SunOS"))
3820 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3821 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3822 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3823 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3828 types which we have not explictedly handled.
3829 AIX, FreeBSD, IRIX, Mach
3834 if (CPPOPTIONS (pfile)->debug_output)
3836 dump_special_to_buffer (pfile, "__BASE_FILE__");
3837 dump_special_to_buffer (pfile, "__VERSION__");
3838 #ifndef NO_BUILTIN_SIZE_TYPE
3839 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3841 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3842 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3844 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3845 dump_special_to_buffer (pfile, "__DATE__");
3846 dump_special_to_buffer (pfile, "__TIME__");
3847 if (!cppReader_isTraditional (pfile))
3848 dump_special_to_buffer (pfile, "__STDC__");
3853 /* Return 1 iff a token ending in C1 followed directly by a token C2
3854 could cause mis-tokenization. */
3857 unsafe_chars (char c1, char c2)
3862 if (c2 == c1 || c2 == '=')
3866 case '0': case '1': case '2': case '3': case '4':
3867 case '5': case '6': case '7': case '8': case '9':
3868 case 'e': case 'E': case 'p': case 'P':
3869 if (c2 == '-' || c2 == '+')
3870 return 1; /* could extend a pre-processing number */
3873 if (c2 == '\'' || c2 == '\"')
3874 return 1; /* Could turn into L"xxx" or L'xxx'. */
3878 case 'a': case 'b': case 'c': case 'd': case 'f':
3879 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3880 case 'm': case 'n': case 'o': case 'q': case 'r':
3881 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3883 case 'A': case 'B': case 'C': case 'D': case 'F':
3884 case 'G': case 'H': case 'I': case 'J': case 'K':
3885 case 'M': case 'N': case 'O': case 'Q': case 'R':
3886 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3888 /* We're in the middle of either a name or a pre-processing number. */
3889 return (is_idchar[(int) c2] || c2 == '.');
3890 case '<': case '>': case '!': case '%': case '#': case ':':
3891 case '^': case '&': case '|': case '*': case '/': case '=':
3892 return (c2 == c1 || c2 == '=');
3897 /* Expand a macro call.
3898 HP points to the symbol that is the macro being called.
3899 Put the result of expansion onto the input stack
3900 so that subsequent input by our caller will use it.
3902 If macro wants arguments, caller has already verified that
3903 an argument list follows; arguments come from the input stack. */
3906 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3909 DEFINITION *defn = hp->value.defn;
3917 size_t old_written = cpplib_getWritten (pfile);
3921 struct argdata *args = NULL;
3923 pfile->output_escapes++;
3924 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3925 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3927 nargs = defn->nargs;
3931 enum cpp_token token = CPP_EOF;
3933 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3935 for (i = 0; i < nargs; i++)
3937 args[i].expanded = 0;
3939 args[i].raw_length = 0;
3940 args[i].expand_length = args[i].stringified_length = -1;
3941 args[i].use_count = 0;
3945 ** Parse all the macro args that are supplied. I counts them.
3946 ** The first NARGS args are stored in ARGS.
3947 ** The rest are discarded. If rest_args is set then we assume
3948 ** macarg absorbed the rest of the args.
3954 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3962 if (i < nargs || (nargs == 0 && i == 0))
3964 /* if we are working on last arg which absorbs rest of args... */
3965 if (i == nargs - 1 && defn->rest_args)
3970 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3971 token = macarg (pfile, rest_args);
3972 args[i].raw_length = cpplib_getWritten (pfile) - args[i].raw;
3973 args[i].newlines = FALSE; /* FIXME */
3977 token = macarg (pfile, 0);
3980 if (token == CPP_EOF || token == CPP_POP)
3982 cppReader_errorWithLine (pfile, start_line, start_column,
3983 cstring_fromCharsNew ("unterminated macro call"));
3988 } while (token == CPP_COMMA);
3990 /* If we got one arg but it was just whitespace, call that 0 args. */
3998 bp = ARG_BASE + args[0].raw;
3999 lim = bp + args[0].raw_length;
4001 /* cpp.texi says for foo ( ) we provide one argument.
4002 However, if foo wants just 0 arguments, treat this as 0. */
4006 while (bp != lim && is_space[(int) *bp])
4016 /* Don't output an error message if we have already output one for
4017 a parse error above. */
4020 if (nargs == 0 && i > 0)
4022 cppReader_error (pfile,
4023 message ("arguments given to macro `%s'", hp->name));
4027 /* traditional C allows foo() if foo wants one argument. */
4028 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4032 /* the rest args token is allowed to absorb 0 tokens */
4033 else if (i == nargs - 1 && defn->rest_args)
4036 cppReader_error (pfile,
4037 message ("macro `%s' used without args", hp->name));
4039 cppReader_error (pfile,
4040 message ("macro `%s' used with just one arg", hp->name));
4043 cppReader_error (pfile,
4044 message ("macro `%s' used with only %d args",
4050 cppReader_error (pfile,
4051 message ("macro `%s' used with too many (%d) args", hp->name, i));
4060 ** If the agrument list was multiple lines, need to insert new lines to keep line
4061 ** numbers accurate.
4064 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4065 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4067 /* If macro wants zero args, we parsed the arglist for checking only.
4068 Read directly from the macro definition. */
4072 xbuf = defn->expansion;
4073 xbuf_len = defn->length;
4077 char *exp = defn->expansion;
4078 int offset; /* offset in expansion,
4079 copied a piece at a time */
4080 size_t totlen; /* total amount of exp buffer filled so far */
4082 register struct reflist *ap, *last_ap;
4084 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4086 /* Macro really takes args. Compute the expansion of this call. */
4088 /* Compute length in characters of the macro's expansion.
4089 Also count number of times each arg is used. */
4090 xbuf_len = defn->length;
4092 llassert (args != NULL);
4094 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4098 struct argdata *arg = &args[ap->argno];
4100 /* Stringify it it hasn't already been */
4103 if (arg->stringified_length < 0)
4105 int arglen = arg->raw_length;
4106 bool escaped = FALSE;
4107 char in_string = '\0';
4110 /* Initially need_space is -1. Otherwise, 1 means the
4111 previous character was a space, but we suppressed it;
4112 0 means the previous character was a non-space. */
4113 int need_space = -1;
4116 arg->stringified = cpplib_getWritten (pfile);
4117 if (!cppReader_isTraditional (pfile))
4118 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4119 for (; i < arglen; i++)
4121 c = (ARG_BASE + arg->raw)[i];
4123 if (in_string == '\0')
4125 /* Internal sequences of whitespace are replaced by
4126 one space except within an string or char token.*/
4127 if (is_space[(int) c])
4129 if (cpplib_getWritten (pfile) > arg->stringified
4130 && (cpplib_getPWritten (pfile))[-1] == '@')
4132 /* "@ " escape markers are removed */
4133 cppReader_adjustWritten (pfile, -1);
4134 /*@innercontinue@*/ continue;
4136 if (need_space == 0)
4138 /*@innercontinue@*/ continue;
4140 else if (need_space > 0)
4141 cppReader_putChar (pfile, ' ');
4157 if (in_string != '\0')
4162 else if (c == '\"' || c == '\'')
4172 /* Escape these chars */
4173 if (c == '\"' || (in_string != '\0' && c == '\\'))
4174 cppReader_putChar (pfile, '\\');
4176 cppReader_putChar (pfile, c);
4179 cpplib_reserve (pfile, 4);
4180 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4182 cppReader_adjustWritten (pfile, 4);
4185 if (!cppReader_isTraditional (pfile))
4186 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4187 arg->stringified_length
4188 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4191 xbuf_len += args[ap->argno].stringified_length;
4193 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4195 /* Add 4 for two newline-space markers to prevent
4196 token concatenation. */
4197 assertSet (args); /*@i534 shouldn't need this */
4198 xbuf_len += args[ap->argno].raw_length + 4;
4202 /* We have an ordinary (expanded) occurrence of the arg.
4203 So compute its expansion, if we have not already. */
4205 assertSet (args); /*@i534 shouldn't need this */
4207 if (args[ap->argno].expand_length < 0)
4209 args[ap->argno].expanded = cpplib_getWritten (pfile);
4210 cpp_expand_to_buffer (pfile,
4211 ARG_BASE + args[ap->argno].raw,
4212 size_fromInt (args[ap->argno].raw_length));
4214 args[ap->argno].expand_length
4215 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4218 /* Add 4 for two newline-space markers to prevent
4219 token concatenation. */
4220 xbuf_len += args[ap->argno].expand_length + 4;
4222 if (args[ap->argno].use_count < 10)
4223 args[ap->argno].use_count++;
4226 xbuf = (char *) dmalloc (xbuf_len + 1);
4230 ** Generate in XBUF the complete expansion
4231 ** with arguments substituted in.
4232 ** TOTLEN is the total size generated so far.
4233 ** OFFSET is the index in the definition
4234 ** of where we are copying from.
4240 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4241 last_ap = ap, ap = ap->next)
4243 register struct argdata *arg = &args[ap->argno];
4244 size_t count_before = totlen;
4246 /* Add chars to XBUF. */
4247 for (i = 0; i < ap->nchars; i++, offset++)
4249 xbuf[totlen++] = exp[offset];
4252 /* If followed by an empty rest arg with concatenation,
4253 delete the last run of nonwhite chars. */
4254 if (rest_zero && totlen > count_before
4255 && ((ap->rest_args && ap->raw_before)
4256 || (last_ap != NULL && last_ap->rest_args
4257 && last_ap->raw_after)))
4259 /* Delete final whitespace. */
4260 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4265 /* Delete the nonwhites before them. */
4266 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4272 if (ap->stringify != 0)
4275 memcpy (xbuf + totlen,
4276 ARG_BASE + arg->stringified,
4277 size_fromInt (arg->stringified_length));
4278 totlen += arg->stringified_length;
4280 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4287 p1 = ARG_BASE + arg->raw;
4288 l1 = p1 + arg->raw_length;
4292 while (p1 != l1 && is_space[(int) *p1])
4297 while (p1 != l1 && is_idchar[(int) *p1])
4299 xbuf[totlen++] = *p1++;
4302 /* Delete any no-reexpansion marker that follows
4303 an identifier at the beginning of the argument
4304 if the argument is concatenated with what precedes it. */
4305 if (p1[0] == '@' && p1[1] == '-')
4310 /* Arg is concatenated after: delete trailing whitespace,
4311 whitespace markers, and no-reexpansion markers. */
4314 if (is_space[(int) l1[-1]]) l1--;
4315 else if (l1[-1] == '-')
4318 /* If a `-' is preceded by an odd number of newlines then it
4319 and the last newline are a no-reexpansion marker. */
4320 while (p2 != p1 && p2[-1] == '\n')
4325 if (((l1 - 1 - p2) & 1) != 0)
4331 /*@innerbreak@*/ break;
4336 /*@innerbreak@*/ break;
4341 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4349 expanded = ARG_BASE + arg->expanded;
4351 if (!ap->raw_before && totlen > 0
4352 && (arg->expand_length != 0)
4353 && !cppReader_isTraditional(pfile)
4354 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4356 xbuf[totlen++] = '@';
4357 xbuf[totlen++] = ' ';
4360 memcpy (xbuf + totlen, expanded,
4361 size_fromInt (arg->expand_length));
4362 totlen += arg->expand_length;
4364 if (!ap->raw_after && totlen > 0
4365 && offset < size_toInt (defn->length)
4366 && !cppReader_isTraditional(pfile)
4367 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4369 xbuf[totlen++] = '@';
4370 xbuf[totlen++] = ' ';
4373 /* If a macro argument with newlines is used multiple times,
4374 then only expand the newlines once. This avoids creating
4375 output lines which don't correspond to any input line,
4376 which confuses gdb and gcov. */
4377 if (arg->use_count > 1 && arg->newlines > 0)
4379 /* Don't bother doing change_newlines for subsequent
4383 = change_newlines (expanded, arg->expand_length);
4387 if (totlen > xbuf_len)
4391 /* if there is anything left of the definition
4392 after handling the arg list, copy that in too. */
4394 for (i = offset; i < size_toInt (defn->length); i++)
4396 /* if we've reached the end of the macro */
4399 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4400 && last_ap->raw_after))
4401 xbuf[totlen++] = exp[i];
4404 xbuf[totlen] = '\0';
4408 pfile->output_escapes--;
4410 /* Now put the expansion on the input stack
4411 so our caller will commence reading from it. */
4412 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4414 if (end_line != start_line)
4416 /* xbuf must have enough newlines */
4417 int newlines = end_line - start_line;
4418 int foundnewlines = 0;
4419 char *xbufptr = xbuf;
4421 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4426 if (*xbufptr == '\0')
4432 if (foundnewlines < newlines)
4434 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4436 while (foundnewlines < newlines)
4438 newbuf = cstring_appendChar (newbuf, '\n');
4443 xbuf = cstring_toCharsSafe (newbuf);
4444 xbuf_len = cstring_length (newbuf);
4446 } /*@=branchstate@*/
4449 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4451 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4452 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4453 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4455 /* Pop the space we've used in the token_buffer for argument expansion. */
4456 cppReader_setWritten (pfile, old_written);
4457 DPRINTF (("Done set written"));
4459 /* Recursive macro use sometimes works traditionally.
4460 #define foo(x,y) bar (x (y,0), y)
4463 if (!cppReader_isTraditional (pfile))
4464 hp->type = T_DISABLED;
4470 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4471 /*@dependent@*/ hashNode hp)
4473 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4480 mbuf->cleanup = cppReader_macroCleanup;
4482 llassert (mbuf->hnode == NULL);
4485 /* The first chars of the expansion should be a "@ " added by
4486 collect_expansion. This is to prevent accidental token-pasting
4487 between the text preceding the macro invocation, and the macro
4490 We would like to avoid adding unneeded spaces (for the sake of
4491 tools that use cpp, such as imake). In some common cases we can
4492 tell that it is safe to omit the space.
4494 The character before the macro invocation cannot have been an
4495 idchar (or else it would have been pasted with the idchars of
4496 the macro name). Therefore, if the first non-space character
4497 of the expansion is an idchar, we do not need the extra space
4498 to prevent token pasting.
4500 Also, we don't need the extra space if the first char is '(',
4501 or some other (less common) characters. */
4503 if (xbuf[0] == '@' && xbuf[1] == ' '
4504 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4505 || xbuf[2] == '\"'))
4507 llassert (mbuf->cur != NULL);
4508 DPRINTF (("Eating: %c", xbuf[2]));
4515 /* Like cpplib_getToken, except that it does not read past end-of-line.
4516 Also, horizontal space is skipped, and macros are popped. */
4518 static enum cpp_token
4519 get_directive_token (cppReader *pfile)
4523 size_t old_written = cpplib_getWritten (pfile);
4524 enum cpp_token token;
4525 cppSkipHspace (pfile);
4526 if (cppReader_peekC (pfile) == '\n')
4531 token = cpplib_getToken (pfile);
4536 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4541 cppReader_setWritten (pfile, old_written);
4542 /*@switchbreak@*/ break;
4550 /* Handle #include and #import.
4551 This function expects to see "fname" or <fname> on the input.
4553 The input is normally in part of the output_buffer following
4554 cpplib_getWritten, and will get overwritten by output_line_command.
4555 I.e. in input file specification has been popped by cppReader_handleDirective.
4559 do_include (cppReader *pfile, struct directive *keyword,
4560 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4562 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4564 char *fbeg, *fend; /* Beginning and end of fname */
4565 enum cpp_token token;
4567 /* Chain of dirs to search */
4568 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4569 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4570 struct file_name_list *searchptr = NULL;
4571 size_t old_written = cpplib_getWritten (pfile);
4574 int f; /* file number */
4575 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4576 f= -1; /* JF we iz paranoid! */
4578 pfile->parsing_include_directive++;
4579 token = get_directive_token (pfile);
4580 pfile->parsing_include_directive--;
4582 if (token == CPP_STRING)
4584 /* FIXME - check no trailing garbage */
4585 fbeg = pfile->token_buffer + old_written + 1;
4586 fend = cpplib_getPWritten (pfile) - 1;
4587 if (fbeg[-1] == '<')
4590 /* If -I-, start with the first -I dir after the -I-. */
4591 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4592 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4594 /* If -I- was specified, don't search current dir, only spec'd ones. */
4595 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4597 cppBuffer *fp = CPPBUFFER (pfile);
4598 /* We have "filename". Figure out directory this source
4599 file is coming from and put it on the front of the list. */
4601 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4606 llassert (fp != NULL);
4610 if (cstring_isDefined (fp->nominal_fname))
4612 nam = cstring_toCharsSafe (fp->nominal_fname);
4614 /* Found a named file. Figure out dir of the file,
4615 and put it in front of the search list. */
4616 dsp[0].next = search_start;
4620 ep = strrchr (nam, CONNECTCHAR);
4622 ep = strrchr (nam, ']');
4623 if (ep == NULL) ep = strrchr (nam, '>');
4624 if (ep == NULL) ep = strrchr (nam, ':');
4625 if (ep != NULL) ep++;
4635 /*@-onlytrans@*/ /* This looks like a memory leak... */
4636 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4640 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
4641 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
4645 dsp[0].fname = cstring_undefined; /* Current directory */
4648 dsp[0].got_name_map = 0;
4659 else if (token == CPP_NAME)
4662 * Support '#include xyz' like VAX-C to allow for easy use of all the
4663 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4664 * code from case '<' is repeated here) and generates a warning.
4666 cppReader_warning (pfile,
4667 "VAX-C-style include specification found, use '#include <filename.h>' !");
4669 /* If -I-, start with the first -I dir after the -I-. */
4670 if (CPPOPTIONS (pfile)->first_bracket_include)
4671 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4672 fbeg = pfile->token_buffer + old_written;
4673 fend = cpplib_getPWritten (pfile);
4678 cppReader_error (pfile,
4679 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4682 cppReader_setWritten (pfile, old_written);
4683 cppReader_skipRestOfLine (pfile);
4689 token = get_directive_token (pfile);
4690 if (token != CPP_VSPACE)
4692 cppReader_errorLit (pfile,
4693 cstring_makeLiteralTemp ("Junk at end of #include"));
4695 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4697 token = get_directive_token (pfile);
4702 ** For #include_next, skip in the search path
4703 ** past the dir in which the containing file was found.
4708 cppBuffer *fp = CPPBUFFER (pfile);
4710 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4712 llassert (fp != NULL);
4714 if (fp->fname != NULL)
4716 /* fp->dir is null if the containing file was specified with
4717 an absolute file name. In that case, don't skip anything. */
4718 if (fp->dir == SELF_DIR_DUMMY)
4720 search_start = CPPOPTIONS (pfile)->include;
4722 else if (fp->dir != NULL)
4724 search_start = fp->dir->next;
4736 cppReader_setWritten (pfile, old_written);
4738 flen = size_fromInt (fend - fbeg);
4740 DPRINTF (("fbeg: %s", fbeg));
4744 cppReader_error (pfile,
4745 message ("Empty file name in #%s", keyword->name));
4750 ** Allocate this permanently, because it gets stored in the definitions
4754 fname = cstring_undefined;
4756 /* + 2 above for slash and terminating null. */
4757 /* + 2 added for '.h' on VMS (to support '#include filename') */
4759 /* If specified file name is absolute, just open it. */
4761 if (osd_isConnectChar (*fbeg)
4762 # if defined (WIN32) || defined (OS2)
4763 || (*(fbeg + 1) == ':')
4767 fname = cstring_copyLength (fbeg, flen);
4769 if (redundant_include_p (pfile, fname))
4771 cstring_free (fname);
4775 f = open_include_file (pfile, fname, NULL);
4777 if (f == IMPORT_FOUND)
4779 return 0; /* Already included this file */
4784 /* Search directory path, trying to open the file.
4785 Copy each filename tried into FNAME. */
4787 for (searchptr = search_start; searchptr != NULL;
4788 searchptr = searchptr->next)
4790 if (!cstring_isEmpty (searchptr->fname))
4792 /* The empty string in a search path is ignored.
4793 This makes it possible to turn off entirely
4794 a standard piece of the list. */
4795 if (cstring_isEmpty (searchptr->fname))
4798 fname = cstring_copy (searchptr->fname);
4799 fname = cstring_appendChar (fname, CONNECTCHAR);
4800 DPRINTF (("Here: %s", fname));
4807 fname = cstring_concatLength (fname, fbeg, flen);
4809 DPRINTF (("fname: %s", fname));
4811 /* Win32 directory fix from Kay Buschner. */
4812 #if defined (WIN32) || defined (OS2)
4813 /* Fix all unixdir slashes to win dir slashes */
4814 if (searchptr->fname && (searchptr->fname[0] != 0))
4816 cstring_replaceAll (fname, '/', '\\');
4821 /* Change this 1/2 Unix 1/2 VMS file specification into a
4822 full VMS file specification */
4823 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4824 /* Fix up the filename */
4825 hack_vms_include_specification (fname);
4827 /* This is a normal VMS filespec, so use it unchanged. */
4828 strncpy (fname, fbeg, flen);
4830 /* if it's '#include filename', add the missing .h */
4831 if (strchr (fname,'.') == NULL) {
4832 strcat (fname, ".h");
4836 /* ??? There are currently 3 separate mechanisms for avoiding processing
4837 of redundant include files: #import, #pragma once, and
4838 redundant_include_p. It would be nice if they were unified. */
4840 if (redundant_include_p (pfile, fname))
4842 cstring_free (fname);
4846 DPRINTF (("Trying: %s", fname));
4848 f = open_include_file (pfile, fname, searchptr);
4850 if (f == IMPORT_FOUND)
4852 return 0; /* Already included this file */
4855 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4857 cppReader_warning (pfile,
4858 message ("Header file %s exists, but is not readable", fname));
4871 /* A file that was not found. */
4872 fname = cstring_copyLength (fbeg, flen);
4874 if (search_start != NULL)
4876 cppReader_error (pfile,
4877 message ("Cannot find include file %s on search path: %x",
4879 searchPath_unparse (search_start)));
4883 cppReader_error (pfile,
4884 message ("No include path in which to find %s", fname));
4889 ** Check to see if this include file is a once-only include file.
4893 struct file_name_list *ptr;
4895 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4897 if (cstring_equal (ptr->fname, fname))
4899 /* This file was included before. */
4906 /* This is the first time for this file. */
4907 /* Add it to list of files included. */
4909 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4910 ptr->control_macro = NULL;
4911 ptr->c_system_include_path = NULL;
4912 ptr->next = pfile->all_include_files;
4914 ptr->got_name_map = NULL;
4916 DPRINTF (("Including file: %s", fname));
4917 pfile->all_include_files = ptr;
4918 assertSet (pfile->all_include_files);
4921 if (angle_brackets != 0)
4923 pfile->system_include_depth++;
4926 /* Actually process the file */
4927 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4929 cstring_free (fname);
4933 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4934 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4936 output_line_command (pfile, 0, enter_file);
4937 pfile->only_seen_white = 2;
4942 pfile->system_include_depth--;
4945 } /*@=branchstate@*/
4950 /* Return nonzero if there is no need to include file NAME
4951 because it has already been included and it contains a conditional
4952 to make a repeated include do nothing. */
4955 redundant_include_p (cppReader *pfile, cstring name)
4957 struct file_name_list *l = pfile->all_include_files;
4959 for (; l != NULL; l = l->next)
4961 if (cstring_equal (name, l->fname)
4962 && (l->control_macro != NULL)
4963 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4972 /* Return nonzero if the given FILENAME is an absolute pathname which
4973 designates a file within one of the known "system" include file
4974 directories. We assume here that if the given FILENAME looks like
4975 it is the name of a file which resides either directly in a "system"
4976 include file directory, or within any subdirectory thereof, then the
4977 given file must be a "system" include file. This function tells us
4978 if we should suppress pedantic errors/warnings for the given FILENAME.
4980 The value is 2 if the file is a C-language system header file
4981 for which C++ should (on most systems) assume `extern "C"'. */
4984 is_system_include (cppReader *pfile, cstring filename)
4986 struct file_name_list *searchptr;
4988 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4990 searchptr = searchptr->next)
4992 if (!cstring_isEmpty (searchptr->fname))
4994 cstring sys_dir = searchptr->fname;
4995 size_t length = cstring_length (sys_dir);
4997 if (cstring_equalLen (sys_dir, filename, length)
4998 && osd_isConnectChar (cstring_getChar (filename, length)))
5000 if (searchptr->c_system_include_path)
5011 /* Convert a character string literal into a nul-terminated string.
5012 The input string is [IN ... LIMIT).
5013 The result is placed in RESULT. RESULT can be the same as IN.
5014 The value returned in the end of the string written to RESULT,
5015 or NULL on error. */
5017 static /*@null@*/ char *
5018 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5019 char *in, char *limit, int handle_escapes)
5039 /*@switchbreak@*/ break;
5043 char *bpc = (char *) in;
5044 int i = (char) cppReader_parseEscape (pfile, &bpc);
5047 *result++ = (char) c;
5048 /*@switchbreak@*/ break;
5062 * interpret #line command. Remembers previously seen fnames
5063 * in its very own hash table.
5066 /*@constant int FNAME_HASHSIZE@*/
5067 #define FNAME_HASHSIZE 37
5070 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5072 cppBuffer *ip = cppReader_getBuffer (pfile);
5074 size_t old_written = cpplib_getWritten (pfile);
5075 enum file_change_code file_change = same_file;
5076 enum cpp_token token;
5078 llassert (ip != NULL);
5079 token = get_directive_token (pfile);
5081 if (token != CPP_NUMBER
5082 || !isdigit(pfile->token_buffer[old_written]))
5084 cppReader_errorLit (pfile,
5085 cstring_makeLiteralTemp ("invalid format `#line' command"));
5087 goto bad_line_directive;
5090 /* The Newline at the end of this line remains to be processed.
5091 To put the next line at the specified line number,
5092 we must store a line number now that is one less. */
5093 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5094 cppReader_setWritten (pfile, old_written);
5096 /* NEW_LINENO is one less than the actual line number here. */
5097 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5098 cppReader_pedwarnLit (pfile,
5099 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5101 token = get_directive_token (pfile);
5103 if (token == CPP_STRING) {
5104 char *fname = pfile->token_buffer + old_written;
5106 static hashNode fname_table[FNAME_HASHSIZE];
5108 hashNode *hash_bucket;
5111 size_t fname_length;
5113 /* Turn the file name, which is a character string literal,
5114 into a null-terminated string. Do this in place. */
5115 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5116 if (end_name == NULL)
5118 cppReader_errorLit (pfile,
5119 cstring_makeLiteralTemp ("invalid format `#line' command"));
5120 goto bad_line_directive;
5123 fname_length = size_fromInt (end_name - fname);
5124 num_start = cpplib_getWritten (pfile);
5126 token = get_directive_token (pfile);
5127 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5128 p = pfile->token_buffer + num_start;
5129 if (cppReader_isPedantic (pfile))
5130 cppReader_pedwarnLit (pfile,
5131 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5133 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5135 cppReader_errorLit (pfile,
5136 cstring_makeLiteralTemp ("invalid format `#line' command"));
5137 goto bad_line_directive;
5140 file_change = enter_file;
5142 file_change = leave_file;
5144 ip->system_header_p = 1;
5145 else /* if (*p == 4) */
5146 ip->system_header_p = 2;
5148 cppReader_setWritten (pfile, num_start);
5149 token = get_directive_token (pfile);
5150 p = pfile->token_buffer + num_start;
5151 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5152 ip->system_header_p = *p == 3 ? 1 : 2;
5153 token = get_directive_token (pfile);
5155 if (token != CPP_VSPACE) {
5156 cppReader_errorLit (pfile,
5157 cstring_makeLiteralTemp ("invalid format `#line' command"));
5159 goto bad_line_directive;
5164 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5166 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5168 if (hp->length == fname_length)
5170 llassert (hp->value.cpval != NULL);
5172 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5174 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5181 /* Didn't find it; cons up a new one. */
5182 hp = (hashNode) dmalloc (sizeof (*hp));
5185 hp->bucket_hdr = NULL;
5187 hp->name = cstring_undefined;
5188 hp->next = *hash_bucket;
5192 hp->length = fname_length;
5193 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5194 memcpy (hp->value.cpval, fname, fname_length);
5195 hp->value.cpval[fname_length] = '\0';
5196 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5199 else if (token != CPP_VSPACE && token != CPP_EOF)
5201 cppReader_errorLit (pfile,
5202 cstring_makeLiteralTemp ("invalid format `#line' command"));
5203 goto bad_line_directive;
5210 ip->lineno = new_lineno;
5212 cppReader_skipRestOfLine (pfile);
5213 cppReader_setWritten (pfile, old_written);
5214 output_line_command (pfile, 0, file_change);
5219 * remove the definition of a symbol from the symbol table.
5220 * according to un*x /lib/cpp, it is not an error to undef
5221 * something that has no definitions, so it isn't one here either.
5225 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5230 char *orig_buf = buf;
5232 SKIP_WHITE_SPACE (buf);
5234 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5236 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5238 /* If we are generating additional info for debugging (with -g) we
5239 need to pass through all effective #undef commands. */
5240 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5242 pass_thru_directive (orig_buf, limit, pfile, keyword);
5245 if (hp->type != T_MACRO)
5247 cppReader_warning (pfile,
5248 message ("Undefining preprocessor builtin: %s",
5252 cppReader_deleteMacro (hp);
5255 if (cppReader_isPedantic (pfile)) {
5257 SKIP_WHITE_SPACE (buf);
5260 cppReader_pedwarnLit (pfile,
5261 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5270 * Report an error detected by the program we are processing.
5271 * Use the text of the line in the error message.
5272 * (We use error because it prints the filename & line#.)
5276 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5277 char *buf, char *limit)
5279 size_t length = size_fromInt (limit - buf);
5280 cstring copy = cstring_copyLength (buf, length);
5281 cstring adv = cstring_advanceWhiteSpace (copy);
5283 cppReader_error (pfile, message ("#error %s", adv));
5284 cstring_free (copy);
5289 * Report a warning detected by the program we are processing.
5290 * Use the text of the line in the warning message, then continue.
5291 * (We use error because it prints the filename & line#.)
5295 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5296 char *buf, char *limit)
5298 size_t length = size_fromInt (limit - buf);
5299 cstring copy = cstring_copyLength (buf, length);
5300 cstring adv = cstring_advanceWhiteSpace (copy);
5301 cppReader_warning (pfile, message ("#warning %s", adv));
5302 cstring_free (copy);
5307 /* #ident has already been copied to the output file, so just ignore it. */
5310 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5311 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5313 /* Allow #ident in system headers, since that's not user's fault. */
5314 if (cppReader_isPedantic (pfile)
5315 && !cppReader_getBufferSafe (pfile)->system_header_p)
5316 cppReader_pedwarnLit (pfile,
5317 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5319 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5324 /* #pragma and its argument line have already been copied to the output file.
5325 Just check for some recognized pragmas that need validation here. */
5328 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5329 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5331 while (*buf == ' ' || *buf == '\t')
5336 if (!strncmp (buf, "implementation", 14)) {
5337 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5338 been included yet. */
5339 struct file_name_list *ptr;
5340 char *p = buf + 14, *fname, *inc_fname;
5342 SKIP_WHITE_SPACE (p);
5343 if (*p == '\n' || *p != '\"')
5347 p = (char *) strchr (fname, '\"');
5348 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5350 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5352 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5353 inc_fname = (inc_fname != NULL)
5354 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5356 if ((inc_fname != NULL)
5357 && (strncmp (inc_fname, fname, fname_len) == 0))
5359 cpp_setLocation (pfile);
5361 ppllerror (message ("`#pragma implementation' for `%s' appears "
5362 "after file is included",
5363 cstring_fromChars (fname)));
5372 * handle #if command by
5373 * 1) inserting special `defined' keyword into the hash table
5374 * that gets turned into 0 or 1 by special_symbol (thus,
5375 * if the luser has a symbol called `defined' already, it won't
5376 * work inside the #if command)
5377 * 2) rescan the input into a temporary output buffer
5378 * 3) pass the output buffer to the yacc parser and collect a value
5379 * 4) clean up the mess left from steps 1 and 2.
5380 * 5) call conditional_skip to skip til the next #endif (etc.),
5381 * or not, depending on the value from step 3.
5385 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5386 char *buf, char *limit)
5388 HOST_WIDE_INT value;
5389 DPRINTF (("Do if: %s", buf));
5390 value = eval_if_expression (pfile, buf, limit - buf);
5391 conditional_skip (pfile, value == 0, T_IF, NULL);
5396 * handle a #elif directive by not changing if_stack either.
5397 * see the comment above do_else.
5400 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5401 char *buf, char *limit)
5403 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5405 cppReader_errorLit (pfile,
5406 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5411 llassert (pfile->if_stack != NULL);
5413 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5415 cppReader_errorLit (pfile,
5416 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5418 if (pfile->if_stack->fname != NULL
5419 && cppReader_getBufferSafe (pfile)->fname != NULL
5420 && !cstring_equal (pfile->if_stack->fname,
5421 cppReader_getBufferSafe (pfile)->nominal_fname))
5422 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5423 fprintf (stderr, ")\n");
5425 pfile->if_stack->type = T_ELIF;
5428 if (pfile->if_stack->if_succeeded)
5430 skip_if_group (pfile, 0);
5434 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5436 skip_if_group (pfile, 0);
5439 ++pfile->if_stack->if_succeeded; /* continue processing input */
5440 output_line_command (pfile, 1, same_file);
5448 * evaluate a #if expression in BUF, of length LENGTH,
5449 * then parse the result as a C expression and return the value as an int.
5452 static HOST_WIDE_INT
5453 eval_if_expression (cppReader *pfile,
5454 /*@unused@*/ char *buf,
5455 /*@unused@*/ int length)
5457 hashNode save_defined;
5458 HOST_WIDE_INT value;
5459 size_t old_written = cpplib_getWritten (pfile);
5461 DPRINTF (("Saving defined..."));
5462 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5463 pfile->pcp_inside_if = 1;
5465 value = cppReader_parseExpression (pfile);
5466 pfile->pcp_inside_if = 0;
5468 /* Clean up special symbol */
5469 DPRINTF (("Removing defined..."));
5470 cppReader_deleteMacro (save_defined);
5471 cppReader_setWritten (pfile, old_written); /* Pop */
5477 * routine to handle ifdef/ifndef. Try to look up the symbol,
5478 * then do or don't skip to the #endif/#else/#elif depending
5479 * on what directive is actually being processed.
5483 do_xifdef (cppReader *pfile, struct directive *keyword,
5484 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5487 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5489 size_t ident_length;
5490 enum cpp_token token;
5491 int start_of_file = 0;
5492 char *control_macro = 0;
5493 size_t old_written = cpplib_getWritten (pfile);
5495 DPRINTF (("do xifdef: %d",
5496 keyword->type == T_IFNDEF));
5498 /* Detect a #ifndef at start of file (not counting comments). */
5499 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5501 start_of_file = pfile->only_seen_white == 2;
5504 pfile->no_macro_expand++;
5505 token = get_directive_token (pfile);
5506 pfile->no_macro_expand--;
5508 ident = pfile->token_buffer + old_written;
5509 DPRINTF (("Ident: %s", ident));
5511 ident_length = cpplib_getWritten (pfile) - old_written;
5512 cppReader_setWritten (pfile, old_written); /* Pop */
5514 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5516 skip = (keyword->type == T_IFDEF);
5517 if (! cppReader_isTraditional (pfile))
5519 cppReader_pedwarn (pfile,
5520 message ("`#%s' with no argument", keyword->name));
5523 else if (token == CPP_NAME)
5525 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5527 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5529 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5531 if (start_of_file && !skip)
5533 DPRINTF (("Not skipping!"));
5534 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
5535 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
5540 skip = (keyword->type == T_IFDEF);
5541 if (! cppReader_isTraditional (pfile))
5543 cppReader_error (pfile,
5544 message ("`#%s' with invalid argument", keyword->name));
5548 if (!cppReader_isTraditional (pfile))
5551 cppSkipHspace (pfile);
5552 c = cppReader_peekC (pfile);
5553 if (c != EOF && c != '\n')
5555 cppReader_pedwarn (pfile,
5556 message ("garbage at end of `#%s' argument", keyword->name));
5560 cppReader_skipRestOfLine (pfile);
5562 DPRINTF (("Conditional skip: %d", skip));
5563 conditional_skip (pfile, skip, T_IF, control_macro);
5567 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5568 If this is a #ifndef starting at the beginning of a file,
5569 CONTROL_MACRO is the macro name tested by the #ifndef.
5570 Otherwise, CONTROL_MACRO is 0. */
5573 conditional_skip (cppReader *pfile, int skip,
5574 enum node_type type,
5575 /*@dependent@*/ char *control_macro)
5577 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5579 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5580 temp->next = pfile->if_stack;
5581 temp->control_macro = control_macro;
5583 temp->if_succeeded = 0;
5585 pfile->if_stack = temp;
5586 pfile->if_stack->type = type;
5590 skip_if_group (pfile, 0);
5595 ++pfile->if_stack->if_succeeded;
5596 output_line_command (pfile, 1, same_file);
5601 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5602 * leaves input ptr at the sharp sign found.
5603 * If ANY is nonzero, return at next directive of any sort.
5607 skip_if_group (cppReader *pfile, int any)
5610 struct directive *kt;
5611 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5612 register int ident_length;
5614 struct parse_marker line_start_mark;
5616 parseSetMark (&line_start_mark, pfile);
5618 if (CPPOPTIONS (pfile)->output_conditionals) {
5619 static char failed[] = "#failed\n";
5620 cppReader_puts (pfile, failed, sizeof(failed)-1);
5622 output_line_command (pfile, 1, same_file);
5626 if (CPPOPTIONS (pfile)->output_conditionals)
5628 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5631 llassert (pbuf->buf != NULL);
5633 start_line = pbuf->buf + line_start_mark.position;
5634 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5637 parseMoveMark (&line_start_mark, pfile);
5639 if (!cppReader_isTraditional (pfile))
5641 cppSkipHspace (pfile);
5644 c = cppReader_getC (pfile);
5647 size_t old_written = cpplib_getWritten (pfile);
5648 cppSkipHspace (pfile);
5650 parse_name (pfile, cppReader_getC (pfile));
5651 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5652 ident = pfile->token_buffer + old_written;
5653 pfile->limit = ident;
5655 for (kt = directive_table; kt->length >= 0; kt++)
5657 cppIfStackFrame *temp;
5658 if (ident_length == kt->length
5659 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5661 /* If we are asked to return on next directive, do so now. */
5672 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5673 temp->next = pfile->if_stack;
5674 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5675 temp->type = kt->type;
5677 temp->if_succeeded = 0;
5678 temp->control_macro = NULL;
5680 pfile->if_stack = temp;
5681 /*@switchbreak@*/ break;
5684 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5685 validate_else (pfile,
5686 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5689 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5691 cppReader_error (pfile,
5692 message ("Preprocessor command #%s is not within a conditional", kt->name));
5693 /*@switchbreak@*/ break;
5695 else if (pfile->if_stack == save_if_stack)
5697 goto done; /* found what we came for */
5704 if (kt->type != T_ENDIF)
5706 llassert (pfile->if_stack != NULL);
5708 if (pfile->if_stack->type == T_ELSE)
5710 cppReader_errorLit (pfile,
5711 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5714 pfile->if_stack->type = kt->type;
5715 /*@switchbreak@*/ break;
5718 temp = pfile->if_stack;
5719 llassert (temp != NULL);
5720 pfile->if_stack = temp->next;
5722 /*@switchbreak@*/ break;
5725 #if defined (OS2) && defined (__IBMC__)
5726 /* Dummy code to eleminate optimization problems with icc */
5735 /* Don't let erroneous code go by. */
5737 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5738 && cppReader_isPedantic (pfile))
5740 cppReader_pedwarnLit (pfile,
5741 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5745 c = cppReader_getC (pfile);
5747 /* We're in the middle of a line. Skip the rest of it. */
5755 case '/': /* possible comment */
5756 c = skip_comment (pfile, NULL);
5759 /*@switchbreak@*/ break;
5762 cppReader_forward (pfile, -1);
5763 old = cpplib_getWritten (pfile);
5764 (void) cpplib_getToken (pfile);
5765 cppReader_setWritten (pfile, old);
5766 /*@switchbreak@*/ break;
5768 /* Char after backslash loses its special meaning. */
5769 if (cppReader_peekC (pfile) == '\n')
5771 cppReader_forward (pfile, 1);
5774 /*@switchbreak@*/ break;
5778 c = cppReader_getC (pfile);
5781 if (CPPOPTIONS (pfile)->output_conditionals) {
5782 static char end_failed[] = "#endfailed\n";
5783 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5786 pfile->only_seen_white = 1;
5788 parseGotoMark (&line_start_mark, pfile);
5789 parseClearMark (&line_start_mark);
5793 * handle a #else directive. Do this by just continuing processing
5794 * without changing if_stack ; this is so that the error message
5795 * for missing #endif's etc. will point to the original #if. It
5796 * is possible that something different would be better.
5800 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5801 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5803 if (cppReader_isPedantic (pfile))
5805 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5808 cppReader_skipRestOfLine (pfile);
5810 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5811 cppReader_errorLit (pfile,
5812 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5815 /* #ifndef can't have its special treatment for containing the whole file
5816 if it has a #else clause. */
5818 llassert (pfile->if_stack != NULL);
5820 pfile->if_stack->control_macro = 0;
5822 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5824 cpp_setLocation (pfile);
5825 genppllerrorhint (FLG_PREPROC,
5826 message ("Pre-processor directive #else after #else"),
5827 message ("%q: Location of match",
5828 fileloc_unparseRaw (pfile->if_stack->fname,
5829 pfile->if_stack->lineno)));
5832 pfile->if_stack->type = T_ELSE;
5835 if (pfile->if_stack->if_succeeded)
5836 skip_if_group (pfile, 0);
5838 ++pfile->if_stack->if_succeeded; /* continue processing input */
5839 output_line_command (pfile, 1, same_file);
5846 * unstack after #endif command
5850 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5851 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5853 if (cppReader_isPedantic (pfile))
5855 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5858 cppReader_skipRestOfLine (pfile);
5860 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5862 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5866 cppIfStackFrame *temp = pfile->if_stack;
5868 llassert (temp != NULL);
5870 pfile->if_stack = temp->next;
5871 if (temp->control_macro != 0)
5873 /* This #endif matched a #ifndef at the start of the file.
5874 See if it is at the end of the file. */
5875 struct parse_marker start_mark;
5878 parseSetMark (&start_mark, pfile);
5882 cppSkipHspace (pfile);
5883 c = cppReader_getC (pfile);
5889 parseGotoMark (&start_mark, pfile);
5890 parseClearMark (&start_mark);
5894 /* If we get here, this #endif ends a #ifndef
5895 that contains all of the file (aside from whitespace).
5896 Arrange not to include the file again
5897 if the macro that was tested is defined.
5899 Do not do this for the top-level file in a -include or any
5900 file in a -imacros. */
5901 struct file_name_list *ifile = pfile->all_include_files;
5903 for ( ; ifile != NULL; ifile = ifile->next)
5905 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5907 ifile->control_macro = temp->control_macro;
5915 output_line_command (pfile, 1, same_file);
5920 /* When an #else or #endif is found while skipping failed conditional,
5921 if -pedantic was specified, this is called to warn about text after
5922 the command name. P points to the first char after the command name. */
5925 validate_else (cppReader *pfile, cstring directive)
5928 cppSkipHspace (pfile);
5929 c = cppReader_peekC (pfile);
5930 if (c != EOF && c != '\n')
5932 cppReader_pedwarn (pfile,
5933 message ("text following `%s' violates ANSI standard", directive));
5938 ** Get the next token, and add it to the text in pfile->token_buffer.
5939 ** Return the kind of token we got.
5943 cpplib_getToken (cppReader *pfile)
5945 return cpplib_getTokenAux (pfile, FALSE);
5949 cpplib_getTokenForceExpand (cppReader *pfile)
5951 return cpplib_getTokenAux (pfile, TRUE);
5955 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5958 size_t old_written = 0;
5959 int start_line, start_column;
5960 enum cpp_token token;
5961 struct cppOptions *opts = CPPOPTIONS (pfile);
5962 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5965 c = cppReader_getC (pfile);
5966 DPRINTF (("Get next token: %c", c));
5971 if (cppReader_getBufferSafe (pfile)->seen_eof)
5973 cppBuffer *buf = cppReader_popBuffer (pfile);
5975 if (buf != cppReader_nullBuffer (pfile))
5986 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
5987 cppReader_getBufferSafe (pfile)->seen_eof = 1;
5989 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
5990 && next_buf != cppReader_nullBuffer (pfile))
5992 /* We're about to return from an #include file.
5993 Emit #line information now (as part of the CPP_POP) result.
5994 But the #line refers to the file we will pop to. */
5995 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5996 CPPBUFFER (pfile) = next_buf;
5997 pfile->input_stack_listing_current = 0;
5998 output_line_command (pfile, 0, leave_file);
5999 CPPBUFFER (pfile) = cur_buffer;
6007 struct parse_marker start_mark;
6012 if (cppReader_peekC (pfile) == '=')
6017 if (opts->put_out_comments)
6019 parseSetMark (&start_mark, pfile);
6023 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6024 &start_line, &start_column);
6025 c = skip_comment (pfile, &newlines);
6026 DPRINTF (("c = %c", c));
6027 if (opts->put_out_comments && (c == '/' || c == EOF))
6029 assertSet (&start_mark);
6030 parseClearMark (&start_mark);
6037 cppReader_errorWithLine (pfile, start_line, start_column,
6038 cstring_makeLiteral ("Unterminated comment"));
6041 c = '/'; /* Initial letter of comment. */
6043 /* Comments are equivalent to spaces.
6044 For -traditional, a comment is equivalent to nothing. */
6046 if (opts->put_out_comments)
6050 assertSet (&start_mark);
6051 res = cpp_handleComment (pfile, &start_mark);
6052 pfile->lineno += newlines;
6055 else if (cppReader_isTraditional (pfile))
6061 cpplib_reserve(pfile, 1);
6062 cppReader_putCharQ (pfile, ' ');
6067 if (!pfile->only_seen_white)
6072 if (cppReader_handleDirective (pfile))
6074 return CPP_DIRECTIVE;
6077 pfile->only_seen_white = 0;
6082 /* A single quoted string is treated like a double -- some
6083 programs (e.g., troff) are perverse this way */
6084 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6085 &start_line, &start_column);
6086 old_written = cpplib_getWritten (pfile);
6088 DPRINTF (("Put char: %c", c));
6089 cppReader_putChar (pfile, c);
6092 int cc = cppReader_getC (pfile);
6093 DPRINTF (("cc: %c", c));
6096 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6098 /* try harder: this string crosses a macro expansion
6099 boundary. This can happen naturally if -traditional.
6100 Otherwise, only -D can make a macro with an unmatched
6103 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6104 (*cppReader_getBufferSafe (pfile)->cleanup)
6105 (cppReader_getBufferSafe (pfile), pfile);
6106 CPPBUFFER (pfile) = next_buf;
6109 if (!cppReader_isTraditional (pfile))
6111 cpp_setLocation (pfile);
6113 setLine (long_toInt (start_line));
6114 setColumn (long_toInt (start_column));
6116 if (pfile->multiline_string_line != long_toInt (start_line)
6117 && pfile->multiline_string_line != 0)
6121 message ("Unterminated string or character constant"),
6122 message ("%q: Possible real start of unterminated constant",
6124 (fileloc_filename (g_currentloc),
6125 pfile->multiline_string_line)));
6126 pfile->multiline_string_line = 0;
6132 message ("Unterminated string or character constant"));
6135 /*@loopbreak@*/ break;
6137 DPRINTF (("putting char: %c", cc));
6138 cppReader_putChar (pfile, cc);
6142 /* Traditionally, end of line ends a string constant with
6143 no error. So exit the loop and record the new line. */
6144 if (cppReader_isTraditional (pfile))
6150 if (cppReader_isPedantic (pfile)
6151 && pfile->multiline_string_line == 0)
6153 cppReader_pedwarnWithLine
6154 (pfile, long_toInt (start_line),
6155 long_toInt (start_column),
6156 cstring_makeLiteral ("String constant runs past end of line"));
6158 if (pfile->multiline_string_line == 0)
6160 pfile->multiline_string_line = start_line;
6163 /*@switchbreak@*/ break;
6166 cc = cppReader_getC (pfile);
6169 /* Backslash newline is replaced by nothing at all. */
6170 cppReader_adjustWritten (pfile, -1);
6175 /* ANSI stupidly requires that in \\ the second \
6176 is *not* prevented from combining with a newline. */
6179 cppReader_putChar (pfile, cc);
6181 /*@switchbreak@*/ break;
6187 /*@switchbreak@*/ break;
6191 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6192 cpplib_getPWritten (pfile));
6193 pfile->only_seen_white = 0;
6194 return c == '\'' ? CPP_CHAR : CPP_STRING;
6197 if (!opts->dollars_in_ident)
6202 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6210 c2 = cppReader_peekC (pfile);
6211 if (c2 == c || c2 == '=')
6221 if (cppReader_peekC (pfile) == '=')
6227 c2 = cppReader_peekC (pfile);
6228 if (c2 == '-' && opts->chill)
6230 /* Chill style comment */
6231 if (opts->put_out_comments)
6233 parseSetMark (&start_mark, pfile);
6236 cppReader_forward (pfile, 1); /* Skip second '-'. */
6240 c = cppReader_getC (pfile);
6242 /*@loopbreak@*/ break;
6245 /* Don't consider final '\n' to be part of comment. */
6246 cppReader_forward (pfile, -1);
6247 /*@loopbreak@*/ break;
6251 goto return_comment;
6253 if (c2 == '-' || c2 == '=' || c2 == '>')
6258 if (pfile->parsing_include_directive)
6262 cppReader_putChar (pfile, c);
6264 /*@loopbreak@*/ break;
6265 c = cppReader_getC (pfile);
6267 if (c == '\n' || c == EOF)
6269 cppReader_errorLit (pfile,
6270 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6271 /*@loopbreak@*/ break;
6279 c2 = cppReader_peekC (pfile);
6284 cppReader_forward (pfile, 1);
6285 cpplib_reserve (pfile, 4);
6286 cppReader_putChar (pfile, c);
6287 cppReader_putChar (pfile, c2);
6289 c3 = cppReader_peekC (pfile);
6291 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6292 cppReader_nullTerminateQ (pfile);
6293 pfile->only_seen_white = 0;
6297 DPRINTF (("Macro @!"));
6298 if (cppReader_getBufferSafe (pfile)->has_escapes)
6300 c = cppReader_getC (pfile);
6301 DPRINTF (("got c: %c", c));
6304 if (pfile->output_escapes)
6305 cppReader_puts (pfile, "@-", 2);
6306 parse_name (pfile, cppReader_getC (pfile));
6309 else if (is_space [c])
6311 cpplib_reserve (pfile, 2);
6312 if (pfile->output_escapes)
6313 cppReader_putCharQ (pfile, '@');
6314 cppReader_putCharQ (pfile, c);
6322 if (pfile->output_escapes)
6324 cppReader_puts (pfile, "@@", 2);
6330 c2 = cppReader_peekC (pfile);
6333 cpplib_reserve(pfile, 2);
6334 cppReader_putCharQ (pfile, '.');
6335 c = cppReader_getC (pfile);
6339 /* FIXME - misses the case "..\\\n." */
6340 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6342 cpplib_reserve(pfile, 4);
6343 cppReader_putCharQ (pfile, '.');
6344 cppReader_putCharQ (pfile, '.');
6345 cppReader_putCharQ (pfile, '.');
6346 cppReader_forward (pfile, 2);
6347 cppReader_nullTerminateQ (pfile);
6348 pfile->only_seen_white = 0;
6354 pfile->only_seen_white = 0;
6356 cpplib_reserve(pfile, 3);
6357 cppReader_putCharQ (pfile, c);
6358 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6359 cppReader_nullTerminateQ (pfile);
6364 c2 = cppReader_peekC (pfile);
6365 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6367 cppReader_putChar (pfile, c);
6368 c = cppReader_getC (pfile);
6373 case '0': case '1': case '2': case '3': case '4':
6374 case '5': case '6': case '7': case '8': case '9':
6379 cpplib_reserve (pfile, 2);
6380 cppReader_putCharQ (pfile, c);
6382 c = cppReader_peekC (pfile);
6384 /*@loopbreak@*/ break;
6385 if (!is_idchar[c] && c != '.'
6386 && ((c2 != 'e' && c2 != 'E'
6387 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6388 || (c != '+' && c != '-')))
6389 /*@loopbreak@*/ break;
6390 cppReader_forward (pfile, 1);
6394 cppReader_nullTerminateQ (pfile);
6395 pfile->only_seen_white = 0;
6398 case 'b': case 'c': case 'd': case 'h': case 'o':
6399 case 'B': case 'C': case 'D': case 'H': case 'O':
6400 if (opts->chill && cppReader_peekC (pfile) == '\'')
6402 pfile->only_seen_white = 0;
6403 cpplib_reserve (pfile, 2);
6404 cppReader_putCharQ (pfile, c);
6405 cppReader_putCharQ (pfile, '\'');
6406 cppReader_forward (pfile, 1);
6409 c = cppReader_getC (pfile);
6411 goto chill_number_eof;
6414 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6416 cppReader_forward (pfile, 2);
6419 /*@loopbreak@*/ break;
6421 cppReader_putChar (pfile, c);
6425 cpplib_reserve (pfile, 2);
6426 cppReader_putCharQ (pfile, c);
6427 cppReader_nullTerminateQ (pfile);
6432 cppReader_forward (pfile, -1);
6434 cppReader_nullTerminate (pfile);
6441 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6442 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6443 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6444 case 'x': case 'y': case 'z':
6445 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6446 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6447 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6453 size_t before_name_written = cpplib_getWritten (pfile);
6455 parse_name (pfile, c);
6456 pfile->only_seen_white = 0;
6458 if (pfile->no_macro_expand)
6460 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6464 ident = pfile->token_buffer + before_name_written;
6465 DPRINTF (("Ident: %s", ident));
6467 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6469 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6473 DPRINTF (("No expand: %s %d", ident, ident_len));
6477 if (hp->type == T_DISABLED)
6479 DPRINTF (("Disabled!"));
6481 if (pfile->output_escapes)
6482 { /* Return "@-IDENT", followed by '\0'. */
6484 cpplib_reserve (pfile, 3);
6485 ident = pfile->token_buffer + before_name_written;
6486 cppReader_adjustWritten (pfile, 2);
6488 for (i = size_toInt (ident_len); i >= 0; i--)
6490 ident[i+2] = ident[i];
6500 ** If macro wants an arglist, verify that a '(' follows.
6501 ** first skip all whitespace, copying it to the output
6502 ** after the macro name. Then, if there is no '(',
6503 ** decide this is not a macro call and leave things that way.
6506 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6508 struct parse_marker macro_mark;
6511 DPRINTF (("Arglist macro!"));
6513 parseSetMark (¯o_mark, pfile);
6515 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6517 cppBuffer *next_buf;
6518 cppSkipHspace (pfile);
6519 if (cppReader_peekC (pfile) != EOF)
6521 DPRINTF (("Peeking!"));
6522 /*@loopbreak@*/ break;
6525 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6526 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6527 CPPBUFFER (pfile) = next_buf;
6530 /* parseSetMark (¯o_mark, pfile); */
6534 cppSkipHspace (pfile);
6535 c = cppReader_peekC (pfile);
6536 DPRINTF (("c: %c", c));
6537 is_macro_call = c == '(';
6539 /*@loopbreak@*/ break;
6540 cppReader_forward (pfile, 1);
6545 parseGotoMark (¯o_mark, pfile);
6548 parseClearMark (¯o_mark);
6552 DPRINTF (("not macro call!"));
6557 /* This is now known to be a macro call. */
6559 /* it might not actually be a macro. */
6560 if (hp->type != T_MACRO)
6565 cppReader_setWritten (pfile, before_name_written);
6566 special_symbol (hp, pfile);
6567 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6568 xbuf = (char *) dmalloc (xbuf_len + 1);
6569 cppReader_setWritten (pfile, before_name_written);
6570 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6571 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6576 ** Expand the macro, reading arguments as needed,
6577 ** and push the expansion on the input stack.
6580 cpplib_macroExpand (pfile, hp);
6581 cppReader_setWritten (pfile, before_name_written);
6584 /* An extra "@ " is added to the end of a macro expansion
6585 to prevent accidental token pasting. We prefer to avoid
6586 unneeded extra spaces (for the sake of cpp-using tools like
6587 imake). Here we remove the space if it is safe to do so. */
6589 llassert (pfile->buffer->rlimit != NULL);
6591 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6592 && pfile->buffer->rlimit[-2] == '@'
6593 && pfile->buffer->rlimit[-1] == ' ')
6595 int c1 = pfile->buffer->rlimit[-3];
6596 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6598 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6599 pfile->buffer->rlimit -= 2;
6605 case ' ': case '\t': case '\v': case '\r':
6608 cppReader_putChar (pfile, c);
6609 c = cppReader_peekC (pfile);
6610 if (c == EOF || !is_hor_space[c])
6611 /*@loopbreak@*/ break;
6612 cppReader_forward (pfile, 1);
6617 c2 = cppReader_peekC (pfile);
6624 cppReader_putChar (pfile, c);
6625 if (pfile->only_seen_white == 0)
6626 pfile->only_seen_white = 1;
6628 output_line_command (pfile, 1, same_file);
6631 case '(': token = CPP_LPAREN; goto char1;
6632 case ')': token = CPP_RPAREN; goto char1;
6633 case '{': token = CPP_LBRACE; goto char1;
6634 case '}': token = CPP_RBRACE; goto char1;
6635 case ',': token = CPP_COMMA; goto char1;
6636 case ';': token = CPP_SEMICOLON; goto char1;
6642 pfile->only_seen_white = 0;
6643 cppReader_putChar (pfile, c);
6652 /* Parse an identifier starting with C. */
6655 parse_name (cppReader *pfile, int c)
6661 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6663 cppReader_forward (pfile, 2);
6667 cppReader_forward (pfile, -1);
6671 if (c == '$' && cppReader_isPedantic (pfile))
6673 cppReader_pedwarnLit (pfile,
6674 cstring_makeLiteralTemp ("`$' in identifier"));
6677 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6678 cppReader_putCharQ (pfile, c);
6679 c = cppReader_getC (pfile);
6685 cppReader_nullTerminateQ (pfile);
6688 /* The file_name_map structure holds a mapping of file names for a
6689 particular directory. This mapping is read from the file named
6690 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6691 map filenames on a file system with severe filename restrictions,
6692 such as DOS. The format of the file name map file is just a series
6693 of lines with two tokens on each line. The first token is the name
6694 to map, and the second token is the actual name to use. */
6696 struct file_name_map
6698 struct file_name_map *map_next;
6703 /*@constant observer char *FILE_NAME_MAP_FILE*/
6704 #define FILE_NAME_MAP_FILE "header.gcc"
6706 /* Read a space delimited string of unlimited length from a stdio
6709 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6715 set = alloc = dmalloc (len + 1);
6720 while ((ch = getc (f)) != EOF && ! is_space[ch])
6722 if (set - alloc == size_toInt (len))
6725 alloc = drealloc (alloc, len + 1);
6726 set = alloc + len / 2;
6727 /*@-branchstate@*/ }
6730 } /*@=branchstate@*/
6733 check (ungetc (ch, f) != EOF);
6735 return cstring_fromChars (alloc);
6738 /* This structure holds a linked list of file name maps, one per directory. */
6740 struct file_name_map_list
6742 struct file_name_map_list *map_list_next;
6743 cstring map_list_name;
6744 /*@null@*/ struct file_name_map *map_list_map;
6747 /* Read the file name map file for DIRNAME. */
6749 static struct file_name_map *
6750 read_name_map (cppReader *pfile, cstring dirname)
6752 struct file_name_map_list *map_list_ptr;
6756 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6757 map_list_ptr != NULL;
6758 map_list_ptr = map_list_ptr->map_list_next)
6760 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6762 return map_list_ptr->map_list_map;
6766 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6767 map_list_ptr->map_list_name = cstring_copy (dirname);
6768 map_list_ptr->map_list_map = NULL;
6770 name = cstring_copy (dirname);
6772 if (cstring_length (dirname) > 0)
6774 name = cstring_appendChar (name, CONNECTCHAR);
6777 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6779 f = fileTable_openReadFile (context_fileTable (), name);
6780 cstring_free (name);
6784 map_list_ptr->map_list_map = NULL;
6790 while ((ch = getc (f)) != EOF)
6793 struct file_name_map *ptr;
6800 from = read_filename_string (ch, f);
6801 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6806 to = read_filename_string (ch, f);
6808 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6809 ptr->map_from = from;
6811 /* Make the real filename absolute. */
6812 if (cstring_length (to) > 1
6813 && osd_isConnectChar (cstring_firstChar (to)))
6819 ptr->map_to = cstring_copy (dirname);
6820 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6821 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6824 ptr->map_next = map_list_ptr->map_list_map;
6825 map_list_ptr->map_list_map = ptr;
6827 while ((ch = getc (f)) != '\n')
6831 /*@innerbreak@*/ break;
6836 assertSet (map_list_ptr->map_list_map);
6837 check (fileTable_closeFile (context_fileTable (),f) == 0);
6840 map_list_ptr->map_list_next = pfile->opts->map_list;
6841 pfile->opts->map_list = map_list_ptr;
6843 return map_list_ptr->map_list_map;
6846 /* Try to open include file FILENAME. SEARCHPTR is the directory
6847 being tried from the include file search path. This function maps
6848 filenames on file systems based on information read by
6852 open_include_file (cppReader *pfile,
6854 struct file_name_list *searchptr)
6856 char *filename = cstring_toCharsSafe (fname);
6857 struct file_name_map *map;
6861 cstring_markOwned (fname);
6863 cpp_setLocation (pfile);
6865 if (context_getFlag (FLG_NEVERINCLUDE))
6867 if (isHeaderFile (fname))
6869 return SKIP_INCLUDE;
6873 if ((searchptr != NULL) && ! searchptr->got_name_map)
6875 searchptr->name_map = read_name_map (pfile,
6876 !cstring_isEmpty (searchptr->fname)
6877 ? searchptr->fname :
6878 cstring_makeLiteralTemp ("."));
6879 searchptr->got_name_map = 1;
6882 /* First check the mapping for the directory we are using. */
6884 if ((searchptr != NULL)
6885 && (searchptr->name_map != NULL))
6889 if (!cstring_isEmpty (searchptr->fname))
6891 from += cstring_length (searchptr->fname) + 1;
6894 for (map = searchptr->name_map;
6896 map = map->map_next)
6898 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6901 ** Found a match. Check if the file should be skipped
6904 if (cpp_skipIncludeFile (map->map_to))
6906 return SKIP_INCLUDE;
6910 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6917 ** Try to find a mapping file for the particular directory we are
6918 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6919 ** in /usr/include/header.gcc and look up types.h in
6920 ** /usr/include/sys/header.gcc.
6923 p = strrchr (filename, CONNECTCHAR);
6930 if ((searchptr != NULL)
6931 && (cstring_isDefined (searchptr->fname))
6932 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6933 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6935 size_fromInt (p - filename)))
6937 /* filename is in SEARCHPTR, which we've already checked. */
6939 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6941 return SKIP_INCLUDE;
6945 return cpp_openIncludeFile (filename);
6951 dir = mstring_copy (".");
6956 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6957 memcpy (dir, filename, size_fromInt (p - filename));
6958 dir[p - filename] = '\0';
6962 for (map = read_name_map (pfile, cstring_fromChars (dir));
6964 map = map->map_next)
6966 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6970 if (cpp_skipIncludeFile (map->map_to))
6972 return SKIP_INCLUDE;
6976 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6983 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6985 return SKIP_INCLUDE;
6989 return cpp_openIncludeFile (filename);
6993 /* Process the contents of include file FNAME, already open on descriptor F,
6995 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6996 "system" include directories (as decided by the `is_system_include'
6998 DIRPTR is the link in the dir path through which this file was found,
6999 or 0 if the file name was absolute or via the current directory.
7000 Return 1 on success, 0 on failure.
7002 The caller is responsible for the cppReader_pushBuffer. */
7005 finclude (cppReader *pfile, int f,
7007 bool system_header_p,
7008 /*@dependent@*/ struct file_name_list *dirptr)
7014 cppBuffer *fp; /* For input stack frame */
7016 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7018 cppReader_perrorWithName (pfile, fname);
7019 check (close (f) == 0);
7020 (void) cppReader_popBuffer (pfile);
7026 fp = cppReader_getBufferSafe (pfile);
7028 /*@-temptrans@*/ /* fname shouldn't really be temp */
7029 fp->nominal_fname = fp->fname = fname;
7033 fp->system_header_p = system_header_p;
7036 fp->cleanup = cppReader_fileCleanup;
7038 if (S_ISREG (st_mode))
7041 fp->buf = (char *) dmalloc (st_size + 2);
7042 fp->alimit = fp->buf + st_size + 2;
7045 /* Read the file contents, knowing that st_size is an upper bound
7046 on the number of bytes we can read. */
7047 length = safe_read (f, fp->buf, size_toInt (st_size));
7048 fp->rlimit = fp->buf + length;
7049 if (length < 0) goto nope;
7051 else if (S_ISDIR (st_mode))
7053 cppReader_error (pfile,
7054 message ("Directory specified in #include: %s", fname));
7055 check (close (f) == 0);
7061 ** Cannot count its file size before reading.
7062 ** First read the entire file into heap and
7063 ** copy them into buffer on stack.
7066 size_t bsize = 2000;
7071 fp->buf = (char *) dmalloc (bsize + 2);
7074 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7077 goto nope; /* error! */
7080 if (st_size != bsize)
7082 break; /* End of file */
7086 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7090 length = size_toInt (st_size);
7093 if ((length > 0 && fp->buf[length - 1] != '\n')
7094 /* Backslash-newline at end is not good enough. */
7095 || (length > 1 && fp->buf[length - 2] == '\\')) {
7096 fp->buf[length++] = '\n';
7099 fp->buf[length] = '\0';
7100 fp->rlimit = fp->buf + length;
7102 /* Close descriptor now, so nesting does not use lots of descriptors. */
7103 check (close (f) == 0);
7105 /* Must do this before calling trigraph_pcp, so that the correct file name
7106 will be printed in warning messages. */
7108 pfile->input_stack_listing_current = 0;
7113 cppReader_perrorWithName (pfile, fname);
7114 check (close (f) == 0);
7120 cpplib_init (cppReader *pfile)
7122 memset ((char *) pfile, 0, sizeof (*pfile));
7124 pfile->get_token = cpplib_getToken;
7125 pfile->token_buffer_size = 200;
7126 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7127 pfile->all_include_files = NULL;
7131 cppReader_setWritten (pfile, 0);
7133 pfile->system_include_depth = 0;
7134 pfile->max_include_len = 0;
7135 pfile->timebuf = NULL;
7136 pfile->only_seen_white = 1;
7138 pfile->buffer = cppReader_nullBuffer (pfile);
7142 cppReader_finish (/*@unused@*/ cppReader *pfile)
7147 /* Free resources used by PFILE.
7148 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7151 cppCleanup (cppReader *pfile)
7153 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7155 (void) cppReader_popBuffer (pfile);
7158 if (pfile->token_buffer != NULL)
7160 sfree (pfile->token_buffer);
7161 pfile->token_buffer = NULL;
7164 while (pfile->if_stack != NULL)
7166 cppIfStackFrame *temp = pfile->if_stack;
7167 pfile->if_stack = temp->next;
7171 while (pfile->all_include_files != NULL)
7173 struct file_name_list *temp = pfile->all_include_files;
7174 pfile->all_include_files = temp->next;
7175 /*@-dependenttrans@*/
7176 cstring_free (temp->fname);
7177 /*@=dependenttrans@*/
7181 cppReader_hashCleanup ();
7185 ** Get the file-mode and data size of the file open on FD
7186 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7190 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7194 if (fstat (fd, &sbuf) < 0) {
7200 if (mode_pointer != NULL)
7202 *mode_pointer = sbuf.st_mode;
7205 if (size_pointer != NULL)
7207 *size_pointer = (size_t) sbuf.st_size;
7213 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7214 retrying if necessary. Return a negative value if an error occurs,
7215 otherwise return the actual number of bytes read,
7216 which must be LEN unless end-of-file was reached. */
7218 static int safe_read (int desc, char *ptr, int len)
7224 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7225 /*@-compdef@*/ /* ptr is an out parameter */
7226 int nchars = _read (desc, ptr, (unsigned) left);
7229 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7238 return (int) nchars;
7252 /* Initialize PMARK to remember the current position of PFILE. */
7255 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7257 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7259 pmark->next = pbuf->marks;
7261 pbuf->marks = pmark;
7265 pmark->position = pbuf->cur - pbuf->buf;
7266 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
7269 /* Cleanup PMARK - we no longer need it. */
7271 void parseClearMark (struct parse_marker *pmark)
7273 struct parse_marker **pp = &pmark->buf->marks;
7275 for (; ; pp = &(*pp)->next)
7277 llassert (*pp != NULL);
7278 if (*pp == pmark) break;
7284 /* Backup the current position of PFILE to that saved in PMARK. */
7287 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7289 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7291 if (pbuf != pmark->buf)
7293 cpp_setLocation (pfile);
7294 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7297 llassert (pbuf->buf != NULL);
7298 pbuf->cur = pbuf->buf + pmark->position;
7299 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
7302 /* Reset PMARK to point to the current position of PFILE. (Same
7303 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7306 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7308 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7310 if (pbuf != pmark->buf)
7312 cpp_setLocation (pfile);
7313 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7316 pmark->position = pbuf->cur - pbuf->buf;
7317 DPRINTF (("move mark: %s", pmark->position));
7320 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7322 struct cppOptions *opts = CPPOPTIONS (pfile);
7325 /* The code looks at the defaults through this pointer, rather than through
7326 the constant structure above. This pointer gets changed if an environment
7327 variable specifies other defaults. */
7329 struct default_include *include_defaults = include_defaults_array;
7331 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7332 /* There seems to be confusion about what CPATH should do,
7333 so for the moment it is not documented. */
7334 /* Some people say that CPATH should replace the standard include dirs,
7335 but that seems pointless: it comes before them, so it overrides them
7338 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7340 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7342 path_include (pfile, cstring_toCharsSafe (xp));
7345 /* Now that dollars_in_ident is known, initialize is_idchar. */
7346 initialize_char_syntax (opts);
7348 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7349 and option processing. */
7351 initialize_builtins (pfile);
7353 /* Do standard #defines and assertions
7354 that identify system and machine type. */
7356 if (!opts->inhibit_predefs) {
7357 char *p = (char *) dmalloc (strlen (predefs) + 1);
7358 strcpy (p, predefs);
7364 while (*p == ' ' || *p == '\t')
7369 /* Handle -D options. */
7370 if (p[0] == '-' && p[1] == 'D')
7374 while (*p && *p != ' ' && *p != '\t')
7384 if (opts->debug_output)
7386 output_line_command (pfile, 0, same_file);
7389 cppReader_define (pfile, q);
7391 while (*p == ' ' || *p == '\t')
7405 opts->done_initializing = 1;
7407 { /* Read the appropriate environment variable and if it exists
7408 replace include_defaults with the listed path. */
7412 int win32_buf_size = 0; /* memory we need to allocate */
7415 if (opts->cplusplus)
7417 epath = getenv ("CPLUS_INCLUDE_PATH");
7421 epath = getenv ("C_INCLUDE_PATH");
7425 ** If the environment var for this language is set,
7426 ** add to the default list of include directories.
7429 if (epath != NULL) {
7430 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7432 char *startp, *endp;
7435 /* if we have a posix path list, convert to win32 path list */
7436 if (cygwin32_posix_path_list_p (epath))
7438 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7439 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7440 cygwin32_posix_to_win32_path_list (epath, win32epath);
7444 for (num_dirs = 1, startp = epath; *startp; startp++)
7446 if (*startp == PATH_SEPARATOR)
7452 = (struct default_include *) dmalloc ((num_dirs
7453 * sizeof (struct default_include))
7454 + sizeof (include_defaults_array));
7457 startp = endp = epath;
7460 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7461 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7463 strncpy (nstore, startp, size_fromInt (endp - startp));
7466 strcpy (nstore, ".");
7470 nstore[endp-startp] = '\0';
7473 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7474 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7475 include_defaults[num_dirs].cxx_aware = 1;
7482 endp = startp = endp + 1;
7489 /* Put the usual defaults back in at the end. */
7490 memcpy ((char *) &include_defaults[num_dirs],
7491 (char *) include_defaults_array,
7492 sizeof (include_defaults_array));
7495 /*@-branchstate@*/ } /*@=branchstate@*/
7498 cppReader_appendIncludeChain (pfile, opts->before_system,
7499 opts->last_before_system);
7500 opts->first_system_include = opts->before_system;
7502 /* Unless -fnostdinc,
7503 tack on the standard include file dirs to the specified list */
7505 if (!opts->no_standard_includes) {
7506 struct default_include *p = include_defaults;
7507 char *specd_prefix = opts->include_prefix;
7508 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7509 int default_len = 0;
7511 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7512 if (default_prefix != NULL) {
7513 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7514 default_len = strlen (default_prefix) - 7;
7515 default_prefix[default_len] = 0;
7519 /* Search "translated" versions of GNU directories.
7520 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7521 if (specd_prefix != 0 && default_len != 0)
7522 for (p = include_defaults; p->fname != NULL; p++) {
7523 /* Some standard dirs are only for C++. */
7525 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7526 /* Does this dir start with the prefix? */
7527 llassert (default_prefix != NULL);
7529 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
7530 size_fromInt (default_len)))
7532 /* Yes; change prefix and add to search list. */
7533 struct file_name_list *nlist
7534 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7535 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7536 char *str = (char *) dmalloc (this_len + 1);
7537 strcpy (str, specd_prefix);
7538 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7541 nlist->fname = cstring_fromChars (str);
7542 nlist->control_macro = 0;
7543 nlist->c_system_include_path = !p->cxx_aware;
7544 nlist->got_name_map = 0;
7546 cppReader_addIncludeChain (pfile, nlist);
7547 if (opts->first_system_include == 0)
7549 opts->first_system_include = nlist;
7555 /* Search ordinary names for GNU include directories. */
7557 for (p = include_defaults; p->fname != NULL; p++)
7559 /* Some standard dirs are only for C++. */
7561 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7563 struct file_name_list *nlist
7564 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7565 nlist->control_macro = 0;
7566 nlist->c_system_include_path = !p->cxx_aware;
7567 nlist->fname = p->fname;
7568 nlist->got_name_map = 0;
7571 cppReader_addIncludeChain (pfile, nlist);
7573 if (opts->first_system_include == 0)
7575 opts->first_system_include = nlist;
7579 sfree (default_prefix);
7582 /* Tack the after_include chain at the end of the include chain. */
7583 cppReader_appendIncludeChain (pfile, opts->after_include,
7584 opts->last_after_include);
7586 if (opts->first_system_include == 0)
7588 opts->first_system_include = opts->after_include;
7591 /* With -v, print the list of dirs to search. */
7592 if (opts->verbose) {
7593 struct file_name_list *p;
7594 fprintf (stderr, "#include \"...\" search starts here:\n");
7596 for (p = opts->include; p != NULL; p = p->next) {
7597 if (p == opts->first_bracket_include)
7598 fprintf (stderr, "#include <...> search starts here:\n");
7600 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7602 fprintf (stderr, "End of search list.\n");
7606 int cppReader_startProcess (cppReader *pfile, cstring fname)
7610 struct cppOptions *opts = CPPOPTIONS (pfile);
7612 fp = cppReader_pushBuffer (pfile, NULL, 0);
7619 if (opts->in_fname == NULL)
7621 opts->in_fname = cstring_makeLiteralTemp ("");
7624 fp->fname = opts->in_fname;
7625 fp->nominal_fname = fp->fname;
7628 /* Copy the entire contents of the main input file into
7629 the stacked input buffer previously allocated for it. */
7631 if (cstring_isEmpty (fname))
7633 fname = cstring_makeLiteralTemp ("");
7636 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7638 cppReader_error (pfile,
7639 message ("Error opening %s for reading: %s",
7640 fname, lldecodeerror (errno)));
7649 if (finclude (pfile, f, fname, 0, NULL))
7651 output_line_command (pfile, 0, same_file);
7657 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7659 return pfile->buffer;
7662 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7664 llassert (pfile->buffer != NULL);
7665 return pfile->buffer;
7668 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7670 llassert (buf->buf != NULL);
7671 return (buf->buf + buf->line_base);
7674 int cpplib_bufPeek (cppBuffer *buf)
7676 if (buf->cur == NULL || buf->rlimit == NULL) {
7680 if (buf->cur < buf->rlimit) {
7687 bool cppBuffer_isMacro (cppBuffer *buf)
7691 return (buf->cleanup == cppReader_macroCleanup);
7698 ** Returns true if the macro should be checked, false
7699 ** if it should be expanded normally.
7702 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7703 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7704 static bool expectiter = FALSE; /* preceeded by @iter@ */
7705 static bool expectenditer = FALSE; /* second after @iter@ */
7706 static bool expectfunction = FALSE; /* preceeded by @function@ */
7707 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7708 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7710 static void cpp_setLocation (cppReader *pfile)
7715 if (pfile->buffer != NULL)
7717 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7719 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7721 DPRINTF (("Looking up: %s", fname));
7723 if (fileTable_exists (context_fileTable (), fname))
7725 fid = fileTable_lookup (context_fileTable (), fname);
7729 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7731 fid = fileTable_lookup (context_fileTable (),
7732 cppReader_getBufferSafe (pfile)->fname);
7737 fid = fileTable_lookup (context_fileTable (),
7738 cppReader_getBufferSafe (pfile)->fname);
7741 line = cppReader_getBufferSafe (pfile)->lineno;
7742 fileloc_free (g_currentloc);
7744 if (fileId_isValid (fid))
7746 g_currentloc = fileloc_create (fid, line, 1);
7750 g_currentloc = fileloc_createBuiltin ();
7755 fileloc_free (g_currentloc);
7756 g_currentloc = fileloc_createBuiltin ();
7760 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7762 bool checkmacro = FALSE;
7763 bool hasParams = FALSE;
7764 bool noexpand = FALSE;
7768 cpp_setLocation (pfile);
7770 DPRINTF (("Should check macro? %s", p));
7772 if (expectiter || expectconstant || expectenditer)
7777 expectenditer = TRUE;
7782 expectconstant = FALSE;
7783 expectenditer = FALSE;
7786 if (notfunction || notparseable)
7788 notfunction = FALSE;
7789 notparseable = FALSE;
7798 llassert (*p == '#');
7801 while (*p == ' ' || *p == '\t')
7806 llassert (*p == 'd'); /* define starts */
7810 while (*p == ' ' || *p == '\t')
7815 sname = cstring_fromChars (p);
7816 DPRINTF (("Check macro: %s", sname));
7818 while (((c = *p) != ' ')
7819 && c != '\0' && c != '('
7820 && c != '\t' && c != '\\' && c != '\n'
7826 hasParams = (c == '(');
7831 notparseable = FALSE;
7833 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7835 DPRINTF (("Clear notfunction"));
7836 notfunction = FALSE;
7851 if (usymtab_existsReal (sname))
7853 uentry ue = usymtab_lookup (sname);
7855 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7857 if (fileloc_isPreproc (uentry_whereLast (ue)))
7863 if (uentry_isSpecified (ue))
7865 checkmacro = context_getFlag (FLG_SPECMACROS);
7871 checkmacro = context_getFlag (FLG_LIBMACROS)
7872 || context_getFlag (FLG_FCNMACROS);
7880 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7882 if (fileloc_isSystemFile (g_currentloc)
7883 && context_getFlag (FLG_SYSTEMDIREXPAND))
7885 ; /* don't check this macro */
7886 DPRINTF (("Don't check 1"));
7894 DPRINTF (("Has params..."));
7896 if (context_getFlag (FLG_FCNMACROS))
7898 if (usymtab_exists (sname))
7901 ** only get here is macro is redefined
7902 ** error reported elsewhere
7905 DPRINTF (("It exists!"));
7910 ** We make it a forward function, since it might be declared elsewhere.
7911 ** After all headers have been processed, we should check the forward
7915 fileloc loc = fileloc_makePreproc (g_currentloc);
7917 /* the line is off-by-one, since the newline was already read */
7922 expectfunction = FALSE;
7925 le = uentry_makeForwardFunction (sname,
7926 typeId_invalid, loc);
7932 /* Do not define here! */
7934 (void) usymtab_addEntry (le);
7938 DPRINTF (("Check: TRUE"));
7942 DPRINTF (("Flag FCN_MACROS not set!"));
7947 DPRINTF (("No params"));
7949 if (context_getFlag (FLG_CONSTMACROS))
7951 bool nocontent = FALSE;
7964 ** Check if there is nothing after the define.
7967 while ((*rest) != '\0' && isspace (*rest))
7974 nocontent = TRUE; /* empty macro, don't check */
7979 if (usymtab_exists (sname))
7985 fileloc loc = fileloc_makePreproc (g_currentloc);
7986 DPRINTF (("Make constant: %s", sname));
7987 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
7988 (void) usymtab_addEntry (le);
7991 checkmacro = !nocontent;
7996 if (checkmacro && usymtab_existsType (sname))
7998 DPRINTF (("Making false..."));
8000 ppllerror (message ("Specified type implemented as macro: %s", sname));
8010 if (usymtab_exists (sname))
8012 uentry ue = usymtab_lookupExpose (sname);
8013 fileloc tloc = fileloc_makePreproc (g_currentloc);
8015 uentry_setDefined (ue, tloc);
8016 fileloc_free (tloc);
8017 uentry_setUsed (ue, fileloc_undefined);
8021 fileloc tloc = fileloc_makePreproc (g_currentloc);
8022 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8023 DPRINTF (("Make expanded macro: %s", sname));
8024 DPRINTF (("Not in symbol table: %s", sname));
8026 (void) usymtab_addGlobalEntry (ue);
8027 fileloc_free (tloc);
8032 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8036 static enum cpp_token
8037 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8039 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8043 bool eliminateComment = FALSE;
8045 llassert (pbuf->buf != NULL);
8047 start = pbuf->buf + smark->position;
8049 llassert (pbuf->cur != NULL);
8050 len = pbuf->cur - start;
8053 && start[1] == context_getCommentMarkerChar ())
8057 char *scomment = start + 2;
8058 char savec = start[len];
8060 cpp_setLocation (pfile);
8061 loc = fileloc_copy (g_currentloc);
8063 start[0] = BEFORE_COMMENT_MARKER[0];
8064 start[1] = BEFORE_COMMENT_MARKER[1];
8066 llassert (start[len - 2] == '*');
8067 start[len - 2] = AFTER_COMMENT_MARKER[0];
8069 llassert (start[len - 1] == '/');
8070 start[len - 1] = AFTER_COMMENT_MARKER[1];
8072 cpplib_reserve(pfile, size_fromInt (1 + len));
8073 cppReader_putCharQ (pfile, c);
8075 cpp_setLocation (pfile);
8079 if (mstring_containsString (scomment, "/*"))
8081 (void) cppoptgenerror
8083 message ("Comment starts inside syntactic comment: %s",
8084 cstring_fromChars (scomment)),
8090 if (mstring_equalPrefix (scomment, "ignore"))
8092 if (!context_getFlag (FLG_NOCOMMENTS))
8094 context_enterSuppressRegion (loc);
8097 else if (mstring_equalPrefix (scomment, "end"))
8099 if (!context_getFlag (FLG_NOCOMMENTS))
8101 context_exitSuppressRegion (loc);
8104 else if (mstring_equalPrefix (scomment, "notparseable"))
8106 notparseable = TRUE;
8108 eliminateComment = TRUE;
8110 else if (mstring_equalPrefix (scomment, "notfunction"))
8114 eliminateComment = TRUE;
8116 else if (mstring_equalPrefix (scomment, "iter"))
8120 else if (mstring_equalPrefix (scomment, "function"))
8122 expectfunction = TRUE;
8124 else if (mstring_equalPrefix (scomment, "constant"))
8126 expectconstant = TRUE;
8130 char sChar = *scomment;
8136 char *rest = scomment + 1;
8138 if (mstring_equalPrefix (rest, "commentchar"))
8140 eliminateComment = TRUE;
8144 ppllerror (cstring_makeLiteral
8145 ("Cannot restore commentchar"));
8149 char *next = scomment + 12; /* strlen commentchar = 12 */
8151 if (*next != ' ' && *next != '\t' && *next != '\n')
8155 ("Syntactic commentchar comment is not followed by a "
8156 "whitespace character: %c",
8161 char cchar = *(next + 1);
8166 (cstring_makeLiteral
8167 ("Cannot set commentchar to NUL"));
8171 context_setCommentMarkerChar (cchar);
8172 /* setComment = TRUE; */
8177 else if (mstring_equalPrefix (scomment, "nestcomment"))
8179 /* fix from Mike Miller <MikeM@xata.com> */
8180 context_fileSetFlag (FLG_NESTCOMMENT,
8181 ynm_fromCodeChar (sChar),
8184 else if (mstring_equalPrefix (rest, "namechecks"))
8186 context_fileSetFlag (FLG_NAMECHECKS,
8187 ynm_fromCodeChar (sChar),
8190 else if (mstring_equalPrefix (rest, "macroredef"))
8192 context_fileSetFlag (FLG_MACROREDEF,
8193 ynm_fromCodeChar (sChar),
8196 else if (mstring_equalPrefix (rest, "usevarargs"))
8198 context_fileSetFlag (FLG_USEVARARGS,
8199 ynm_fromCodeChar (sChar),
8202 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8204 context_fileSetFlag (FLG_MACRONEXTLINE,
8205 ynm_fromCodeChar (sChar),
8208 else if (mstring_equalPrefix (rest, "allmacros")
8209 || mstring_equalPrefix (rest, "fcnmacros")
8210 || mstring_equalPrefix (rest, "constmacros"))
8214 if (mstring_equalPrefix (rest, "allmacros"))
8218 else if (mstring_equalPrefix (rest, "fcnmacros"))
8224 llassert (mstring_equalPrefix (rest, "constmacros"));
8225 fl = FLG_CONSTMACROS;
8228 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8229 notfunction = FALSE;
8242 if (eliminateComment)
8247 /* Replaces comment char's in start with spaces */
8249 for (i = 2; i < len - 2; i++)
8251 if (start[i] == BEFORE_COMMENT_MARKER[0]
8252 || start[i] == BEFORE_COMMENT_MARKER[1]
8253 || start[i] == context_getCommentMarkerChar ())
8259 cppReader_putStrN (pfile, start, size_fromInt (len));
8260 parseClearMark (smark);
8270 ** Output the comment as all spaces so line/column
8271 ** in output file is still correct.
8275 cstring lintcomment = cstring_undefined;
8277 if (context_getFlag (FLG_LINTCOMMENTS))
8279 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8281 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8283 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8285 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8287 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8289 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8291 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8293 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8295 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8297 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8301 lintcomment = cstring_undefined;
8306 lintcomment = cstring_undefined;
8309 if (cstring_isDefined (lintcomment))
8311 c = BEFORE_COMMENT_MARKER[0];
8312 start[0] = BEFORE_COMMENT_MARKER[1];
8314 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8316 for (i = 1; i < len - 2; i++)
8318 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8321 start[len - 2] = AFTER_COMMENT_MARKER[0];
8322 start[len - 1] = AFTER_COMMENT_MARKER[1];
8326 /* Replaces char's in start with spaces */
8327 for (i = 0; i < len; i++)
8331 && start[i + 1] == '*') {
8332 (void) cppoptgenerror
8334 message ("Comment starts inside comment"),
8338 if (start[i] != '\n')
8345 cpplib_reserve (pfile, size_fromInt (1 + len));
8346 cppReader_putCharQ (pfile, c);
8347 cppReader_putStrN (pfile, start, size_fromInt (len));
8348 parseClearMark (smark);
8354 static int cpp_openIncludeFile (char *filename)
8356 int res = open (filename, O_RDONLY, 0666);
8358 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8361 if (!fileTable_exists (context_fileTable (),
8362 cstring_fromChars (filename)))
8364 if (fileloc_isXHFile (g_currentloc))
8367 ** Files includes by XH files are also XH files
8370 (void) fileTable_addXHFile (context_fileTable (),
8371 cstring_fromChars (filename));
8375 (void) fileTable_addHeaderFile (context_fileTable (),
8376 cstring_fromChars (filename));
8381 DPRINTF (("File already exists: %s", filename));
8388 static bool cpp_skipIncludeFile (cstring fname)
8390 if (context_isSystemDir (fname))
8392 DPRINTF (("System dir: %s", fname));
8394 if (lcllib_isSkipHeader (fname))
8396 DPRINTF (("Skip include TRUE: %s", fname));
8400 if (context_getFlag (FLG_SKIPSYSHEADERS))
8402 DPRINTF (("Skip include TRUE: %s", fname));
8407 if (context_getFlag (FLG_SINGLEINCLUDE))
8409 fname = removePreDirs (fname);
8411 # if defined (WIN32) || defined (OS2)
8412 cstring_replaceAll (fname, '\\', '/');
8415 if (fileTable_exists (context_fileTable (), fname))
8417 DPRINTF (("Skip include TRUE: %s", fname));
8422 DPRINTF (("Skip include FALSE: %s", fname));
8426 static int cpp_peekN (cppReader *pfile, int n)
8428 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8430 llassert (buf->cur != NULL);
8432 return (buf->rlimit - buf->cur >= (n)
8437 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8442 void cppBuffer_forward (cppBuffer *buf, int n)
8444 llassert (buf->cur != NULL);