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 # include <time.h> /* Reported by Paul Smith */
79 # include <sys/time.h>
80 # include <sys/resource.h>
82 # include <sys/times.h>
87 # endif /* not WIN32 */
89 /* This defines "errno" properly for VMS, and gives us EACCES. */
92 # include "splintMacros.nf"
96 # include "cpperror.h"
104 ** This is really kludgey code...
110 #define NO_SHORTNAMES
118 /*@constant int IMPORT_FOUND@*/
119 # define IMPORT_FOUND -2
121 /*@constant int SKIP_INCLUDE@*/
122 # define SKIP_INCLUDE IMPORT_FOUND
124 /*@constant unused int IMPORT_NOT_FOUND@*/
125 # define IMPORT_NOT_FOUND -1
128 /*@constant unused int STDC_VALUE@*/
132 /* By default, colon separates directories in a path. */
133 #ifndef PATH_SEPARATOR
134 /*@constant char PATH_SEPARATOR@*/
135 #define PATH_SEPARATOR ':'
138 static void parse_name (cppReader *, int);
140 static int cpp_openIncludeFile (char *p_filename)
141 /*@modifies fileSystem @*/ ;
143 static void cpp_setLocation (cppReader *p_pfile)
144 /*@modifies g_currentloc@*/ ;
146 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
147 struct parse_marker *p_smark)
148 /*@modifies p_pfile, p_smark@*/;
150 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
152 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
154 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
160 /* Symbols to predefine. */
162 #ifdef CPP_PREDEFINES
163 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
165 static /*@observer@*/ char *predefs = "";
168 /* We let tm.h override the types used here, to handle trivial differences
169 such as the choice of unsigned int or long unsigned int for size_t.
170 When machines start needing nontrivial differences in the size type,
171 it would be best to do something here to figure out automatically
172 from other information what type to use. */
174 /* The string value for __SIZE_TYPE__. */
177 /*@constant observer char *SIZE_TYPE@*/
178 #define SIZE_TYPE "long unsigned int"
181 /* The string value for __PTRDIFF_TYPE__. */
184 /*@constant observer char *PTRDIFF_TYPE@*/
185 #define PTRDIFF_TYPE "long int"
188 /* The string value for __WCHAR_TYPE__. */
191 /*@constant observer char *WCHAR_TYPE@*/
192 #define WCHAR_TYPE "int"
195 /* The string value for __USER_LABEL_PREFIX__ */
197 #ifndef USER_LABEL_PREFIX
198 /*@constant observer char *USER_LABEL_PREFIX@*/
199 #define USER_LABEL_PREFIX ""
202 /* The string value for __REGISTER_PREFIX__ */
204 #ifndef REGISTER_PREFIX
205 /*@constant observer char *REGISTER_PREFIX@*/
206 #define REGISTER_PREFIX ""
209 /* table to tell if char can be part of a C identifier. */
210 static bool is_idchar[256];
211 /* table to tell if char can be first char of a c identifier. */
212 static bool is_idstart[256];
213 /* table to tell if c is horizontal space. */
214 static bool is_hor_space[256];
215 /* table to tell if c is horizontal or vertical space. */
216 static bool is_space[256];
218 static /*@exposed@*/ /*@null@*/ cppBuffer *
219 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
220 /*@uses p_pfile->buffer@*/
221 /*@modifies nothing@*/ ;
224 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
227 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
229 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
231 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
232 # define cppBuffer_get(BUFFER) \
233 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
235 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
236 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
237 modifies *p_file; @*/
238 # define cppReader_puts(PFILE, STR, N) \
239 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
241 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
243 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
244 modifies *p_file; @*/
245 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
247 /* Append character CH to PFILE's output buffer. Make space if need be. */
249 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
250 modifies *p_file; @*/
251 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
253 /* Make sure PFILE->limit is followed by '\0'. */
254 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
255 modifies *p_file; @*/
257 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
259 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
260 modifies *p_file; @*/
261 # define cppReader_nullTerminate(PFILE) \
262 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
264 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
265 modifies *p_file; @*/
266 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
268 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
269 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
271 /*@function static observer char *cppReader_wcharType (cppReader *)
272 modifies nothing; @*/
274 # define cppReader_wcharType(PFILE) \
275 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
277 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
279 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
280 # define cppReader_forward(pfile, N) \
281 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
283 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
284 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
286 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
287 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
289 /* Move all backslash-newline pairs out of embarrassing places.
290 Exchange all such pairs following BP
291 with any potentially-embarrassing characters that follow them.
292 Potentially-embarrassing characters are / and *
293 (because a backslash-newline inside a comment delimiter
294 would cause it not to be recognized). */
297 # define NEWLINE_FIX \
298 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
300 /* Same, but assume we've already read the potential '\\' into C. */
302 # define NEWLINE_FIX1(C) do { \
303 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
306 static void parseSetMark (/*@out@*/ struct parse_marker *,
308 static void parseClearMark (struct parse_marker *);
309 static void parseGotoMark (struct parse_marker *, cppReader *);
310 static void parseMoveMark (struct parse_marker *, cppReader *);
312 /* If we have a huge buffer, may need to cache more recent counts */
313 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
315 static /*@exposed@*/ /*@null@*/ cppBuffer *
316 cppReader_pushBuffer (cppReader *p_pfile,
317 /*@owned@*/ /*@null@*/ char *, size_t)
318 /*@modifies p_pfile@*/ ;
320 static void cppReader_appendIncludeChain
322 /*@keep@*/ struct file_name_list *p_first,
323 /*@dependent@*/ struct file_name_list *p_last);
325 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
326 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
328 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
329 /*@unused@*/ cppReader *p_pfile);
331 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
332 /*@unused@*/ cppReader *p_pfile);
334 static int cppReader_handleDirective (cppReader *p_pfile);
336 static void cppReader_scanBuffer (cppReader *p_pfile);
338 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
341 ** WIN32 (at least the VC++ include files) does not define mode_t.
344 /*@-incondefs@*/ /*@-czechtypes@*/
345 typedef unsigned int mode_t;
346 /*@=incondefs@*/ /*@=czechtypes@*/
350 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
351 /*@out@*/ size_t *p_size_pointer);
352 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
356 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
357 ** (Note that it is false while we're expanding marco *arguments*.)
360 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
362 static void path_include (cppReader *p_pfile, char *p_path)
363 /*@modifies p_pfile@*/ ;
365 static void initialize_builtins (cppReader *p_pfile)
366 /*@modifies p_pfile@*/ ;
368 static void initialize_char_syntax (struct cppOptions *p_opts) ;
370 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
372 bool p_system_header_p,
373 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
375 static void validate_else (cppReader *p_pfile, cstring p_directive);
377 static void conditional_skip (cppReader *p_pfile, int p_skip,
378 enum node_type p_type,
379 /*@dependent@*/ /*@null@*/ char *p_control_macro);
381 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
385 static void skip_if_group (cppReader *p_pfile, int p_any);
387 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
388 char *p_beg2, int p_len2, bool p_last);
391 extern void fancy_abort ();
394 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
395 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
397 static /*@observer@*/ /*@null@*/ struct file_name_map *
398 read_name_map (cppReader *p_pfile, cstring p_dirname);
400 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
402 static int open_include_file (cppReader *p_pfile,
403 /*@owned@*/ cstring p_fname,
404 /*@null@*/ struct file_name_list *p_searchptr);
406 static void push_macro_expansion (cppReader *,
407 /*@owned@*/ char *, size_t,
408 /*@dependent@*/ hashNode);
410 /* Last arg to output_line_command. */
411 enum file_change_code {
412 same_file, enter_file, leave_file
415 /* `struct directive' defines one #-directive, including how to handle it. */
418 int length; /* Length of name */
419 /*@null@*/ int (*func)(); /* Function to handle directive */
420 /*@observer@*/ cstring name; /* Name of directive */
421 enum node_type type; /* Code which describes which directive. */
422 bool command_reads_line; /* One if rest of line is read by func. */
423 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
424 bool pass_thru; /* Copy preprocessed directive to output file.*/
427 /* These functions are declared to return int instead of void since they
428 are going to be placed in a table and some old compilers have trouble with
429 pointers to functions returning void. */
431 static int do_define (cppReader *, /*@null@*/ struct directive *,
432 /*@exposed@*/ char *, char *);
433 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
434 /*@exposed@*/ char *, char *, bool);
436 static int do_line (cppReader *, /*@null@*/ struct directive *);
437 static int do_include (cppReader *, struct directive *, char *, char *);
438 static int do_undef (cppReader *, struct directive *, char *, char *);
439 static int do_error (cppReader *, struct directive *, char *, char *);
440 static int do_pragma (cppReader *, struct directive *, char *, char *);
441 static int do_ident (cppReader *, struct directive *, char *, char *);
442 static int do_if (cppReader *, struct directive *, char *, char *);
443 static int do_xifdef (cppReader *, struct directive *, char *, char *);
444 static int do_else (cppReader *, struct directive *, char *, char *);
445 static int do_elif (cppReader *, struct directive *, char *, char *);
446 static int do_endif (cppReader *, struct directive *, char *, char *);
447 static int do_warning (cppReader *, struct directive *, char *, char *);
449 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
450 via the same directory as the file that #included it. */
452 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
453 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
455 /* #include "file" looks in source file dir, then stack. */
456 /* #include <file> just looks in the stack. */
457 /* -I directories are added to the end, then the defaults are added. */
461 static struct default_include {
462 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
463 int cplusplus; /* Only look here if we're compiling C++. */
464 int cxx_aware; /* Includes in this directory don't need to
465 be wrapped in extern "C" when compiling
467 } include_defaults_array[]
469 /* This is the dir for fixincludes. Put it just before
470 the files that we fix. */
471 { GCC_INCLUDE_DIR, 0, 0 },
472 { GCC_INCLUDE_DIR2, 0, 0 },
473 { cstring_undefined, 0, 0 }
476 /*@noaccess cstring@*/
478 /* Here is the actual list of #-directives, most-often-used first.
479 The initialize_builtins function assumes #define is the very first. */
483 static struct directive directive_table[] = {
484 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
485 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
486 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
487 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
488 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
489 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
490 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
491 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
492 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
493 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
494 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
495 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
496 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
497 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
498 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
499 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
501 /*@noaccess cstring@*/
503 static cstring searchPath_unparse (struct file_name_list *search_start)
505 cstring res = cstring_newEmpty ();
506 struct file_name_list *searchptr = NULL;
508 for (searchptr = search_start; searchptr != NULL;
509 searchptr = searchptr->next)
511 if (!cstring_isEmpty (searchptr->fname)) {
512 res = cstring_concatFree1 (res, searchptr->fname);
513 if (searchptr->next != NULL) {
514 res = cstring_appendChar (res, ';');
524 initialize_char_syntax (struct cppOptions *opts)
529 * Set up is_idchar and is_idstart tables. These should be
530 * faster than saying (is_alpha (c) || c == '_'), etc.
531 * Set up these things before calling any routines tthat
535 for (i = 'a'; i <= 'z'; i++) {
536 is_idchar[i - 'a' + 'A'] = TRUE;
537 is_idchar[(int) i] = TRUE;
538 is_idstart[i - 'a' + 'A'] = TRUE;
539 is_idstart[(int) i] = TRUE;
542 for (i = '0'; i <= '9'; i++)
544 is_idchar[(int) i] = TRUE;
547 is_idchar['_'] = TRUE;
548 is_idstart['_'] = TRUE;
549 is_idchar['$'] = opts->dollars_in_ident;
550 is_idstart['$'] = opts->dollars_in_ident;
552 /* horizontal space table */
553 is_hor_space[' '] = TRUE;
554 is_hor_space['\t'] = TRUE;
555 is_hor_space['\v'] = TRUE;
556 is_hor_space['\f'] = TRUE;
557 is_hor_space['\r'] = TRUE;
559 is_space[' '] = TRUE;
560 is_space['\t'] = TRUE;
561 is_space['\v'] = TRUE;
562 is_space['\f'] = TRUE;
563 is_space['\n'] = TRUE;
564 is_space['\r'] = TRUE;
567 bool isIdentifierChar (char c)
569 return is_idchar[(int) c];
572 /* Place into P_PFILE a quoted string representing the string SRC.
573 Caller must reserve enough space in pfile->token_buffer. */
576 quote_string (cppReader *pfile, char *src)
580 cppReader_putCharQ (pfile, '\"');
583 switch ((c = *src++))
587 cppReader_putCharQ (pfile, c);
590 sprintf (cpplib_getPWritten (pfile), "\\%03o",
592 cppReader_adjustWritten (pfile, (size_t) 4);
594 /*@switchbreak@*/ break;
598 cppReader_putCharQ (pfile, '\\');
599 cppReader_putCharQ (pfile, c);
600 /*@switchbreak@*/ break;
603 cppReader_putCharQ (pfile, '\"');
604 cppReader_nullTerminateQ (pfile);
610 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
613 cppReader_growBuffer (cppReader *pfile, size_t n)
615 size_t old_written = cpplib_getWritten (pfile);
616 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
617 pfile->token_buffer = (char *)
618 drealloc (pfile->token_buffer, pfile->token_buffer_size);
619 cppReader_setWritten (pfile, old_written);
623 * process a given definition string, for initialization
624 * If STR is just an identifier, define it with value 1.
625 * If STR has anything after the identifier, then it should
626 * be identifier=definition.
630 cppReader_define (cppReader *pfile, char *str)
635 DPRINTF (("Cpp reader define: %s", str));
637 if (!is_idstart[(int) *p])
639 DPRINTF (("ERROR 1"));
640 cppReader_error (pfile,
641 message ("Malformed option `-D%s'",
642 cstring_fromChars (str)));
649 DPRINTF (("Here 2"));
651 while (is_idchar[(int) *p])
658 while (*p != ')' && *p != '\0') {
667 message ("Malformed option: -D%s (no closing parenthesis)",
668 cstring_fromChars (str)));
672 DPRINTF (("Here 2"));
676 buf = (char *) dmalloc (size_fromInt (p - str + 4));
677 strcpy ((char *) buf, str);
678 strcat ((char *) buf, " 1");
682 DPRINTF (("ERROR 2"));
683 cppReader_error (pfile,
684 message ("Malformed option: -D%s (expected '=', found '%c')",
685 cstring_fromChars (str),
692 /* Copy the entire option so we can modify it. */
693 DPRINTF (("Copying..."));
694 buf = (char *) dmalloc (2 * strlen (str) + 1);
695 strncpy (buf, str, size_fromInt (p - str));
697 /* Change the = to a space. */
699 /* Scan for any backslash-newline and remove it. */
705 if (*p == '\\' && p[1] == '\n')
711 DPRINTF (("Here we are..."));
715 llassert (buf != NULL);
716 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
717 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
721 /* Append a chain of `struct file_name_list's
722 to the end of the main include chain.
723 FIRST is gthe beginning of the chain to append, and LAST is the end. */
726 cppReader_appendIncludeChain (cppReader *pfile,
727 struct file_name_list *first,
728 struct file_name_list *last)
730 struct cppOptions *opts = CPPOPTIONS (pfile);
731 struct file_name_list *dir;
733 if (first == NULL || last == NULL)
738 if (opts->include == 0)
740 opts->include = first;
744 llassert (opts->last_include->next == NULL);
745 opts->last_include->next = first;
748 if (opts->first_bracket_include == 0)
750 opts->first_bracket_include = first;
752 for (dir = first; ; dir = dir->next) {
753 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
754 if (len > pfile->max_include_len)
755 pfile->max_include_len = len;
761 llassert (last->next == NULL);
762 /* last->next = NULL; */
763 opts->last_include = last;
767 static /*@unused@*/ void
768 cppReader_showIncludeChain (cppReader *pfile)
770 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
776 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
780 fprintf (stderr, "\n");
784 fprintf (stderr, "No includes\n");
790 cppReader_getIncludePath ()
792 cppReader *pfile = &g_cppState;
793 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
794 cstring res = cstring_undefined;
800 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
806 res = cstring_makeLiteral ("<no include path>");
813 cppReader_addIncludeChain (cppReader *pfile, struct file_name_list *dir)
815 struct cppOptions *opts = CPPOPTIONS (pfile);
822 if (opts->include == 0)
828 llassert (opts->last_include->next == NULL);
829 opts->last_include->next = dir;
832 if (opts->first_bracket_include == 0)
834 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
835 opts->first_bracket_include = dir;
836 if (len > pfile->max_include_len)
838 pfile->max_include_len = len;
843 opts->last_include = dir;
844 /* cppReader_showIncludeChain (pfile); */
847 /* Given a colon-separated list of file names PATH,
848 add all the names to the search path for include files. */
851 path_include (cppReader *pfile, char *path)
858 /* if we have a posix path list, convert to win32 path list */
859 win32temp = (char *) dmalloc /*@i4@*/
860 (cygwin32_posix_to_win32_path_list_buf_size (path));
861 cygwin32_posix_to_win32_path_list (path, win32temp);
871 struct file_name_list *dirtmp;
873 /* Find the end of this name. */
874 while (*q != '\0' && *q != PATH_SEPARATOR)
881 /* An empty name in the path stands for the current directory. */
882 name = (char *) dmalloc ((size_t) 2);
888 /* Otherwise use the directory that is named. */
889 name = (char *) dmalloc (size_fromInt (q - p + 1));
890 memcpy (name, p, size_fromInt (q - p));
894 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
895 dirtmp->next = 0; /* New one goes on the end */
896 dirtmp->control_macro = 0;
897 dirtmp->c_system_include_path = 0;
898 dirtmp->fname = cstring_fromChars (name);
899 dirtmp->got_name_map = 0;
900 cppReader_addIncludeChain (pfile, dirtmp);
902 /* Advance past this name. */
906 /* Skip the colon. */
912 cppOptions_init (cppOptions *opts)
914 memset ((char *) opts, 0, sizeof *opts);
917 opts->in_fname = NULL;
918 opts->out_fname = NULL;
920 /* Initialize is_idchar to allow $. */
921 opts->dollars_in_ident = TRUE;
923 opts->no_line_commands = 0;
924 opts->no_trigraphs = TRUE;
925 opts->put_out_comments = 1;
926 opts->print_include_names = 0;
927 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
931 opts->cplusplus_comments = 1;
936 opts->pedantic_errors = 0;
937 opts->warn_comments = 0;
938 opts->warnings_are_errors = 0;
940 initialize_char_syntax (opts);
944 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
950 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
951 /*@unused@*/ cppReader *pfile)
957 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
959 hashNode macro = pbuf->hnode;
961 if (macro->type == T_DISABLED)
963 macro->type = T_MACRO;
966 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
974 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
976 if (pbuf->buf != NULL)
983 /* Assuming we have read '/'.
984 If this is the start of a comment (followed by '*' or '/'),
985 skip to the end of the comment, and return ' '.
986 Return EOF if we reached the end of file before the end of the comment.
987 If not the start of a comment, return '/'. */
990 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
994 llassert (pfile->buffer != NULL);
995 llassert (pfile->buffer->cur != NULL);
997 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1004 cppReader_forward (pfile, 2);
1007 if (cppReader_peekC (pfile) == '*')
1009 cppReader_forward (pfile, 1);
1014 c = cppReader_getC (pfile);
1021 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1028 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1031 if (prev_c == (int) '*' && c == (int) '/')
1036 if (c == (int) '\n' && (linep != NULL))
1042 else if (cppReader_peekC (pfile) == '/'
1043 && CPPOPTIONS (pfile)->cplusplus_comments)
1046 (void) cppoptgenerror
1047 (FLG_SLASHSLASHCOMMENT,
1048 message ("C++ style // comment"
1052 cppReader_forward (pfile, 1);
1056 c = cppReader_getC (pfile);
1060 /* Allow hash comment to be terminated by EOF. */
1064 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1066 cppReader_forward (pfile, 1);
1067 c = cppReader_getC (pfile);
1075 if (c == (int) '\n')
1077 /* Don't consider final '\n' to be part of comment. */
1078 cppReader_forward (pfile, -1);
1089 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1091 cppSkipHspace (cppReader *pfile)
1099 llassert (pfile->buffer != NULL);
1101 c = cppReader_peekC (pfile);
1105 return 0; /* FIXME */
1108 if (is_hor_space[c])
1110 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1111 cppReader_pedwarn (pfile,
1112 message ("%s in preprocessing directive",
1114 ? cstring_makeLiteralTemp ("formfeed")
1115 : cstring_makeLiteralTemp ("vertical tab")));
1118 cppReader_forward (pfile, 1);
1122 cppReader_forward (pfile, 1);
1123 c = skip_comment (pfile, NULL);
1127 cppReader_forward (pfile, -1);
1130 if (c == EOF || c == '/')
1135 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1137 cppReader_forward (pfile, 2);
1139 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1140 && is_hor_space [cpp_peekN (pfile, 1)])
1142 cppReader_forward (pfile, 2);
1151 /* Read the rest of the current line.
1152 The line is appended to PFILE's output buffer. */
1155 copy_rest_of_line (cppReader *pfile)
1157 struct cppOptions *opts = CPPOPTIONS (pfile);
1164 llassert (pfile->buffer != NULL);
1166 c = cppReader_getC (pfile);
1172 if (cppReader_peekC (pfile) == '\n')
1174 cppReader_forward (pfile, 1);
1178 /*@fallthrough@*/ case '\'': case '\"':
1179 goto scan_directive_token;
1182 nextc = cppReader_peekC (pfile);
1185 ** was (opts->cplusplus_comments && nextc == '*')
1190 || (opts->cplusplus_comments && nextc == '/'))
1192 goto scan_directive_token;
1194 /*@switchbreak@*/ break;
1197 if (cppReader_isPedantic (pfile))
1198 cppReader_pedwarn (pfile,
1199 message ("%s in preprocessing directive",
1201 ? cstring_makeLiteralTemp ("formfeed")
1202 : cstring_makeLiteralTemp ("vertical tab")));
1203 /*@switchbreak@*/ break;
1206 cppReader_forward (pfile, -1);
1208 scan_directive_token:
1209 cppReader_forward (pfile, -1);
1210 (void) cpplib_getToken (pfile);
1213 cppReader_putChar (pfile, c);
1216 cppReader_nullTerminate (pfile);
1220 cppReader_skipRestOfLine (cppReader *pfile)
1222 size_t old = cpplib_getWritten (pfile);
1223 copy_rest_of_line (pfile);
1224 cppReader_setWritten (pfile, old);
1227 /* Handle a possible # directive.
1228 '#' has already been read. */
1231 cppReader_handleDirective (cppReader *pfile)
1234 struct directive *kt = NULL;
1236 size_t after_ident = 0;
1238 char *line_end = NULL;
1239 size_t old_written = cpplib_getWritten (pfile);
1240 int nspaces = cppSkipHspace (pfile);
1242 c = cppReader_peekC (pfile);
1244 if (c >= '0' && c <= '9')
1246 /* Handle # followed by a line number. */
1247 if (cppReader_isPedantic (pfile))
1249 cppReader_pedwarnLit
1251 cstring_makeLiteralTemp ("`#' followed by integer"));
1254 (void) do_line (pfile, NULL);
1255 goto done_a_directive;
1259 /* Now find the directive name. */
1261 cppReader_putChar (pfile, '#');
1263 parse_name (pfile, cppReader_getC (pfile));
1265 llassert (pfile->token_buffer != NULL);
1266 ident = pfile->token_buffer + old_written + 1;
1268 ident_length = cpplib_getPWritten (pfile) - ident;
1270 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1272 /* A line of just `#' becomes blank. */
1276 for (kt = directive_table; ; kt++)
1278 if (kt->length <= 0)
1280 return 0; /* goto not_a_directive; */
1283 if (kt->length == ident_length
1284 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1290 if (kt->command_reads_line)
1296 /* Nonzero means do not delete comments within the directive.
1297 #define needs this when -traditional. */
1298 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1299 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1300 CPPOPTIONS (pfile)->put_out_comments = comments;
1301 after_ident = cpplib_getWritten (pfile);
1302 copy_rest_of_line (pfile);
1303 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1307 /* For #pragma and #define, we may want to pass through the directive.
1308 Other directives may create output, but we don't want the directive
1309 itself out, so we pop it now. For example #include may write a #line
1310 command (see comment in do_include), and conditionals may emit
1311 #failed ... #endfailed stuff. But note that popping the buffer
1312 means the parameters to kt->func may point after pfile->limit
1313 so these parameters are invalid as soon as something gets appended
1314 to the token_buffer. */
1316 line_end = cpplib_getPWritten (pfile);
1319 if (!kt->pass_thru && kt->type != T_DEFINE)
1321 cppReader_setWritten (pfile, old_written);
1324 llassert (pfile->token_buffer != NULL);
1326 /* was kt->pass_thru || */
1328 if (kt->type == T_DEFINE
1329 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1331 char *p = pfile->token_buffer + old_written;
1334 ** Still need to record value for preprocessing, so
1335 ** #ifdef's, etc. using the value behave correctly.
1338 (void) do_defineAux (pfile, kt,
1339 pfile->token_buffer + after_ident,
1348 SKIP_WHITE_SPACE (p);
1350 llassert (*p == 'd');
1353 llassert (*p == 'e');
1356 llassert (*p == 'f');
1359 llassert (*p == 'i');
1362 llassert (*p == 'n');
1365 llassert (*p == 'e');
1368 ** This is way-bogus. We use the last char to record the number of
1369 ** spaces. Its too hard to get them back into the input stream.
1372 if (nspaces > 9) nspaces = 9;
1374 *p++ = '0' + nspaces;
1376 return 0; /* not_a_directive */
1378 else if (kt->pass_thru)
1380 /* Just leave the entire #define in the output stack. */
1381 return 0; /* not_a_directive */
1384 else if (kt->type == T_DEFINE
1385 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1387 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1388 SKIP_WHITE_SPACE (p);
1390 while (is_idchar[(int) *p])
1396 cppReader_putChar (pfile, '\n');
1398 else if (kt->type == T_DEFINE)
1400 cppReader_setWritten (pfile, old_written);
1411 llassert (kt->func != NULL);
1412 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1417 /* Pass a directive through to the output file.
1418 BUF points to the contents of the directive, as a contiguous string.
1419 LIMIT points to the first character past the end of the directive.
1420 KEYWORD is the keyword-table entry for the directive. */
1423 pass_thru_directive (char *buf, char *limit,
1425 struct directive *keyword)
1427 int keyword_length = keyword->length;
1429 cpplib_reserve (pfile,
1430 size_fromInt (2 + keyword_length + (limit - buf)));
1431 cppReader_putCharQ (pfile, '#');
1432 /*@-observertrans@*/
1433 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1434 size_fromInt (keyword_length));
1435 /*:=observertrans@*/
1437 if (limit != buf && buf[0] != ' ')
1439 /* Was a bug, since reserve only used 1 + ... */
1440 cppReader_putCharQ (pfile, ' ');
1443 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1446 /* Read a replacement list for a macro with parameters.
1447 Build the DEFINITION structure.
1448 Reads characters of text starting at BUF until END.
1449 ARGLIST specifies the formal parameters to look for
1450 in the text of the definition; NARGS is the number of args
1451 in that list, or -1 for a macro name that wants no argument list.
1452 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1453 and NAMELEN is its length in characters.
1455 Note that comments, backslash-newlines, and leading white space
1456 have already been deleted from the argument. */
1459 collect_expansion (cppReader *pfile, char *buf, char *limit,
1460 int nargs, /*@null@*/ struct arglist *arglist)
1463 char *p, *lastp, *exp_p;
1464 struct reflist *endpat = NULL;
1465 /* Pointer to first nonspace after last ## seen. */
1467 /* Pointer to first nonspace after last single-# seen. */
1468 char *stringify = 0;
1470 char expected_delimiter = '\0';
1473 /* Scan thru the replacement list, ignoring comments and quoted
1474 strings, picking up on the macro calls. It does a linear search
1475 thru the arg list on every potential symbol. Profiling might say
1476 that something smarter should happen. */
1481 /* Find the beginning of the trailing whitespace. */
1484 while (p < limit && is_space[(int) limit[-1]])
1489 /* Allocate space for the text in the macro definition.
1490 Leading and trailing whitespace chars need 2 bytes each.
1491 Each other input char may or may not need 1 byte,
1492 so this is an upper bound. The extra 5 are for invented
1493 leading and trailing newline-marker and final null. */
1494 maxsize = (sizeof (*defn) + (limit - p) + 5);
1496 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1505 defn = (DEFINITION *) dmalloc (maxsize);
1506 defn->noExpand = FALSE;
1508 defn->pattern = NULL;
1509 defn->nargs = nargs;
1510 defn->predefined = NULL;
1512 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1515 defn->rest_args = NULL;
1516 defn->args.argnames = NULL;
1522 /* Add one initial space escape-marker to prevent accidental
1523 token-pasting (often removed by cpplib_macroExpand). */
1527 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1528 cppReader_errorLit (pfile,
1529 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1533 /* Process the main body of the definition. */
1535 int skipped_arg = 0;
1536 register char c = *p++;
1540 if (!cppReader_isTraditional (pfile)) {
1544 if (expected_delimiter != '\0')
1546 if (c == expected_delimiter)
1547 expected_delimiter = '\0';
1551 expected_delimiter = c;
1553 /*@switchbreak@*/ break;
1556 if (p < limit && (expected_delimiter != '\0'))
1558 /* In a string, backslash goes through
1559 and makes next char ordinary. */
1562 /*@switchbreak@*/ break;
1565 /* An '@' in a string or character constant stands for itself,
1566 and does not need to be escaped. */
1567 if (expected_delimiter == '\0')
1572 /*@switchbreak@*/ break;
1575 /* # is ordinary inside a string. */
1576 if (expected_delimiter != '\0')
1578 /*@switchbreak@*/ break;
1581 if (p < limit && *p == '#') {
1582 /* ##: concatenate preceding and following tokens. */
1583 /* Take out the first #, discard preceding whitespace. */
1587 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1593 /* Skip the second #. */
1595 /* Discard following whitespace. */
1596 SKIP_WHITE_SPACE (p);
1600 cppReader_errorLit (pfile,
1601 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1603 } else if (nargs >= 0) {
1604 /* Single #: stringify following argument ref.
1605 Don't leave the # in the expansion. */
1607 SKIP_WHITE_SPACE (p);
1608 if (p == limit || ! is_idstart[(int) *p]
1609 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1610 cppReader_errorLit (pfile,
1611 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1618 /*@switchbreak@*/ break;
1621 /* In -traditional mode, recognize arguments inside strings and
1622 and character constants, and ignore special properties of #.
1623 Arguments inside strings are considered "stringified", but no
1624 extra quote marks are supplied. */
1628 if (expected_delimiter != '\0') {
1629 if (c == expected_delimiter)
1630 expected_delimiter = '\0';
1632 expected_delimiter = c;
1633 /*@switchbreak@*/ break;
1636 /* Backslash quotes delimiters and itself, but not macro args. */
1637 if (expected_delimiter != '\0' && p < limit
1638 && (*p == expected_delimiter || *p == '\\')) {
1642 /*@switchbreak@*/ break;
1645 if (expected_delimiter != '\0') /* No comments inside strings. */
1646 /*@switchbreak@*/ break;
1648 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1649 this must be -traditional. So replace the comment with
1653 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1658 /*@switchbreak@*/ break;
1662 /* Handle the start of a symbol. */
1663 if (is_idchar[(int) c] && nargs > 0) {
1664 char *id_beg = p - 1;
1668 while (p != limit && is_idchar[(int) *p])
1673 id_len = size_fromInt (p - id_beg);
1675 if (is_idstart[(int) c]
1676 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1677 register struct arglist *arg;
1679 for (arg = arglist; arg != NULL; arg = arg->next) {
1680 struct reflist *tpat;
1682 if (arg->name[0] == c
1683 && arg->length == id_len
1684 && strncmp (arg->name, id_beg, id_len) == 0) {
1687 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1688 if (cppReader_isTraditional (pfile)) {
1689 cppReader_warning (pfile,
1690 message ("macro argument `%x' is stringified.",
1691 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1693 cppReader_warning (pfile,
1694 message ("macro arg `%x' would be stringified with -traditional.",
1695 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1698 /* If ANSI, don't actually substitute inside a string. */
1699 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1700 /*@innerbreak@*/ break;
1701 /* make a pat node for this arg and append it to the end of
1703 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1705 tpat->raw_before = (concat == id_beg);
1706 tpat->raw_after = 0;
1707 tpat->rest_args = arg->rest_args;
1708 tpat->stringify = (cppReader_isTraditional (pfile)
1709 ? expected_delimiter != '\0'
1710 : stringify == id_beg);
1714 defn->pattern = tpat;
1718 endpat->next = tpat;
1720 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1724 tpat->argno = arg->argno;
1725 tpat->nchars = exp_p - lastp;
1729 SKIP_WHITE_SPACE (p1);
1731 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1733 tpat->raw_after = 1;
1736 lastp = exp_p; /* place to start copying from next time */
1739 /*@innerbreak@*/ break;
1744 /* If this was not a macro arg, copy it into the expansion. */
1745 if (skipped_arg == 0) {
1746 register char *lim1 = p;
1754 if (stringify == id_beg)
1755 cppReader_errorLit (pfile,
1756 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1761 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1763 /* If ANSI, put in a "@ " marker to prevent token pasting.
1764 But not if "inside a string" (which in ANSI mode
1765 happens only for -D option). */
1772 defn->length = size_fromInt (exp_p - defn->expansion);
1774 /* Crash now if we overrun the allocated size. */
1775 if (defn->length + 1 > maxsize)
1777 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1785 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1789 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1790 int nargs, /*@null@*/ struct arglist *arglist)
1793 char *p, *lastp, *exp_p;
1794 struct reflist *endpat = NULL;
1795 /* Pointer to first nonspace after last ## seen. */
1797 /* Pointer to first nonspace after last single-# seen. */
1798 char *stringify = 0;
1800 char expected_delimiter = '\0';
1803 /* Scan thru the replacement list, ignoring comments and quoted
1804 strings, picking up on the macro calls. It does a linear search
1805 thru the arg list on every potential symbol. Profiling might say
1806 that something smarter should happen. */
1810 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1811 fileloc_unparse (loc)));
1814 /* Find the beginning of the trailing whitespace. */
1817 while (p < limit && is_space[(int) limit[-1]])
1822 /* Allocate space for the text in the macro definition.
1823 Leading and trailing whitespace chars need 2 bytes each.
1824 Each other input char may or may not need 1 byte,
1825 so this is an upper bound. The extra 5 are for invented
1826 leading and trailing newline-marker and final null. */
1827 maxsize = (sizeof (*defn) + (limit - p) + 5);
1829 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1838 defn = (DEFINITION *) dmalloc (maxsize);
1839 defn->noExpand = FALSE;
1841 defn->pattern = NULL;
1842 defn->nargs = nargs;
1843 defn->predefined = NULL;
1845 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1848 defn->rest_args = NULL;
1849 defn->args.argnames = NULL;
1855 /* Add one initial space escape-marker to prevent accidental
1856 token-pasting (often removed by cpplib_macroExpand). */
1860 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1861 voptgenerror (FLG_PREPROC,
1862 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1867 /* Process the main body of the definition. */
1869 int skipped_arg = 0;
1870 register char c = *p++;
1874 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1878 if (expected_delimiter != '\0')
1880 if (c == expected_delimiter)
1881 expected_delimiter = '\0';
1885 expected_delimiter = c;
1887 /*@switchbreak@*/ break;
1890 if (p < limit && (expected_delimiter != '\0'))
1892 /* In a string, backslash goes through
1893 and makes next char ordinary. */
1896 /*@switchbreak@*/ break;
1899 /* An '@' in a string or character constant stands for itself,
1900 and does not need to be escaped. */
1901 if (expected_delimiter == '\0')
1906 /*@switchbreak@*/ break;
1909 /* # is ordinary inside a string. */
1910 if (expected_delimiter != '\0')
1912 /*@switchbreak@*/ break;
1915 if (p < limit && *p == '#') {
1916 /* ##: concatenate preceding and following tokens. */
1917 /* Take out the first #, discard preceding whitespace. */
1921 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1927 /* Skip the second #. */
1929 /* Discard following whitespace. */
1930 SKIP_WHITE_SPACE (p);
1934 voptgenerror (FLG_PREPROC,
1935 cstring_makeLiteral ("`##' at end of macro definition"),
1938 } else if (nargs >= 0) {
1939 /* Single #: stringify following argument ref.
1940 Don't leave the # in the expansion. */
1942 SKIP_WHITE_SPACE (p);
1943 if (p == limit || ! is_idstart[(int) *p]
1944 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1948 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1957 /*@switchbreak@*/ break;
1960 /* In -traditional mode, recognize arguments inside strings and
1961 and character constants, and ignore special properties of #.
1962 Arguments inside strings are considered "stringified", but no
1963 extra quote marks are supplied. */
1967 if (expected_delimiter != '\0') {
1968 if (c == expected_delimiter)
1969 expected_delimiter = '\0';
1971 expected_delimiter = c;
1972 /*@switchbreak@*/ break;
1975 /* Backslash quotes delimiters and itself, but not macro args. */
1976 if (expected_delimiter != '\0' && p < limit
1977 && (*p == expected_delimiter || *p == '\\')) {
1981 /*@switchbreak@*/ break;
1984 if (expected_delimiter != '\0') /* No comments inside strings. */
1985 /*@switchbreak@*/ break;
1987 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1988 this must be -traditional. So replace the comment with
1992 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1997 /*@switchbreak@*/ break;
2001 /* Handle the start of a symbol. */
2002 if (is_idchar[(int) c] && nargs > 0) {
2003 char *id_beg = p - 1;
2007 while (p != limit && is_idchar[(int) *p])
2012 id_len = size_fromInt (p - id_beg);
2014 if (is_idstart[(int) c]
2015 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2016 register struct arglist *arg;
2018 for (arg = arglist; arg != NULL; arg = arg->next) {
2019 struct reflist *tpat;
2021 if (arg->name[0] == c
2022 && arg->length == id_len
2023 && strncmp (arg->name, id_beg, id_len) == 0) {
2026 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2027 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2028 voptgenerror (FLG_PREPROC,
2029 message ("macro argument `%x' is stringified.",
2030 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2034 voptgenerror (FLG_PREPROC,
2035 message ("Macro arg `%x' would be stringified with -traditional.",
2036 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2041 /* If ANSI, don't actually substitute inside a string. */
2042 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2043 /*@innerbreak@*/ break;
2044 /* make a pat node for this arg and append it to the end of
2046 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2048 tpat->raw_before = (concat == id_beg);
2049 tpat->raw_after = 0;
2050 tpat->rest_args = arg->rest_args;
2051 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2052 ? expected_delimiter != '\0'
2053 : stringify == id_beg);
2057 defn->pattern = tpat;
2061 endpat->next = tpat;
2063 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2067 tpat->argno = arg->argno;
2068 tpat->nchars = exp_p - lastp;
2072 SKIP_WHITE_SPACE (p1);
2074 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2076 tpat->raw_after = 1;
2079 lastp = exp_p; /* place to start copying from next time */
2082 /*@innerbreak@*/ break;
2087 /* If this was not a macro arg, copy it into the expansion. */
2088 if (skipped_arg == 0) {
2089 register char *lim1 = p;
2097 if (stringify == id_beg)
2101 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2108 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2110 /* If ANSI, put in a "@ " marker to prevent token pasting.
2111 But not if "inside a string" (which in ANSI mode
2112 happens only for -D option). */
2119 defn->length = size_fromInt (exp_p - defn->expansion);
2121 /* Crash now if we overrun the allocated size. */
2122 if (defn->length + 1 > maxsize)
2124 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2131 * special extension string that can be added to the last macro argument to
2132 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2133 * #define wow(a, b...) process (b, a, b)
2134 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2135 * { wow (one, two); } -> { process (two, one, two); }
2136 * if this "rest_arg" is used with the concat token '##' and if it is not
2137 * supplied then the token attached to with ## will not be outputted. Ex:
2138 * #define wow (a, b...) process (b ## , a, ## b)
2139 * { wow (1, 2); } -> { process (2, 1, 2); }
2140 * { wow (one); } -> { process (one); {
2143 /*@-readonlytrans@*/
2144 static char rest_extension[] = "...";
2145 /*:=readonlytrans@*/
2148 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2150 /* Create a DEFINITION node from a #define directive. Arguments are
2151 as for do_define. */
2154 static /*@null@*/ macroDef
2155 create_definition (/*@exposed@*/ char *buf, char *limit,
2156 cppReader *pfile, bool predefinition,
2159 char *bp; /* temp ptr into input buffer */
2160 char *symname; /* remember where symbol name starts */
2161 size_t sym_length; /* and how long it is */
2162 int rest_args = 0; /* really int! */
2165 cstring file = (CPPBUFFER (pfile) != NULL)
2166 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2168 int arglengths = 0; /* Accumulate lengths of arg names
2169 plus number of args. */
2173 DPRINTF (("Create definition: %s", buf));
2176 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2180 while (is_hor_space[(int) *bp])
2185 symname = bp; /* remember where it starts */
2187 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2191 /* Lossage will occur if identifiers or control keywords are broken
2192 across lines using backslash. This is not the right place to take
2196 struct arglist *arg_ptrs = NULL;
2199 bp++; /* skip '(' */
2200 SKIP_WHITE_SPACE (bp);
2202 /* Loop over macro argument names. */
2205 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2207 temp->next = arg_ptrs;
2208 temp->argno = argno++;
2209 temp->rest_args = 0;
2215 cppReader_pedwarn (pfile,
2216 message ("another parameter follows `%s'",
2217 cstring_fromChars (rest_extension)));
2220 if (!is_idstart[(int) *bp])
2222 cppReader_pedwarnLit (pfile,
2223 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
2226 /* Find the end of the arg name. */
2227 while (is_idchar[(int) *bp])
2230 /* do we have a "special" rest-args extension here? */
2231 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2232 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2235 temp->rest_args = 1;
2236 /*@innerbreak@*/ break;
2240 temp->length = size_fromInt (bp - temp->name);
2244 bp += REST_EXTENSION_LENGTH;
2247 arglengths += temp->length + 2;
2248 SKIP_WHITE_SPACE (bp);
2250 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2251 cppReader_errorLit (pfile,
2252 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2258 SKIP_WHITE_SPACE (bp);
2261 cppReader_errorLit (pfile,
2262 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2266 struct arglist *otemp;
2268 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2270 if (temp->length == otemp->length &&
2271 strncmp (temp->name, otemp->name, temp->length) == 0) {
2272 cstring name = cstring_copyLength (temp->name, temp->length);
2273 cppReader_error (pfile,
2274 message ("duplicate argument name `%x' in `#define'", name));
2281 ++bp; /* skip paren */
2282 SKIP_WHITE_SPACE (bp);
2283 /* now everything from bp before limit is the definition. */
2284 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2285 defn->rest_args = rest_args;
2287 /* Now set defn->args.argnames to the result of concatenating
2288 the argument names in reverse order
2289 with comma-space between them. */
2290 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2293 struct arglist *temp;
2295 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2297 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2299 if (temp->next != 0)
2301 defn->args.argnames[i++] = ',';
2302 defn->args.argnames[i++] = ' ';
2306 defn->args.argnames[i] = '\0';
2311 /* Simple expansion or empty definition. */
2315 if (is_hor_space[(int) *bp]) {
2317 SKIP_WHITE_SPACE (bp);
2320 case '!': case '\"': case '#': case '%': case '&': case '\'':
2321 case ')': case '*': case '+': case ',': case '-': case '.':
2322 case '/': case ':': case ';': case '<': case '=': case '>':
2323 case '?': case '[': case '\\': case ']': case '^': case '{':
2324 case '|': case '}': case '~':
2325 cppReader_warning (pfile,
2326 message ("Missing white space after #define %x",
2327 cstring_prefix (cstring_fromChars (symname),
2332 cppReader_pedwarn (pfile,
2333 message ("Missing white space after #define %x",
2334 cstring_prefix (cstring_fromChars (symname),
2340 /* now everything from bp before limit is the definition. */
2341 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2342 defn->args.argnames = mstring_createEmpty ();
2345 defn->noExpand = noExpand;
2346 DPRINTF (("No expand: %d", noExpand));
2350 /* not: llassert (cstring_isUndefined (defn->file)); */
2353 /* OP is null if this is a predefinition */
2354 defn->predefined = predefinition;
2356 mdef.symnam = symname;
2357 mdef.symlen = sym_length;
2368 cpplib_createDefinition (cstring def,
2373 char *buf = cstring_toCharsSafe (def);
2374 char *limit = buf + cstring_length (def);
2375 char *bp; /* temp ptr into input buffer */
2376 char *symname; /* remember where symbol name starts */
2377 size_t sym_length; /* and how long it is */
2378 int rest_args = 0; /* really int! */
2379 int line = fileloc_lineno (loc);
2380 cstring file = fileloc_filename (loc);
2382 int arglengths = 0; /* Accumulate lengths of arg names
2383 plus number of args. */
2388 DPRINTF (("Creating definition: %s", buf));
2390 while (is_hor_space[(int) *bp])
2395 symname = bp; /* remember where it starts */
2397 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2399 DPRINTF (("length: %d", sym_length));
2403 DPRINTF (("Here: %s", bp));
2405 /* Lossage will occur if identifiers or control keywords are broken
2406 across lines using backslash. This is not the right place to take
2410 struct arglist *arg_ptrs = NULL;
2413 bp++; /* skip '(' */
2414 SKIP_WHITE_SPACE (bp);
2416 /* Loop over macro argument names. */
2419 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2421 temp->next = arg_ptrs;
2422 temp->argno = argno++;
2423 temp->rest_args = 0;
2429 voptgenerror (FLG_PREPROC,
2430 message ("Another parameter follows %s",
2431 cstring_fromChars (rest_extension)),
2435 if (!is_idstart[(int) *bp])
2437 voptgenerror (FLG_PREPROC,
2438 message ("Invalid character in macro parameter name: %c", *bp),
2442 /* Find the end of the arg name. */
2443 while (is_idchar[(int) *bp])
2446 /* do we have a "special" rest-args extension here? */
2447 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2448 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2451 temp->rest_args = 1;
2452 /*@innerbreak@*/ break;
2456 temp->length = size_fromInt (bp - temp->name);
2460 bp += REST_EXTENSION_LENGTH;
2463 arglengths += temp->length + 2;
2464 SKIP_WHITE_SPACE (bp);
2466 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2467 voptgenerror (FLG_PREPROC,
2468 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2475 SKIP_WHITE_SPACE (bp);
2478 voptgenerror (FLG_PREPROC,
2479 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2484 struct arglist *otemp;
2486 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2488 if (temp->length == otemp->length &&
2489 strncmp (temp->name, otemp->name, temp->length) == 0) {
2490 cstring name = cstring_copyLength (temp->name, temp->length);
2492 voptgenerror (FLG_PREPROC,
2493 message ("Duplicate argument name in #define: %s", name),
2501 ++bp; /* skip paren */
2502 SKIP_WHITE_SPACE (bp);
2503 /* now everything from bp before limit is the definition. */
2504 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2505 defn->rest_args = rest_args;
2507 /* Now set defn->args.argnames to the result of concatenating
2508 the argument names in reverse order
2509 with comma-space between them. */
2510 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2513 struct arglist *temp;
2515 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2516 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2518 if (temp->next != 0) {
2519 defn->args.argnames[i++] = ',';
2520 defn->args.argnames[i++] = ' ';
2524 defn->args.argnames[i] = '\0';
2529 /* Simple expansion or empty definition. */
2533 if (is_hor_space[(int) *bp]) {
2535 SKIP_WHITE_SPACE (bp);
2538 case '!': case '\"': case '#': case '%': case '&': case '\'':
2539 case ')': case '*': case '+': case ',': case '-': case '.':
2540 case '/': case ':': case ';': case '<': case '=': case '>':
2541 case '?': case '[': case '\\': case ']': case '^': case '{':
2542 case '|': case '}': case '~':
2543 voptgenerror (FLG_PREPROC,
2544 message ("Missing white space after #define %x",
2545 cstring_prefix (cstring_fromChars (symname),
2551 voptgenerror (FLG_PREPROC,
2552 message ("Missing white space after #define %x",
2553 cstring_prefix (cstring_fromChars (symname),
2561 /* now everything from bp before limit is the definition. */
2562 llassert (limit > bp);
2563 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2564 defn->args.argnames = mstring_createEmpty ();
2567 defn->noExpand = noExpand;
2568 DPRINTF (("No expand: %d", noExpand));
2572 /* not: llassert (cstring_isUndefined (defn->file)); */
2575 /* OP is null if this is a predefinition */
2576 defn->predefined = predefinition;
2579 mdef.symnam = symname;
2580 mdef.symlen = sym_length;
2590 /* Check a purported macro name SYMNAME, and yield its length.
2591 USAGE is the kind of name this is intended for. */
2593 size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2598 for (p = symname; is_idchar[(int) *p]; p++)
2603 sym_length = size_fromInt (p - symname);
2606 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2608 cppReader_error (pfile, message ("invalid %s name", usage));
2610 else if (!is_idstart[(int) *symname])
2612 char *msg = (char *) dmalloc (sym_length + 1);
2613 memcpy (msg, symname, sym_length);
2614 msg[sym_length] = '\0';
2615 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2616 cstring_fromChars (msg)));
2621 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2623 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2632 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2635 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2640 for (p = symname; is_idchar[(int) *p]; p++)
2645 sym_length = size_fromInt (p - symname);
2648 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2650 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2651 cstring_fromChars (symname)), loc);
2653 else if (!is_idstart[(int) *symname])
2655 char *msg = (char *) dmalloc (sym_length + 1);
2656 memcpy (msg, symname, sym_length);
2657 msg[sym_length] = '\0';
2658 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2659 cstring_fromChars (msg)),
2665 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2667 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2674 /* Return zero if two DEFINITIONs are isomorphic. */
2677 compare_defs (DEFINITION *d1, DEFINITION *d2)
2679 register struct reflist *a1, *a2;
2680 register char *p1 = d1->expansion;
2681 register char *p2 = d2->expansion;
2684 if (d1->nargs != d2->nargs)
2689 llassert (d1->args.argnames != NULL);
2690 llassert (d2->args.argnames != NULL);
2692 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2697 for (a1 = d1->pattern, a2 = d2->pattern;
2698 (a1 != NULL) && (a2 != NULL);
2699 a1 = a1->next, a2 = a2->next) {
2700 if (!((a1->nchars == a2->nchars
2701 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2702 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2703 || a1->argno != a2->argno
2704 || a1->stringify != a2->stringify
2705 || a1->raw_before != a2->raw_before
2706 || a1->raw_after != a2->raw_after)
2715 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2716 p2, d2->length - (p2 - d2->expansion), 1))
2722 /* Return TRUE if two parts of two macro definitions are effectively different.
2723 One of the parts starts at BEG1 and has LEN1 chars;
2724 the other has LEN2 chars at BEG2.
2725 Any sequence of whitespace matches any other sequence of whitespace.
2726 FIRST means these parts are the first of a macro definition;
2727 so ignore leading whitespace entirely.
2728 LAST means these parts are the last of a macro definition;
2729 so ignore trailing whitespace entirely. */
2732 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2734 char *end1 = beg1 + len1;
2735 char *end2 = beg2 + len2;
2738 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2739 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2742 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2743 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2745 while (beg1 != end1 && beg2 != end2) {
2746 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2747 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2748 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2749 } else if (*beg1 == *beg2) {
2753 return (beg1 != end1) || (beg2 != end2);
2756 /* Process a #define command.
2757 BUF points to the contents of the #define command, as a contiguous string.
2758 LIMIT points to the first character past the end of the definition.
2759 KEYWORD is the keyword-table entry for #define,
2760 or NULL for a "predefined" macro. */
2763 do_defineAux (cppReader *pfile, struct directive *keyword,
2764 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2770 DPRINTF (("Define aux: %d", noExpand));
2773 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2778 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2780 DPRINTF (("Macro: %s / %s",
2781 cstring_copyLength (mdef.symnam, mdef.symlen),
2782 bool_unparse (noExpand)));
2784 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2788 /* Redefining a precompiled key is ok. */
2789 if (hp->type == T_PCSTRING)
2791 /* Redefining a macro is ok if the definitions are the same. */
2792 else if (hp->type == T_MACRO)
2793 ok = !compare_defs (mdef.defn, hp->value.defn);
2794 /* Redefining a constant is ok with -D. */
2795 else if (hp->type == T_CONST)
2796 ok = !CPPOPTIONS (pfile)->done_initializing;
2801 /* Print the warning if it's not ok. */
2805 ** If we are passing through #define and #undef directives, do
2806 ** that for this re-definition now.
2809 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2811 /* llassert (keyword != NULL); */
2812 pass_thru_directive (buf, limit, pfile, keyword);
2815 cpp_setLocation (pfile);
2817 if (hp->type == T_MACRO)
2819 if (hp->value.defn->noExpand)
2821 ; /* error will be reported checking macros */
2827 message ("Macro %q already defined",
2828 cstring_copyLength (mdef.symnam, mdef.symlen)),
2829 message ("%q: Previous definition of %q",
2830 fileloc_unparseRaw (hp->value.defn->file,
2831 (int) hp->value.defn->line),
2832 cstring_copyLength (mdef.symnam, mdef.symlen)));
2837 genppllerror (FLG_MACROREDEF,
2838 message ("Macro %q already defined",
2839 cstring_copyLength (mdef.symnam,
2845 /* Replace the old definition. */
2847 hp->value.defn = mdef.defn;
2852 ** If we are passing through #define and #undef directives, do
2853 ** that for this new definition now.
2858 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2860 pass_thru_directive (buf, limit, pfile, keyword);
2863 DPRINTF (("Define macro: %s / %d",
2864 mdef.symnam, mdef.defn->noExpand));
2866 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2868 } /*@=branchstate@*/
2878 do_define (cppReader *pfile, struct directive *keyword,
2879 /*@exposed@*/ char *buf, char *limit)
2881 DPRINTF (("Regular do define"));
2883 /* drl added 04-26-2002 */
2885 if ( context_getFlag (FLG_GENERATECODE) && (keyword !=NULL) )
2887 outputCode(message( "#define %s\n",buf ) );
2892 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2895 /* This structure represents one parsed argument in a macro call.
2896 `raw' points to the argument text as written (`raw_length' is its length).
2897 `expanded' points to the argument's macro-expansion
2898 (its length is `expand_length').
2899 `stringified_length' is the length the argument would have
2901 `use_count' is the number of times this macro arg is substituted
2902 into the macro. If the actual use count exceeds 10,
2903 the value stored is 10. */
2905 /* raw and expanded are relative to ARG_BASE */
2907 #define ARG_BASE ((pfile)->token_buffer)
2910 /* Strings relative to pfile->token_buffer */
2916 int stringified_length;
2921 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2922 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2923 as the new input buffer.
2924 Return the new buffer, or NULL on failure. */
2926 /*@null@*/ /*@exposed@*/ cppBuffer *
2927 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2929 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2931 if (buf == pfile->buffer_stack)
2933 cppReader_fatalError
2935 message ("%s: macro or `#include' recursion too deep",
2936 (buf->fname != NULL)
2938 : cstring_makeLiteral ("<no name>")));
2939 sfreeEventually (buffer);
2943 llassert (buf != NULL);
2946 memset ((char *) buf, 0, sizeof (*buf));
2947 CPPBUFFER (pfile) = buf;
2949 buf->if_stack = pfile->if_stack;
2950 buf->cleanup = cppReader_nullCleanup;
2951 buf->underflow = cppReader_nullUnderflow;
2953 buf->cur = buf->buf;
2957 buf->alimit = buf->rlimit = buffer + length;
2961 buf->alimit = buf->rlimit = NULL;
2968 cppReader_popBuffer (cppReader *pfile)
2970 cppBuffer *buf = CPPBUFFER (pfile);
2972 llassert (buf != NULL);
2974 (void) (*buf->cleanup) (buf, pfile);
2975 return ++CPPBUFFER (pfile);
2978 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2979 Pop the buffer when done. */
2982 cppReader_scanBuffer (cppReader *pfile)
2984 cppBuffer *buffer = CPPBUFFER (pfile);
2987 enum cpp_token token;
2989 token = cpplib_getToken (pfile);
2991 if (token == CPP_EOF) /* Should not happen ... */
2996 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2998 (void) cppReader_popBuffer (pfile);
3006 * Rescan a string (which may have escape marks) into pfile's buffer.
3007 * Place the result in pfile->token_buffer.
3009 * The input is copied before it is scanned, so it is safe to pass
3010 * it something from the token_buffer that will get overwritten
3011 * (because it follows cpplib_getWritten). This is used by do_include.
3015 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3017 register cppBuffer *ip;
3018 char *limit = buf + length;
3019 char *buf1, *p1, *p2;
3021 /* evans - 2001-08-26
3022 ** length is unsigned - this doesn't make sense
3028 /* Set up the input on the input stack. */
3030 buf1 = (char *) dmalloc (length + 1);
3040 buf1[length] = '\0';
3042 ip = cppReader_pushBuffer (pfile, buf1, length);
3047 ip->has_escapes = TRUE;
3049 /* Scan the input, create the output. */
3050 cppReader_scanBuffer (pfile);
3052 cppReader_nullTerminate (pfile);
3056 adjust_position (char *buf, char *limit, int *linep, int *colp)
3062 (*linep)++, (*colp) = 1;
3068 /* Move line_base forward, updating lineno and colno. */
3071 update_position (cppBuffer *pbuf)
3074 char *new_pos = pbuf->cur;
3075 register struct parse_marker *mark;
3077 llassert (pbuf->buf != NULL);
3078 old_pos = pbuf->buf + pbuf->line_base;
3080 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3082 if (pbuf->buf + mark->position < new_pos)
3083 new_pos = pbuf->buf + mark->position;
3085 pbuf->line_base += new_pos - old_pos;
3087 llassert (old_pos != NULL);
3088 llassert (new_pos != NULL);
3090 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3094 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3095 /*@null@*/ /*@out@*/ int *colp)
3103 } /*@=branchstate@*/
3107 *linep = pbuf->lineno;
3108 *colp = pbuf->colno;
3110 llassert (pbuf->buf != NULL);
3111 llassert (pbuf->cur != NULL);
3113 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3122 /* Return the cppBuffer that corresponds to a file (not a macro). */
3124 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3126 cppBuffer *ip = cppReader_getBuffer (pfile);
3129 ip != NULL && ip != cppReader_nullBuffer (pfile);
3130 ip = cppBuffer_prevBuffer (ip))
3132 if (ip->fname != NULL)
3142 count_newlines (char *buf, char *limit)
3144 register long count = 0;
3156 * write out a #line command, for instance, after an #include file.
3157 * If CONDITIONAL is nonzero, we can omit the #line if it would
3158 * appear to be a no-op, and we can output a few newlines instead
3159 * if we want to increase the line number by a small amount.
3160 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3164 output_line_command (cppReader *pfile, bool conditional,
3165 enum file_change_code file_change)
3168 cppBuffer *ip = CPPBUFFER (pfile);
3171 llassert (ip != NULL);
3173 if (ip->fname == NULL)
3176 update_position (ip);
3178 if (CPPOPTIONS (pfile)->no_line_commands
3179 || CPPOPTIONS (pfile)->no_output)
3182 buf = CPPBUFFER (pfile);
3184 llassert (buf != NULL);
3189 llassert (ip->cur != NULL);
3191 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3193 if (CPPOPTIONS (pfile)->no_line_commands)
3197 if (line == pfile->lineno)
3200 /* If the inherited line number is a little too small,
3201 output some newlines instead of a #line command. */
3203 if (line > pfile->lineno && line < pfile->lineno + 8)
3205 cpplib_reserve (pfile, 20);
3206 while (line > pfile->lineno)
3208 cppReader_putCharQ (pfile, '\n');
3216 cpplib_reserve (pfile,
3217 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
3220 #ifdef OUTPUT_LINE_COMMANDS
3221 static char sharp_line[] = "#line ";
3223 static char sharp_line[] = "# ";
3225 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3228 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3229 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3231 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3233 if (file_change != same_file) {
3234 cppReader_putCharQ (pfile, ' ');
3235 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3237 /* Tell cc1 if following text comes from a system header file. */
3238 if (ip->system_header_p != '\0') {
3239 cppReader_putCharQ (pfile, ' ');
3240 cppReader_putCharQ (pfile, '3');
3242 #ifndef NO_IMPLICIT_EXTERN_C
3243 /* Tell cc1plus if following text should be treated as C. */
3244 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3245 cppReader_putCharQ (pfile, ' ');
3246 cppReader_putCharQ (pfile, '4');
3249 cppReader_putCharQ (pfile, '\n');
3250 pfile->lineno = line;
3255 * Parse a macro argument and append the info on PFILE's token_buffer.
3256 * REST_ARGS means to absorb the rest of the args.
3257 * Return nonzero to indicate a syntax error.
3260 static enum cpp_token
3261 macarg (cppReader *pfile, int rest_args)
3264 enum cpp_token token;
3265 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3266 bool oldexpand = pfile->no_macro_expand;
3267 CPPOPTIONS (pfile)->put_out_comments = 1;
3269 /* Try to parse as much of the argument as exists at this
3270 input stack level. */
3272 pfile->no_macro_expand = TRUE;
3276 token = cpplib_getToken (pfile);
3283 /* If we've hit end of file, it's an error (reported by caller).
3284 Ditto if it's the end of cpp_expand_to_buffer text.
3285 If we've hit end of macro, just continue. */
3286 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3288 /*@switchbreak@*/ break;
3291 /*@switchbreak@*/ break;
3295 /*@switchbreak@*/ break;
3297 /* if we've returned to lowest level and
3298 we aren't absorbing all args */
3299 if (paren == 0 && rest_args == 0)
3301 /*@switchbreak@*/ break;
3303 /* Remove ',' or ')' from argument buffer. */
3304 cppReader_adjustWritten (pfile, -1);
3312 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3313 pfile->no_macro_expand = oldexpand;
3319 /* Turn newlines to spaces in the string of length LENGTH at START,
3320 except inside of string constants.
3321 The string is copied into itself with its beginning staying fixed. */
3324 change_newlines (char *start, int length)
3328 register char *limit;
3332 limit = start + length;
3335 while (ibp < limit) {
3336 *obp++ = c = *ibp++;
3341 /* Notice and skip strings, so that we don't delete newlines in them. */
3344 while (ibp < limit) {
3345 *obp++ = c = *ibp++;
3347 /*@innerbreak@*/ break;
3348 if (c == '\n' && quotec == '\'')
3349 /*@innerbreak@*/ break;
3352 /*@switchbreak@*/ break;
3359 static /*@observer@*/ struct tm *
3360 timestamp (/*@returned@*/ cppReader *pfile)
3362 if (pfile->timebuf == NULL)
3364 time_t t = time ((time_t *) 0);
3365 pfile->timebuf = localtime (&t);
3368 llassert (pfile->timebuf != NULL);
3370 return pfile->timebuf;
3373 static ob_mstring monthnames[] = {
3374 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3375 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3379 * expand things like __FILE__. Place the expansion into the output
3380 * buffer *without* rescanning.
3384 special_symbol (hashNode hp, cppReader *pfile)
3386 cstring buf = cstring_undefined;
3392 int paren = 0; /* For special `defined' keyword */
3394 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3396 if (ip == cppReader_nullBuffer (pfile))
3398 cppReader_errorLit (pfile,
3399 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3400 return; /* the show must go on */
3403 if (ip != NULL && ip->fname != NULL)
3415 if (hp->type == T_BASE_FILE)
3417 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3419 ip = cppBuffer_prevBuffer (ip);
3423 llassert (ip != NULL);
3424 string = cstring_toCharsSafe (ip->nominal_fname);
3431 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3432 quote_string (pfile, string);
3436 case T_INCLUDE_LEVEL:
3438 ip = cppReader_getBuffer (pfile);
3440 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3441 ip = cppBuffer_prevBuffer (ip))
3443 if (ip != NULL && ip->fname != NULL)
3449 buf = message ("%d", true_indepth - 1);
3453 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3456 #ifndef NO_BUILTIN_SIZE_TYPE
3458 buf = cstring_makeLiteral (SIZE_TYPE);
3462 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3463 case T_PTRDIFF_TYPE:
3464 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3469 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3472 case T_USER_LABEL_PREFIX_TYPE:
3473 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3476 case T_REGISTER_PREFIX_TYPE:
3477 buf = cstring_makeLiteral (REGISTER_PREFIX);
3481 buf = message ("%d", hp->value.ival);
3488 int line = ip->lineno;
3489 int col = ip->colno;
3491 llassert (ip->cur != NULL);
3492 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3494 buf = message ("%d", (int) line);
3506 char *sbuf = (char *) dmalloc (20);
3507 timebuf = timestamp (pfile);
3508 if (hp->type == T_DATE)
3510 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3511 timebuf->tm_mday, timebuf->tm_year + 1900);
3515 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3519 buf = cstring_fromCharsNew (sbuf);
3524 case T_SPEC_DEFINED:
3525 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3526 ip = cppReader_getBuffer (pfile);
3527 llassert (ip != NULL);
3528 llassert (ip->cur != NULL);
3529 SKIP_WHITE_SPACE (ip->cur);
3531 if (*ip->cur == '(')
3534 ip->cur++; /* Skip over the paren */
3535 SKIP_WHITE_SPACE (ip->cur);
3538 if (!is_idstart[(int) *ip->cur])
3540 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3543 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3546 buf = cstring_makeLiteral (" 1 ");
3549 while (is_idchar[(int) *ip->cur])
3554 SKIP_WHITE_SPACE (ip->cur);
3558 if (*ip->cur != ')')
3566 cppReader_errorLit (pfile,
3567 cstring_makeLiteralTemp ("`defined' without an identifier"));
3571 cpp_setLocation (pfile);
3572 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3575 len = cstring_length (buf);
3577 cpplib_reserve (pfile, len + 1);
3578 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3579 cppReader_nullTerminateQ (pfile);
3585 /* Write out a #define command for the special named MACRO_NAME
3586 to PFILE's token_buffer. */
3589 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3591 static char define_directive[] = "#define ";
3592 size_t macro_name_length = strlen (macro_name);
3593 output_line_command (pfile, 0, same_file);
3594 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3595 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3596 cppReader_putStrN (pfile, macro_name, macro_name_length);
3597 cppReader_putCharQ (pfile, ' ');
3598 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3599 cppReader_putChar (pfile, '\n');
3602 /* Initialize the built-in macros. */
3605 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3606 int len, enum node_type type,
3607 int ivalue, /*@null@*/ /*@only@*/ char *value,
3610 cstring sname = cstring_fromCharsNew (name);
3612 llassert (usymtab_inGlobalScope ());
3615 ** Be careful here: this is done before the ctype table has
3616 ** been initialized.
3619 if (!usymtab_exists (sname))
3621 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3623 if (ctype_equal (ctyp, ctype_string))
3625 qualList ql = qualList_new ();
3626 ql = qualList_add (ql, qual_createObserver ());
3627 uentry_reflectQualifiers (ue, ql);
3631 usymtab_addGlobalEntry (ue);
3638 (void) cpphash_install (name, len, type, ivalue, value, hash);
3639 cstring_free (sname);
3643 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3644 int len, enum node_type type,
3646 /*@only@*/ /*@null@*/ char *value, int hash)
3648 cstring sname = cstring_fromChars (name);
3649 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3651 llassert (usymtab_inGlobalScope ());
3653 if (!usymtab_existsTypeEither (sname))
3655 uentry ue = uentry_makeDatatype (sname, ctyp,
3657 fileloc_createBuiltin ());
3658 llassert (!usymtab_existsEither (sname));
3659 usymtab_addGlobalEntry (ue);
3662 (void) cpphash_install (name, len, type, ivalue, value, hash);
3666 initialize_builtins (cppReader *pfile)
3668 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3669 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3670 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3671 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3672 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3673 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3674 #ifndef NO_BUILTIN_SIZE_TYPE
3675 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3677 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3678 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3680 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3681 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3682 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3683 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3686 ** No, don't define __STDC__
3689 if (!cppReader_isTraditional (pfile))
3691 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3698 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3702 ** This is supplied using a -D by the compiler driver
3703 ** so that it is present only when truly compiling with GNU C.
3706 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3708 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3709 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3711 /*drl 1/9/2001/ try to define the right symbol for the architecture
3712 We use autoconf to determine the target cpu
3714 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3716 /*drl 1/2/2002 set some flags based on uname
3717 I'd like to be able to do this with autoconf macro instead...
3720 /*Thanks to Nelson Beebe for suggesting possible values for these */
3722 if (! strcmp (UNAME, "Linux"))
3725 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3729 else if(! strcmp (UNAME, "Darwin"))
3731 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3732 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3734 else if(! strcmp (UNAME, "HP-UX"))
3736 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3737 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3738 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3739 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3740 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3741 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3742 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3743 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3744 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3745 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3746 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3747 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3748 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3749 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3750 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3751 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3752 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3753 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3754 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3755 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3756 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3757 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3758 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3760 else if(! strcmp (UNAME, "IRIX64"))
3762 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3763 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3764 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3765 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3766 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3767 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3769 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3770 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3771 /*_MIPS_SIM=_ABIN32*/
3772 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3773 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3774 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3775 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3776 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3780 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3781 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3782 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3783 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3785 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3786 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3787 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3788 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3789 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3790 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3791 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3792 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3793 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3794 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3795 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3797 else if(! strcmp (UNAME, "OSF1"))
3799 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3801 else if (!strcmp (UNAME, "Rhapsody"))
3803 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3807 else if (!strcmp (UNAME, "SunOS"))
3809 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3810 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3811 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3812 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3817 types which we have not explictedly handled.
3818 AIX, FreeBSD, IRIX, Mach
3823 if (CPPOPTIONS (pfile)->debug_output)
3825 dump_special_to_buffer (pfile, "__BASE_FILE__");
3826 dump_special_to_buffer (pfile, "__VERSION__");
3827 #ifndef NO_BUILTIN_SIZE_TYPE
3828 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3830 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3831 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3833 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3834 dump_special_to_buffer (pfile, "__DATE__");
3835 dump_special_to_buffer (pfile, "__TIME__");
3836 if (!cppReader_isTraditional (pfile))
3837 dump_special_to_buffer (pfile, "__STDC__");
3842 /* Return 1 iff a token ending in C1 followed directly by a token C2
3843 could cause mis-tokenization. */
3846 unsafe_chars (char c1, char c2)
3851 if (c2 == c1 || c2 == '=')
3855 case '0': case '1': case '2': case '3': case '4':
3856 case '5': case '6': case '7': case '8': case '9':
3857 case 'e': case 'E': case 'p': case 'P':
3858 if (c2 == '-' || c2 == '+')
3859 return 1; /* could extend a pre-processing number */
3862 if (c2 == '\'' || c2 == '\"')
3863 return 1; /* Could turn into L"xxx" or L'xxx'. */
3867 case 'a': case 'b': case 'c': case 'd': case 'f':
3868 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3869 case 'm': case 'n': case 'o': case 'q': case 'r':
3870 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3872 case 'A': case 'B': case 'C': case 'D': case 'F':
3873 case 'G': case 'H': case 'I': case 'J': case 'K':
3874 case 'M': case 'N': case 'O': case 'Q': case 'R':
3875 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3877 /* We're in the middle of either a name or a pre-processing number. */
3878 return (is_idchar[(int) c2] || c2 == '.');
3879 case '<': case '>': case '!': case '%': case '#': case ':':
3880 case '^': case '&': case '|': case '*': case '/': case '=':
3881 return (c2 == c1 || c2 == '=');
3886 /* Expand a macro call.
3887 HP points to the symbol that is the macro being called.
3888 Put the result of expansion onto the input stack
3889 so that subsequent input by our caller will use it.
3891 If macro wants arguments, caller has already verified that
3892 an argument list follows; arguments come from the input stack. */
3895 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3898 DEFINITION *defn = hp->value.defn;
3906 size_t old_written = cpplib_getWritten (pfile);
3910 struct argdata *args = NULL;
3912 pfile->output_escapes++;
3913 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3914 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3916 nargs = defn->nargs;
3920 enum cpp_token token = CPP_EOF;
3922 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3924 for (i = 0; i < nargs; i++)
3926 args[i].expanded = 0;
3928 args[i].raw_length = 0;
3929 args[i].expand_length = args[i].stringified_length = -1;
3930 args[i].use_count = 0;
3934 ** Parse all the macro args that are supplied. I counts them.
3935 ** The first NARGS args are stored in ARGS.
3936 ** The rest are discarded. If rest_args is set then we assume
3937 ** macarg absorbed the rest of the args.
3943 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3951 if (i < nargs || (nargs == 0 && i == 0))
3953 /* if we are working on last arg which absorbs rest of args... */
3954 if (i == nargs - 1 && defn->rest_args)
3959 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3960 token = macarg (pfile, rest_args);
3961 args[i].raw_length = cpplib_getWritten (pfile) - args[i].raw;
3962 args[i].newlines = FALSE; /* FIXME */
3966 token = macarg (pfile, 0);
3969 if (token == CPP_EOF || token == CPP_POP)
3971 cppReader_errorWithLine (pfile, start_line, start_column,
3972 cstring_fromCharsNew ("unterminated macro call"));
3977 } while (token == CPP_COMMA);
3979 /* If we got one arg but it was just whitespace, call that 0 args. */
3987 bp = ARG_BASE + args[0].raw;
3988 lim = bp + args[0].raw_length;
3990 /* cpp.texi says for foo ( ) we provide one argument.
3991 However, if foo wants just 0 arguments, treat this as 0. */
3995 while (bp != lim && is_space[(int) *bp])
4005 /* Don't output an error message if we have already output one for
4006 a parse error above. */
4009 if (nargs == 0 && i > 0)
4011 cppReader_error (pfile,
4012 message ("arguments given to macro `%s'", hp->name));
4016 /* traditional C allows foo() if foo wants one argument. */
4017 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4021 /* the rest args token is allowed to absorb 0 tokens */
4022 else if (i == nargs - 1 && defn->rest_args)
4025 cppReader_error (pfile,
4026 message ("macro `%s' used without args", hp->name));
4028 cppReader_error (pfile,
4029 message ("macro `%s' used with just one arg", hp->name));
4032 cppReader_error (pfile,
4033 message ("macro `%s' used with only %d args",
4039 cppReader_error (pfile,
4040 message ("macro `%s' used with too many (%d) args", hp->name, i));
4049 ** If the agrument list was multiple lines, need to insert new lines to keep line
4050 ** numbers accurate.
4053 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4054 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4056 /* If macro wants zero args, we parsed the arglist for checking only.
4057 Read directly from the macro definition. */
4061 xbuf = defn->expansion;
4062 xbuf_len = defn->length;
4066 char *exp = defn->expansion;
4067 int offset; /* offset in expansion,
4068 copied a piece at a time */
4069 size_t totlen; /* total amount of exp buffer filled so far */
4071 register struct reflist *ap, *last_ap;
4073 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4075 /* Macro really takes args. Compute the expansion of this call. */
4077 /* Compute length in characters of the macro's expansion.
4078 Also count number of times each arg is used. */
4079 xbuf_len = defn->length;
4081 llassert (args != NULL);
4083 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4087 struct argdata *arg = &args[ap->argno];
4089 /* Stringify it it hasn't already been */
4092 if (arg->stringified_length < 0)
4094 int arglen = arg->raw_length;
4095 bool escaped = FALSE;
4096 char in_string = '\0';
4099 /* Initially need_space is -1. Otherwise, 1 means the
4100 previous character was a space, but we suppressed it;
4101 0 means the previous character was a non-space. */
4102 int need_space = -1;
4105 arg->stringified = cpplib_getWritten (pfile);
4106 if (!cppReader_isTraditional (pfile))
4107 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4108 for (; i < arglen; i++)
4110 c = (ARG_BASE + arg->raw)[i];
4112 if (in_string == '\0')
4114 /* Internal sequences of whitespace are replaced by
4115 one space except within an string or char token.*/
4116 if (is_space[(int) c])
4118 if (cpplib_getWritten (pfile) > arg->stringified
4119 && (cpplib_getPWritten (pfile))[-1] == '@')
4121 /* "@ " escape markers are removed */
4122 cppReader_adjustWritten (pfile, -1);
4123 /*@innercontinue@*/ continue;
4125 if (need_space == 0)
4127 /*@innercontinue@*/ continue;
4129 else if (need_space > 0)
4130 cppReader_putChar (pfile, ' ');
4146 if (in_string != '\0')
4151 else if (c == '\"' || c == '\'')
4161 /* Escape these chars */
4162 if (c == '\"' || (in_string != '\0' && c == '\\'))
4163 cppReader_putChar (pfile, '\\');
4165 cppReader_putChar (pfile, c);
4168 cpplib_reserve (pfile, 4);
4169 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4171 cppReader_adjustWritten (pfile, 4);
4174 if (!cppReader_isTraditional (pfile))
4175 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4176 arg->stringified_length
4177 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4180 xbuf_len += args[ap->argno].stringified_length;
4182 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4184 /* Add 4 for two newline-space markers to prevent
4185 token concatenation. */
4186 assertSet (args); /*@i534 shouldn't need this */
4187 xbuf_len += args[ap->argno].raw_length + 4;
4191 /* We have an ordinary (expanded) occurrence of the arg.
4192 So compute its expansion, if we have not already. */
4194 assertSet (args); /*@i534 shouldn't need this */
4196 if (args[ap->argno].expand_length < 0)
4198 args[ap->argno].expanded = cpplib_getWritten (pfile);
4199 cpp_expand_to_buffer (pfile,
4200 ARG_BASE + args[ap->argno].raw,
4201 size_fromInt (args[ap->argno].raw_length));
4203 args[ap->argno].expand_length
4204 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4207 /* Add 4 for two newline-space markers to prevent
4208 token concatenation. */
4209 xbuf_len += args[ap->argno].expand_length + 4;
4211 if (args[ap->argno].use_count < 10)
4212 args[ap->argno].use_count++;
4215 xbuf = (char *) dmalloc (xbuf_len + 1);
4219 ** Generate in XBUF the complete expansion
4220 ** with arguments substituted in.
4221 ** TOTLEN is the total size generated so far.
4222 ** OFFSET is the index in the definition
4223 ** of where we are copying from.
4229 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4230 last_ap = ap, ap = ap->next)
4232 register struct argdata *arg = &args[ap->argno];
4233 size_t count_before = totlen;
4235 /* Add chars to XBUF. */
4236 for (i = 0; i < ap->nchars; i++, offset++)
4238 xbuf[totlen++] = exp[offset];
4241 /* If followed by an empty rest arg with concatenation,
4242 delete the last run of nonwhite chars. */
4243 if (rest_zero && totlen > count_before
4244 && ((ap->rest_args && ap->raw_before)
4245 || (last_ap != NULL && last_ap->rest_args
4246 && last_ap->raw_after)))
4248 /* Delete final whitespace. */
4249 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4254 /* Delete the nonwhites before them. */
4255 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4261 if (ap->stringify != 0)
4264 memcpy (xbuf + totlen,
4265 ARG_BASE + arg->stringified,
4266 size_fromInt (arg->stringified_length));
4267 totlen += arg->stringified_length;
4269 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4276 p1 = ARG_BASE + arg->raw;
4277 l1 = p1 + arg->raw_length;
4281 while (p1 != l1 && is_space[(int) *p1])
4286 while (p1 != l1 && is_idchar[(int) *p1])
4288 xbuf[totlen++] = *p1++;
4291 /* Delete any no-reexpansion marker that follows
4292 an identifier at the beginning of the argument
4293 if the argument is concatenated with what precedes it. */
4294 if (p1[0] == '@' && p1[1] == '-')
4299 /* Arg is concatenated after: delete trailing whitespace,
4300 whitespace markers, and no-reexpansion markers. */
4303 if (is_space[(int) l1[-1]]) l1--;
4304 else if (l1[-1] == '-')
4307 /* If a `-' is preceded by an odd number of newlines then it
4308 and the last newline are a no-reexpansion marker. */
4309 while (p2 != p1 && p2[-1] == '\n')
4314 if (((l1 - 1 - p2) & 1) != 0)
4320 /*@innerbreak@*/ break;
4325 /*@innerbreak@*/ break;
4330 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4338 expanded = ARG_BASE + arg->expanded;
4340 if (!ap->raw_before && totlen > 0
4341 && (arg->expand_length != 0)
4342 && !cppReader_isTraditional(pfile)
4343 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4345 xbuf[totlen++] = '@';
4346 xbuf[totlen++] = ' ';
4349 memcpy (xbuf + totlen, expanded,
4350 size_fromInt (arg->expand_length));
4351 totlen += arg->expand_length;
4353 if (!ap->raw_after && totlen > 0
4354 && offset < size_toInt (defn->length)
4355 && !cppReader_isTraditional(pfile)
4356 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4358 xbuf[totlen++] = '@';
4359 xbuf[totlen++] = ' ';
4362 /* If a macro argument with newlines is used multiple times,
4363 then only expand the newlines once. This avoids creating
4364 output lines which don't correspond to any input line,
4365 which confuses gdb and gcov. */
4366 if (arg->use_count > 1 && arg->newlines > 0)
4368 /* Don't bother doing change_newlines for subsequent
4372 = change_newlines (expanded, arg->expand_length);
4376 if (totlen > xbuf_len)
4380 /* if there is anything left of the definition
4381 after handling the arg list, copy that in too. */
4383 for (i = offset; i < size_toInt (defn->length); i++)
4385 /* if we've reached the end of the macro */
4388 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4389 && last_ap->raw_after))
4390 xbuf[totlen++] = exp[i];
4393 xbuf[totlen] = '\0';
4397 pfile->output_escapes--;
4399 /* Now put the expansion on the input stack
4400 so our caller will commence reading from it. */
4401 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4403 if (end_line != start_line)
4405 /* xbuf must have enough newlines */
4406 int newlines = end_line - start_line;
4407 int foundnewlines = 0;
4408 char *xbufptr = xbuf;
4410 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4415 if (*xbufptr == '\0')
4421 if (foundnewlines < newlines)
4423 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4425 while (foundnewlines < newlines)
4427 newbuf = cstring_appendChar (newbuf, '\n');
4432 xbuf = cstring_toCharsSafe (newbuf);
4433 xbuf_len = cstring_length (newbuf);
4435 } /*@=branchstate@*/
4438 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4440 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4441 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4443 /* Pop the space we've used in the token_buffer for argument expansion. */
4444 cppReader_setWritten (pfile, old_written);
4446 /* Recursive macro use sometimes works traditionally.
4447 #define foo(x,y) bar (x (y,0), y)
4450 if (!cppReader_isTraditional (pfile))
4451 hp->type = T_DISABLED;
4457 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4458 /*@dependent@*/ hashNode hp)
4460 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4467 mbuf->cleanup = cppReader_macroCleanup;
4469 llassert (mbuf->hnode == NULL);
4472 /* The first chars of the expansion should be a "@ " added by
4473 collect_expansion. This is to prevent accidental token-pasting
4474 between the text preceding the macro invocation, and the macro
4477 We would like to avoid adding unneeded spaces (for the sake of
4478 tools that use cpp, such as imake). In some common cases we can
4479 tell that it is safe to omit the space.
4481 The character before the macro invocation cannot have been an
4482 idchar (or else it would have been pasted with the idchars of
4483 the macro name). Therefore, if the first non-space character
4484 of the expansion is an idchar, we do not need the extra space
4485 to prevent token pasting.
4487 Also, we don't need the extra space if the first char is '(',
4488 or some other (less common) characters. */
4490 if (xbuf[0] == '@' && xbuf[1] == ' '
4491 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4492 || xbuf[2] == '\"'))
4494 llassert (mbuf->cur != NULL);
4500 /* Like cpplib_getToken, except that it does not read past end-of-line.
4501 Also, horizontal space is skipped, and macros are popped. */
4503 static enum cpp_token
4504 get_directive_token (cppReader *pfile)
4508 size_t old_written = cpplib_getWritten (pfile);
4509 enum cpp_token token;
4510 cppSkipHspace (pfile);
4511 if (cppReader_peekC (pfile) == '\n')
4516 token = cpplib_getToken (pfile);
4521 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4526 cppReader_setWritten (pfile, old_written);
4527 /*@switchbreak@*/ break;
4535 /* Handle #include and #import.
4536 This function expects to see "fname" or <fname> on the input.
4538 The input is normally in part of the output_buffer following
4539 cpplib_getWritten, and will get overwritten by output_line_command.
4540 I.e. in input file specification has been popped by cppReader_handleDirective.
4544 do_include (cppReader *pfile, struct directive *keyword,
4545 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4547 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4549 char *fbeg, *fend; /* Beginning and end of fname */
4550 enum cpp_token token;
4552 /* Chain of dirs to search */
4553 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4554 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4555 struct file_name_list *searchptr = NULL;
4556 size_t old_written = cpplib_getWritten (pfile);
4559 int f; /* file number */
4560 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4561 f= -1; /* JF we iz paranoid! */
4563 pfile->parsing_include_directive++;
4564 token = get_directive_token (pfile);
4565 pfile->parsing_include_directive--;
4567 if (token == CPP_STRING)
4569 /* FIXME - check no trailing garbage */
4570 fbeg = pfile->token_buffer + old_written + 1;
4571 fend = cpplib_getPWritten (pfile) - 1;
4572 if (fbeg[-1] == '<')
4575 /* If -I-, start with the first -I dir after the -I-. */
4576 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4577 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4579 /* If -I- was specified, don't search current dir, only spec'd ones. */
4580 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4582 cppBuffer *fp = CPPBUFFER (pfile);
4583 /* We have "filename". Figure out directory this source
4584 file is coming from and put it on the front of the list. */
4586 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4591 llassert (fp != NULL);
4595 if (cstring_isDefined (fp->nominal_fname))
4597 nam = cstring_toCharsSafe (fp->nominal_fname);
4599 /* Found a named file. Figure out dir of the file,
4600 and put it in front of the search list. */
4601 dsp[0].next = search_start;
4605 ep = strrchr (nam, CONNECTCHAR);
4607 ep = strrchr (nam, ']');
4608 if (ep == NULL) ep = strrchr (nam, '>');
4609 if (ep == NULL) ep = strrchr (nam, ':');
4610 if (ep != NULL) ep++;
4620 /*@-onlytrans@*/ /* This looks like a memory leak... */
4621 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4625 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
4626 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
4630 dsp[0].fname = cstring_undefined; /* Current directory */
4633 dsp[0].got_name_map = 0;
4644 else if (token == CPP_NAME)
4647 * Support '#include xyz' like VAX-C to allow for easy use of all the
4648 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4649 * code from case '<' is repeated here) and generates a warning.
4651 cppReader_warning (pfile,
4652 "VAX-C-style include specification found, use '#include <filename.h>' !");
4654 /* If -I-, start with the first -I dir after the -I-. */
4655 if (CPPOPTIONS (pfile)->first_bracket_include)
4656 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4657 fbeg = pfile->token_buffer + old_written;
4658 fend = cpplib_getPWritten (pfile);
4663 cppReader_error (pfile,
4664 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4667 cppReader_setWritten (pfile, old_written);
4668 cppReader_skipRestOfLine (pfile);
4674 token = get_directive_token (pfile);
4675 if (token != CPP_VSPACE)
4677 cppReader_errorLit (pfile,
4678 cstring_makeLiteralTemp ("Junk at end of #include"));
4680 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4682 token = get_directive_token (pfile);
4687 ** For #include_next, skip in the search path
4688 ** past the dir in which the containing file was found.
4693 cppBuffer *fp = CPPBUFFER (pfile);
4695 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4697 llassert (fp != NULL);
4699 if (fp->fname != NULL)
4701 /* fp->dir is null if the containing file was specified with
4702 an absolute file name. In that case, don't skip anything. */
4703 if (fp->dir == SELF_DIR_DUMMY)
4705 search_start = CPPOPTIONS (pfile)->include;
4707 else if (fp->dir != NULL)
4709 search_start = fp->dir->next;
4721 cppReader_setWritten (pfile, old_written);
4723 flen = size_fromInt (fend - fbeg);
4725 DPRINTF (("fbeg: %s", fbeg));
4729 cppReader_error (pfile,
4730 message ("Empty file name in #%s", keyword->name));
4735 ** Allocate this permanently, because it gets stored in the definitions
4739 fname = cstring_undefined;
4741 /* + 2 above for slash and terminating null. */
4742 /* + 2 added for '.h' on VMS (to support '#include filename') */
4744 /* If specified file name is absolute, just open it. */
4746 if (osd_isConnectChar (*fbeg)
4747 # if defined (WIN32) || defined (OS2)
4748 || (*(fbeg + 1) == ':')
4752 fname = cstring_copyLength (fbeg, flen);
4754 if (redundant_include_p (pfile, fname))
4756 cstring_free (fname);
4760 f = open_include_file (pfile, fname, NULL);
4762 if (f == IMPORT_FOUND)
4764 return 0; /* Already included this file */
4769 /* Search directory path, trying to open the file.
4770 Copy each filename tried into FNAME. */
4772 for (searchptr = search_start; searchptr != NULL;
4773 searchptr = searchptr->next)
4775 if (!cstring_isEmpty (searchptr->fname))
4777 /* The empty string in a search path is ignored.
4778 This makes it possible to turn off entirely
4779 a standard piece of the list. */
4780 if (cstring_isEmpty (searchptr->fname))
4783 fname = cstring_copy (searchptr->fname);
4784 fname = cstring_appendChar (fname, CONNECTCHAR);
4785 DPRINTF (("Here: %s", fname));
4792 fname = cstring_concatLength (fname, fbeg, flen);
4794 DPRINTF (("fname: %s", fname));
4796 /* Win32 directory fix from Kay Buschner. */
4797 #if defined (WIN32) || defined (OS2)
4798 /* Fix all unixdir slashes to win dir slashes */
4799 if (searchptr->fname && (searchptr->fname[0] != 0))
4801 cstring_replaceAll (fname, '/', '\\');
4806 /* Change this 1/2 Unix 1/2 VMS file specification into a
4807 full VMS file specification */
4808 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4809 /* Fix up the filename */
4810 hack_vms_include_specification (fname);
4812 /* This is a normal VMS filespec, so use it unchanged. */
4813 strncpy (fname, fbeg, flen);
4815 /* if it's '#include filename', add the missing .h */
4816 if (strchr (fname,'.') == NULL) {
4817 strcat (fname, ".h");
4821 /* ??? There are currently 3 separate mechanisms for avoiding processing
4822 of redundant include files: #import, #pragma once, and
4823 redundant_include_p. It would be nice if they were unified. */
4825 if (redundant_include_p (pfile, fname))
4828 Needed to duplicate this code so that
4829 system library includes would print out right
4831 if ( context_getFlag (FLG_GENERATECODE) )
4833 /*drl 04-15-2002 for .h files we just output an include
4834 for other files we output code from the included file
4836 if (cstring_isDotH (cstring_fromChars(fbeg) ) )
4840 outputCode(message( "#include <%s>\n",
4841 cstring_fromChars(fbeg) ));
4845 outputCode(message( "#include \"%s\"\n",
4846 cstring_fromChars(fbeg) ));
4850 cstring_free (fname);
4854 DPRINTF (("Trying: %s", fname));
4856 f = open_include_file (pfile, fname, searchptr);
4858 if (f == IMPORT_FOUND)
4861 Needed to duplicate this code so that
4862 system library includes would print out right
4864 if ( context_getFlag (FLG_GENERATECODE) )
4866 /*drl 04-15-2002 for .h files we just output an include
4867 for other files we output code from the included file
4869 if (cstring_isDotH (cstring_fromChars(fbeg) ) )
4873 outputCode(message( "#include <%s>\n",
4874 cstring_fromChars(fbeg) ));
4878 outputCode(message( "#include \"%s\"\n",
4879 cstring_fromChars(fbeg) ));
4885 return 0; /* Already included this file */
4888 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4890 cppReader_warning (pfile,
4891 message ("Header file %s exists, but is not readable", fname));
4903 /*drl start modifictions 03-19-2002*/
4904 if ( context_getFlag (FLG_GENERATECODE) )
4906 /*drl 04-15-2002 for .h files we just output an include
4907 for other files we output code from the included file
4909 if (cstring_isDotH (cstring_fromChars(fbeg) ) )
4913 outputCode(message( "#include <%s>\n",
4914 cstring_fromChars(fbeg) ));
4918 outputCode(message( "#include \"%s\"\n",
4919 cstring_fromChars(fbeg) ));
4923 /*drl 04-18-2002 moved the above block */
4927 /* A file that was not found. */
4928 fname = cstring_copyLength (fbeg, flen);
4930 if (search_start != NULL)
4932 cppReader_error (pfile,
4933 message ("Cannot find include file %s on search path: %x",
4935 searchPath_unparse (search_start)));
4939 cppReader_error (pfile,
4940 message ("No include path in which to find %s", fname));
4945 ** Check to see if this include file is a once-only include file.
4949 struct file_name_list *ptr;
4951 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4953 if (cstring_equal (ptr->fname, fname))
4955 /* This file was included before. */
4962 /* This is the first time for this file. */
4963 /* Add it to list of files included. */
4965 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4966 ptr->control_macro = NULL;
4967 ptr->c_system_include_path = NULL;
4968 ptr->next = pfile->all_include_files;
4970 ptr->got_name_map = NULL;
4972 DPRINTF (("Including file: %s", fname));
4973 pfile->all_include_files = ptr;
4974 assertSet (pfile->all_include_files);
4977 if (angle_brackets != 0)
4979 pfile->system_include_depth++;
4982 /* Actually process the file */
4983 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4985 cstring_free (fname);
4989 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4990 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4992 output_line_command (pfile, 0, enter_file);
4993 pfile->only_seen_white = 2;
4998 pfile->system_include_depth--;
5001 } /*@=branchstate@*/
5006 /* Return nonzero if there is no need to include file NAME
5007 because it has already been included and it contains a conditional
5008 to make a repeated include do nothing. */
5011 redundant_include_p (cppReader *pfile, cstring name)
5013 struct file_name_list *l = pfile->all_include_files;
5015 for (; l != NULL; l = l->next)
5017 if (cstring_equal (name, l->fname)
5018 && (l->control_macro != NULL)
5019 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
5028 /* Return nonzero if the given FILENAME is an absolute pathname which
5029 designates a file within one of the known "system" include file
5030 directories. We assume here that if the given FILENAME looks like
5031 it is the name of a file which resides either directly in a "system"
5032 include file directory, or within any subdirectory thereof, then the
5033 given file must be a "system" include file. This function tells us
5034 if we should suppress pedantic errors/warnings for the given FILENAME.
5036 The value is 2 if the file is a C-language system header file
5037 for which C++ should (on most systems) assume `extern "C"'. */
5040 is_system_include (cppReader *pfile, cstring filename)
5042 struct file_name_list *searchptr;
5044 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
5046 searchptr = searchptr->next)
5048 if (!cstring_isEmpty (searchptr->fname))
5050 cstring sys_dir = searchptr->fname;
5051 size_t length = cstring_length (sys_dir);
5053 if (cstring_equalLen (sys_dir, filename, length)
5054 && osd_isConnectChar (cstring_getChar (filename, length)))
5056 if (searchptr->c_system_include_path)
5067 /* Convert a character string literal into a nul-terminated string.
5068 The input string is [IN ... LIMIT).
5069 The result is placed in RESULT. RESULT can be the same as IN.
5070 The value returned in the end of the string written to RESULT,
5071 or NULL on error. */
5073 static /*@null@*/ char *
5074 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5075 char *in, char *limit, int handle_escapes)
5095 /*@switchbreak@*/ break;
5099 char *bpc = (char *) in;
5100 int i = (char) cppReader_parseEscape (pfile, &bpc);
5103 *result++ = (char) c;
5104 /*@switchbreak@*/ break;
5118 * interpret #line command. Remembers previously seen fnames
5119 * in its very own hash table.
5122 /*@constant int FNAME_HASHSIZE@*/
5123 #define FNAME_HASHSIZE 37
5126 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5128 cppBuffer *ip = cppReader_getBuffer (pfile);
5130 size_t old_written = cpplib_getWritten (pfile);
5131 enum file_change_code file_change = same_file;
5132 enum cpp_token token;
5134 llassert (ip != NULL);
5135 token = get_directive_token (pfile);
5137 if (token != CPP_NUMBER
5138 || !isdigit(pfile->token_buffer[old_written]))
5140 cppReader_errorLit (pfile,
5141 cstring_makeLiteralTemp ("invalid format `#line' command"));
5143 goto bad_line_directive;
5146 /* The Newline at the end of this line remains to be processed.
5147 To put the next line at the specified line number,
5148 we must store a line number now that is one less. */
5149 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5150 cppReader_setWritten (pfile, old_written);
5152 /* NEW_LINENO is one less than the actual line number here. */
5153 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5154 cppReader_pedwarnLit (pfile,
5155 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5157 token = get_directive_token (pfile);
5159 if (token == CPP_STRING) {
5160 char *fname = pfile->token_buffer + old_written;
5162 static hashNode fname_table[FNAME_HASHSIZE];
5164 hashNode *hash_bucket;
5167 size_t fname_length;
5169 /* Turn the file name, which is a character string literal,
5170 into a null-terminated string. Do this in place. */
5171 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5172 if (end_name == NULL)
5174 cppReader_errorLit (pfile,
5175 cstring_makeLiteralTemp ("invalid format `#line' command"));
5176 goto bad_line_directive;
5179 fname_length = size_fromInt (end_name - fname);
5180 num_start = cpplib_getWritten (pfile);
5182 token = get_directive_token (pfile);
5183 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5184 p = pfile->token_buffer + num_start;
5185 if (cppReader_isPedantic (pfile))
5186 cppReader_pedwarnLit (pfile,
5187 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5189 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5191 cppReader_errorLit (pfile,
5192 cstring_makeLiteralTemp ("invalid format `#line' command"));
5193 goto bad_line_directive;
5196 file_change = enter_file;
5198 file_change = leave_file;
5200 ip->system_header_p = 1;
5201 else /* if (*p == 4) */
5202 ip->system_header_p = 2;
5204 cppReader_setWritten (pfile, num_start);
5205 token = get_directive_token (pfile);
5206 p = pfile->token_buffer + num_start;
5207 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5208 ip->system_header_p = *p == 3 ? 1 : 2;
5209 token = get_directive_token (pfile);
5211 if (token != CPP_VSPACE) {
5212 cppReader_errorLit (pfile,
5213 cstring_makeLiteralTemp ("invalid format `#line' command"));
5215 goto bad_line_directive;
5220 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5222 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5224 if (hp->length == fname_length)
5226 llassert (hp->value.cpval != NULL);
5228 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5230 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5237 /* Didn't find it; cons up a new one. */
5238 hp = (hashNode) dmalloc (sizeof (*hp));
5241 hp->bucket_hdr = NULL;
5243 hp->name = cstring_undefined;
5244 hp->next = *hash_bucket;
5248 hp->length = fname_length;
5249 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5250 memcpy (hp->value.cpval, fname, fname_length);
5251 hp->value.cpval[fname_length] = '\0';
5252 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5255 else if (token != CPP_VSPACE && token != CPP_EOF)
5257 cppReader_errorLit (pfile,
5258 cstring_makeLiteralTemp ("invalid format `#line' command"));
5259 goto bad_line_directive;
5266 ip->lineno = new_lineno;
5268 cppReader_skipRestOfLine (pfile);
5269 cppReader_setWritten (pfile, old_written);
5270 output_line_command (pfile, 0, file_change);
5275 * remove the definition of a symbol from the symbol table.
5276 * according to un*x /lib/cpp, it is not an error to undef
5277 * something that has no definitions, so it isn't one here either.
5281 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5286 char *orig_buf = buf;
5288 SKIP_WHITE_SPACE (buf);
5290 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5292 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5294 /* If we are generating additional info for debugging (with -g) we
5295 need to pass through all effective #undef commands. */
5296 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5298 pass_thru_directive (orig_buf, limit, pfile, keyword);
5301 if (hp->type != T_MACRO)
5303 cppReader_warning (pfile,
5304 message ("Undefining preprocessor builtin: %s",
5308 cppReader_deleteMacro (hp);
5311 if (cppReader_isPedantic (pfile)) {
5313 SKIP_WHITE_SPACE (buf);
5316 cppReader_pedwarnLit (pfile,
5317 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5326 * Report an error detected by the program we are processing.
5327 * Use the text of the line in the error message.
5328 * (We use error because it prints the filename & line#.)
5332 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5333 char *buf, char *limit)
5335 size_t length = size_fromInt (limit - buf);
5336 cstring copy = cstring_copyLength (buf, length);
5337 cstring adv = cstring_advanceWhiteSpace (copy);
5339 cppReader_error (pfile, message ("#error %s", adv));
5340 cstring_free (copy);
5345 * Report a warning detected by the program we are processing.
5346 * Use the text of the line in the warning message, then continue.
5347 * (We use error because it prints the filename & line#.)
5351 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5352 char *buf, char *limit)
5354 size_t length = size_fromInt (limit - buf);
5355 cstring copy = cstring_copyLength (buf, length);
5356 cstring adv = cstring_advanceWhiteSpace (copy);
5357 cppReader_warning (pfile, message ("#warning %s", adv));
5358 cstring_free (copy);
5363 /* #ident has already been copied to the output file, so just ignore it. */
5366 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5367 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5369 /* Allow #ident in system headers, since that's not user's fault. */
5370 if (cppReader_isPedantic (pfile)
5371 && !cppReader_getBufferSafe (pfile)->system_header_p)
5372 cppReader_pedwarnLit (pfile,
5373 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5375 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5380 /* #pragma and its argument line have already been copied to the output file.
5381 Just check for some recognized pragmas that need validation here. */
5384 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5385 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5387 while (*buf == ' ' || *buf == '\t')
5392 if (!strncmp (buf, "implementation", 14)) {
5393 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5394 been included yet. */
5395 struct file_name_list *ptr;
5396 char *p = buf + 14, *fname, *inc_fname;
5398 SKIP_WHITE_SPACE (p);
5399 if (*p == '\n' || *p != '\"')
5403 p = (char *) strchr (fname, '\"');
5404 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5406 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5408 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5409 inc_fname = (inc_fname != NULL)
5410 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5412 if ((inc_fname != NULL)
5413 && (strncmp (inc_fname, fname, fname_len) == 0))
5415 cpp_setLocation (pfile);
5417 ppllerror (message ("`#pragma implementation' for `%s' appears "
5418 "after file is included",
5419 cstring_fromChars (fname)));
5428 * handle #if command by
5429 * 1) inserting special `defined' keyword into the hash table
5430 * that gets turned into 0 or 1 by special_symbol (thus,
5431 * if the luser has a symbol called `defined' already, it won't
5432 * work inside the #if command)
5433 * 2) rescan the input into a temporary output buffer
5434 * 3) pass the output buffer to the yacc parser and collect a value
5435 * 4) clean up the mess left from steps 1 and 2.
5436 * 5) call conditional_skip to skip til the next #endif (etc.),
5437 * or not, depending on the value from step 3.
5441 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5442 char *buf, char *limit)
5444 HOST_WIDE_INT value;
5445 DPRINTF (("Do if: %s", buf));
5446 value = eval_if_expression (pfile, buf, limit - buf);
5447 conditional_skip (pfile, value == 0, T_IF, NULL);
5452 * handle a #elif directive by not changing if_stack either.
5453 * see the comment above do_else.
5456 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5457 char *buf, char *limit)
5459 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5461 cppReader_errorLit (pfile,
5462 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5467 llassert (pfile->if_stack != NULL);
5469 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5471 cppReader_errorLit (pfile,
5472 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5474 if (pfile->if_stack->fname != NULL
5475 && cppReader_getBufferSafe (pfile)->fname != NULL
5476 && !cstring_equal (pfile->if_stack->fname,
5477 cppReader_getBufferSafe (pfile)->nominal_fname))
5478 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5479 fprintf (stderr, ")\n");
5481 pfile->if_stack->type = T_ELIF;
5484 if (pfile->if_stack->if_succeeded)
5486 skip_if_group (pfile, 0);
5490 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5492 skip_if_group (pfile, 0);
5495 ++pfile->if_stack->if_succeeded; /* continue processing input */
5496 output_line_command (pfile, 1, same_file);
5504 * evaluate a #if expression in BUF, of length LENGTH,
5505 * then parse the result as a C expression and return the value as an int.
5508 static HOST_WIDE_INT
5509 eval_if_expression (cppReader *pfile,
5510 /*@unused@*/ char *buf,
5511 /*@unused@*/ int length)
5513 hashNode save_defined;
5514 HOST_WIDE_INT value;
5515 size_t old_written = cpplib_getWritten (pfile);
5517 DPRINTF (("Saving defined..."));
5518 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5519 pfile->pcp_inside_if = 1;
5521 value = cppReader_parseExpression (pfile);
5522 pfile->pcp_inside_if = 0;
5524 /* Clean up special symbol */
5525 DPRINTF (("Removing defined..."));
5526 cppReader_deleteMacro (save_defined);
5527 cppReader_setWritten (pfile, old_written); /* Pop */
5533 * routine to handle ifdef/ifndef. Try to look up the symbol,
5534 * then do or don't skip to the #endif/#else/#elif depending
5535 * on what directive is actually being processed.
5539 do_xifdef (cppReader *pfile, struct directive *keyword,
5540 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5543 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5545 size_t ident_length;
5546 enum cpp_token token;
5547 int start_of_file = 0;
5548 char *control_macro = 0;
5549 size_t old_written = cpplib_getWritten (pfile);
5551 DPRINTF (("do xifdef: %d",
5552 keyword->type == T_IFNDEF));
5554 /* Detect a #ifndef at start of file (not counting comments). */
5555 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5557 start_of_file = pfile->only_seen_white == 2;
5560 pfile->no_macro_expand++;
5561 token = get_directive_token (pfile);
5562 pfile->no_macro_expand--;
5564 ident = pfile->token_buffer + old_written;
5565 DPRINTF (("Ident: %s", ident));
5567 ident_length = cpplib_getWritten (pfile) - old_written;
5568 cppReader_setWritten (pfile, old_written); /* Pop */
5570 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5572 skip = (keyword->type == T_IFDEF);
5573 if (! cppReader_isTraditional (pfile))
5575 cppReader_pedwarn (pfile,
5576 message ("`#%s' with no argument", keyword->name));
5579 else if (token == CPP_NAME)
5581 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5583 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5585 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5587 if (start_of_file && !skip)
5589 DPRINTF (("Not skipping!"));
5590 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
5591 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
5596 skip = (keyword->type == T_IFDEF);
5597 if (! cppReader_isTraditional (pfile))
5599 cppReader_error (pfile,
5600 message ("`#%s' with invalid argument", keyword->name));
5604 if (!cppReader_isTraditional (pfile))
5607 cppSkipHspace (pfile);
5608 c = cppReader_peekC (pfile);
5609 if (c != EOF && c != '\n')
5611 cppReader_pedwarn (pfile,
5612 message ("garbage at end of `#%s' argument", keyword->name));
5616 cppReader_skipRestOfLine (pfile);
5618 DPRINTF (("Conditional skip: %d", skip));
5619 conditional_skip (pfile, skip, T_IF, control_macro);
5623 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5624 If this is a #ifndef starting at the beginning of a file,
5625 CONTROL_MACRO is the macro name tested by the #ifndef.
5626 Otherwise, CONTROL_MACRO is 0. */
5629 conditional_skip (cppReader *pfile, int skip,
5630 enum node_type type,
5631 /*@dependent@*/ char *control_macro)
5633 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5635 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5636 temp->next = pfile->if_stack;
5637 temp->control_macro = control_macro;
5639 temp->if_succeeded = 0;
5641 pfile->if_stack = temp;
5642 pfile->if_stack->type = type;
5646 skip_if_group (pfile, 0);
5651 ++pfile->if_stack->if_succeeded;
5652 output_line_command (pfile, 1, same_file);
5657 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5658 * leaves input ptr at the sharp sign found.
5659 * If ANY is nonzero, return at next directive of any sort.
5663 skip_if_group (cppReader *pfile, int any)
5666 struct directive *kt;
5667 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5668 register int ident_length;
5670 struct parse_marker line_start_mark;
5672 parseSetMark (&line_start_mark, pfile);
5674 if (CPPOPTIONS (pfile)->output_conditionals) {
5675 static char failed[] = "#failed\n";
5676 cppReader_puts (pfile, failed, sizeof(failed)-1);
5678 output_line_command (pfile, 1, same_file);
5682 if (CPPOPTIONS (pfile)->output_conditionals)
5684 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5687 llassert (pbuf->buf != NULL);
5689 start_line = pbuf->buf + line_start_mark.position;
5690 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5693 parseMoveMark (&line_start_mark, pfile);
5695 if (!cppReader_isTraditional (pfile))
5697 cppSkipHspace (pfile);
5700 c = cppReader_getC (pfile);
5703 size_t old_written = cpplib_getWritten (pfile);
5704 cppSkipHspace (pfile);
5706 parse_name (pfile, cppReader_getC (pfile));
5707 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5708 ident = pfile->token_buffer + old_written;
5709 pfile->limit = ident;
5711 for (kt = directive_table; kt->length >= 0; kt++)
5713 cppIfStackFrame *temp;
5714 if (ident_length == kt->length
5715 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5717 /* If we are asked to return on next directive, do so now. */
5728 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5729 temp->next = pfile->if_stack;
5730 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5731 temp->type = kt->type;
5733 temp->if_succeeded = 0;
5734 temp->control_macro = NULL;
5736 pfile->if_stack = temp;
5737 /*@switchbreak@*/ break;
5740 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5741 validate_else (pfile,
5742 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5745 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5747 cppReader_error (pfile,
5748 message ("Preprocessor command #%s is not within a conditional", kt->name));
5749 /*@switchbreak@*/ break;
5751 else if (pfile->if_stack == save_if_stack)
5753 goto done; /* found what we came for */
5760 if (kt->type != T_ENDIF)
5762 llassert (pfile->if_stack != NULL);
5764 if (pfile->if_stack->type == T_ELSE)
5766 cppReader_errorLit (pfile,
5767 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5770 pfile->if_stack->type = kt->type;
5771 /*@switchbreak@*/ break;
5774 temp = pfile->if_stack;
5775 llassert (temp != NULL);
5776 pfile->if_stack = temp->next;
5778 /*@switchbreak@*/ break;
5781 #if defined (OS2) && defined (__IBMC__)
5782 /* Dummy code to eleminate optimization problems with icc */
5791 /* Don't let erroneous code go by. */
5793 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5794 && cppReader_isPedantic (pfile))
5796 cppReader_pedwarnLit (pfile,
5797 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5801 c = cppReader_getC (pfile);
5803 /* We're in the middle of a line. Skip the rest of it. */
5811 case '/': /* possible comment */
5812 c = skip_comment (pfile, NULL);
5815 /*@switchbreak@*/ break;
5818 cppReader_forward (pfile, -1);
5819 old = cpplib_getWritten (pfile);
5820 (void) cpplib_getToken (pfile);
5821 cppReader_setWritten (pfile, old);
5822 /*@switchbreak@*/ break;
5824 /* Char after backslash loses its special meaning. */
5825 if (cppReader_peekC (pfile) == '\n')
5827 cppReader_forward (pfile, 1);
5830 /*@switchbreak@*/ break;
5834 c = cppReader_getC (pfile);
5837 if (CPPOPTIONS (pfile)->output_conditionals) {
5838 static char end_failed[] = "#endfailed\n";
5839 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5842 pfile->only_seen_white = 1;
5844 parseGotoMark (&line_start_mark, pfile);
5845 parseClearMark (&line_start_mark);
5849 * handle a #else directive. Do this by just continuing processing
5850 * without changing if_stack ; this is so that the error message
5851 * for missing #endif's etc. will point to the original #if. It
5852 * is possible that something different would be better.
5856 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5857 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5859 if (cppReader_isPedantic (pfile))
5861 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5864 cppReader_skipRestOfLine (pfile);
5866 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5867 cppReader_errorLit (pfile,
5868 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5871 /* #ifndef can't have its special treatment for containing the whole file
5872 if it has a #else clause. */
5874 llassert (pfile->if_stack != NULL);
5876 pfile->if_stack->control_macro = 0;
5878 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5880 cpp_setLocation (pfile);
5881 genppllerrorhint (FLG_PREPROC,
5882 message ("Pre-processor directive #else after #else"),
5883 message ("%q: Location of match",
5884 fileloc_unparseRaw (pfile->if_stack->fname,
5885 pfile->if_stack->lineno)));
5888 pfile->if_stack->type = T_ELSE;
5891 if (pfile->if_stack->if_succeeded)
5892 skip_if_group (pfile, 0);
5894 ++pfile->if_stack->if_succeeded; /* continue processing input */
5895 output_line_command (pfile, 1, same_file);
5902 * unstack after #endif command
5906 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5907 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5909 if (cppReader_isPedantic (pfile))
5911 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5914 cppReader_skipRestOfLine (pfile);
5916 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5918 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5922 cppIfStackFrame *temp = pfile->if_stack;
5924 llassert (temp != NULL);
5926 pfile->if_stack = temp->next;
5927 if (temp->control_macro != 0)
5929 /* This #endif matched a #ifndef at the start of the file.
5930 See if it is at the end of the file. */
5931 struct parse_marker start_mark;
5934 parseSetMark (&start_mark, pfile);
5938 cppSkipHspace (pfile);
5939 c = cppReader_getC (pfile);
5945 parseGotoMark (&start_mark, pfile);
5946 parseClearMark (&start_mark);
5950 /* If we get here, this #endif ends a #ifndef
5951 that contains all of the file (aside from whitespace).
5952 Arrange not to include the file again
5953 if the macro that was tested is defined.
5955 Do not do this for the top-level file in a -include or any
5956 file in a -imacros. */
5957 struct file_name_list *ifile = pfile->all_include_files;
5959 for ( ; ifile != NULL; ifile = ifile->next)
5961 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5963 ifile->control_macro = temp->control_macro;
5971 output_line_command (pfile, 1, same_file);
5976 /* When an #else or #endif is found while skipping failed conditional,
5977 if -pedantic was specified, this is called to warn about text after
5978 the command name. P points to the first char after the command name. */
5981 validate_else (cppReader *pfile, cstring directive)
5984 cppSkipHspace (pfile);
5985 c = cppReader_peekC (pfile);
5986 if (c != EOF && c != '\n')
5988 cppReader_pedwarn (pfile,
5989 message ("text following `%s' violates ANSI standard", directive));
5994 ** Get the next token, and add it to the text in pfile->token_buffer.
5995 ** Return the kind of token we got.
5999 cpplib_getToken (cppReader *pfile)
6001 return cpplib_getTokenAux (pfile, FALSE);
6005 cpplib_getTokenForceExpand (cppReader *pfile)
6007 return cpplib_getTokenAux (pfile, TRUE);
6011 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
6014 size_t old_written = 0;
6015 int start_line, start_column;
6016 enum cpp_token token;
6017 struct cppOptions *opts = CPPOPTIONS (pfile);
6018 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
6021 c = cppReader_getC (pfile);
6022 DPRINTF (("Get next token: %c", c));
6027 if (cppReader_getBufferSafe (pfile)->seen_eof)
6029 cppBuffer *buf = cppReader_popBuffer (pfile);
6031 if (buf != cppReader_nullBuffer (pfile))
6042 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6043 cppReader_getBufferSafe (pfile)->seen_eof = 1;
6045 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
6046 && next_buf != cppReader_nullBuffer (pfile))
6048 /* We're about to return from an #include file.
6049 Emit #line information now (as part of the CPP_POP) result.
6050 But the #line refers to the file we will pop to. */
6051 cppBuffer *cur_buffer = CPPBUFFER (pfile);
6052 CPPBUFFER (pfile) = next_buf;
6053 pfile->input_stack_listing_current = 0;
6054 output_line_command (pfile, 0, leave_file);
6055 CPPBUFFER (pfile) = cur_buffer;
6063 struct parse_marker start_mark;
6068 if (cppReader_peekC (pfile) == '=')
6073 if (opts->put_out_comments)
6075 parseSetMark (&start_mark, pfile);
6079 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6080 &start_line, &start_column);
6081 c = skip_comment (pfile, &newlines);
6083 if (opts->put_out_comments && (c == '/' || c == EOF))
6085 assertSet (&start_mark);
6086 parseClearMark (&start_mark);
6093 cppReader_errorWithLine (pfile, start_line, start_column,
6094 cstring_makeLiteral ("Unterminated comment"));
6097 c = '/'; /* Initial letter of comment. */
6099 /* Comments are equivalent to spaces.
6100 For -traditional, a comment is equivalent to nothing. */
6102 if (opts->put_out_comments)
6106 assertSet (&start_mark);
6107 res = cpp_handleComment (pfile, &start_mark);
6108 pfile->lineno += newlines;
6111 else if (cppReader_isTraditional (pfile))
6117 cpplib_reserve(pfile, 1);
6118 cppReader_putCharQ (pfile, ' ');
6123 if (!pfile->only_seen_white)
6128 if (cppReader_handleDirective (pfile))
6130 return CPP_DIRECTIVE;
6133 pfile->only_seen_white = 0;
6138 /* A single quoted string is treated like a double -- some
6139 programs (e.g., troff) are perverse this way */
6140 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6141 &start_line, &start_column);
6142 old_written = cpplib_getWritten (pfile);
6144 cppReader_putChar (pfile, c);
6147 int cc = cppReader_getC (pfile);
6150 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6152 /* try harder: this string crosses a macro expansion
6153 boundary. This can happen naturally if -traditional.
6154 Otherwise, only -D can make a macro with an unmatched
6157 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6158 (*cppReader_getBufferSafe (pfile)->cleanup)
6159 (cppReader_getBufferSafe (pfile), pfile);
6160 CPPBUFFER (pfile) = next_buf;
6163 if (!cppReader_isTraditional (pfile))
6165 cpp_setLocation (pfile);
6167 setLine (long_toInt (start_line));
6168 setColumn (long_toInt (start_column));
6170 if (pfile->multiline_string_line != long_toInt (start_line)
6171 && pfile->multiline_string_line != 0)
6175 message ("Unterminated string or character constant"),
6176 message ("%q: Possible real start of unterminated constant",
6178 (fileloc_filename (g_currentloc),
6179 pfile->multiline_string_line)));
6180 pfile->multiline_string_line = 0;
6186 message ("Unterminated string or character constant"));
6189 /*@loopbreak@*/ break;
6191 cppReader_putChar (pfile, cc);
6195 /* Traditionally, end of line ends a string constant with
6196 no error. So exit the loop and record the new line. */
6197 if (cppReader_isTraditional (pfile))
6203 if (cppReader_isPedantic (pfile)
6204 && pfile->multiline_string_line == 0)
6206 cppReader_pedwarnWithLine
6207 (pfile, long_toInt (start_line),
6208 long_toInt (start_column),
6209 cstring_makeLiteral ("String constant runs past end of line"));
6211 if (pfile->multiline_string_line == 0)
6213 pfile->multiline_string_line = start_line;
6216 /*@switchbreak@*/ break;
6219 cc = cppReader_getC (pfile);
6222 /* Backslash newline is replaced by nothing at all. */
6223 cppReader_adjustWritten (pfile, -1);
6228 /* ANSI stupidly requires that in \\ the second \
6229 is *not* prevented from combining with a newline. */
6232 cppReader_putChar (pfile, cc);
6234 /*@switchbreak@*/ break;
6240 /*@switchbreak@*/ break;
6244 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6245 cpplib_getPWritten (pfile));
6246 pfile->only_seen_white = 0;
6247 return c == '\'' ? CPP_CHAR : CPP_STRING;
6250 if (!opts->dollars_in_ident)
6255 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6263 c2 = cppReader_peekC (pfile);
6264 if (c2 == c || c2 == '=')
6274 if (cppReader_peekC (pfile) == '=')
6280 c2 = cppReader_peekC (pfile);
6281 if (c2 == '-' && opts->chill)
6283 /* Chill style comment */
6284 if (opts->put_out_comments)
6286 parseSetMark (&start_mark, pfile);
6289 cppReader_forward (pfile, 1); /* Skip second '-'. */
6293 c = cppReader_getC (pfile);
6295 /*@loopbreak@*/ break;
6298 /* Don't consider final '\n' to be part of comment. */
6299 cppReader_forward (pfile, -1);
6300 /*@loopbreak@*/ break;
6304 goto return_comment;
6306 if (c2 == '-' || c2 == '=' || c2 == '>')
6311 if (pfile->parsing_include_directive)
6315 cppReader_putChar (pfile, c);
6317 /*@loopbreak@*/ break;
6318 c = cppReader_getC (pfile);
6320 if (c == '\n' || c == EOF)
6322 cppReader_errorLit (pfile,
6323 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6324 /*@loopbreak@*/ break;
6332 c2 = cppReader_peekC (pfile);
6337 cppReader_forward (pfile, 1);
6338 cpplib_reserve (pfile, 4);
6339 cppReader_putChar (pfile, c);
6340 cppReader_putChar (pfile, c2);
6342 c3 = cppReader_peekC (pfile);
6344 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6345 cppReader_nullTerminateQ (pfile);
6346 pfile->only_seen_white = 0;
6350 if (cppReader_getBufferSafe (pfile)->has_escapes)
6352 c = cppReader_getC (pfile);
6355 if (pfile->output_escapes)
6356 cppReader_puts (pfile, "@-", 2);
6357 parse_name (pfile, cppReader_getC (pfile));
6360 else if (is_space [c])
6362 cpplib_reserve (pfile, 2);
6363 if (pfile->output_escapes)
6364 cppReader_putCharQ (pfile, '@');
6365 cppReader_putCharQ (pfile, c);
6373 if (pfile->output_escapes)
6375 cppReader_puts (pfile, "@@", 2);
6381 c2 = cppReader_peekC (pfile);
6384 cpplib_reserve(pfile, 2);
6385 cppReader_putCharQ (pfile, '.');
6386 c = cppReader_getC (pfile);
6390 /* FIXME - misses the case "..\\\n." */
6391 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6393 cpplib_reserve(pfile, 4);
6394 cppReader_putCharQ (pfile, '.');
6395 cppReader_putCharQ (pfile, '.');
6396 cppReader_putCharQ (pfile, '.');
6397 cppReader_forward (pfile, 2);
6398 cppReader_nullTerminateQ (pfile);
6399 pfile->only_seen_white = 0;
6405 pfile->only_seen_white = 0;
6407 cpplib_reserve(pfile, 3);
6408 cppReader_putCharQ (pfile, c);
6409 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6410 cppReader_nullTerminateQ (pfile);
6415 c2 = cppReader_peekC (pfile);
6416 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6418 cppReader_putChar (pfile, c);
6419 c = cppReader_getC (pfile);
6424 case '0': case '1': case '2': case '3': case '4':
6425 case '5': case '6': case '7': case '8': case '9':
6430 cpplib_reserve (pfile, 2);
6431 cppReader_putCharQ (pfile, c);
6433 c = cppReader_peekC (pfile);
6435 /*@loopbreak@*/ break;
6436 if (!is_idchar[c] && c != '.'
6437 && ((c2 != 'e' && c2 != 'E'
6438 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6439 || (c != '+' && c != '-')))
6440 /*@loopbreak@*/ break;
6441 cppReader_forward (pfile, 1);
6445 cppReader_nullTerminateQ (pfile);
6446 pfile->only_seen_white = 0;
6449 case 'b': case 'c': case 'd': case 'h': case 'o':
6450 case 'B': case 'C': case 'D': case 'H': case 'O':
6451 if (opts->chill && cppReader_peekC (pfile) == '\'')
6453 pfile->only_seen_white = 0;
6454 cpplib_reserve (pfile, 2);
6455 cppReader_putCharQ (pfile, c);
6456 cppReader_putCharQ (pfile, '\'');
6457 cppReader_forward (pfile, 1);
6460 c = cppReader_getC (pfile);
6462 goto chill_number_eof;
6465 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6467 cppReader_forward (pfile, 2);
6470 /*@loopbreak@*/ break;
6472 cppReader_putChar (pfile, c);
6476 cpplib_reserve (pfile, 2);
6477 cppReader_putCharQ (pfile, c);
6478 cppReader_nullTerminateQ (pfile);
6483 cppReader_forward (pfile, -1);
6485 cppReader_nullTerminate (pfile);
6492 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6493 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6494 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6495 case 'x': case 'y': case 'z':
6496 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6497 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6498 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6504 size_t before_name_written = cpplib_getWritten (pfile);
6506 parse_name (pfile, c);
6507 pfile->only_seen_white = 0;
6509 if (pfile->no_macro_expand)
6511 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6515 ident = pfile->token_buffer + before_name_written;
6516 DPRINTF (("Ident: %s", ident));
6518 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6520 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6524 DPRINTF (("No expand: %s %d", ident, ident_len));
6528 if (hp->type == T_DISABLED)
6530 DPRINTF (("Disabled!"));
6532 if (pfile->output_escapes)
6533 { /* Return "@-IDENT", followed by '\0'. */
6535 cpplib_reserve (pfile, 3);
6536 ident = pfile->token_buffer + before_name_written;
6537 cppReader_adjustWritten (pfile, 2);
6539 for (i = size_toInt (ident_len); i >= 0; i--)
6541 ident[i+2] = ident[i];
6551 ** If macro wants an arglist, verify that a '(' follows.
6552 ** first skip all whitespace, copying it to the output
6553 ** after the macro name. Then, if there is no '(',
6554 ** decide this is not a macro call and leave things that way.
6557 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6559 struct parse_marker macro_mark;
6562 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6564 cppBuffer *next_buf;
6565 cppSkipHspace (pfile);
6566 if (cppReader_peekC (pfile) != EOF)
6568 /*@loopbreak@*/ break;
6571 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6572 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6573 CPPBUFFER (pfile) = next_buf;
6576 parseSetMark (¯o_mark, pfile);
6580 cppSkipHspace (pfile);
6581 c = cppReader_peekC (pfile);
6582 is_macro_call = c == '(';
6584 /*@loopbreak@*/ break;
6585 cppReader_forward (pfile, 1);
6590 parseGotoMark (¯o_mark, pfile);
6593 parseClearMark (¯o_mark);
6600 /* This is now known to be a macro call. */
6602 /* it might not actually be a macro. */
6603 if (hp->type != T_MACRO)
6608 cppReader_setWritten (pfile, before_name_written);
6609 special_symbol (hp, pfile);
6610 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6611 xbuf = (char *) dmalloc (xbuf_len + 1);
6612 cppReader_setWritten (pfile, before_name_written);
6613 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6614 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6619 ** Expand the macro, reading arguments as needed,
6620 ** and push the expansion on the input stack.
6623 cpplib_macroExpand (pfile, hp);
6624 cppReader_setWritten (pfile, before_name_written);
6627 /* An extra "@ " is added to the end of a macro expansion
6628 to prevent accidental token pasting. We prefer to avoid
6629 unneeded extra spaces (for the sake of cpp-using tools like
6630 imake). Here we remove the space if it is safe to do so. */
6632 llassert (pfile->buffer->rlimit != NULL);
6634 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6635 && pfile->buffer->rlimit[-2] == '@'
6636 && pfile->buffer->rlimit[-1] == ' ')
6638 int c1 = pfile->buffer->rlimit[-3];
6639 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6641 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6642 pfile->buffer->rlimit -= 2;
6648 case ' ': case '\t': case '\v': case '\r':
6651 cppReader_putChar (pfile, c);
6652 c = cppReader_peekC (pfile);
6653 if (c == EOF || !is_hor_space[c])
6654 /*@loopbreak@*/ break;
6655 cppReader_forward (pfile, 1);
6660 c2 = cppReader_peekC (pfile);
6667 cppReader_putChar (pfile, c);
6668 if (pfile->only_seen_white == 0)
6669 pfile->only_seen_white = 1;
6671 output_line_command (pfile, 1, same_file);
6674 case '(': token = CPP_LPAREN; goto char1;
6675 case ')': token = CPP_RPAREN; goto char1;
6676 case '{': token = CPP_LBRACE; goto char1;
6677 case '}': token = CPP_RBRACE; goto char1;
6678 case ',': token = CPP_COMMA; goto char1;
6679 case ';': token = CPP_SEMICOLON; goto char1;
6685 pfile->only_seen_white = 0;
6686 cppReader_putChar (pfile, c);
6695 /* Parse an identifier starting with C. */
6698 parse_name (cppReader *pfile, int c)
6704 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6706 cppReader_forward (pfile, 2);
6710 cppReader_forward (pfile, -1);
6714 if (c == '$' && cppReader_isPedantic (pfile))
6716 cppReader_pedwarnLit (pfile,
6717 cstring_makeLiteralTemp ("`$' in identifier"));
6720 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6721 cppReader_putCharQ (pfile, c);
6722 c = cppReader_getC (pfile);
6728 cppReader_nullTerminateQ (pfile);
6731 /* The file_name_map structure holds a mapping of file names for a
6732 particular directory. This mapping is read from the file named
6733 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6734 map filenames on a file system with severe filename restrictions,
6735 such as DOS. The format of the file name map file is just a series
6736 of lines with two tokens on each line. The first token is the name
6737 to map, and the second token is the actual name to use. */
6739 struct file_name_map
6741 struct file_name_map *map_next;
6746 /*@constant observer char *FILE_NAME_MAP_FILE*/
6747 #define FILE_NAME_MAP_FILE "header.gcc"
6749 /* Read a space delimited string of unlimited length from a stdio
6752 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6758 set = alloc = dmalloc (len + 1);
6763 while ((ch = getc (f)) != EOF && ! is_space[ch])
6765 if (set - alloc == size_toInt (len))
6768 alloc = drealloc (alloc, len + 1);
6769 set = alloc + len / 2;
6770 /*@-branchstate@*/ }
6773 } /*@=branchstate@*/
6776 check (ungetc (ch, f) != EOF);
6778 return cstring_fromChars (alloc);
6781 /* This structure holds a linked list of file name maps, one per directory. */
6783 struct file_name_map_list
6785 struct file_name_map_list *map_list_next;
6786 cstring map_list_name;
6787 /*@null@*/ struct file_name_map *map_list_map;
6790 /* Read the file name map file for DIRNAME. */
6792 static struct file_name_map *
6793 read_name_map (cppReader *pfile, cstring dirname)
6795 struct file_name_map_list *map_list_ptr;
6799 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6800 map_list_ptr != NULL;
6801 map_list_ptr = map_list_ptr->map_list_next)
6803 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6805 return map_list_ptr->map_list_map;
6809 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6810 map_list_ptr->map_list_name = cstring_copy (dirname);
6811 map_list_ptr->map_list_map = NULL;
6813 name = cstring_copy (dirname);
6815 if (cstring_length (dirname) > 0)
6817 name = cstring_appendChar (name, CONNECTCHAR);
6820 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6822 f = fileTable_openReadFile (context_fileTable (), name);
6823 cstring_free (name);
6827 map_list_ptr->map_list_map = NULL;
6833 while ((ch = getc (f)) != EOF)
6836 struct file_name_map *ptr;
6843 from = read_filename_string (ch, f);
6844 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6849 to = read_filename_string (ch, f);
6851 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6852 ptr->map_from = from;
6854 /* Make the real filename absolute. */
6855 if (cstring_length (to) > 1
6856 && osd_isConnectChar (cstring_firstChar (to)))
6862 ptr->map_to = cstring_copy (dirname);
6863 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6864 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6867 ptr->map_next = map_list_ptr->map_list_map;
6868 map_list_ptr->map_list_map = ptr;
6870 while ((ch = getc (f)) != '\n')
6874 /*@innerbreak@*/ break;
6879 assertSet (map_list_ptr->map_list_map);
6880 check (fileTable_closeFile (context_fileTable (),f) == 0);
6883 map_list_ptr->map_list_next = pfile->opts->map_list;
6884 pfile->opts->map_list = map_list_ptr;
6886 return map_list_ptr->map_list_map;
6889 /* Try to open include file FILENAME. SEARCHPTR is the directory
6890 being tried from the include file search path. This function maps
6891 filenames on file systems based on information read by
6895 open_include_file (cppReader *pfile,
6897 struct file_name_list *searchptr)
6899 char *filename = cstring_toCharsSafe (fname);
6900 struct file_name_map *map;
6904 cstring_markOwned (fname);
6906 cpp_setLocation (pfile);
6908 if (context_getFlag (FLG_NEVERINCLUDE))
6910 if (isHeaderFile (fname))
6912 return SKIP_INCLUDE;
6916 if ((searchptr != NULL) && ! searchptr->got_name_map)
6918 searchptr->name_map = read_name_map (pfile,
6919 !cstring_isEmpty (searchptr->fname)
6920 ? searchptr->fname :
6921 cstring_makeLiteralTemp ("."));
6922 searchptr->got_name_map = 1;
6925 /* First check the mapping for the directory we are using. */
6927 if ((searchptr != NULL)
6928 && (searchptr->name_map != NULL))
6932 if (!cstring_isEmpty (searchptr->fname))
6934 from += cstring_length (searchptr->fname) + 1;
6937 for (map = searchptr->name_map;
6939 map = map->map_next)
6941 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6944 ** Found a match. Check if the file should be skipped
6947 if (cpp_skipIncludeFile (map->map_to))
6949 return SKIP_INCLUDE;
6953 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6960 ** Try to find a mapping file for the particular directory we are
6961 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6962 ** in /usr/include/header.gcc and look up types.h in
6963 ** /usr/include/sys/header.gcc.
6966 p = strrchr (filename, CONNECTCHAR);
6973 if ((searchptr != NULL)
6974 && (cstring_isDefined (searchptr->fname))
6975 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6976 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6978 size_fromInt (p - filename)))
6980 /* filename is in SEARCHPTR, which we've already checked. */
6982 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6984 return SKIP_INCLUDE;
6988 return cpp_openIncludeFile (filename);
6994 dir = mstring_copy (".");
6999 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
7000 memcpy (dir, filename, size_fromInt (p - filename));
7001 dir[p - filename] = '\0';
7005 for (map = read_name_map (pfile, cstring_fromChars (dir));
7007 map = map->map_next)
7009 if (cstring_equal (map->map_from, cstring_fromChars (from)))
7013 if (cpp_skipIncludeFile (map->map_to))
7015 return SKIP_INCLUDE;
7019 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
7026 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
7028 return SKIP_INCLUDE;
7032 return cpp_openIncludeFile (filename);
7036 /* Process the contents of include file FNAME, already open on descriptor F,
7038 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
7039 "system" include directories (as decided by the `is_system_include'
7041 DIRPTR is the link in the dir path through which this file was found,
7042 or 0 if the file name was absolute or via the current directory.
7043 Return 1 on success, 0 on failure.
7045 The caller is responsible for the cppReader_pushBuffer. */
7048 finclude (cppReader *pfile, int f,
7050 bool system_header_p,
7051 /*@dependent@*/ struct file_name_list *dirptr)
7057 cppBuffer *fp; /* For input stack frame */
7059 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7061 cppReader_perrorWithName (pfile, fname);
7062 check (close (f) == 0);
7063 (void) cppReader_popBuffer (pfile);
7069 fp = cppReader_getBufferSafe (pfile);
7071 /*@-temptrans@*/ /* fname shouldn't really be temp */
7072 fp->nominal_fname = fp->fname = fname;
7076 fp->system_header_p = system_header_p;
7079 fp->cleanup = cppReader_fileCleanup;
7081 if (S_ISREG (st_mode))
7084 fp->buf = (char *) dmalloc (st_size + 2);
7085 fp->alimit = fp->buf + st_size + 2;
7088 /* Read the file contents, knowing that st_size is an upper bound
7089 on the number of bytes we can read. */
7090 length = safe_read (f, fp->buf, size_toInt (st_size));
7091 fp->rlimit = fp->buf + length;
7092 if (length < 0) goto nope;
7094 else if (S_ISDIR (st_mode))
7096 cppReader_error (pfile,
7097 message ("Directory specified in #include: %s", fname));
7098 check (close (f) == 0);
7104 ** Cannot count its file size before reading.
7105 ** First read the entire file into heap and
7106 ** copy them into buffer on stack.
7109 size_t bsize = 2000;
7114 fp->buf = (char *) dmalloc (bsize + 2);
7117 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7120 goto nope; /* error! */
7123 if (st_size != bsize)
7125 break; /* End of file */
7129 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7133 length = size_toInt (st_size);
7136 if ((length > 0 && fp->buf[length - 1] != '\n')
7137 /* Backslash-newline at end is not good enough. */
7138 || (length > 1 && fp->buf[length - 2] == '\\')) {
7139 fp->buf[length++] = '\n';
7142 fp->buf[length] = '\0';
7143 fp->rlimit = fp->buf + length;
7145 /* Close descriptor now, so nesting does not use lots of descriptors. */
7146 check (close (f) == 0);
7148 /* Must do this before calling trigraph_pcp, so that the correct file name
7149 will be printed in warning messages. */
7151 pfile->input_stack_listing_current = 0;
7156 cppReader_perrorWithName (pfile, fname);
7157 check (close (f) == 0);
7163 cpplib_init (cppReader *pfile)
7165 memset ((char *) pfile, 0, sizeof (*pfile));
7167 pfile->get_token = cpplib_getToken;
7168 pfile->token_buffer_size = 200;
7169 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7170 pfile->all_include_files = NULL;
7174 cppReader_setWritten (pfile, 0);
7176 pfile->system_include_depth = 0;
7177 pfile->max_include_len = 0;
7178 pfile->timebuf = NULL;
7179 pfile->only_seen_white = 1;
7181 pfile->buffer = cppReader_nullBuffer (pfile);
7185 cppReader_finish (/*@unused@*/ cppReader *pfile)
7190 /* Free resources used by PFILE.
7191 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7194 cppCleanup (cppReader *pfile)
7196 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7198 (void) cppReader_popBuffer (pfile);
7201 if (pfile->token_buffer != NULL)
7203 sfree (pfile->token_buffer);
7204 pfile->token_buffer = NULL;
7207 while (pfile->if_stack != NULL)
7209 cppIfStackFrame *temp = pfile->if_stack;
7210 pfile->if_stack = temp->next;
7214 while (pfile->all_include_files != NULL)
7216 struct file_name_list *temp = pfile->all_include_files;
7217 pfile->all_include_files = temp->next;
7218 /*@-dependenttrans@*/
7219 cstring_free (temp->fname);
7220 /*@=dependenttrans@*/
7224 cppReader_hashCleanup ();
7228 ** Get the file-mode and data size of the file open on FD
7229 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7233 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7237 if (fstat (fd, &sbuf) < 0) {
7243 if (mode_pointer != NULL)
7245 *mode_pointer = sbuf.st_mode;
7248 if (size_pointer != NULL)
7250 *size_pointer = (size_t) sbuf.st_size;
7256 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7257 retrying if necessary. Return a negative value if an error occurs,
7258 otherwise return the actual number of bytes read,
7259 which must be LEN unless end-of-file was reached. */
7261 static int safe_read (int desc, char *ptr, int len)
7267 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7268 /*@-compdef@*/ /* ptr is an out parameter */
7269 int nchars = _read (desc, ptr, (unsigned) left);
7272 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7281 return (int) nchars;
7295 /* Initialize PMARK to remember the current position of PFILE. */
7298 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7300 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7302 pmark->next = pbuf->marks;
7304 pbuf->marks = pmark;
7308 pmark->position = pbuf->cur - pbuf->buf;
7311 /* Cleanup PMARK - we no longer need it. */
7313 void parseClearMark (struct parse_marker *pmark)
7315 struct parse_marker **pp = &pmark->buf->marks;
7317 for (; ; pp = &(*pp)->next)
7319 llassert (*pp != NULL);
7320 if (*pp == pmark) break;
7326 /* Backup the current position of PFILE to that saved in PMARK. */
7329 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7331 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7333 if (pbuf != pmark->buf)
7335 cpp_setLocation (pfile);
7336 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7339 llassert (pbuf->buf != NULL);
7340 pbuf->cur = pbuf->buf + pmark->position;
7343 /* Reset PMARK to point to the current position of PFILE. (Same
7344 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7347 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7349 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7351 if (pbuf != pmark->buf)
7353 cpp_setLocation (pfile);
7354 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7357 pmark->position = pbuf->cur - pbuf->buf;
7360 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7362 struct cppOptions *opts = CPPOPTIONS (pfile);
7365 /* The code looks at the defaults through this pointer, rather than through
7366 the constant structure above. This pointer gets changed if an environment
7367 variable specifies other defaults. */
7369 struct default_include *include_defaults = include_defaults_array;
7371 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7372 /* There seems to be confusion about what CPATH should do,
7373 so for the moment it is not documented. */
7374 /* Some people say that CPATH should replace the standard include dirs,
7375 but that seems pointless: it comes before them, so it overrides them
7378 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7380 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7382 path_include (pfile, cstring_toCharsSafe (xp));
7385 /* Now that dollars_in_ident is known, initialize is_idchar. */
7386 initialize_char_syntax (opts);
7388 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7389 and option processing. */
7391 initialize_builtins (pfile);
7393 /* Do standard #defines and assertions
7394 that identify system and machine type. */
7396 if (!opts->inhibit_predefs) {
7397 char *p = (char *) dmalloc (strlen (predefs) + 1);
7398 strcpy (p, predefs);
7404 while (*p == ' ' || *p == '\t')
7409 /* Handle -D options. */
7410 if (p[0] == '-' && p[1] == 'D')
7414 while (*p && *p != ' ' && *p != '\t')
7424 if (opts->debug_output)
7426 output_line_command (pfile, 0, same_file);
7429 cppReader_define (pfile, q);
7431 while (*p == ' ' || *p == '\t')
7445 opts->done_initializing = 1;
7447 { /* Read the appropriate environment variable and if it exists
7448 replace include_defaults with the listed path. */
7452 int win32_buf_size = 0; /* memory we need to allocate */
7455 if (opts->cplusplus)
7457 epath = getenv ("CPLUS_INCLUDE_PATH");
7461 epath = getenv ("C_INCLUDE_PATH");
7465 ** If the environment var for this language is set,
7466 ** add to the default list of include directories.
7469 if (epath != NULL) {
7470 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7472 char *startp, *endp;
7475 /* if we have a posix path list, convert to win32 path list */
7476 if (cygwin32_posix_path_list_p (epath))
7478 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7479 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7480 cygwin32_posix_to_win32_path_list (epath, win32epath);
7484 for (num_dirs = 1, startp = epath; *startp; startp++)
7486 if (*startp == PATH_SEPARATOR)
7492 = (struct default_include *) dmalloc ((num_dirs
7493 * sizeof (struct default_include))
7494 + sizeof (include_defaults_array));
7497 startp = endp = epath;
7500 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7501 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7503 strncpy (nstore, startp, size_fromInt (endp - startp));
7506 strcpy (nstore, ".");
7510 nstore[endp-startp] = '\0';
7513 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7514 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7515 include_defaults[num_dirs].cxx_aware = 1;
7522 endp = startp = endp + 1;
7529 /* Put the usual defaults back in at the end. */
7530 memcpy ((char *) &include_defaults[num_dirs],
7531 (char *) include_defaults_array,
7532 sizeof (include_defaults_array));
7535 /*@-branchstate@*/ } /*@=branchstate@*/
7538 cppReader_appendIncludeChain (pfile, opts->before_system,
7539 opts->last_before_system);
7540 opts->first_system_include = opts->before_system;
7542 /* Unless -fnostdinc,
7543 tack on the standard include file dirs to the specified list */
7545 if (!opts->no_standard_includes) {
7546 struct default_include *p = include_defaults;
7547 char *specd_prefix = opts->include_prefix;
7548 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7549 int default_len = 0;
7551 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7552 if (default_prefix != NULL) {
7553 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7554 default_len = strlen (default_prefix) - 7;
7555 default_prefix[default_len] = 0;
7559 /* Search "translated" versions of GNU directories.
7560 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7561 if (specd_prefix != 0 && default_len != 0)
7562 for (p = include_defaults; p->fname != NULL; p++) {
7563 /* Some standard dirs are only for C++. */
7565 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7566 /* Does this dir start with the prefix? */
7567 llassert (default_prefix != NULL);
7569 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
7570 size_fromInt (default_len)))
7572 /* Yes; change prefix and add to search list. */
7573 struct file_name_list *nlist
7574 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7575 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7576 char *str = (char *) dmalloc (this_len + 1);
7577 strcpy (str, specd_prefix);
7578 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7581 nlist->fname = cstring_fromChars (str);
7582 nlist->control_macro = 0;
7583 nlist->c_system_include_path = !p->cxx_aware;
7584 nlist->got_name_map = 0;
7586 cppReader_addIncludeChain (pfile, nlist);
7587 if (opts->first_system_include == 0)
7589 opts->first_system_include = nlist;
7595 /* Search ordinary names for GNU include directories. */
7597 for (p = include_defaults; p->fname != NULL; p++)
7599 /* Some standard dirs are only for C++. */
7601 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7603 struct file_name_list *nlist
7604 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7605 nlist->control_macro = 0;
7606 nlist->c_system_include_path = !p->cxx_aware;
7607 nlist->fname = p->fname;
7608 nlist->got_name_map = 0;
7611 cppReader_addIncludeChain (pfile, nlist);
7613 if (opts->first_system_include == 0)
7615 opts->first_system_include = nlist;
7619 sfree (default_prefix);
7622 /* Tack the after_include chain at the end of the include chain. */
7623 cppReader_appendIncludeChain (pfile, opts->after_include,
7624 opts->last_after_include);
7626 if (opts->first_system_include == 0)
7628 opts->first_system_include = opts->after_include;
7631 /* With -v, print the list of dirs to search. */
7632 if (opts->verbose) {
7633 struct file_name_list *p;
7634 fprintf (stderr, "#include \"...\" search starts here:\n");
7636 for (p = opts->include; p != NULL; p = p->next) {
7637 if (p == opts->first_bracket_include)
7638 fprintf (stderr, "#include <...> search starts here:\n");
7640 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7642 fprintf (stderr, "End of search list.\n");
7646 int cppReader_startProcess (cppReader *pfile, cstring fname)
7650 struct cppOptions *opts = CPPOPTIONS (pfile);
7652 fp = cppReader_pushBuffer (pfile, NULL, 0);
7659 if (opts->in_fname == NULL)
7661 opts->in_fname = cstring_makeLiteralTemp ("");
7664 fp->fname = opts->in_fname;
7665 fp->nominal_fname = fp->fname;
7668 /* Copy the entire contents of the main input file into
7669 the stacked input buffer previously allocated for it. */
7671 if (cstring_isEmpty (fname))
7673 fname = cstring_makeLiteralTemp ("");
7676 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7678 cppReader_error (pfile,
7679 message ("Error opening %s for reading: %s",
7680 fname, lldecodeerror (errno)));
7689 if (finclude (pfile, f, fname, 0, NULL))
7691 output_line_command (pfile, 0, same_file);
7697 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7699 return pfile->buffer;
7702 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7704 llassert (pfile->buffer != NULL);
7705 return pfile->buffer;
7708 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7710 llassert (buf->buf != NULL);
7711 return (buf->buf + buf->line_base);
7714 int cpplib_bufPeek (cppBuffer *buf)
7716 if (buf->cur == NULL || buf->rlimit == NULL) {
7720 if (buf->cur < buf->rlimit) {
7727 bool cppBuffer_isMacro (cppBuffer *buf)
7731 return (buf->cleanup == cppReader_macroCleanup);
7738 ** Returns true if the macro should be checked, false
7739 ** if it should be expanded normally.
7742 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7743 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7744 static bool expectiter = FALSE; /* preceeded by @iter@ */
7745 static bool expectenditer = FALSE; /* second after @iter@ */
7746 static bool expectfunction = FALSE; /* preceeded by @function@ */
7747 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7748 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7750 static void cpp_setLocation (cppReader *pfile)
7755 if (pfile->buffer != NULL)
7757 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7759 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7761 DPRINTF (("Looking up: %s", fname));
7763 if (fileTable_exists (context_fileTable (), fname))
7765 fid = fileTable_lookup (context_fileTable (), fname);
7769 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7771 fid = fileTable_lookup (context_fileTable (),
7772 cppReader_getBufferSafe (pfile)->fname);
7777 fid = fileTable_lookup (context_fileTable (),
7778 cppReader_getBufferSafe (pfile)->fname);
7781 line = cppReader_getBufferSafe (pfile)->lineno;
7782 fileloc_free (g_currentloc);
7784 if (fileId_isValid (fid))
7786 g_currentloc = fileloc_create (fid, line, 1);
7790 g_currentloc = fileloc_createBuiltin ();
7795 fileloc_free (g_currentloc);
7796 g_currentloc = fileloc_createBuiltin ();
7800 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7802 bool checkmacro = FALSE;
7803 bool hasParams = FALSE;
7804 bool noexpand = FALSE;
7808 cpp_setLocation (pfile);
7810 DPRINTF (("Should check macro? %s", p));
7812 if (expectiter || expectconstant || expectenditer)
7817 expectenditer = TRUE;
7822 expectconstant = FALSE;
7823 expectenditer = FALSE;
7826 if (notfunction || notparseable)
7828 notfunction = FALSE;
7829 notparseable = FALSE;
7838 llassert (*p == '#');
7841 while (*p == ' ' || *p == '\t')
7846 llassert (*p == 'd'); /* define starts */
7850 while (*p == ' ' || *p == '\t')
7855 sname = cstring_fromChars (p);
7856 DPRINTF (("Check macro: %s", sname));
7858 while (((c = *p) != ' ')
7859 && c != '\0' && c != '('
7860 && c != '\t' && c != '\\' && c != '\n'
7866 hasParams = (c == '(');
7871 notparseable = FALSE;
7873 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7875 DPRINTF (("Clear notfunction"));
7876 notfunction = FALSE;
7891 if (usymtab_existsReal (sname))
7893 uentry ue = usymtab_lookup (sname);
7895 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7897 if (fileloc_isPreproc (uentry_whereLast (ue)))
7903 if (uentry_isSpecified (ue))
7905 checkmacro = context_getFlag (FLG_SPECMACROS);
7911 checkmacro = context_getFlag (FLG_LIBMACROS)
7912 || context_getFlag (FLG_FCNMACROS);
7920 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7922 if (fileloc_isSystemFile (g_currentloc)
7923 && context_getFlag (FLG_SYSTEMDIREXPAND))
7925 ; /* don't check this macro */
7926 DPRINTF (("Don't check 1"));
7934 DPRINTF (("Has params..."));
7936 if (context_getFlag (FLG_FCNMACROS))
7938 if (usymtab_exists (sname))
7941 ** only get here is macro is redefined
7942 ** error reported elsewhere
7945 DPRINTF (("It exists!"));
7950 ** We make it a forward function, since it might be declared elsewhere.
7951 ** After all headers have been processed, we should check the forward
7955 fileloc loc = fileloc_makePreproc (g_currentloc);
7957 /* the line is off-by-one, since the newline was already read */
7962 expectfunction = FALSE;
7965 le = uentry_makeForwardFunction (sname,
7966 typeId_invalid, loc);
7972 /* Do not define here! */
7974 (void) usymtab_addEntry (le);
7978 DPRINTF (("Check: TRUE"));
7982 DPRINTF (("Flag FCN_MACROS not set!"));
7987 DPRINTF (("No params"));
7989 if (context_getFlag (FLG_CONSTMACROS))
7991 bool nocontent = FALSE;
8004 ** Check if there is nothing after the define.
8007 while ((*rest) != '\0' && isspace (*rest))
8014 nocontent = TRUE; /* empty macro, don't check */
8019 if (usymtab_exists (sname))
8025 fileloc loc = fileloc_makePreproc (g_currentloc);
8026 DPRINTF (("Make constant: %s", sname));
8027 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
8028 (void) usymtab_addEntry (le);
8031 checkmacro = !nocontent;
8036 if (checkmacro && usymtab_existsType (sname))
8038 DPRINTF (("Making false..."));
8040 ppllerror (message ("Specified type implemented as macro: %s", sname));
8050 if (usymtab_exists (sname))
8052 uentry ue = usymtab_lookupExpose (sname);
8053 fileloc tloc = fileloc_makePreproc (g_currentloc);
8055 uentry_setDefined (ue, tloc);
8056 fileloc_free (tloc);
8057 uentry_setUsed (ue, fileloc_undefined);
8061 fileloc tloc = fileloc_makePreproc (g_currentloc);
8062 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8063 DPRINTF (("Make expanded macro: %s", sname));
8064 DPRINTF (("Not in symbol table: %s", sname));
8066 (void) usymtab_addGlobalEntry (ue);
8067 fileloc_free (tloc);
8072 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8076 static enum cpp_token
8077 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8079 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8083 bool eliminateComment = FALSE;
8085 llassert (pbuf->buf != NULL);
8087 start = pbuf->buf + smark->position;
8089 llassert (pbuf->cur != NULL);
8090 len = pbuf->cur - start;
8093 && start[1] == context_getCommentMarkerChar ())
8097 char *scomment = start + 2;
8098 char savec = start[len];
8100 cpp_setLocation (pfile);
8101 loc = fileloc_copy (g_currentloc);
8103 start[0] = BEFORE_COMMENT_MARKER[0];
8104 start[1] = BEFORE_COMMENT_MARKER[1];
8106 llassert (start[len - 2] == '*');
8107 start[len - 2] = AFTER_COMMENT_MARKER[0];
8109 llassert (start[len - 1] == '/');
8110 start[len - 1] = AFTER_COMMENT_MARKER[1];
8112 cpplib_reserve(pfile, size_fromInt (1 + len));
8113 cppReader_putCharQ (pfile, c);
8115 cpp_setLocation (pfile);
8119 if (mstring_containsString (scomment, "/*"))
8121 (void) cppoptgenerror
8123 message ("Comment starts inside syntactic comment: %s",
8124 cstring_fromChars (scomment)),
8130 if (mstring_equalPrefix (scomment, "ignore"))
8132 if (!context_getFlag (FLG_NOCOMMENTS))
8134 context_enterSuppressRegion (loc);
8137 else if (mstring_equalPrefix (scomment, "end"))
8139 if (!context_getFlag (FLG_NOCOMMENTS))
8141 context_exitSuppressRegion (loc);
8144 else if (mstring_equalPrefix (scomment, "notparseable"))
8146 notparseable = TRUE;
8148 eliminateComment = TRUE;
8150 else if (mstring_equalPrefix (scomment, "notfunction"))
8154 eliminateComment = TRUE;
8156 else if (mstring_equalPrefix (scomment, "iter"))
8160 else if (mstring_equalPrefix (scomment, "function"))
8162 expectfunction = TRUE;
8164 else if (mstring_equalPrefix (scomment, "constant"))
8166 expectconstant = TRUE;
8170 char sChar = *scomment;
8176 char *rest = scomment + 1;
8178 if (mstring_equalPrefix (rest, "commentchar"))
8180 eliminateComment = TRUE;
8184 ppllerror (cstring_makeLiteral
8185 ("Cannot restore commentchar"));
8189 char *next = scomment + 12; /* strlen commentchar = 12 */
8191 if (*next != ' ' && *next != '\t' && *next != '\n')
8195 ("Syntactic commentchar comment is not followed by a "
8196 "whitespace character: %c",
8201 char cchar = *(next + 1);
8206 (cstring_makeLiteral
8207 ("Cannot set commentchar to NUL"));
8211 context_setCommentMarkerChar (cchar);
8212 /* setComment = TRUE; */
8217 else if (mstring_equalPrefix (scomment, "nestcomment"))
8219 /* fix from Mike Miller <MikeM@xata.com> */
8220 context_fileSetFlag (FLG_NESTCOMMENT,
8221 ynm_fromCodeChar (sChar),
8224 else if (mstring_equalPrefix (rest, "namechecks"))
8226 context_fileSetFlag (FLG_NAMECHECKS,
8227 ynm_fromCodeChar (sChar),
8230 else if (mstring_equalPrefix (rest, "macroredef"))
8232 context_fileSetFlag (FLG_MACROREDEF,
8233 ynm_fromCodeChar (sChar),
8236 else if (mstring_equalPrefix (rest, "usevarargs"))
8238 context_fileSetFlag (FLG_USEVARARGS,
8239 ynm_fromCodeChar (sChar),
8242 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8244 context_fileSetFlag (FLG_MACRONEXTLINE,
8245 ynm_fromCodeChar (sChar),
8248 else if (mstring_equalPrefix (rest, "allmacros")
8249 || mstring_equalPrefix (rest, "fcnmacros")
8250 || mstring_equalPrefix (rest, "constmacros"))
8254 if (mstring_equalPrefix (rest, "allmacros"))
8258 else if (mstring_equalPrefix (rest, "fcnmacros"))
8264 llassert (mstring_equalPrefix (rest, "constmacros"));
8265 fl = FLG_CONSTMACROS;
8268 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8269 notfunction = FALSE;
8282 if (eliminateComment)
8287 /* Replaces comment char's in start with spaces */
8289 for (i = 2; i < len - 2; i++)
8291 if (start[i] == BEFORE_COMMENT_MARKER[0]
8292 || start[i] == BEFORE_COMMENT_MARKER[1]
8293 || start[i] == context_getCommentMarkerChar ())
8299 cppReader_putStrN (pfile, start, size_fromInt (len));
8300 parseClearMark (smark);
8310 ** Output the comment as all spaces so line/column
8311 ** in output file is still correct.
8315 cstring lintcomment = cstring_undefined;
8317 if (context_getFlag (FLG_LINTCOMMENTS))
8319 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8321 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8323 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8325 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8327 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8329 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8331 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8333 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8335 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8337 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8341 lintcomment = cstring_undefined;
8346 lintcomment = cstring_undefined;
8349 if (cstring_isDefined (lintcomment))
8351 c = BEFORE_COMMENT_MARKER[0];
8352 start[0] = BEFORE_COMMENT_MARKER[1];
8354 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8356 for (i = 1; i < len - 2; i++)
8358 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8361 start[len - 2] = AFTER_COMMENT_MARKER[0];
8362 start[len - 1] = AFTER_COMMENT_MARKER[1];
8366 /* Replaces char's in start with spaces */
8367 for (i = 0; i < len; i++)
8371 && start[i + 1] == '*') {
8372 (void) cppoptgenerror
8374 message ("Comment starts inside comment"),
8378 if (start[i] != '\n')
8385 cpplib_reserve (pfile, size_fromInt (1 + len));
8386 cppReader_putCharQ (pfile, c);
8387 cppReader_putStrN (pfile, start, size_fromInt (len));
8388 parseClearMark (smark);
8394 static int cpp_openIncludeFile (char *filename)
8396 int res = open (filename, O_RDONLY, 0666);
8398 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8401 if (!fileTable_exists (context_fileTable (),
8402 cstring_fromChars (filename)))
8404 if (fileloc_isXHFile (g_currentloc))
8407 ** Files includes by XH files are also XH files
8410 (void) fileTable_addXHFile (context_fileTable (),
8411 cstring_fromChars (filename));
8415 (void) fileTable_addHeaderFile (context_fileTable (),
8416 cstring_fromChars (filename));
8421 DPRINTF (("File already exists: %s", filename));
8428 static bool cpp_skipIncludeFile (cstring fname)
8430 if (context_isSystemDir (fname))
8432 DPRINTF (("System dir: %s", fname));
8434 if (lcllib_isSkipHeader (fname))
8436 DPRINTF (("Skip include TRUE: %s", fname));
8440 if (context_getFlag (FLG_SKIPSYSHEADERS))
8442 DPRINTF (("Skip include TRUE: %s", fname));
8447 if (context_getFlag (FLG_SINGLEINCLUDE))
8449 fname = removePreDirs (fname);
8451 # if defined (WIN32) || defined (OS2)
8452 cstring_replaceAll (fname, '\\', '/');
8455 if (fileTable_exists (context_fileTable (), fname))
8457 DPRINTF (("Skip include TRUE: %s", fname));
8462 DPRINTF (("Skip include FALSE: %s", fname));
8466 static int cpp_peekN (cppReader *pfile, int n)
8468 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8470 llassert (buf->cur != NULL);
8472 return (buf->rlimit - buf->cur >= (n)
8477 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8482 void cppBuffer_forward (cppBuffer *buf, int n)
8484 llassert (buf->cur != NULL);