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));
2772 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2777 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2779 DPRINTF (("Macro: %s / %s",
2780 cstring_copyLength (mdef.symnam, mdef.symlen),
2781 bool_unparse (noExpand)));
2783 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2787 /* Redefining a precompiled key is ok. */
2788 if (hp->type == T_PCSTRING)
2790 /* Redefining a macro is ok if the definitions are the same. */
2791 else if (hp->type == T_MACRO)
2792 ok = !compare_defs (mdef.defn, hp->value.defn);
2793 /* Redefining a constant is ok with -D. */
2794 else if (hp->type == T_CONST)
2795 ok = !CPPOPTIONS (pfile)->done_initializing;
2800 /* Print the warning if it's not ok. */
2804 ** If we are passing through #define and #undef directives, do
2805 ** that for this re-definition now.
2808 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2810 /* llassert (keyword != NULL); */
2811 pass_thru_directive (buf, limit, pfile, keyword);
2814 cpp_setLocation (pfile);
2816 if (hp->type == T_MACRO)
2818 if (hp->value.defn->noExpand)
2820 ; /* error will be reported checking macros */
2826 message ("Macro %q already defined",
2827 cstring_copyLength (mdef.symnam, mdef.symlen)),
2828 message ("%q: Previous definition of %q",
2829 fileloc_unparseRaw (hp->value.defn->file,
2830 (int) hp->value.defn->line),
2831 cstring_copyLength (mdef.symnam, mdef.symlen)));
2836 genppllerror (FLG_MACROREDEF,
2837 message ("Macro %q already defined",
2838 cstring_copyLength (mdef.symnam,
2844 /* Replace the old definition. */
2846 hp->value.defn = mdef.defn;
2851 ** If we are passing through #define and #undef directives, do
2852 ** that for this new definition now.
2857 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2859 pass_thru_directive (buf, limit, pfile, keyword);
2862 DPRINTF (("Define macro: %s / %d",
2863 mdef.symnam, mdef.defn->noExpand));
2865 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2867 } /*@=branchstate@*/
2877 do_define (cppReader *pfile, struct directive *keyword,
2878 /*@exposed@*/ char *buf, char *limit)
2880 DPRINTF (("Regular do define"));
2881 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2884 /* This structure represents one parsed argument in a macro call.
2885 `raw' points to the argument text as written (`raw_length' is its length).
2886 `expanded' points to the argument's macro-expansion
2887 (its length is `expand_length').
2888 `stringified_length' is the length the argument would have
2890 `use_count' is the number of times this macro arg is substituted
2891 into the macro. If the actual use count exceeds 10,
2892 the value stored is 10. */
2894 /* raw and expanded are relative to ARG_BASE */
2896 #define ARG_BASE ((pfile)->token_buffer)
2899 /* Strings relative to pfile->token_buffer */
2905 int stringified_length;
2910 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2911 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2912 as the new input buffer.
2913 Return the new buffer, or NULL on failure. */
2915 /*@null@*/ /*@exposed@*/ cppBuffer *
2916 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2918 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2920 if (buf == pfile->buffer_stack)
2922 cppReader_fatalError
2924 message ("%s: macro or `#include' recursion too deep",
2925 (buf->fname != NULL)
2927 : cstring_makeLiteral ("<no name>")));
2928 sfreeEventually (buffer);
2932 llassert (buf != NULL);
2935 memset ((char *) buf, 0, sizeof (*buf));
2936 CPPBUFFER (pfile) = buf;
2938 buf->if_stack = pfile->if_stack;
2939 buf->cleanup = cppReader_nullCleanup;
2940 buf->underflow = cppReader_nullUnderflow;
2942 buf->cur = buf->buf;
2946 buf->alimit = buf->rlimit = buffer + length;
2950 buf->alimit = buf->rlimit = NULL;
2957 cppReader_popBuffer (cppReader *pfile)
2959 cppBuffer *buf = CPPBUFFER (pfile);
2961 llassert (buf != NULL);
2963 (void) (*buf->cleanup) (buf, pfile);
2964 return ++CPPBUFFER (pfile);
2967 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2968 Pop the buffer when done. */
2971 cppReader_scanBuffer (cppReader *pfile)
2973 cppBuffer *buffer = CPPBUFFER (pfile);
2976 enum cpp_token token;
2978 token = cpplib_getToken (pfile);
2980 if (token == CPP_EOF) /* Should not happen ... */
2985 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2987 (void) cppReader_popBuffer (pfile);
2995 * Rescan a string (which may have escape marks) into pfile's buffer.
2996 * Place the result in pfile->token_buffer.
2998 * The input is copied before it is scanned, so it is safe to pass
2999 * it something from the token_buffer that will get overwritten
3000 * (because it follows cpplib_getWritten). This is used by do_include.
3004 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3006 register cppBuffer *ip;
3007 char *limit = buf + length;
3008 char *buf1, *p1, *p2;
3010 /* evans - 2001-08-26
3011 ** length is unsigned - this doesn't make sense
3017 /* Set up the input on the input stack. */
3019 buf1 = (char *) dmalloc (length + 1);
3029 buf1[length] = '\0';
3031 ip = cppReader_pushBuffer (pfile, buf1, length);
3036 ip->has_escapes = TRUE;
3038 /* Scan the input, create the output. */
3039 cppReader_scanBuffer (pfile);
3041 cppReader_nullTerminate (pfile);
3045 adjust_position (char *buf, char *limit, int *linep, int *colp)
3051 (*linep)++, (*colp) = 1;
3057 /* Move line_base forward, updating lineno and colno. */
3060 update_position (cppBuffer *pbuf)
3063 char *new_pos = pbuf->cur;
3064 register struct parse_marker *mark;
3066 llassert (pbuf->buf != NULL);
3067 old_pos = pbuf->buf + pbuf->line_base;
3069 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3071 if (pbuf->buf + mark->position < new_pos)
3072 new_pos = pbuf->buf + mark->position;
3074 pbuf->line_base += new_pos - old_pos;
3076 llassert (old_pos != NULL);
3077 llassert (new_pos != NULL);
3079 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3083 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3084 /*@null@*/ /*@out@*/ int *colp)
3092 } /*@=branchstate@*/
3096 *linep = pbuf->lineno;
3097 *colp = pbuf->colno;
3099 llassert (pbuf->buf != NULL);
3100 llassert (pbuf->cur != NULL);
3102 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3111 /* Return the cppBuffer that corresponds to a file (not a macro). */
3113 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3115 cppBuffer *ip = cppReader_getBuffer (pfile);
3118 ip != NULL && ip != cppReader_nullBuffer (pfile);
3119 ip = cppBuffer_prevBuffer (ip))
3121 if (ip->fname != NULL)
3131 count_newlines (char *buf, char *limit)
3133 register long count = 0;
3145 * write out a #line command, for instance, after an #include file.
3146 * If CONDITIONAL is nonzero, we can omit the #line if it would
3147 * appear to be a no-op, and we can output a few newlines instead
3148 * if we want to increase the line number by a small amount.
3149 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3153 output_line_command (cppReader *pfile, bool conditional,
3154 enum file_change_code file_change)
3157 cppBuffer *ip = CPPBUFFER (pfile);
3160 llassert (ip != NULL);
3162 if (ip->fname == NULL)
3165 update_position (ip);
3167 if (CPPOPTIONS (pfile)->no_line_commands
3168 || CPPOPTIONS (pfile)->no_output)
3171 buf = CPPBUFFER (pfile);
3173 llassert (buf != NULL);
3178 llassert (ip->cur != NULL);
3180 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3182 if (CPPOPTIONS (pfile)->no_line_commands)
3186 if (line == pfile->lineno)
3189 /* If the inherited line number is a little too small,
3190 output some newlines instead of a #line command. */
3192 if (line > pfile->lineno && line < pfile->lineno + 8)
3194 cpplib_reserve (pfile, 20);
3195 while (line > pfile->lineno)
3197 cppReader_putCharQ (pfile, '\n');
3205 cpplib_reserve (pfile,
3206 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
3209 #ifdef OUTPUT_LINE_COMMANDS
3210 static char sharp_line[] = "#line ";
3212 static char sharp_line[] = "# ";
3214 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3217 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3218 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3220 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3222 if (file_change != same_file) {
3223 cppReader_putCharQ (pfile, ' ');
3224 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3226 /* Tell cc1 if following text comes from a system header file. */
3227 if (ip->system_header_p != '\0') {
3228 cppReader_putCharQ (pfile, ' ');
3229 cppReader_putCharQ (pfile, '3');
3231 #ifndef NO_IMPLICIT_EXTERN_C
3232 /* Tell cc1plus if following text should be treated as C. */
3233 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3234 cppReader_putCharQ (pfile, ' ');
3235 cppReader_putCharQ (pfile, '4');
3238 cppReader_putCharQ (pfile, '\n');
3239 pfile->lineno = line;
3244 * Parse a macro argument and append the info on PFILE's token_buffer.
3245 * REST_ARGS means to absorb the rest of the args.
3246 * Return nonzero to indicate a syntax error.
3249 static enum cpp_token
3250 macarg (cppReader *pfile, int rest_args)
3253 enum cpp_token token;
3254 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3255 bool oldexpand = pfile->no_macro_expand;
3256 CPPOPTIONS (pfile)->put_out_comments = 1;
3258 /* Try to parse as much of the argument as exists at this
3259 input stack level. */
3261 pfile->no_macro_expand = TRUE;
3265 token = cpplib_getToken (pfile);
3272 /* If we've hit end of file, it's an error (reported by caller).
3273 Ditto if it's the end of cpp_expand_to_buffer text.
3274 If we've hit end of macro, just continue. */
3275 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3277 /*@switchbreak@*/ break;
3280 /*@switchbreak@*/ break;
3284 /*@switchbreak@*/ break;
3286 /* if we've returned to lowest level and
3287 we aren't absorbing all args */
3288 if (paren == 0 && rest_args == 0)
3290 /*@switchbreak@*/ break;
3292 /* Remove ',' or ')' from argument buffer. */
3293 cppReader_adjustWritten (pfile, -1);
3301 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3302 pfile->no_macro_expand = oldexpand;
3308 /* Turn newlines to spaces in the string of length LENGTH at START,
3309 except inside of string constants.
3310 The string is copied into itself with its beginning staying fixed. */
3313 change_newlines (char *start, int length)
3317 register char *limit;
3321 limit = start + length;
3324 while (ibp < limit) {
3325 *obp++ = c = *ibp++;
3330 /* Notice and skip strings, so that we don't delete newlines in them. */
3333 while (ibp < limit) {
3334 *obp++ = c = *ibp++;
3336 /*@innerbreak@*/ break;
3337 if (c == '\n' && quotec == '\'')
3338 /*@innerbreak@*/ break;
3341 /*@switchbreak@*/ break;
3348 static /*@observer@*/ struct tm *
3349 timestamp (/*@returned@*/ cppReader *pfile)
3351 if (pfile->timebuf == NULL)
3353 time_t t = time ((time_t *) 0);
3354 pfile->timebuf = localtime (&t);
3357 llassert (pfile->timebuf != NULL);
3359 return pfile->timebuf;
3362 static ob_mstring monthnames[] = {
3363 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3364 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3368 * expand things like __FILE__. Place the expansion into the output
3369 * buffer *without* rescanning.
3373 special_symbol (hashNode hp, cppReader *pfile)
3375 cstring buf = cstring_undefined;
3381 int paren = 0; /* For special `defined' keyword */
3383 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3385 if (ip == cppReader_nullBuffer (pfile))
3387 cppReader_errorLit (pfile,
3388 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3389 return; /* the show must go on */
3392 if (ip != NULL && ip->fname != NULL)
3404 if (hp->type == T_BASE_FILE)
3406 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3408 ip = cppBuffer_prevBuffer (ip);
3412 llassert (ip != NULL);
3413 string = cstring_toCharsSafe (ip->nominal_fname);
3420 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3421 quote_string (pfile, string);
3425 case T_INCLUDE_LEVEL:
3427 ip = cppReader_getBuffer (pfile);
3429 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3430 ip = cppBuffer_prevBuffer (ip))
3432 if (ip != NULL && ip->fname != NULL)
3438 buf = message ("%d", true_indepth - 1);
3442 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3445 #ifndef NO_BUILTIN_SIZE_TYPE
3447 buf = cstring_makeLiteral (SIZE_TYPE);
3451 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3452 case T_PTRDIFF_TYPE:
3453 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3458 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3461 case T_USER_LABEL_PREFIX_TYPE:
3462 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3465 case T_REGISTER_PREFIX_TYPE:
3466 buf = cstring_makeLiteral (REGISTER_PREFIX);
3470 buf = message ("%d", hp->value.ival);
3477 int line = ip->lineno;
3478 int col = ip->colno;
3480 llassert (ip->cur != NULL);
3481 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3483 buf = message ("%d", (int) line);
3495 char *sbuf = (char *) dmalloc (20);
3496 timebuf = timestamp (pfile);
3497 if (hp->type == T_DATE)
3499 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3500 timebuf->tm_mday, timebuf->tm_year + 1900);
3504 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3508 buf = cstring_fromCharsNew (sbuf);
3513 case T_SPEC_DEFINED:
3514 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3515 ip = cppReader_getBuffer (pfile);
3516 llassert (ip != NULL);
3517 llassert (ip->cur != NULL);
3518 SKIP_WHITE_SPACE (ip->cur);
3520 if (*ip->cur == '(')
3523 ip->cur++; /* Skip over the paren */
3524 SKIP_WHITE_SPACE (ip->cur);
3527 if (!is_idstart[(int) *ip->cur])
3529 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3532 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3535 buf = cstring_makeLiteral (" 1 ");
3538 while (is_idchar[(int) *ip->cur])
3543 SKIP_WHITE_SPACE (ip->cur);
3547 if (*ip->cur != ')')
3555 cppReader_errorLit (pfile,
3556 cstring_makeLiteralTemp ("`defined' without an identifier"));
3560 cpp_setLocation (pfile);
3561 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3564 len = cstring_length (buf);
3566 cpplib_reserve (pfile, len + 1);
3567 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3568 cppReader_nullTerminateQ (pfile);
3574 /* Write out a #define command for the special named MACRO_NAME
3575 to PFILE's token_buffer. */
3578 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3580 static char define_directive[] = "#define ";
3581 size_t macro_name_length = strlen (macro_name);
3582 output_line_command (pfile, 0, same_file);
3583 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3584 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3585 cppReader_putStrN (pfile, macro_name, macro_name_length);
3586 cppReader_putCharQ (pfile, ' ');
3587 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3588 cppReader_putChar (pfile, '\n');
3591 /* Initialize the built-in macros. */
3594 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3595 int len, enum node_type type,
3596 int ivalue, /*@null@*/ /*@only@*/ char *value,
3599 cstring sname = cstring_fromCharsNew (name);
3601 llassert (usymtab_inGlobalScope ());
3604 ** Be careful here: this is done before the ctype table has
3605 ** been initialized.
3608 if (!usymtab_exists (sname))
3610 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3612 if (ctype_equal (ctyp, ctype_string))
3614 qualList ql = qualList_new ();
3615 ql = qualList_add (ql, qual_createObserver ());
3616 uentry_reflectQualifiers (ue, ql);
3620 usymtab_addGlobalEntry (ue);
3627 (void) cpphash_install (name, len, type, ivalue, value, hash);
3628 cstring_free (sname);
3632 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3633 int len, enum node_type type,
3635 /*@only@*/ /*@null@*/ char *value, int hash)
3637 cstring sname = cstring_fromChars (name);
3638 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3640 llassert (usymtab_inGlobalScope ());
3642 if (!usymtab_existsTypeEither (sname))
3644 uentry ue = uentry_makeDatatype (sname, ctyp,
3646 fileloc_createBuiltin ());
3647 llassert (!usymtab_existsEither (sname));
3648 usymtab_addGlobalEntry (ue);
3651 (void) cpphash_install (name, len, type, ivalue, value, hash);
3655 initialize_builtins (cppReader *pfile)
3657 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3658 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3659 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3660 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3661 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3662 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3663 #ifndef NO_BUILTIN_SIZE_TYPE
3664 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3666 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3667 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3669 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3670 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3671 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3672 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3675 ** No, don't define __STDC__
3678 if (!cppReader_isTraditional (pfile))
3680 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3687 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3691 ** This is supplied using a -D by the compiler driver
3692 ** so that it is present only when truly compiling with GNU C.
3695 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3697 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3698 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3700 /*drl 1/9/2001/ try to define the right symbol for the architecture
3701 We use autoconf to determine the target cpu
3703 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3705 /*drl 1/2/2002 set some flags based on uname
3706 I'd like to be able to do this with autoconf macro instead...
3709 /*Thanks to Nelson Beebe for suggesting possible values for these */
3711 if (! strcmp (UNAME, "Linux"))
3714 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3718 else if(! strcmp (UNAME, "Darwin"))
3720 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3721 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3723 else if(! strcmp (UNAME, "HP-UX"))
3725 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3726 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3727 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3728 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3729 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3730 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3731 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3732 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3733 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3734 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3735 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3736 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3737 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3738 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3739 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3740 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3741 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3742 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3743 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3744 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3745 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3746 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3747 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3749 else if(! strcmp (UNAME, "IRIX64"))
3751 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3752 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3753 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3754 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3755 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3756 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3757 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3758 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3759 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3760 /*_MIPS_SIM=_ABIN32*/
3761 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3762 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3763 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3764 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3765 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3766 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3767 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3769 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3770 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3771 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3772 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3773 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3774 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3775 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3779 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3780 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3781 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3782 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3783 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3786 else if(! strcmp (UNAME, "OSF1"))
3788 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 else if (!strcmp (UNAME, "Rhapsody"))
3792 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3793 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3796 else if (!strcmp (UNAME, "SunOS"))
3798 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3799 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3800 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3801 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3806 types which we have not explictedly handled.
3807 AIX, FreeBSD, IRIX, Mach
3812 if (CPPOPTIONS (pfile)->debug_output)
3814 dump_special_to_buffer (pfile, "__BASE_FILE__");
3815 dump_special_to_buffer (pfile, "__VERSION__");
3816 #ifndef NO_BUILTIN_SIZE_TYPE
3817 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3819 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3820 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3822 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3823 dump_special_to_buffer (pfile, "__DATE__");
3824 dump_special_to_buffer (pfile, "__TIME__");
3825 if (!cppReader_isTraditional (pfile))
3826 dump_special_to_buffer (pfile, "__STDC__");
3831 /* Return 1 iff a token ending in C1 followed directly by a token C2
3832 could cause mis-tokenization. */
3835 unsafe_chars (char c1, char c2)
3840 if (c2 == c1 || c2 == '=')
3844 case '0': case '1': case '2': case '3': case '4':
3845 case '5': case '6': case '7': case '8': case '9':
3846 case 'e': case 'E': case 'p': case 'P':
3847 if (c2 == '-' || c2 == '+')
3848 return 1; /* could extend a pre-processing number */
3851 if (c2 == '\'' || c2 == '\"')
3852 return 1; /* Could turn into L"xxx" or L'xxx'. */
3856 case 'a': case 'b': case 'c': case 'd': case 'f':
3857 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3858 case 'm': case 'n': case 'o': case 'q': case 'r':
3859 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3861 case 'A': case 'B': case 'C': case 'D': case 'F':
3862 case 'G': case 'H': case 'I': case 'J': case 'K':
3863 case 'M': case 'N': case 'O': case 'Q': case 'R':
3864 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3866 /* We're in the middle of either a name or a pre-processing number. */
3867 return (is_idchar[(int) c2] || c2 == '.');
3868 case '<': case '>': case '!': case '%': case '#': case ':':
3869 case '^': case '&': case '|': case '*': case '/': case '=':
3870 return (c2 == c1 || c2 == '=');
3875 /* Expand a macro call.
3876 HP points to the symbol that is the macro being called.
3877 Put the result of expansion onto the input stack
3878 so that subsequent input by our caller will use it.
3880 If macro wants arguments, caller has already verified that
3881 an argument list follows; arguments come from the input stack. */
3884 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3887 DEFINITION *defn = hp->value.defn;
3895 size_t old_written = cpplib_getWritten (pfile);
3899 struct argdata *args = NULL;
3901 pfile->output_escapes++;
3902 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3903 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3905 nargs = defn->nargs;
3909 enum cpp_token token = CPP_EOF;
3911 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3913 for (i = 0; i < nargs; i++)
3915 args[i].expanded = 0;
3917 args[i].raw_length = 0;
3918 args[i].expand_length = args[i].stringified_length = -1;
3919 args[i].use_count = 0;
3923 ** Parse all the macro args that are supplied. I counts them.
3924 ** The first NARGS args are stored in ARGS.
3925 ** The rest are discarded. If rest_args is set then we assume
3926 ** macarg absorbed the rest of the args.
3932 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3940 if (i < nargs || (nargs == 0 && i == 0))
3942 /* if we are working on last arg which absorbs rest of args... */
3943 if (i == nargs - 1 && defn->rest_args)
3948 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3949 token = macarg (pfile, rest_args);
3950 args[i].raw_length = cpplib_getWritten (pfile) - args[i].raw;
3951 args[i].newlines = FALSE; /* FIXME */
3955 token = macarg (pfile, 0);
3958 if (token == CPP_EOF || token == CPP_POP)
3960 cppReader_errorWithLine (pfile, start_line, start_column,
3961 cstring_fromCharsNew ("unterminated macro call"));
3966 } while (token == CPP_COMMA);
3968 /* If we got one arg but it was just whitespace, call that 0 args. */
3976 bp = ARG_BASE + args[0].raw;
3977 lim = bp + args[0].raw_length;
3979 /* cpp.texi says for foo ( ) we provide one argument.
3980 However, if foo wants just 0 arguments, treat this as 0. */
3984 while (bp != lim && is_space[(int) *bp])
3994 /* Don't output an error message if we have already output one for
3995 a parse error above. */
3998 if (nargs == 0 && i > 0)
4000 cppReader_error (pfile,
4001 message ("arguments given to macro `%s'", hp->name));
4005 /* traditional C allows foo() if foo wants one argument. */
4006 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4010 /* the rest args token is allowed to absorb 0 tokens */
4011 else if (i == nargs - 1 && defn->rest_args)
4014 cppReader_error (pfile,
4015 message ("macro `%s' used without args", hp->name));
4017 cppReader_error (pfile,
4018 message ("macro `%s' used with just one arg", hp->name));
4021 cppReader_error (pfile,
4022 message ("macro `%s' used with only %d args",
4028 cppReader_error (pfile,
4029 message ("macro `%s' used with too many (%d) args", hp->name, i));
4038 ** If the agrument list was multiple lines, need to insert new lines to keep line
4039 ** numbers accurate.
4042 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4043 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4045 /* If macro wants zero args, we parsed the arglist for checking only.
4046 Read directly from the macro definition. */
4050 xbuf = defn->expansion;
4051 xbuf_len = defn->length;
4055 char *exp = defn->expansion;
4056 int offset; /* offset in expansion,
4057 copied a piece at a time */
4058 size_t totlen; /* total amount of exp buffer filled so far */
4060 register struct reflist *ap, *last_ap;
4062 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4064 /* Macro really takes args. Compute the expansion of this call. */
4066 /* Compute length in characters of the macro's expansion.
4067 Also count number of times each arg is used. */
4068 xbuf_len = defn->length;
4070 llassert (args != NULL);
4072 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4076 struct argdata *arg = &args[ap->argno];
4078 /* Stringify it it hasn't already been */
4081 if (arg->stringified_length < 0)
4083 int arglen = arg->raw_length;
4084 bool escaped = FALSE;
4085 char in_string = '\0';
4088 /* Initially need_space is -1. Otherwise, 1 means the
4089 previous character was a space, but we suppressed it;
4090 0 means the previous character was a non-space. */
4091 int need_space = -1;
4094 arg->stringified = cpplib_getWritten (pfile);
4095 if (!cppReader_isTraditional (pfile))
4096 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4097 for (; i < arglen; i++)
4099 c = (ARG_BASE + arg->raw)[i];
4101 if (in_string == '\0')
4103 /* Internal sequences of whitespace are replaced by
4104 one space except within an string or char token.*/
4105 if (is_space[(int) c])
4107 if (cpplib_getWritten (pfile) > arg->stringified
4108 && (cpplib_getPWritten (pfile))[-1] == '@')
4110 /* "@ " escape markers are removed */
4111 cppReader_adjustWritten (pfile, -1);
4112 /*@innercontinue@*/ continue;
4114 if (need_space == 0)
4116 /*@innercontinue@*/ continue;
4118 else if (need_space > 0)
4119 cppReader_putChar (pfile, ' ');
4135 if (in_string != '\0')
4140 else if (c == '\"' || c == '\'')
4150 /* Escape these chars */
4151 if (c == '\"' || (in_string != '\0' && c == '\\'))
4152 cppReader_putChar (pfile, '\\');
4154 cppReader_putChar (pfile, c);
4157 cpplib_reserve (pfile, 4);
4158 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4160 cppReader_adjustWritten (pfile, 4);
4163 if (!cppReader_isTraditional (pfile))
4164 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4165 arg->stringified_length
4166 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4169 xbuf_len += args[ap->argno].stringified_length;
4171 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4173 /* Add 4 for two newline-space markers to prevent
4174 token concatenation. */
4175 assertSet (args); /*@i534 shouldn't need this */
4176 xbuf_len += args[ap->argno].raw_length + 4;
4180 /* We have an ordinary (expanded) occurrence of the arg.
4181 So compute its expansion, if we have not already. */
4183 assertSet (args); /*@i534 shouldn't need this */
4185 if (args[ap->argno].expand_length < 0)
4187 args[ap->argno].expanded = cpplib_getWritten (pfile);
4188 cpp_expand_to_buffer (pfile,
4189 ARG_BASE + args[ap->argno].raw,
4190 size_fromInt (args[ap->argno].raw_length));
4192 args[ap->argno].expand_length
4193 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4196 /* Add 4 for two newline-space markers to prevent
4197 token concatenation. */
4198 xbuf_len += args[ap->argno].expand_length + 4;
4200 if (args[ap->argno].use_count < 10)
4201 args[ap->argno].use_count++;
4204 xbuf = (char *) dmalloc (xbuf_len + 1);
4208 ** Generate in XBUF the complete expansion
4209 ** with arguments substituted in.
4210 ** TOTLEN is the total size generated so far.
4211 ** OFFSET is the index in the definition
4212 ** of where we are copying from.
4218 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4219 last_ap = ap, ap = ap->next)
4221 register struct argdata *arg = &args[ap->argno];
4222 size_t count_before = totlen;
4224 /* Add chars to XBUF. */
4225 for (i = 0; i < ap->nchars; i++, offset++)
4227 xbuf[totlen++] = exp[offset];
4230 /* If followed by an empty rest arg with concatenation,
4231 delete the last run of nonwhite chars. */
4232 if (rest_zero && totlen > count_before
4233 && ((ap->rest_args && ap->raw_before)
4234 || (last_ap != NULL && last_ap->rest_args
4235 && last_ap->raw_after)))
4237 /* Delete final whitespace. */
4238 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4243 /* Delete the nonwhites before them. */
4244 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4250 if (ap->stringify != 0)
4253 memcpy (xbuf + totlen,
4254 ARG_BASE + arg->stringified,
4255 size_fromInt (arg->stringified_length));
4256 totlen += arg->stringified_length;
4258 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4265 p1 = ARG_BASE + arg->raw;
4266 l1 = p1 + arg->raw_length;
4270 while (p1 != l1 && is_space[(int) *p1])
4275 while (p1 != l1 && is_idchar[(int) *p1])
4277 xbuf[totlen++] = *p1++;
4280 /* Delete any no-reexpansion marker that follows
4281 an identifier at the beginning of the argument
4282 if the argument is concatenated with what precedes it. */
4283 if (p1[0] == '@' && p1[1] == '-')
4288 /* Arg is concatenated after: delete trailing whitespace,
4289 whitespace markers, and no-reexpansion markers. */
4292 if (is_space[(int) l1[-1]]) l1--;
4293 else if (l1[-1] == '-')
4296 /* If a `-' is preceded by an odd number of newlines then it
4297 and the last newline are a no-reexpansion marker. */
4298 while (p2 != p1 && p2[-1] == '\n')
4303 if (((l1 - 1 - p2) & 1) != 0)
4309 /*@innerbreak@*/ break;
4314 /*@innerbreak@*/ break;
4319 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4327 expanded = ARG_BASE + arg->expanded;
4329 if (!ap->raw_before && totlen > 0
4330 && (arg->expand_length != 0)
4331 && !cppReader_isTraditional(pfile)
4332 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4334 xbuf[totlen++] = '@';
4335 xbuf[totlen++] = ' ';
4338 memcpy (xbuf + totlen, expanded,
4339 size_fromInt (arg->expand_length));
4340 totlen += arg->expand_length;
4342 if (!ap->raw_after && totlen > 0
4343 && offset < size_toInt (defn->length)
4344 && !cppReader_isTraditional(pfile)
4345 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4347 xbuf[totlen++] = '@';
4348 xbuf[totlen++] = ' ';
4351 /* If a macro argument with newlines is used multiple times,
4352 then only expand the newlines once. This avoids creating
4353 output lines which don't correspond to any input line,
4354 which confuses gdb and gcov. */
4355 if (arg->use_count > 1 && arg->newlines > 0)
4357 /* Don't bother doing change_newlines for subsequent
4361 = change_newlines (expanded, arg->expand_length);
4365 if (totlen > xbuf_len)
4369 /* if there is anything left of the definition
4370 after handling the arg list, copy that in too. */
4372 for (i = offset; i < size_toInt (defn->length); i++)
4374 /* if we've reached the end of the macro */
4377 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4378 && last_ap->raw_after))
4379 xbuf[totlen++] = exp[i];
4382 xbuf[totlen] = '\0';
4386 pfile->output_escapes--;
4388 /* Now put the expansion on the input stack
4389 so our caller will commence reading from it. */
4390 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4392 if (end_line != start_line)
4394 /* xbuf must have enough newlines */
4395 int newlines = end_line - start_line;
4396 int foundnewlines = 0;
4397 char *xbufptr = xbuf;
4399 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4404 if (*xbufptr == '\0')
4410 if (foundnewlines < newlines)
4412 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4414 while (foundnewlines < newlines)
4416 newbuf = cstring_appendChar (newbuf, '\n');
4421 xbuf = cstring_toCharsSafe (newbuf);
4422 xbuf_len = cstring_length (newbuf);
4424 } /*@=branchstate@*/
4427 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4429 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4430 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4432 /* Pop the space we've used in the token_buffer for argument expansion. */
4433 cppReader_setWritten (pfile, old_written);
4435 /* Recursive macro use sometimes works traditionally.
4436 #define foo(x,y) bar (x (y,0), y)
4439 if (!cppReader_isTraditional (pfile))
4440 hp->type = T_DISABLED;
4446 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4447 /*@dependent@*/ hashNode hp)
4449 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4456 mbuf->cleanup = cppReader_macroCleanup;
4458 llassert (mbuf->hnode == NULL);
4461 /* The first chars of the expansion should be a "@ " added by
4462 collect_expansion. This is to prevent accidental token-pasting
4463 between the text preceding the macro invocation, and the macro
4466 We would like to avoid adding unneeded spaces (for the sake of
4467 tools that use cpp, such as imake). In some common cases we can
4468 tell that it is safe to omit the space.
4470 The character before the macro invocation cannot have been an
4471 idchar (or else it would have been pasted with the idchars of
4472 the macro name). Therefore, if the first non-space character
4473 of the expansion is an idchar, we do not need the extra space
4474 to prevent token pasting.
4476 Also, we don't need the extra space if the first char is '(',
4477 or some other (less common) characters. */
4479 if (xbuf[0] == '@' && xbuf[1] == ' '
4480 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4481 || xbuf[2] == '\"'))
4483 llassert (mbuf->cur != NULL);
4489 /* Like cpplib_getToken, except that it does not read past end-of-line.
4490 Also, horizontal space is skipped, and macros are popped. */
4492 static enum cpp_token
4493 get_directive_token (cppReader *pfile)
4497 size_t old_written = cpplib_getWritten (pfile);
4498 enum cpp_token token;
4499 cppSkipHspace (pfile);
4500 if (cppReader_peekC (pfile) == '\n')
4505 token = cpplib_getToken (pfile);
4510 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4515 cppReader_setWritten (pfile, old_written);
4516 /*@switchbreak@*/ break;
4524 /* Handle #include and #import.
4525 This function expects to see "fname" or <fname> on the input.
4527 The input is normally in part of the output_buffer following
4528 cpplib_getWritten, and will get overwritten by output_line_command.
4529 I.e. in input file specification has been popped by cppReader_handleDirective.
4533 do_include (cppReader *pfile, struct directive *keyword,
4534 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4536 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4538 char *fbeg, *fend; /* Beginning and end of fname */
4539 enum cpp_token token;
4541 /* Chain of dirs to search */
4542 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4543 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4544 struct file_name_list *searchptr = NULL;
4545 size_t old_written = cpplib_getWritten (pfile);
4548 int f; /* file number */
4549 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4550 f= -1; /* JF we iz paranoid! */
4552 pfile->parsing_include_directive++;
4553 token = get_directive_token (pfile);
4554 pfile->parsing_include_directive--;
4556 if (token == CPP_STRING)
4558 /* FIXME - check no trailing garbage */
4559 fbeg = pfile->token_buffer + old_written + 1;
4560 fend = cpplib_getPWritten (pfile) - 1;
4561 if (fbeg[-1] == '<')
4564 /* If -I-, start with the first -I dir after the -I-. */
4565 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4566 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4568 /* If -I- was specified, don't search current dir, only spec'd ones. */
4569 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4571 cppBuffer *fp = CPPBUFFER (pfile);
4572 /* We have "filename". Figure out directory this source
4573 file is coming from and put it on the front of the list. */
4575 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4580 llassert (fp != NULL);
4584 if (cstring_isDefined (fp->nominal_fname))
4586 nam = cstring_toCharsSafe (fp->nominal_fname);
4588 /* Found a named file. Figure out dir of the file,
4589 and put it in front of the search list. */
4590 dsp[0].next = search_start;
4594 ep = strrchr (nam, CONNECTCHAR);
4596 ep = strrchr (nam, ']');
4597 if (ep == NULL) ep = strrchr (nam, '>');
4598 if (ep == NULL) ep = strrchr (nam, ':');
4599 if (ep != NULL) ep++;
4609 /*@-onlytrans@*/ /* This looks like a memory leak... */
4610 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4614 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
4615 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
4619 dsp[0].fname = cstring_undefined; /* Current directory */
4622 dsp[0].got_name_map = 0;
4633 else if (token == CPP_NAME)
4636 * Support '#include xyz' like VAX-C to allow for easy use of all the
4637 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4638 * code from case '<' is repeated here) and generates a warning.
4640 cppReader_warning (pfile,
4641 "VAX-C-style include specification found, use '#include <filename.h>' !");
4643 /* If -I-, start with the first -I dir after the -I-. */
4644 if (CPPOPTIONS (pfile)->first_bracket_include)
4645 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4646 fbeg = pfile->token_buffer + old_written;
4647 fend = cpplib_getPWritten (pfile);
4652 cppReader_error (pfile,
4653 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4656 cppReader_setWritten (pfile, old_written);
4657 cppReader_skipRestOfLine (pfile);
4663 token = get_directive_token (pfile);
4664 if (token != CPP_VSPACE)
4666 cppReader_errorLit (pfile,
4667 cstring_makeLiteralTemp ("Junk at end of #include"));
4669 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4671 token = get_directive_token (pfile);
4676 ** For #include_next, skip in the search path
4677 ** past the dir in which the containing file was found.
4682 cppBuffer *fp = CPPBUFFER (pfile);
4684 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4686 llassert (fp != NULL);
4688 if (fp->fname != NULL)
4690 /* fp->dir is null if the containing file was specified with
4691 an absolute file name. In that case, don't skip anything. */
4692 if (fp->dir == SELF_DIR_DUMMY)
4694 search_start = CPPOPTIONS (pfile)->include;
4696 else if (fp->dir != NULL)
4698 search_start = fp->dir->next;
4710 cppReader_setWritten (pfile, old_written);
4712 flen = size_fromInt (fend - fbeg);
4714 DPRINTF (("fbeg: %s", fbeg));
4718 cppReader_error (pfile,
4719 message ("Empty file name in #%s", keyword->name));
4724 ** Allocate this permanently, because it gets stored in the definitions
4728 fname = cstring_undefined;
4730 /* + 2 above for slash and terminating null. */
4731 /* + 2 added for '.h' on VMS (to support '#include filename') */
4733 /* If specified file name is absolute, just open it. */
4735 if (osd_isConnectChar (*fbeg)
4736 # if defined (WIN32) || defined (OS2)
4737 || (*(fbeg + 1) == ':')
4741 fname = cstring_copyLength (fbeg, flen);
4743 if (redundant_include_p (pfile, fname))
4745 cstring_free (fname);
4749 f = open_include_file (pfile, fname, NULL);
4751 if (f == IMPORT_FOUND)
4753 return 0; /* Already included this file */
4758 /* Search directory path, trying to open the file.
4759 Copy each filename tried into FNAME. */
4761 for (searchptr = search_start; searchptr != NULL;
4762 searchptr = searchptr->next)
4764 if (!cstring_isEmpty (searchptr->fname))
4766 /* The empty string in a search path is ignored.
4767 This makes it possible to turn off entirely
4768 a standard piece of the list. */
4769 if (cstring_isEmpty (searchptr->fname))
4772 fname = cstring_copy (searchptr->fname);
4773 fname = cstring_appendChar (fname, CONNECTCHAR);
4774 DPRINTF (("Here: %s", fname));
4781 fname = cstring_concatLength (fname, fbeg, flen);
4783 DPRINTF (("fname: %s", fname));
4785 /* Win32 directory fix from Kay Buschner. */
4786 #if defined (WIN32) || defined (OS2)
4787 /* Fix all unixdir slashes to win dir slashes */
4788 if (searchptr->fname && (searchptr->fname[0] != 0))
4790 cstring_replaceAll (fname, '/', '\\');
4795 /* Change this 1/2 Unix 1/2 VMS file specification into a
4796 full VMS file specification */
4797 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4798 /* Fix up the filename */
4799 hack_vms_include_specification (fname);
4801 /* This is a normal VMS filespec, so use it unchanged. */
4802 strncpy (fname, fbeg, flen);
4804 /* if it's '#include filename', add the missing .h */
4805 if (strchr (fname,'.') == NULL) {
4806 strcat (fname, ".h");
4810 /* ??? There are currently 3 separate mechanisms for avoiding processing
4811 of redundant include files: #import, #pragma once, and
4812 redundant_include_p. It would be nice if they were unified. */
4814 if (redundant_include_p (pfile, fname))
4816 cstring_free (fname);
4820 DPRINTF (("Trying: %s", fname));
4822 f = open_include_file (pfile, fname, searchptr);
4824 if (f == IMPORT_FOUND)
4826 return 0; /* Already included this file */
4829 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4831 cppReader_warning (pfile,
4832 message ("Header file %s exists, but is not readable", fname));
4845 /* A file that was not found. */
4846 fname = cstring_copyLength (fbeg, flen);
4848 if (search_start != NULL)
4850 cppReader_error (pfile,
4851 message ("Cannot find include file %s on search path: %x",
4853 searchPath_unparse (search_start)));
4857 cppReader_error (pfile,
4858 message ("No include path in which to find %s", fname));
4863 ** Check to see if this include file is a once-only include file.
4867 struct file_name_list *ptr;
4869 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4871 if (cstring_equal (ptr->fname, fname))
4873 /* This file was included before. */
4880 /* This is the first time for this file. */
4881 /* Add it to list of files included. */
4883 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4884 ptr->control_macro = NULL;
4885 ptr->c_system_include_path = NULL;
4886 ptr->next = pfile->all_include_files;
4888 ptr->got_name_map = NULL;
4890 DPRINTF (("Including file: %s", fname));
4891 pfile->all_include_files = ptr;
4892 assertSet (pfile->all_include_files);
4895 if (angle_brackets != 0)
4897 pfile->system_include_depth++;
4900 /* Actually process the file */
4901 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4903 cstring_free (fname);
4907 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4908 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4910 output_line_command (pfile, 0, enter_file);
4911 pfile->only_seen_white = 2;
4916 pfile->system_include_depth--;
4919 } /*@=branchstate@*/
4924 /* Return nonzero if there is no need to include file NAME
4925 because it has already been included and it contains a conditional
4926 to make a repeated include do nothing. */
4929 redundant_include_p (cppReader *pfile, cstring name)
4931 struct file_name_list *l = pfile->all_include_files;
4933 for (; l != NULL; l = l->next)
4935 if (cstring_equal (name, l->fname)
4936 && (l->control_macro != NULL)
4937 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4946 /* Return nonzero if the given FILENAME is an absolute pathname which
4947 designates a file within one of the known "system" include file
4948 directories. We assume here that if the given FILENAME looks like
4949 it is the name of a file which resides either directly in a "system"
4950 include file directory, or within any subdirectory thereof, then the
4951 given file must be a "system" include file. This function tells us
4952 if we should suppress pedantic errors/warnings for the given FILENAME.
4954 The value is 2 if the file is a C-language system header file
4955 for which C++ should (on most systems) assume `extern "C"'. */
4958 is_system_include (cppReader *pfile, cstring filename)
4960 struct file_name_list *searchptr;
4962 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4964 searchptr = searchptr->next)
4966 if (!cstring_isEmpty (searchptr->fname))
4968 cstring sys_dir = searchptr->fname;
4969 size_t length = cstring_length (sys_dir);
4971 if (cstring_equalLen (sys_dir, filename, length)
4972 && osd_isConnectChar (cstring_getChar (filename, length)))
4974 if (searchptr->c_system_include_path)
4985 /* Convert a character string literal into a nul-terminated string.
4986 The input string is [IN ... LIMIT).
4987 The result is placed in RESULT. RESULT can be the same as IN.
4988 The value returned in the end of the string written to RESULT,
4989 or NULL on error. */
4991 static /*@null@*/ char *
4992 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4993 char *in, char *limit, int handle_escapes)
5013 /*@switchbreak@*/ break;
5017 char *bpc = (char *) in;
5018 int i = (char) cppReader_parseEscape (pfile, &bpc);
5021 *result++ = (char) c;
5022 /*@switchbreak@*/ break;
5036 * interpret #line command. Remembers previously seen fnames
5037 * in its very own hash table.
5040 /*@constant int FNAME_HASHSIZE@*/
5041 #define FNAME_HASHSIZE 37
5044 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5046 cppBuffer *ip = cppReader_getBuffer (pfile);
5048 size_t old_written = cpplib_getWritten (pfile);
5049 enum file_change_code file_change = same_file;
5050 enum cpp_token token;
5052 llassert (ip != NULL);
5053 token = get_directive_token (pfile);
5055 if (token != CPP_NUMBER
5056 || !isdigit(pfile->token_buffer[old_written]))
5058 cppReader_errorLit (pfile,
5059 cstring_makeLiteralTemp ("invalid format `#line' command"));
5061 goto bad_line_directive;
5064 /* The Newline at the end of this line remains to be processed.
5065 To put the next line at the specified line number,
5066 we must store a line number now that is one less. */
5067 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5068 cppReader_setWritten (pfile, old_written);
5070 /* NEW_LINENO is one less than the actual line number here. */
5071 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5072 cppReader_pedwarnLit (pfile,
5073 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5075 token = get_directive_token (pfile);
5077 if (token == CPP_STRING) {
5078 char *fname = pfile->token_buffer + old_written;
5080 static hashNode fname_table[FNAME_HASHSIZE];
5082 hashNode *hash_bucket;
5085 size_t fname_length;
5087 /* Turn the file name, which is a character string literal,
5088 into a null-terminated string. Do this in place. */
5089 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5090 if (end_name == NULL)
5092 cppReader_errorLit (pfile,
5093 cstring_makeLiteralTemp ("invalid format `#line' command"));
5094 goto bad_line_directive;
5097 fname_length = size_fromInt (end_name - fname);
5098 num_start = cpplib_getWritten (pfile);
5100 token = get_directive_token (pfile);
5101 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5102 p = pfile->token_buffer + num_start;
5103 if (cppReader_isPedantic (pfile))
5104 cppReader_pedwarnLit (pfile,
5105 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5107 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5109 cppReader_errorLit (pfile,
5110 cstring_makeLiteralTemp ("invalid format `#line' command"));
5111 goto bad_line_directive;
5114 file_change = enter_file;
5116 file_change = leave_file;
5118 ip->system_header_p = 1;
5119 else /* if (*p == 4) */
5120 ip->system_header_p = 2;
5122 cppReader_setWritten (pfile, num_start);
5123 token = get_directive_token (pfile);
5124 p = pfile->token_buffer + num_start;
5125 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5126 ip->system_header_p = *p == 3 ? 1 : 2;
5127 token = get_directive_token (pfile);
5129 if (token != CPP_VSPACE) {
5130 cppReader_errorLit (pfile,
5131 cstring_makeLiteralTemp ("invalid format `#line' command"));
5133 goto bad_line_directive;
5138 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5140 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5142 if (hp->length == fname_length)
5144 llassert (hp->value.cpval != NULL);
5146 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5148 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5155 /* Didn't find it; cons up a new one. */
5156 hp = (hashNode) dmalloc (sizeof (*hp));
5159 hp->bucket_hdr = NULL;
5161 hp->name = cstring_undefined;
5162 hp->next = *hash_bucket;
5166 hp->length = fname_length;
5167 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5168 memcpy (hp->value.cpval, fname, fname_length);
5169 hp->value.cpval[fname_length] = '\0';
5170 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5173 else if (token != CPP_VSPACE && token != CPP_EOF)
5175 cppReader_errorLit (pfile,
5176 cstring_makeLiteralTemp ("invalid format `#line' command"));
5177 goto bad_line_directive;
5184 ip->lineno = new_lineno;
5186 cppReader_skipRestOfLine (pfile);
5187 cppReader_setWritten (pfile, old_written);
5188 output_line_command (pfile, 0, file_change);
5193 * remove the definition of a symbol from the symbol table.
5194 * according to un*x /lib/cpp, it is not an error to undef
5195 * something that has no definitions, so it isn't one here either.
5199 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5204 char *orig_buf = buf;
5206 SKIP_WHITE_SPACE (buf);
5208 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5210 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5212 /* If we are generating additional info for debugging (with -g) we
5213 need to pass through all effective #undef commands. */
5214 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5216 pass_thru_directive (orig_buf, limit, pfile, keyword);
5219 if (hp->type != T_MACRO)
5221 cppReader_warning (pfile,
5222 message ("Undefining preprocessor builtin: %s",
5226 cppReader_deleteMacro (hp);
5229 if (cppReader_isPedantic (pfile)) {
5231 SKIP_WHITE_SPACE (buf);
5234 cppReader_pedwarnLit (pfile,
5235 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5244 * Report an error detected by the program we are processing.
5245 * Use the text of the line in the error message.
5246 * (We use error because it prints the filename & line#.)
5250 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5251 char *buf, char *limit)
5253 size_t length = size_fromInt (limit - buf);
5254 cstring copy = cstring_copyLength (buf, length);
5255 cstring adv = cstring_advanceWhiteSpace (copy);
5257 cppReader_error (pfile, message ("#error %s", adv));
5258 cstring_free (copy);
5263 * Report a warning detected by the program we are processing.
5264 * Use the text of the line in the warning message, then continue.
5265 * (We use error because it prints the filename & line#.)
5269 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5270 char *buf, char *limit)
5272 size_t length = size_fromInt (limit - buf);
5273 cstring copy = cstring_copyLength (buf, length);
5274 cstring adv = cstring_advanceWhiteSpace (copy);
5275 cppReader_warning (pfile, message ("#warning %s", adv));
5276 cstring_free (copy);
5281 /* #ident has already been copied to the output file, so just ignore it. */
5284 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5285 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5287 /* Allow #ident in system headers, since that's not user's fault. */
5288 if (cppReader_isPedantic (pfile)
5289 && !cppReader_getBufferSafe (pfile)->system_header_p)
5290 cppReader_pedwarnLit (pfile,
5291 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5293 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5298 /* #pragma and its argument line have already been copied to the output file.
5299 Just check for some recognized pragmas that need validation here. */
5302 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5303 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5305 while (*buf == ' ' || *buf == '\t')
5310 if (!strncmp (buf, "implementation", 14)) {
5311 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5312 been included yet. */
5313 struct file_name_list *ptr;
5314 char *p = buf + 14, *fname, *inc_fname;
5316 SKIP_WHITE_SPACE (p);
5317 if (*p == '\n' || *p != '\"')
5321 p = (char *) strchr (fname, '\"');
5322 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5324 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5326 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5327 inc_fname = (inc_fname != NULL)
5328 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5330 if ((inc_fname != NULL)
5331 && (strncmp (inc_fname, fname, fname_len) == 0))
5333 cpp_setLocation (pfile);
5335 ppllerror (message ("`#pragma implementation' for `%s' appears "
5336 "after file is included",
5337 cstring_fromChars (fname)));
5346 * handle #if command by
5347 * 1) inserting special `defined' keyword into the hash table
5348 * that gets turned into 0 or 1 by special_symbol (thus,
5349 * if the luser has a symbol called `defined' already, it won't
5350 * work inside the #if command)
5351 * 2) rescan the input into a temporary output buffer
5352 * 3) pass the output buffer to the yacc parser and collect a value
5353 * 4) clean up the mess left from steps 1 and 2.
5354 * 5) call conditional_skip to skip til the next #endif (etc.),
5355 * or not, depending on the value from step 3.
5359 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5360 char *buf, char *limit)
5362 HOST_WIDE_INT value;
5363 DPRINTF (("Do if: %s", buf));
5364 value = eval_if_expression (pfile, buf, limit - buf);
5365 conditional_skip (pfile, value == 0, T_IF, NULL);
5370 * handle a #elif directive by not changing if_stack either.
5371 * see the comment above do_else.
5374 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5375 char *buf, char *limit)
5377 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5379 cppReader_errorLit (pfile,
5380 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5385 llassert (pfile->if_stack != NULL);
5387 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5389 cppReader_errorLit (pfile,
5390 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5392 if (pfile->if_stack->fname != NULL
5393 && cppReader_getBufferSafe (pfile)->fname != NULL
5394 && !cstring_equal (pfile->if_stack->fname,
5395 cppReader_getBufferSafe (pfile)->nominal_fname))
5396 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5397 fprintf (stderr, ")\n");
5399 pfile->if_stack->type = T_ELIF;
5402 if (pfile->if_stack->if_succeeded)
5404 skip_if_group (pfile, 0);
5408 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5410 skip_if_group (pfile, 0);
5413 ++pfile->if_stack->if_succeeded; /* continue processing input */
5414 output_line_command (pfile, 1, same_file);
5422 * evaluate a #if expression in BUF, of length LENGTH,
5423 * then parse the result as a C expression and return the value as an int.
5426 static HOST_WIDE_INT
5427 eval_if_expression (cppReader *pfile,
5428 /*@unused@*/ char *buf,
5429 /*@unused@*/ int length)
5431 hashNode save_defined;
5432 HOST_WIDE_INT value;
5433 size_t old_written = cpplib_getWritten (pfile);
5435 DPRINTF (("Saving defined..."));
5436 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5437 pfile->pcp_inside_if = 1;
5439 value = cppReader_parseExpression (pfile);
5440 pfile->pcp_inside_if = 0;
5442 /* Clean up special symbol */
5443 DPRINTF (("Removing defined..."));
5444 cppReader_deleteMacro (save_defined);
5445 cppReader_setWritten (pfile, old_written); /* Pop */
5451 * routine to handle ifdef/ifndef. Try to look up the symbol,
5452 * then do or don't skip to the #endif/#else/#elif depending
5453 * on what directive is actually being processed.
5457 do_xifdef (cppReader *pfile, struct directive *keyword,
5458 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5461 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5463 size_t ident_length;
5464 enum cpp_token token;
5465 int start_of_file = 0;
5466 char *control_macro = 0;
5467 size_t old_written = cpplib_getWritten (pfile);
5469 DPRINTF (("do xifdef: %d",
5470 keyword->type == T_IFNDEF));
5472 /* Detect a #ifndef at start of file (not counting comments). */
5473 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5475 start_of_file = pfile->only_seen_white == 2;
5478 pfile->no_macro_expand++;
5479 token = get_directive_token (pfile);
5480 pfile->no_macro_expand--;
5482 ident = pfile->token_buffer + old_written;
5483 DPRINTF (("Ident: %s", ident));
5485 ident_length = cpplib_getWritten (pfile) - old_written;
5486 cppReader_setWritten (pfile, old_written); /* Pop */
5488 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5490 skip = (keyword->type == T_IFDEF);
5491 if (! cppReader_isTraditional (pfile))
5493 cppReader_pedwarn (pfile,
5494 message ("`#%s' with no argument", keyword->name));
5497 else if (token == CPP_NAME)
5499 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5501 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5503 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5505 if (start_of_file && !skip)
5507 DPRINTF (("Not skipping!"));
5508 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
5509 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
5514 skip = (keyword->type == T_IFDEF);
5515 if (! cppReader_isTraditional (pfile))
5517 cppReader_error (pfile,
5518 message ("`#%s' with invalid argument", keyword->name));
5522 if (!cppReader_isTraditional (pfile))
5525 cppSkipHspace (pfile);
5526 c = cppReader_peekC (pfile);
5527 if (c != EOF && c != '\n')
5529 cppReader_pedwarn (pfile,
5530 message ("garbage at end of `#%s' argument", keyword->name));
5534 cppReader_skipRestOfLine (pfile);
5536 DPRINTF (("Conditional skip: %d", skip));
5537 conditional_skip (pfile, skip, T_IF, control_macro);
5541 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5542 If this is a #ifndef starting at the beginning of a file,
5543 CONTROL_MACRO is the macro name tested by the #ifndef.
5544 Otherwise, CONTROL_MACRO is 0. */
5547 conditional_skip (cppReader *pfile, int skip,
5548 enum node_type type,
5549 /*@dependent@*/ char *control_macro)
5551 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5553 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5554 temp->next = pfile->if_stack;
5555 temp->control_macro = control_macro;
5557 temp->if_succeeded = 0;
5559 pfile->if_stack = temp;
5560 pfile->if_stack->type = type;
5564 skip_if_group (pfile, 0);
5569 ++pfile->if_stack->if_succeeded;
5570 output_line_command (pfile, 1, same_file);
5575 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5576 * leaves input ptr at the sharp sign found.
5577 * If ANY is nonzero, return at next directive of any sort.
5581 skip_if_group (cppReader *pfile, int any)
5584 struct directive *kt;
5585 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5586 register int ident_length;
5588 struct parse_marker line_start_mark;
5590 parseSetMark (&line_start_mark, pfile);
5592 if (CPPOPTIONS (pfile)->output_conditionals) {
5593 static char failed[] = "#failed\n";
5594 cppReader_puts (pfile, failed, sizeof(failed)-1);
5596 output_line_command (pfile, 1, same_file);
5600 if (CPPOPTIONS (pfile)->output_conditionals)
5602 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5605 llassert (pbuf->buf != NULL);
5607 start_line = pbuf->buf + line_start_mark.position;
5608 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5611 parseMoveMark (&line_start_mark, pfile);
5613 if (!cppReader_isTraditional (pfile))
5615 cppSkipHspace (pfile);
5618 c = cppReader_getC (pfile);
5621 size_t old_written = cpplib_getWritten (pfile);
5622 cppSkipHspace (pfile);
5624 parse_name (pfile, cppReader_getC (pfile));
5625 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5626 ident = pfile->token_buffer + old_written;
5627 pfile->limit = ident;
5629 for (kt = directive_table; kt->length >= 0; kt++)
5631 cppIfStackFrame *temp;
5632 if (ident_length == kt->length
5633 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5635 /* If we are asked to return on next directive, do so now. */
5646 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5647 temp->next = pfile->if_stack;
5648 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5649 temp->type = kt->type;
5651 temp->if_succeeded = 0;
5652 temp->control_macro = NULL;
5654 pfile->if_stack = temp;
5655 /*@switchbreak@*/ break;
5658 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5659 validate_else (pfile,
5660 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5663 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5665 cppReader_error (pfile,
5666 message ("Preprocessor command #%s is not within a conditional", kt->name));
5667 /*@switchbreak@*/ break;
5669 else if (pfile->if_stack == save_if_stack)
5671 goto done; /* found what we came for */
5678 if (kt->type != T_ENDIF)
5680 llassert (pfile->if_stack != NULL);
5682 if (pfile->if_stack->type == T_ELSE)
5684 cppReader_errorLit (pfile,
5685 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5688 pfile->if_stack->type = kt->type;
5689 /*@switchbreak@*/ break;
5692 temp = pfile->if_stack;
5693 llassert (temp != NULL);
5694 pfile->if_stack = temp->next;
5696 /*@switchbreak@*/ break;
5699 #if defined (OS2) && defined (__IBMC__)
5700 /* Dummy code to eleminate optimization problems with icc */
5709 /* Don't let erroneous code go by. */
5711 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5712 && cppReader_isPedantic (pfile))
5714 cppReader_pedwarnLit (pfile,
5715 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5719 c = cppReader_getC (pfile);
5721 /* We're in the middle of a line. Skip the rest of it. */
5729 case '/': /* possible comment */
5730 c = skip_comment (pfile, NULL);
5733 /*@switchbreak@*/ break;
5736 cppReader_forward (pfile, -1);
5737 old = cpplib_getWritten (pfile);
5738 (void) cpplib_getToken (pfile);
5739 cppReader_setWritten (pfile, old);
5740 /*@switchbreak@*/ break;
5742 /* Char after backslash loses its special meaning. */
5743 if (cppReader_peekC (pfile) == '\n')
5745 cppReader_forward (pfile, 1);
5748 /*@switchbreak@*/ break;
5752 c = cppReader_getC (pfile);
5755 if (CPPOPTIONS (pfile)->output_conditionals) {
5756 static char end_failed[] = "#endfailed\n";
5757 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5760 pfile->only_seen_white = 1;
5762 parseGotoMark (&line_start_mark, pfile);
5763 parseClearMark (&line_start_mark);
5767 * handle a #else directive. Do this by just continuing processing
5768 * without changing if_stack ; this is so that the error message
5769 * for missing #endif's etc. will point to the original #if. It
5770 * is possible that something different would be better.
5774 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5775 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5777 if (cppReader_isPedantic (pfile))
5779 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5782 cppReader_skipRestOfLine (pfile);
5784 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5785 cppReader_errorLit (pfile,
5786 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5789 /* #ifndef can't have its special treatment for containing the whole file
5790 if it has a #else clause. */
5792 llassert (pfile->if_stack != NULL);
5794 pfile->if_stack->control_macro = 0;
5796 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5798 cpp_setLocation (pfile);
5799 genppllerrorhint (FLG_PREPROC,
5800 message ("Pre-processor directive #else after #else"),
5801 message ("%q: Location of match",
5802 fileloc_unparseRaw (pfile->if_stack->fname,
5803 pfile->if_stack->lineno)));
5806 pfile->if_stack->type = T_ELSE;
5809 if (pfile->if_stack->if_succeeded)
5810 skip_if_group (pfile, 0);
5812 ++pfile->if_stack->if_succeeded; /* continue processing input */
5813 output_line_command (pfile, 1, same_file);
5820 * unstack after #endif command
5824 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5825 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5827 if (cppReader_isPedantic (pfile))
5829 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5832 cppReader_skipRestOfLine (pfile);
5834 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5836 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5840 cppIfStackFrame *temp = pfile->if_stack;
5842 llassert (temp != NULL);
5844 pfile->if_stack = temp->next;
5845 if (temp->control_macro != 0)
5847 /* This #endif matched a #ifndef at the start of the file.
5848 See if it is at the end of the file. */
5849 struct parse_marker start_mark;
5852 parseSetMark (&start_mark, pfile);
5856 cppSkipHspace (pfile);
5857 c = cppReader_getC (pfile);
5863 parseGotoMark (&start_mark, pfile);
5864 parseClearMark (&start_mark);
5868 /* If we get here, this #endif ends a #ifndef
5869 that contains all of the file (aside from whitespace).
5870 Arrange not to include the file again
5871 if the macro that was tested is defined.
5873 Do not do this for the top-level file in a -include or any
5874 file in a -imacros. */
5875 struct file_name_list *ifile = pfile->all_include_files;
5877 for ( ; ifile != NULL; ifile = ifile->next)
5879 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5881 ifile->control_macro = temp->control_macro;
5889 output_line_command (pfile, 1, same_file);
5894 /* When an #else or #endif is found while skipping failed conditional,
5895 if -pedantic was specified, this is called to warn about text after
5896 the command name. P points to the first char after the command name. */
5899 validate_else (cppReader *pfile, cstring directive)
5902 cppSkipHspace (pfile);
5903 c = cppReader_peekC (pfile);
5904 if (c != EOF && c != '\n')
5906 cppReader_pedwarn (pfile,
5907 message ("text following `%s' violates ANSI standard", directive));
5912 ** Get the next token, and add it to the text in pfile->token_buffer.
5913 ** Return the kind of token we got.
5917 cpplib_getToken (cppReader *pfile)
5919 return cpplib_getTokenAux (pfile, FALSE);
5923 cpplib_getTokenForceExpand (cppReader *pfile)
5925 return cpplib_getTokenAux (pfile, TRUE);
5929 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5932 size_t old_written = 0;
5933 int start_line, start_column;
5934 enum cpp_token token;
5935 struct cppOptions *opts = CPPOPTIONS (pfile);
5936 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5939 c = cppReader_getC (pfile);
5940 DPRINTF (("Get next token: %c", c));
5945 if (cppReader_getBufferSafe (pfile)->seen_eof)
5947 cppBuffer *buf = cppReader_popBuffer (pfile);
5949 if (buf != cppReader_nullBuffer (pfile))
5960 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
5961 cppReader_getBufferSafe (pfile)->seen_eof = 1;
5963 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
5964 && next_buf != cppReader_nullBuffer (pfile))
5966 /* We're about to return from an #include file.
5967 Emit #line information now (as part of the CPP_POP) result.
5968 But the #line refers to the file we will pop to. */
5969 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5970 CPPBUFFER (pfile) = next_buf;
5971 pfile->input_stack_listing_current = 0;
5972 output_line_command (pfile, 0, leave_file);
5973 CPPBUFFER (pfile) = cur_buffer;
5981 struct parse_marker start_mark;
5986 if (cppReader_peekC (pfile) == '=')
5991 if (opts->put_out_comments)
5993 parseSetMark (&start_mark, pfile);
5997 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
5998 &start_line, &start_column);
5999 c = skip_comment (pfile, &newlines);
6001 if (opts->put_out_comments && (c == '/' || c == EOF))
6003 assertSet (&start_mark);
6004 parseClearMark (&start_mark);
6011 cppReader_errorWithLine (pfile, start_line, start_column,
6012 cstring_makeLiteral ("Unterminated comment"));
6015 c = '/'; /* Initial letter of comment. */
6017 /* Comments are equivalent to spaces.
6018 For -traditional, a comment is equivalent to nothing. */
6020 if (opts->put_out_comments)
6024 assertSet (&start_mark);
6025 res = cpp_handleComment (pfile, &start_mark);
6026 pfile->lineno += newlines;
6029 else if (cppReader_isTraditional (pfile))
6035 cpplib_reserve(pfile, 1);
6036 cppReader_putCharQ (pfile, ' ');
6041 if (!pfile->only_seen_white)
6046 if (cppReader_handleDirective (pfile))
6048 return CPP_DIRECTIVE;
6051 pfile->only_seen_white = 0;
6056 /* A single quoted string is treated like a double -- some
6057 programs (e.g., troff) are perverse this way */
6058 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6059 &start_line, &start_column);
6060 old_written = cpplib_getWritten (pfile);
6062 cppReader_putChar (pfile, c);
6065 int cc = cppReader_getC (pfile);
6068 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6070 /* try harder: this string crosses a macro expansion
6071 boundary. This can happen naturally if -traditional.
6072 Otherwise, only -D can make a macro with an unmatched
6075 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6076 (*cppReader_getBufferSafe (pfile)->cleanup)
6077 (cppReader_getBufferSafe (pfile), pfile);
6078 CPPBUFFER (pfile) = next_buf;
6081 if (!cppReader_isTraditional (pfile))
6083 cpp_setLocation (pfile);
6085 setLine (long_toInt (start_line));
6086 setColumn (long_toInt (start_column));
6088 if (pfile->multiline_string_line != long_toInt (start_line)
6089 && pfile->multiline_string_line != 0)
6093 message ("Unterminated string or character constant"),
6094 message ("%q: Possible real start of unterminated constant",
6096 (fileloc_filename (g_currentloc),
6097 pfile->multiline_string_line)));
6098 pfile->multiline_string_line = 0;
6104 message ("Unterminated string or character constant"));
6107 /*@loopbreak@*/ break;
6109 cppReader_putChar (pfile, cc);
6113 /* Traditionally, end of line ends a string constant with
6114 no error. So exit the loop and record the new line. */
6115 if (cppReader_isTraditional (pfile))
6121 if (cppReader_isPedantic (pfile)
6122 && pfile->multiline_string_line == 0)
6124 cppReader_pedwarnWithLine
6125 (pfile, long_toInt (start_line),
6126 long_toInt (start_column),
6127 cstring_makeLiteral ("String constant runs past end of line"));
6129 if (pfile->multiline_string_line == 0)
6131 pfile->multiline_string_line = start_line;
6134 /*@switchbreak@*/ break;
6137 cc = cppReader_getC (pfile);
6140 /* Backslash newline is replaced by nothing at all. */
6141 cppReader_adjustWritten (pfile, -1);
6146 /* ANSI stupidly requires that in \\ the second \
6147 is *not* prevented from combining with a newline. */
6150 cppReader_putChar (pfile, cc);
6152 /*@switchbreak@*/ break;
6158 /*@switchbreak@*/ break;
6162 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6163 cpplib_getPWritten (pfile));
6164 pfile->only_seen_white = 0;
6165 return c == '\'' ? CPP_CHAR : CPP_STRING;
6168 if (!opts->dollars_in_ident)
6173 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6181 c2 = cppReader_peekC (pfile);
6182 if (c2 == c || c2 == '=')
6192 if (cppReader_peekC (pfile) == '=')
6198 c2 = cppReader_peekC (pfile);
6199 if (c2 == '-' && opts->chill)
6201 /* Chill style comment */
6202 if (opts->put_out_comments)
6204 parseSetMark (&start_mark, pfile);
6207 cppReader_forward (pfile, 1); /* Skip second '-'. */
6211 c = cppReader_getC (pfile);
6213 /*@loopbreak@*/ break;
6216 /* Don't consider final '\n' to be part of comment. */
6217 cppReader_forward (pfile, -1);
6218 /*@loopbreak@*/ break;
6222 goto return_comment;
6224 if (c2 == '-' || c2 == '=' || c2 == '>')
6229 if (pfile->parsing_include_directive)
6233 cppReader_putChar (pfile, c);
6235 /*@loopbreak@*/ break;
6236 c = cppReader_getC (pfile);
6238 if (c == '\n' || c == EOF)
6240 cppReader_errorLit (pfile,
6241 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6242 /*@loopbreak@*/ break;
6250 c2 = cppReader_peekC (pfile);
6255 cppReader_forward (pfile, 1);
6256 cpplib_reserve (pfile, 4);
6257 cppReader_putChar (pfile, c);
6258 cppReader_putChar (pfile, c2);
6260 c3 = cppReader_peekC (pfile);
6262 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6263 cppReader_nullTerminateQ (pfile);
6264 pfile->only_seen_white = 0;
6268 if (cppReader_getBufferSafe (pfile)->has_escapes)
6270 c = cppReader_getC (pfile);
6273 if (pfile->output_escapes)
6274 cppReader_puts (pfile, "@-", 2);
6275 parse_name (pfile, cppReader_getC (pfile));
6278 else if (is_space [c])
6280 cpplib_reserve (pfile, 2);
6281 if (pfile->output_escapes)
6282 cppReader_putCharQ (pfile, '@');
6283 cppReader_putCharQ (pfile, c);
6291 if (pfile->output_escapes)
6293 cppReader_puts (pfile, "@@", 2);
6299 c2 = cppReader_peekC (pfile);
6302 cpplib_reserve(pfile, 2);
6303 cppReader_putCharQ (pfile, '.');
6304 c = cppReader_getC (pfile);
6308 /* FIXME - misses the case "..\\\n." */
6309 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6311 cpplib_reserve(pfile, 4);
6312 cppReader_putCharQ (pfile, '.');
6313 cppReader_putCharQ (pfile, '.');
6314 cppReader_putCharQ (pfile, '.');
6315 cppReader_forward (pfile, 2);
6316 cppReader_nullTerminateQ (pfile);
6317 pfile->only_seen_white = 0;
6323 pfile->only_seen_white = 0;
6325 cpplib_reserve(pfile, 3);
6326 cppReader_putCharQ (pfile, c);
6327 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6328 cppReader_nullTerminateQ (pfile);
6333 c2 = cppReader_peekC (pfile);
6334 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6336 cppReader_putChar (pfile, c);
6337 c = cppReader_getC (pfile);
6342 case '0': case '1': case '2': case '3': case '4':
6343 case '5': case '6': case '7': case '8': case '9':
6348 cpplib_reserve (pfile, 2);
6349 cppReader_putCharQ (pfile, c);
6351 c = cppReader_peekC (pfile);
6353 /*@loopbreak@*/ break;
6354 if (!is_idchar[c] && c != '.'
6355 && ((c2 != 'e' && c2 != 'E'
6356 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6357 || (c != '+' && c != '-')))
6358 /*@loopbreak@*/ break;
6359 cppReader_forward (pfile, 1);
6363 cppReader_nullTerminateQ (pfile);
6364 pfile->only_seen_white = 0;
6367 case 'b': case 'c': case 'd': case 'h': case 'o':
6368 case 'B': case 'C': case 'D': case 'H': case 'O':
6369 if (opts->chill && cppReader_peekC (pfile) == '\'')
6371 pfile->only_seen_white = 0;
6372 cpplib_reserve (pfile, 2);
6373 cppReader_putCharQ (pfile, c);
6374 cppReader_putCharQ (pfile, '\'');
6375 cppReader_forward (pfile, 1);
6378 c = cppReader_getC (pfile);
6380 goto chill_number_eof;
6383 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6385 cppReader_forward (pfile, 2);
6388 /*@loopbreak@*/ break;
6390 cppReader_putChar (pfile, c);
6394 cpplib_reserve (pfile, 2);
6395 cppReader_putCharQ (pfile, c);
6396 cppReader_nullTerminateQ (pfile);
6401 cppReader_forward (pfile, -1);
6403 cppReader_nullTerminate (pfile);
6410 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6411 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6412 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6413 case 'x': case 'y': case 'z':
6414 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6415 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6416 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6422 size_t before_name_written = cpplib_getWritten (pfile);
6424 parse_name (pfile, c);
6425 pfile->only_seen_white = 0;
6427 if (pfile->no_macro_expand)
6429 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6433 ident = pfile->token_buffer + before_name_written;
6434 DPRINTF (("Ident: %s", ident));
6436 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6438 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6442 DPRINTF (("No expand: %s %d", ident, ident_len));
6446 if (hp->type == T_DISABLED)
6448 DPRINTF (("Disabled!"));
6450 if (pfile->output_escapes)
6451 { /* Return "@-IDENT", followed by '\0'. */
6453 cpplib_reserve (pfile, 3);
6454 ident = pfile->token_buffer + before_name_written;
6455 cppReader_adjustWritten (pfile, 2);
6457 for (i = size_toInt (ident_len); i >= 0; i--)
6459 ident[i+2] = ident[i];
6469 ** If macro wants an arglist, verify that a '(' follows.
6470 ** first skip all whitespace, copying it to the output
6471 ** after the macro name. Then, if there is no '(',
6472 ** decide this is not a macro call and leave things that way.
6475 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6477 struct parse_marker macro_mark;
6480 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6482 cppBuffer *next_buf;
6483 cppSkipHspace (pfile);
6484 if (cppReader_peekC (pfile) != EOF)
6486 /*@loopbreak@*/ break;
6489 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6490 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6491 CPPBUFFER (pfile) = next_buf;
6494 parseSetMark (¯o_mark, pfile);
6498 cppSkipHspace (pfile);
6499 c = cppReader_peekC (pfile);
6500 is_macro_call = c == '(';
6502 /*@loopbreak@*/ break;
6503 cppReader_forward (pfile, 1);
6508 parseGotoMark (¯o_mark, pfile);
6511 parseClearMark (¯o_mark);
6518 /* This is now known to be a macro call. */
6520 /* it might not actually be a macro. */
6521 if (hp->type != T_MACRO)
6526 cppReader_setWritten (pfile, before_name_written);
6527 special_symbol (hp, pfile);
6528 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6529 xbuf = (char *) dmalloc (xbuf_len + 1);
6530 cppReader_setWritten (pfile, before_name_written);
6531 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6532 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6537 ** Expand the macro, reading arguments as needed,
6538 ** and push the expansion on the input stack.
6541 cpplib_macroExpand (pfile, hp);
6542 cppReader_setWritten (pfile, before_name_written);
6545 /* An extra "@ " is added to the end of a macro expansion
6546 to prevent accidental token pasting. We prefer to avoid
6547 unneeded extra spaces (for the sake of cpp-using tools like
6548 imake). Here we remove the space if it is safe to do so. */
6550 llassert (pfile->buffer->rlimit != NULL);
6552 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6553 && pfile->buffer->rlimit[-2] == '@'
6554 && pfile->buffer->rlimit[-1] == ' ')
6556 int c1 = pfile->buffer->rlimit[-3];
6557 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6559 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6560 pfile->buffer->rlimit -= 2;
6566 case ' ': case '\t': case '\v': case '\r':
6569 cppReader_putChar (pfile, c);
6570 c = cppReader_peekC (pfile);
6571 if (c == EOF || !is_hor_space[c])
6572 /*@loopbreak@*/ break;
6573 cppReader_forward (pfile, 1);
6578 c2 = cppReader_peekC (pfile);
6585 cppReader_putChar (pfile, c);
6586 if (pfile->only_seen_white == 0)
6587 pfile->only_seen_white = 1;
6589 output_line_command (pfile, 1, same_file);
6592 case '(': token = CPP_LPAREN; goto char1;
6593 case ')': token = CPP_RPAREN; goto char1;
6594 case '{': token = CPP_LBRACE; goto char1;
6595 case '}': token = CPP_RBRACE; goto char1;
6596 case ',': token = CPP_COMMA; goto char1;
6597 case ';': token = CPP_SEMICOLON; goto char1;
6603 pfile->only_seen_white = 0;
6604 cppReader_putChar (pfile, c);
6613 /* Parse an identifier starting with C. */
6616 parse_name (cppReader *pfile, int c)
6622 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6624 cppReader_forward (pfile, 2);
6628 cppReader_forward (pfile, -1);
6632 if (c == '$' && cppReader_isPedantic (pfile))
6634 cppReader_pedwarnLit (pfile,
6635 cstring_makeLiteralTemp ("`$' in identifier"));
6638 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6639 cppReader_putCharQ (pfile, c);
6640 c = cppReader_getC (pfile);
6646 cppReader_nullTerminateQ (pfile);
6649 /* The file_name_map structure holds a mapping of file names for a
6650 particular directory. This mapping is read from the file named
6651 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6652 map filenames on a file system with severe filename restrictions,
6653 such as DOS. The format of the file name map file is just a series
6654 of lines with two tokens on each line. The first token is the name
6655 to map, and the second token is the actual name to use. */
6657 struct file_name_map
6659 struct file_name_map *map_next;
6664 /*@constant observer char *FILE_NAME_MAP_FILE*/
6665 #define FILE_NAME_MAP_FILE "header.gcc"
6667 /* Read a space delimited string of unlimited length from a stdio
6670 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6676 set = alloc = dmalloc (len + 1);
6681 while ((ch = getc (f)) != EOF && ! is_space[ch])
6683 if (set - alloc == size_toInt (len))
6686 alloc = drealloc (alloc, len + 1);
6687 set = alloc + len / 2;
6688 /*@-branchstate@*/ }
6691 } /*@=branchstate@*/
6694 check (ungetc (ch, f) != EOF);
6696 return cstring_fromChars (alloc);
6699 /* This structure holds a linked list of file name maps, one per directory. */
6701 struct file_name_map_list
6703 struct file_name_map_list *map_list_next;
6704 cstring map_list_name;
6705 /*@null@*/ struct file_name_map *map_list_map;
6708 /* Read the file name map file for DIRNAME. */
6710 static struct file_name_map *
6711 read_name_map (cppReader *pfile, cstring dirname)
6713 struct file_name_map_list *map_list_ptr;
6717 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6718 map_list_ptr != NULL;
6719 map_list_ptr = map_list_ptr->map_list_next)
6721 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6723 return map_list_ptr->map_list_map;
6727 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6728 map_list_ptr->map_list_name = cstring_copy (dirname);
6729 map_list_ptr->map_list_map = NULL;
6731 name = cstring_copy (dirname);
6733 if (cstring_length (dirname) > 0)
6735 name = cstring_appendChar (name, CONNECTCHAR);
6738 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6740 f = fileTable_openFile (context_fileTable (), name, "r");
6741 cstring_free (name);
6745 map_list_ptr->map_list_map = NULL;
6751 while ((ch = getc (f)) != EOF)
6754 struct file_name_map *ptr;
6761 from = read_filename_string (ch, f);
6762 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6767 to = read_filename_string (ch, f);
6769 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6770 ptr->map_from = from;
6772 /* Make the real filename absolute. */
6773 if (cstring_length (to) > 1
6774 && osd_isConnectChar (cstring_firstChar (to)))
6780 ptr->map_to = cstring_copy (dirname);
6781 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6782 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6785 ptr->map_next = map_list_ptr->map_list_map;
6786 map_list_ptr->map_list_map = ptr;
6788 while ((ch = getc (f)) != '\n')
6792 /*@innerbreak@*/ break;
6797 assertSet (map_list_ptr->map_list_map);
6798 check (fileTable_closeFile (context_fileTable (),f) == 0);
6801 map_list_ptr->map_list_next = pfile->opts->map_list;
6802 pfile->opts->map_list = map_list_ptr;
6804 return map_list_ptr->map_list_map;
6807 /* Try to open include file FILENAME. SEARCHPTR is the directory
6808 being tried from the include file search path. This function maps
6809 filenames on file systems based on information read by
6813 open_include_file (cppReader *pfile,
6815 struct file_name_list *searchptr)
6817 char *filename = cstring_toCharsSafe (fname);
6818 struct file_name_map *map;
6822 cstring_markOwned (fname);
6824 cpp_setLocation (pfile);
6826 if (context_getFlag (FLG_NEVERINCLUDE))
6828 if (isHeaderFile (fname))
6830 return SKIP_INCLUDE;
6834 if ((searchptr != NULL) && ! searchptr->got_name_map)
6836 searchptr->name_map = read_name_map (pfile,
6837 !cstring_isEmpty (searchptr->fname)
6838 ? searchptr->fname :
6839 cstring_makeLiteralTemp ("."));
6840 searchptr->got_name_map = 1;
6843 /* First check the mapping for the directory we are using. */
6845 if ((searchptr != NULL)
6846 && (searchptr->name_map != NULL))
6850 if (!cstring_isEmpty (searchptr->fname))
6852 from += cstring_length (searchptr->fname) + 1;
6855 for (map = searchptr->name_map;
6857 map = map->map_next)
6859 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6862 ** Found a match. Check if the file should be skipped
6865 if (cpp_skipIncludeFile (map->map_to))
6867 return SKIP_INCLUDE;
6871 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6878 ** Try to find a mapping file for the particular directory we are
6879 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6880 ** in /usr/include/header.gcc and look up types.h in
6881 ** /usr/include/sys/header.gcc.
6884 p = strrchr (filename, CONNECTCHAR);
6891 if ((searchptr != NULL)
6892 && (cstring_isDefined (searchptr->fname))
6893 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6894 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6896 size_fromInt (p - filename)))
6898 /* filename is in SEARCHPTR, which we've already checked. */
6900 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6902 return SKIP_INCLUDE;
6906 return cpp_openIncludeFile (filename);
6912 dir = mstring_copy (".");
6917 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6918 memcpy (dir, filename, size_fromInt (p - filename));
6919 dir[p - filename] = '\0';
6923 for (map = read_name_map (pfile, cstring_fromChars (dir));
6925 map = map->map_next)
6927 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6931 if (cpp_skipIncludeFile (map->map_to))
6933 return SKIP_INCLUDE;
6937 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6944 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6946 return SKIP_INCLUDE;
6950 return cpp_openIncludeFile (filename);
6954 /* Process the contents of include file FNAME, already open on descriptor F,
6956 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6957 "system" include directories (as decided by the `is_system_include'
6959 DIRPTR is the link in the dir path through which this file was found,
6960 or 0 if the file name was absolute or via the current directory.
6961 Return 1 on success, 0 on failure.
6963 The caller is responsible for the cppReader_pushBuffer. */
6966 finclude (cppReader *pfile, int f,
6968 bool system_header_p,
6969 /*@dependent@*/ struct file_name_list *dirptr)
6975 cppBuffer *fp; /* For input stack frame */
6977 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
6979 cppReader_perrorWithName (pfile, fname);
6980 check (close (f) == 0);
6981 (void) cppReader_popBuffer (pfile);
6987 fp = cppReader_getBufferSafe (pfile);
6989 /*@-temptrans@*/ /* fname shouldn't really be temp */
6990 fp->nominal_fname = fp->fname = fname;
6994 fp->system_header_p = system_header_p;
6997 fp->cleanup = cppReader_fileCleanup;
6999 if (S_ISREG (st_mode))
7002 fp->buf = (char *) dmalloc (st_size + 2);
7003 fp->alimit = fp->buf + st_size + 2;
7006 /* Read the file contents, knowing that st_size is an upper bound
7007 on the number of bytes we can read. */
7008 length = safe_read (f, fp->buf, size_toInt (st_size));
7009 fp->rlimit = fp->buf + length;
7010 if (length < 0) goto nope;
7012 else if (S_ISDIR (st_mode))
7014 cppReader_error (pfile,
7015 message ("Directory specified in #include: %s", fname));
7016 check (close (f) == 0);
7022 ** Cannot count its file size before reading.
7023 ** First read the entire file into heap and
7024 ** copy them into buffer on stack.
7027 size_t bsize = 2000;
7032 fp->buf = (char *) dmalloc (bsize + 2);
7035 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7038 goto nope; /* error! */
7041 if (st_size != bsize)
7043 break; /* End of file */
7047 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7051 length = size_toInt (st_size);
7054 if ((length > 0 && fp->buf[length - 1] != '\n')
7055 /* Backslash-newline at end is not good enough. */
7056 || (length > 1 && fp->buf[length - 2] == '\\')) {
7057 fp->buf[length++] = '\n';
7060 fp->buf[length] = '\0';
7061 fp->rlimit = fp->buf + length;
7063 /* Close descriptor now, so nesting does not use lots of descriptors. */
7064 check (close (f) == 0);
7066 /* Must do this before calling trigraph_pcp, so that the correct file name
7067 will be printed in warning messages. */
7069 pfile->input_stack_listing_current = 0;
7074 cppReader_perrorWithName (pfile, fname);
7075 check (close (f) == 0);
7081 cpplib_init (cppReader *pfile)
7083 memset ((char *) pfile, 0, sizeof (*pfile));
7085 pfile->get_token = cpplib_getToken;
7086 pfile->token_buffer_size = 200;
7087 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7088 pfile->all_include_files = NULL;
7092 cppReader_setWritten (pfile, 0);
7094 pfile->system_include_depth = 0;
7095 pfile->max_include_len = 0;
7096 pfile->timebuf = NULL;
7097 pfile->only_seen_white = 1;
7099 pfile->buffer = cppReader_nullBuffer (pfile);
7103 cppReader_finish (/*@unused@*/ cppReader *pfile)
7108 /* Free resources used by PFILE.
7109 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7112 cppCleanup (cppReader *pfile)
7114 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7116 (void) cppReader_popBuffer (pfile);
7119 if (pfile->token_buffer != NULL)
7121 sfree (pfile->token_buffer);
7122 pfile->token_buffer = NULL;
7125 while (pfile->if_stack != NULL)
7127 cppIfStackFrame *temp = pfile->if_stack;
7128 pfile->if_stack = temp->next;
7132 while (pfile->all_include_files != NULL)
7134 struct file_name_list *temp = pfile->all_include_files;
7135 pfile->all_include_files = temp->next;
7136 /*@-dependenttrans@*/
7137 cstring_free (temp->fname);
7138 /*@=dependenttrans@*/
7142 cppReader_hashCleanup ();
7146 ** Get the file-mode and data size of the file open on FD
7147 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7151 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7155 if (fstat (fd, &sbuf) < 0) {
7161 if (mode_pointer != NULL)
7163 *mode_pointer = sbuf.st_mode;
7166 if (size_pointer != NULL)
7168 *size_pointer = (size_t) sbuf.st_size;
7174 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7175 retrying if necessary. Return a negative value if an error occurs,
7176 otherwise return the actual number of bytes read,
7177 which must be LEN unless end-of-file was reached. */
7179 static int safe_read (int desc, char *ptr, int len)
7185 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7186 /*@-compdef@*/ /* ptr is an out parameter */
7187 int nchars = _read (desc, ptr, (unsigned) left);
7190 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7199 return (int) nchars;
7213 /* Initialize PMARK to remember the current position of PFILE. */
7216 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7218 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7220 pmark->next = pbuf->marks;
7222 pbuf->marks = pmark;
7226 pmark->position = pbuf->cur - pbuf->buf;
7229 /* Cleanup PMARK - we no longer need it. */
7231 void parseClearMark (struct parse_marker *pmark)
7233 struct parse_marker **pp = &pmark->buf->marks;
7235 for (; ; pp = &(*pp)->next)
7237 llassert (*pp != NULL);
7238 if (*pp == pmark) break;
7244 /* Backup the current position of PFILE to that saved in PMARK. */
7247 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7249 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7251 if (pbuf != pmark->buf)
7253 cpp_setLocation (pfile);
7254 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7257 llassert (pbuf->buf != NULL);
7258 pbuf->cur = pbuf->buf + pmark->position;
7261 /* Reset PMARK to point to the current position of PFILE. (Same
7262 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7265 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7267 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7269 if (pbuf != pmark->buf)
7271 cpp_setLocation (pfile);
7272 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7275 pmark->position = pbuf->cur - pbuf->buf;
7278 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7280 struct cppOptions *opts = CPPOPTIONS (pfile);
7283 /* The code looks at the defaults through this pointer, rather than through
7284 the constant structure above. This pointer gets changed if an environment
7285 variable specifies other defaults. */
7287 struct default_include *include_defaults = include_defaults_array;
7289 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7290 /* There seems to be confusion about what CPATH should do,
7291 so for the moment it is not documented. */
7292 /* Some people say that CPATH should replace the standard include dirs,
7293 but that seems pointless: it comes before them, so it overrides them
7296 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7298 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7300 path_include (pfile, cstring_toCharsSafe (xp));
7303 /* Now that dollars_in_ident is known, initialize is_idchar. */
7304 initialize_char_syntax (opts);
7306 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7307 and option processing. */
7309 initialize_builtins (pfile);
7311 /* Do standard #defines and assertions
7312 that identify system and machine type. */
7314 if (!opts->inhibit_predefs) {
7315 char *p = (char *) dmalloc (strlen (predefs) + 1);
7316 strcpy (p, predefs);
7322 while (*p == ' ' || *p == '\t')
7327 /* Handle -D options. */
7328 if (p[0] == '-' && p[1] == 'D')
7332 while (*p && *p != ' ' && *p != '\t')
7342 if (opts->debug_output)
7344 output_line_command (pfile, 0, same_file);
7347 cppReader_define (pfile, q);
7349 while (*p == ' ' || *p == '\t')
7363 opts->done_initializing = 1;
7365 { /* Read the appropriate environment variable and if it exists
7366 replace include_defaults with the listed path. */
7370 int win32_buf_size = 0; /* memory we need to allocate */
7373 if (opts->cplusplus)
7375 epath = getenv ("CPLUS_INCLUDE_PATH");
7379 epath = getenv ("C_INCLUDE_PATH");
7383 ** If the environment var for this language is set,
7384 ** add to the default list of include directories.
7387 if (epath != NULL) {
7388 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7390 char *startp, *endp;
7393 /* if we have a posix path list, convert to win32 path list */
7394 if (cygwin32_posix_path_list_p (epath))
7396 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7397 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7398 cygwin32_posix_to_win32_path_list (epath, win32epath);
7402 for (num_dirs = 1, startp = epath; *startp; startp++)
7404 if (*startp == PATH_SEPARATOR)
7410 = (struct default_include *) dmalloc ((num_dirs
7411 * sizeof (struct default_include))
7412 + sizeof (include_defaults_array));
7415 startp = endp = epath;
7418 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7419 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7421 strncpy (nstore, startp, size_fromInt (endp - startp));
7424 strcpy (nstore, ".");
7428 nstore[endp-startp] = '\0';
7431 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7432 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7433 include_defaults[num_dirs].cxx_aware = 1;
7440 endp = startp = endp + 1;
7447 /* Put the usual defaults back in at the end. */
7448 memcpy ((char *) &include_defaults[num_dirs],
7449 (char *) include_defaults_array,
7450 sizeof (include_defaults_array));
7453 /*@-branchstate@*/ } /*@=branchstate@*/
7456 cppReader_appendIncludeChain (pfile, opts->before_system,
7457 opts->last_before_system);
7458 opts->first_system_include = opts->before_system;
7460 /* Unless -fnostdinc,
7461 tack on the standard include file dirs to the specified list */
7463 if (!opts->no_standard_includes) {
7464 struct default_include *p = include_defaults;
7465 char *specd_prefix = opts->include_prefix;
7466 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7467 int default_len = 0;
7469 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7470 if (default_prefix != NULL) {
7471 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7472 default_len = strlen (default_prefix) - 7;
7473 default_prefix[default_len] = 0;
7477 /* Search "translated" versions of GNU directories.
7478 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7479 if (specd_prefix != 0 && default_len != 0)
7480 for (p = include_defaults; p->fname != NULL; p++) {
7481 /* Some standard dirs are only for C++. */
7483 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7484 /* Does this dir start with the prefix? */
7485 llassert (default_prefix != NULL);
7487 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
7488 size_fromInt (default_len)))
7490 /* Yes; change prefix and add to search list. */
7491 struct file_name_list *nlist
7492 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7493 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7494 char *str = (char *) dmalloc (this_len + 1);
7495 strcpy (str, specd_prefix);
7496 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7499 nlist->fname = cstring_fromChars (str);
7500 nlist->control_macro = 0;
7501 nlist->c_system_include_path = !p->cxx_aware;
7502 nlist->got_name_map = 0;
7504 cppReader_addIncludeChain (pfile, nlist);
7505 if (opts->first_system_include == 0)
7507 opts->first_system_include = nlist;
7513 /* Search ordinary names for GNU include directories. */
7515 for (p = include_defaults; p->fname != NULL; p++)
7517 /* Some standard dirs are only for C++. */
7519 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7521 struct file_name_list *nlist
7522 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7523 nlist->control_macro = 0;
7524 nlist->c_system_include_path = !p->cxx_aware;
7525 nlist->fname = p->fname;
7526 nlist->got_name_map = 0;
7529 cppReader_addIncludeChain (pfile, nlist);
7531 if (opts->first_system_include == 0)
7533 opts->first_system_include = nlist;
7537 sfree (default_prefix);
7540 /* Tack the after_include chain at the end of the include chain. */
7541 cppReader_appendIncludeChain (pfile, opts->after_include,
7542 opts->last_after_include);
7544 if (opts->first_system_include == 0)
7546 opts->first_system_include = opts->after_include;
7549 /* With -v, print the list of dirs to search. */
7550 if (opts->verbose) {
7551 struct file_name_list *p;
7552 fprintf (stderr, "#include \"...\" search starts here:\n");
7554 for (p = opts->include; p != NULL; p = p->next) {
7555 if (p == opts->first_bracket_include)
7556 fprintf (stderr, "#include <...> search starts here:\n");
7558 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7560 fprintf (stderr, "End of search list.\n");
7564 int cppReader_startProcess (cppReader *pfile, cstring fname)
7568 struct cppOptions *opts = CPPOPTIONS (pfile);
7570 fp = cppReader_pushBuffer (pfile, NULL, 0);
7577 if (opts->in_fname == NULL)
7579 opts->in_fname = cstring_makeLiteralTemp ("");
7582 fp->fname = opts->in_fname;
7583 fp->nominal_fname = fp->fname;
7586 /* Copy the entire contents of the main input file into
7587 the stacked input buffer previously allocated for it. */
7589 if (cstring_isEmpty (fname))
7591 fname = cstring_makeLiteralTemp ("");
7594 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7596 cppReader_error (pfile,
7597 message ("Error opening %s for reading: %s",
7598 fname, lldecodeerror (errno)));
7607 if (finclude (pfile, f, fname, 0, NULL))
7609 output_line_command (pfile, 0, same_file);
7615 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7617 return pfile->buffer;
7620 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7622 llassert (pfile->buffer != NULL);
7623 return pfile->buffer;
7626 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7628 llassert (buf->buf != NULL);
7629 return (buf->buf + buf->line_base);
7632 int cpplib_bufPeek (cppBuffer *buf)
7634 if (buf->cur == NULL || buf->rlimit == NULL) {
7638 if (buf->cur < buf->rlimit) {
7645 bool cppBuffer_isMacro (cppBuffer *buf)
7649 return (buf->cleanup == cppReader_macroCleanup);
7656 ** Returns true if the macro should be checked, false
7657 ** if it should be expanded normally.
7660 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7661 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7662 static bool expectiter = FALSE; /* preceeded by @iter@ */
7663 static bool expectenditer = FALSE; /* second after @iter@ */
7664 static bool expectfunction = FALSE; /* preceeded by @function@ */
7665 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7666 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7668 static void cpp_setLocation (cppReader *pfile)
7673 if (pfile->buffer != NULL)
7675 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7677 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7679 DPRINTF (("Looking up: %s", fname));
7681 if (fileTable_exists (context_fileTable (), fname))
7683 fid = fileTable_lookup (context_fileTable (), fname);
7687 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7689 fid = fileTable_lookup (context_fileTable (),
7690 cppReader_getBufferSafe (pfile)->fname);
7695 fid = fileTable_lookup (context_fileTable (),
7696 cppReader_getBufferSafe (pfile)->fname);
7699 line = cppReader_getBufferSafe (pfile)->lineno;
7700 fileloc_free (g_currentloc);
7702 if (fileId_isValid (fid))
7704 g_currentloc = fileloc_create (fid, line, 1);
7708 g_currentloc = fileloc_createBuiltin ();
7713 fileloc_free (g_currentloc);
7714 g_currentloc = fileloc_createBuiltin ();
7718 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7720 bool checkmacro = FALSE;
7721 bool hasParams = FALSE;
7722 bool noexpand = FALSE;
7726 cpp_setLocation (pfile);
7728 DPRINTF (("Should check macro? %s", p));
7730 if (expectiter || expectconstant || expectenditer)
7735 expectenditer = TRUE;
7740 expectconstant = FALSE;
7741 expectenditer = FALSE;
7744 if (notfunction || notparseable)
7746 notfunction = FALSE;
7747 notparseable = FALSE;
7756 llassert (*p == '#');
7759 while (*p == ' ' || *p == '\t')
7764 llassert (*p == 'd'); /* define starts */
7768 while (*p == ' ' || *p == '\t')
7773 sname = cstring_fromChars (p);
7774 DPRINTF (("Check macro: %s", sname));
7776 while (((c = *p) != ' ')
7777 && c != '\0' && c != '('
7778 && c != '\t' && c != '\\' && c != '\n'
7784 hasParams = (c == '(');
7789 notparseable = FALSE;
7791 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7793 DPRINTF (("Clear notfunction"));
7794 notfunction = FALSE;
7809 if (usymtab_existsReal (sname))
7811 uentry ue = usymtab_lookup (sname);
7813 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7815 if (fileloc_isPreproc (uentry_whereLast (ue)))
7821 if (uentry_isSpecified (ue))
7823 checkmacro = context_getFlag (FLG_SPECMACROS);
7829 checkmacro = context_getFlag (FLG_LIBMACROS)
7830 || context_getFlag (FLG_FCNMACROS);
7838 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7840 if (fileloc_isSystemFile (g_currentloc)
7841 && context_getFlag (FLG_SYSTEMDIREXPAND))
7843 ; /* don't check this macro */
7844 DPRINTF (("Don't check 1"));
7852 DPRINTF (("Has params..."));
7854 if (context_getFlag (FLG_FCNMACROS))
7856 if (usymtab_exists (sname))
7859 ** only get here is macro is redefined
7860 ** error reported elsewhere
7863 DPRINTF (("It exists!"));
7868 ** We make it a forward function, since it might be declared elsewhere.
7869 ** After all headers have been processed, we should check the forward
7873 fileloc loc = fileloc_makePreproc (g_currentloc);
7875 /* the line is off-by-one, since the newline was already read */
7880 expectfunction = FALSE;
7883 le = uentry_makeForwardFunction (sname,
7884 typeId_invalid, loc);
7890 /* Do not define here! */
7892 (void) usymtab_addEntry (le);
7896 DPRINTF (("Check: TRUE"));
7900 DPRINTF (("Flag FCN_MACROS not set!"));
7905 DPRINTF (("No params"));
7907 if (context_getFlag (FLG_CONSTMACROS))
7909 bool nocontent = FALSE;
7922 ** Check if there is nothing after the define.
7925 while ((*rest) != '\0' && isspace (*rest))
7932 nocontent = TRUE; /* empty macro, don't check */
7937 if (usymtab_exists (sname))
7943 fileloc loc = fileloc_makePreproc (g_currentloc);
7944 DPRINTF (("Make constant: %s", sname));
7945 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
7946 (void) usymtab_addEntry (le);
7949 checkmacro = !nocontent;
7954 if (checkmacro && usymtab_existsType (sname))
7956 DPRINTF (("Making false..."));
7958 ppllerror (message ("Specified type implemented as macro: %s", sname));
7968 if (usymtab_exists (sname))
7970 uentry ue = usymtab_lookupExpose (sname);
7971 fileloc tloc = fileloc_makePreproc (g_currentloc);
7973 uentry_setDefined (ue, tloc);
7974 fileloc_free (tloc);
7975 uentry_setUsed (ue, fileloc_undefined);
7979 fileloc tloc = fileloc_makePreproc (g_currentloc);
7980 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7981 DPRINTF (("Make expanded macro: %s", sname));
7982 DPRINTF (("Not in symbol table: %s", sname));
7984 (void) usymtab_addGlobalEntry (ue);
7985 fileloc_free (tloc);
7990 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7994 static enum cpp_token
7995 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7997 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8001 bool eliminateComment = FALSE;
8003 llassert (pbuf->buf != NULL);
8005 start = pbuf->buf + smark->position;
8007 llassert (pbuf->cur != NULL);
8008 len = pbuf->cur - start;
8011 && start[1] == context_getCommentMarkerChar ())
8015 char *scomment = start + 2;
8016 char savec = start[len];
8018 cpp_setLocation (pfile);
8019 loc = fileloc_copy (g_currentloc);
8021 start[0] = BEFORE_COMMENT_MARKER[0];
8022 start[1] = BEFORE_COMMENT_MARKER[1];
8024 llassert (start[len - 2] == '*');
8025 start[len - 2] = AFTER_COMMENT_MARKER[0];
8027 llassert (start[len - 1] == '/');
8028 start[len - 1] = AFTER_COMMENT_MARKER[1];
8030 cpplib_reserve(pfile, size_fromInt (1 + len));
8031 cppReader_putCharQ (pfile, c);
8033 cpp_setLocation (pfile);
8037 if (mstring_containsString (scomment, "/*"))
8039 (void) cppoptgenerror
8041 message ("Comment starts inside syntactic comment: %s",
8042 cstring_fromChars (scomment)),
8048 if (mstring_equalPrefix (scomment, "ignore"))
8050 if (!context_getFlag (FLG_NOCOMMENTS))
8052 context_enterSuppressRegion (loc);
8055 else if (mstring_equalPrefix (scomment, "end"))
8057 if (!context_getFlag (FLG_NOCOMMENTS))
8059 context_exitSuppressRegion (loc);
8062 else if (mstring_equalPrefix (scomment, "notparseable"))
8064 notparseable = TRUE;
8066 eliminateComment = TRUE;
8068 else if (mstring_equalPrefix (scomment, "notfunction"))
8072 eliminateComment = TRUE;
8074 else if (mstring_equalPrefix (scomment, "iter"))
8078 else if (mstring_equalPrefix (scomment, "function"))
8080 expectfunction = TRUE;
8082 else if (mstring_equalPrefix (scomment, "constant"))
8084 expectconstant = TRUE;
8088 char sChar = *scomment;
8094 char *rest = scomment + 1;
8096 if (mstring_equalPrefix (rest, "commentchar"))
8098 eliminateComment = TRUE;
8102 ppllerror (cstring_makeLiteral
8103 ("Cannot restore commentchar"));
8107 char *next = scomment + 12; /* strlen commentchar = 12 */
8109 if (*next != ' ' && *next != '\t' && *next != '\n')
8113 ("Syntactic commentchar comment is not followed by a "
8114 "whitespace character: %c",
8119 char cchar = *(next + 1);
8124 (cstring_makeLiteral
8125 ("Cannot set commentchar to NUL"));
8129 context_setCommentMarkerChar (cchar);
8130 /* setComment = TRUE; */
8135 else if (mstring_equalPrefix (scomment, "nestcomment"))
8137 /* fix from Mike Miller <MikeM@xata.com> */
8138 context_fileSetFlag (FLG_NESTCOMMENT,
8139 ynm_fromCodeChar (sChar),
8142 else if (mstring_equalPrefix (rest, "namechecks"))
8144 context_fileSetFlag (FLG_NAMECHECKS,
8145 ynm_fromCodeChar (sChar),
8148 else if (mstring_equalPrefix (rest, "macroredef"))
8150 context_fileSetFlag (FLG_MACROREDEF,
8151 ynm_fromCodeChar (sChar),
8154 else if (mstring_equalPrefix (rest, "usevarargs"))
8156 context_fileSetFlag (FLG_USEVARARGS,
8157 ynm_fromCodeChar (sChar),
8160 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8162 context_fileSetFlag (FLG_MACRONEXTLINE,
8163 ynm_fromCodeChar (sChar),
8166 else if (mstring_equalPrefix (rest, "allmacros")
8167 || mstring_equalPrefix (rest, "fcnmacros")
8168 || mstring_equalPrefix (rest, "constmacros"))
8172 if (mstring_equalPrefix (rest, "allmacros"))
8176 else if (mstring_equalPrefix (rest, "fcnmacros"))
8182 llassert (mstring_equalPrefix (rest, "constmacros"));
8183 fl = FLG_CONSTMACROS;
8186 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8187 notfunction = FALSE;
8200 if (eliminateComment)
8205 /* Replaces comment char's in start with spaces */
8207 for (i = 2; i < len - 2; i++)
8209 if (start[i] == BEFORE_COMMENT_MARKER[0]
8210 || start[i] == BEFORE_COMMENT_MARKER[1]
8211 || start[i] == context_getCommentMarkerChar ())
8217 cppReader_putStrN (pfile, start, size_fromInt (len));
8218 parseClearMark (smark);
8228 ** Output the comment as all spaces so line/column
8229 ** in output file is still correct.
8233 cstring lintcomment = cstring_undefined;
8235 if (context_getFlag (FLG_LINTCOMMENTS))
8237 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8239 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8241 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8243 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8245 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8247 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8249 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8251 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8253 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8255 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8259 lintcomment = cstring_undefined;
8264 lintcomment = cstring_undefined;
8267 if (cstring_isDefined (lintcomment))
8269 c = BEFORE_COMMENT_MARKER[0];
8270 start[0] = BEFORE_COMMENT_MARKER[1];
8272 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8274 for (i = 1; i < len - 2; i++)
8276 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8279 start[len - 2] = AFTER_COMMENT_MARKER[0];
8280 start[len - 1] = AFTER_COMMENT_MARKER[1];
8284 /* Replaces char's in start with spaces */
8285 for (i = 0; i < len; i++)
8289 && start[i + 1] == '*') {
8290 (void) cppoptgenerror
8292 message ("Comment starts inside comment"),
8296 if (start[i] != '\n')
8303 cpplib_reserve (pfile, size_fromInt (1 + len));
8304 cppReader_putCharQ (pfile, c);
8305 cppReader_putStrN (pfile, start, size_fromInt (len));
8306 parseClearMark (smark);
8312 static int cpp_openIncludeFile (char *filename)
8314 int res = open (filename, O_RDONLY, 0666);
8316 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8319 if (!fileTable_exists (context_fileTable (),
8320 cstring_fromChars (filename)))
8322 if (fileloc_isXHFile (g_currentloc))
8325 ** Files includes by XH files are also XH files
8328 (void) fileTable_addXHFile (context_fileTable (),
8329 cstring_fromChars (filename));
8333 (void) fileTable_addHeaderFile (context_fileTable (),
8334 cstring_fromChars (filename));
8339 DPRINTF (("File already exists: %s", filename));
8346 static bool cpp_skipIncludeFile (cstring fname)
8348 if (context_isSystemDir (fname))
8350 DPRINTF (("System dir: %s", fname));
8352 if (lcllib_isSkipHeader (fname))
8354 DPRINTF (("Skip include TRUE: %s", fname));
8358 if (context_getFlag (FLG_SKIPSYSHEADERS))
8360 DPRINTF (("Skip include TRUE: %s", fname));
8365 if (context_getFlag (FLG_SINGLEINCLUDE))
8367 fname = removePreDirs (fname);
8369 # if defined (WIN32) || defined (OS2)
8370 cstring_replaceAll (fname, '\\', '/');
8373 if (fileTable_exists (context_fileTable (), fname))
8375 DPRINTF (("Skip include TRUE: %s", fname));
8380 DPRINTF (("Skip include FALSE: %s", fname));
8384 static int cpp_peekN (cppReader *pfile, int n)
8386 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8388 llassert (buf->cur != NULL);
8390 return (buf->rlimit - buf->cur >= (n)
8395 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8400 void cppBuffer_forward (cppBuffer *buf, int n)
8402 llassert (buf->cur != NULL);