2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
29 Contributed by Per Bothner, 1994-95.
30 Based on CCCP program by Paul Rubin, June 1986
31 Adapted to ANSI C, Richard Stallman, Jan 1987
33 This program is free software; you can redistribute it and/or modify it
34 under the terms of the GNU General Public License as published by the
35 Free Software Foundation; either version 2, or (at your option) any
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 In other words, you are welcome to use, share and improve this program.
48 You are forbidden to forbid anyone else to use, share and improve
49 what you give them. Help stamp out software-hoarding! */
53 * - OS2 drive specs like WIN32
54 * - Includes for IBMs OS/2 compiler
65 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
68 # include <sys/types.h>
69 # include <sys/stat.h>
71 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
73 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
78 ** evans 2002-07-03: exception for WATCOM 10.6 compiler suggest by Adam Clarke
80 # if !defined (USG) && !defined (__WATCOMC__)
81 # include <time.h> /* Reported by Paul Smith */
82 # include <sys/time.h>
83 # include <sys/resource.h>
85 # include <sys/times.h>
90 # endif /* not WIN32 */
92 /* This defines "errno" properly for VMS, and gives us EACCES. */
95 # include "splintMacros.nf"
99 # include "cpperror.h"
100 # include "cpphash.h"
102 # include "version.h"
106 ** This is really kludgey code...
112 /* Warnings for using sprintf - suppress them all for now... */
113 /*@-bufferoverflowhigh@*/
116 #define NO_SHORTNAMES
124 /*@constant int IMPORT_FOUND@*/
125 # define IMPORT_FOUND -2
127 /*@constant int SKIP_INCLUDE@*/
128 # define SKIP_INCLUDE IMPORT_FOUND
130 /*@constant unused int IMPORT_NOT_FOUND@*/
131 # define IMPORT_NOT_FOUND -1
134 /*@constant unused int STDC_VALUE@*/
138 /* By default, colon separates directories in a path. */
139 #ifndef PATH_SEPARATOR
140 /*@constant char PATH_SEPARATOR@*/
141 #define PATH_SEPARATOR ':'
144 static void parse_name (cppReader *, int);
146 static int cpp_openIncludeFile (char *p_filename)
147 /*@modifies fileSystem @*/ ;
149 static void cpp_setLocation (cppReader *p_pfile)
150 /*@modifies g_currentloc@*/ ;
152 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
153 struct parse_marker *p_smark)
154 /*@modifies p_pfile, p_smark@*/;
156 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
158 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
160 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
166 /* Symbols to predefine. */
168 #ifdef CPP_PREDEFINES
169 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
171 static /*@observer@*/ char *predefs = "";
174 /* We let tm.h override the types used here, to handle trivial differences
175 such as the choice of unsigned int or long unsigned int for size_t.
176 When machines start needing nontrivial differences in the size type,
177 it would be best to do something here to figure out automatically
178 from other information what type to use. */
180 /* The string value for __SIZE_TYPE__. */
183 /*@constant observer char *SIZE_TYPE@*/
184 #define SIZE_TYPE "long unsigned int"
187 /* The string value for __PTRDIFF_TYPE__. */
190 /*@constant observer char *PTRDIFF_TYPE@*/
191 #define PTRDIFF_TYPE "long int"
194 /* The string value for __WCHAR_TYPE__. */
197 /*@constant observer char *WCHAR_TYPE@*/
198 #define WCHAR_TYPE "int"
201 /* The string value for __USER_LABEL_PREFIX__ */
203 #ifndef USER_LABEL_PREFIX
204 /*@constant observer char *USER_LABEL_PREFIX@*/
205 #define USER_LABEL_PREFIX ""
208 /* The string value for __REGISTER_PREFIX__ */
210 #ifndef REGISTER_PREFIX
211 /*@constant observer char *REGISTER_PREFIX@*/
212 #define REGISTER_PREFIX ""
215 /* table to tell if char can be part of a C identifier. */
216 static bool is_idchar[256];
217 /* table to tell if char can be first char of a c identifier. */
218 static bool is_idstart[256];
219 /* table to tell if c is horizontal space. */
220 static bool is_hor_space[256];
221 /* table to tell if c is horizontal or vertical space. */
222 static bool is_space[256];
224 static /*@exposed@*/ /*@null@*/ cppBuffer *
225 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
226 /*@uses p_pfile->buffer@*/
227 /*@modifies nothing@*/ ;
230 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
233 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
235 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
237 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
238 # define cppBuffer_get(BUFFER) \
239 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
241 /*@function static int cppBuffer_reachedEOF (sef cppBuffer *p_b) modifies nothing; @*/
242 # define cppBuffer_reachedEOF(b) \
243 ((b)->cur < (b)->rlimit ? FALSE : TRUE)
245 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
246 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
247 modifies *p_file; @*/
248 # define cppReader_puts(PFILE, STR, N) \
249 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
251 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
253 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
254 modifies *p_file; @*/
255 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
257 static void cppReader_putCharQ (cppReader *p_file, char p_ch)
259 fprintf (stderr, "put char: %c\n", p_ch);
260 (*(p_file)->limit++ = (p_ch));
263 /* Append character CH to PFILE's output buffer. Make space if need be. */
265 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
266 modifies *p_file; @*/
267 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
269 /* Make sure PFILE->limit is followed by '\0'. */
270 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
271 modifies *p_file; @*/
273 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
275 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
276 modifies *p_file; @*/
277 # define cppReader_nullTerminate(PFILE) \
278 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
280 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
281 modifies *p_file; @*/
282 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
284 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
285 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
287 /*@function static observer char *cppReader_wcharType (cppReader *)
288 modifies nothing; @*/
290 # define cppReader_wcharType(PFILE) \
291 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
293 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
295 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
296 # define cppReader_forward(pfile, N) \
297 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
299 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
300 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
302 /*@function static int cppReader_reachedEOF (sef cppReader *p_pfile) modifies *p_pfile; @*/
303 # define cppReader_reachedEOF(pfile) (cppBuffer_reachedEOF (cppReader_getBufferSafe (pfile)))
305 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
306 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
308 /* Move all backslash-newline pairs out of embarrassing places.
309 Exchange all such pairs following BP
310 with any potentially-embarrassing characters that follow them.
311 Potentially-embarrassing characters are / and *
312 (because a backslash-newline inside a comment delimiter
313 would cause it not to be recognized). */
316 # define NEWLINE_FIX \
317 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
319 /* Same, but assume we've already read the potential '\\' into C. */
321 # define NEWLINE_FIX1(C) do { \
322 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
325 static void parseSetMark (/*@out@*/ struct parse_marker *,
327 static void parseClearMark (struct parse_marker *);
328 static void parseGotoMark (struct parse_marker *, cppReader *);
329 static void parseMoveMark (struct parse_marker *, cppReader *);
331 /* If we have a huge buffer, may need to cache more recent counts */
332 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
334 static /*@exposed@*/ /*@null@*/ cppBuffer *
335 cppReader_pushBuffer (cppReader *p_pfile,
336 /*@owned@*/ /*@null@*/ char *, size_t)
337 /*@modifies p_pfile@*/ ;
339 static void cppReader_appendIncludeChain
341 /*@keep@*/ struct file_name_list *p_first,
342 /*@dependent@*/ struct file_name_list *p_last);
344 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
345 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
347 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
348 /*@unused@*/ cppReader *p_pfile);
350 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
351 /*@unused@*/ cppReader *p_pfile);
353 static int cppReader_handleDirective (cppReader *p_pfile);
355 static void cppReader_scanBuffer (cppReader *p_pfile);
357 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
360 ** WIN32 (at least the VC++ include files) does not define mode_t.
363 /*@-incondefs@*/ /*@-czechtypes@*/
364 typedef unsigned int mode_t;
365 /*@=incondefs@*/ /*@=czechtypes@*/
369 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
370 /*@out@*/ size_t *p_size_pointer);
371 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
375 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
376 ** (Note that it is false while we're expanding marco *arguments*.)
379 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
381 static void path_include (cppReader *p_pfile, char *p_path)
382 /*@modifies p_pfile@*/ ;
384 static void initialize_builtins (cppReader *p_pfile)
385 /*@modifies p_pfile@*/ ;
387 static void initialize_char_syntax (struct cppOptions *p_opts) ;
389 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
391 bool p_system_header_p,
392 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
394 static void validate_else (cppReader *p_pfile, cstring p_directive);
396 static void conditional_skip (cppReader *p_pfile, int p_skip,
397 enum node_type p_type,
398 /*@dependent@*/ /*@null@*/ char *p_control_macro);
400 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
404 static void skip_if_group (cppReader *p_pfile, int p_any);
406 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
407 char *p_beg2, int p_len2, bool p_last);
410 extern void fancy_abort ();
413 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
414 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
416 static /*@observer@*/ /*@null@*/ struct file_name_map *
417 read_name_map (cppReader *p_pfile, cstring p_dirname);
419 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
421 static int open_include_file (cppReader *p_pfile,
422 /*@owned@*/ cstring p_fname,
423 /*@null@*/ struct file_name_list *p_searchptr);
425 static void push_macro_expansion (cppReader *,
426 /*@owned@*/ char *, size_t,
427 /*@dependent@*/ hashNode);
429 /* Last arg to output_line_command. */
430 enum file_change_code {
431 same_file, enter_file, leave_file
434 /* `struct directive' defines one #-directive, including how to handle it. */
437 int length; /* Length of name */
438 /*@null@*/ int (*func)(); /* Function to handle directive */
439 /*@observer@*/ cstring name; /* Name of directive */
440 enum node_type type; /* Code which describes which directive. */
441 bool command_reads_line; /* One if rest of line is read by func. */
442 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
443 bool pass_thru; /* Copy preprocessed directive to output file.*/
446 /* These functions are declared to return int instead of void since they
447 are going to be placed in a table and some old compilers have trouble with
448 pointers to functions returning void. */
450 static int do_define (cppReader *, /*@null@*/ struct directive *,
451 /*@exposed@*/ char *, char *);
452 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
453 /*@exposed@*/ char *, char *, bool);
455 static int do_line (cppReader *, /*@null@*/ struct directive *);
456 static int do_include (cppReader *, struct directive *, char *, char *);
457 static int do_undef (cppReader *, struct directive *, char *, char *);
458 static int do_error (cppReader *, struct directive *, char *, char *);
459 static int do_pragma (cppReader *, struct directive *, char *, char *);
460 static int do_ident (cppReader *, struct directive *, char *, char *);
461 static int do_if (cppReader *, struct directive *, char *, char *);
462 static int do_xifdef (cppReader *, struct directive *, char *, char *);
463 static int do_else (cppReader *, struct directive *, char *, char *);
464 static int do_elif (cppReader *, struct directive *, char *, char *);
465 static int do_endif (cppReader *, struct directive *, char *, char *);
466 static int do_warning (cppReader *, struct directive *, char *, char *);
468 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
469 via the same directory as the file that #included it. */
471 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
472 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
474 /* #include "file" looks in source file dir, then stack. */
475 /* #include <file> just looks in the stack. */
476 /* -I directories are added to the end, then the defaults are added. */
480 static struct default_include {
481 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
482 int cplusplus; /* Only look here if we're compiling C++. */
483 int cxx_aware; /* Includes in this directory don't need to
484 be wrapped in extern "C" when compiling
486 } include_defaults_array[]
488 /* This is the dir for fixincludes. Put it just before
489 the files that we fix. */
490 { GCC_INCLUDE_DIR, 0, 0 },
491 { GCC_INCLUDE_DIR2, 0, 0 },
492 { cstring_undefined, 0, 0 }
495 /*@noaccess cstring@*/
497 /* Here is the actual list of #-directives, most-often-used first.
498 The initialize_builtins function assumes #define is the very first. */
502 static struct directive directive_table[] = {
503 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
504 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
505 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
506 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
507 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
508 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
509 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
510 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
511 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
512 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
513 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
514 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
515 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
516 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
517 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
518 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
520 /*@noaccess cstring@*/
522 static cstring searchPath_unparse (struct file_name_list *search_start)
524 cstring res = cstring_newEmpty ();
525 struct file_name_list *searchptr = NULL;
527 for (searchptr = search_start; searchptr != NULL;
528 searchptr = searchptr->next)
530 if (!cstring_isEmpty (searchptr->fname)) {
531 res = cstring_concatFree1 (res, searchptr->fname);
532 if (searchptr->next != NULL) {
533 res = cstring_appendChar (res, ';');
543 initialize_char_syntax (struct cppOptions *opts)
548 * Set up is_idchar and is_idstart tables. These should be
549 * faster than saying (is_alpha (c) || c == '_'), etc.
550 * Set up these things before calling any routines tthat
554 for (i = 'a'; i <= 'z'; i++) {
555 is_idchar[i - 'a' + 'A'] = TRUE;
556 is_idchar[(int) i] = TRUE;
557 is_idstart[i - 'a' + 'A'] = TRUE;
558 is_idstart[(int) i] = TRUE;
561 for (i = '0'; i <= '9'; i++)
563 is_idchar[(int) i] = TRUE;
566 is_idchar['_'] = TRUE;
567 is_idstart['_'] = TRUE;
568 is_idchar['$'] = opts->dollars_in_ident;
569 is_idstart['$'] = opts->dollars_in_ident;
571 /* horizontal space table */
572 is_hor_space[' '] = TRUE;
573 is_hor_space['\t'] = TRUE;
574 is_hor_space['\v'] = TRUE;
575 is_hor_space['\f'] = TRUE;
576 is_hor_space['\r'] = TRUE;
578 is_space[' '] = TRUE;
579 is_space['\t'] = TRUE;
580 is_space['\v'] = TRUE;
581 is_space['\f'] = TRUE;
582 is_space['\n'] = TRUE;
583 is_space['\r'] = TRUE;
586 bool isIdentifierChar (char c)
588 return is_idchar[(int) c];
591 /* Place into P_PFILE a quoted string representing the string SRC.
592 Caller must reserve enough space in pfile->token_buffer. */
595 quote_string (cppReader *pfile, char *src)
599 cppReader_putCharQ (pfile, '\"');
602 switch ((c = *src++))
606 cppReader_putCharQ (pfile, c);
609 sprintf (cpplib_getPWritten (pfile), "\\%03o",
611 cppReader_adjustWritten (pfile, (size_t) 4);
613 /*@switchbreak@*/ break;
617 cppReader_putCharQ (pfile, '\\');
618 cppReader_putCharQ (pfile, c);
619 /*@switchbreak@*/ break;
622 cppReader_putCharQ (pfile, '\"');
623 cppReader_nullTerminateQ (pfile);
629 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
632 cppReader_growBuffer (cppReader *pfile, size_t n)
634 size_t old_written = cpplib_getWritten (pfile);
635 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
636 pfile->token_buffer = (char *)
637 drealloc (pfile->token_buffer, pfile->token_buffer_size);
638 cppReader_setWritten (pfile, old_written);
642 * process a given definition string, for initialization
643 * If STR is just an identifier, define it with value 1.
644 * If STR has anything after the identifier, then it should
645 * be identifier=definition.
649 cppReader_define (cppReader *pfile, char *str)
654 DPRINTF (("Cpp reader define: %s", str));
656 if (!is_idstart[(int) *p])
658 DPRINTF (("ERROR 1"));
659 cppReader_error (pfile,
660 message ("Malformed option `-D%s'",
661 cstring_fromChars (str)));
668 DPRINTF (("Here 2"));
670 while (is_idchar[(int) *p])
677 while (*p != ')' && *p != '\0') {
686 message ("Malformed option: -D%s (no closing parenthesis)",
687 cstring_fromChars (str)));
691 DPRINTF (("Here 2"));
695 buf = (char *) dmalloc (size_fromInt (p - str + 4));
696 strcpy ((char *) buf, str);
697 strcat ((char *) buf, " 1");
701 DPRINTF (("ERROR 2"));
702 cppReader_error (pfile,
703 message ("Malformed option: -D%s (expected '=', found '%c')",
704 cstring_fromChars (str),
711 /* Copy the entire option so we can modify it. */
712 DPRINTF (("Copying..."));
713 buf = (char *) dmalloc (2 * strlen (str) + 1);
714 strncpy (buf, str, size_fromInt (p - str));
716 /* Change the = to a space. */
718 /* Scan for any backslash-newline and remove it. */
724 if (*p == '\\' && p[1] == '\n')
730 DPRINTF (("Here we are..."));
734 llassert (buf != NULL);
735 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
736 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
740 /* Append a chain of `struct file_name_list's
741 to the end of the main include chain.
742 FIRST is gthe beginning of the chain to append, and LAST is the end. */
745 cppReader_appendIncludeChain (cppReader *pfile,
746 struct file_name_list *first,
747 struct file_name_list *last)
749 struct cppOptions *opts = CPPOPTIONS (pfile);
750 struct file_name_list *dir;
752 if (first == NULL || last == NULL)
757 if (opts->include == 0)
759 opts->include = first;
763 llassert (opts->last_include->next == NULL);
764 opts->last_include->next = first;
767 if (opts->first_bracket_include == 0)
769 opts->first_bracket_include = first;
771 for (dir = first; ; dir = dir->next) {
772 size_t len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
774 if (len > pfile->max_include_len)
776 pfile->max_include_len = len;
786 llassert (last->next == NULL);
787 /* last->next = NULL; */
788 opts->last_include = last;
792 static /*@unused@*/ void
793 cppReader_showIncludeChain (cppReader *pfile)
795 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
801 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
805 fprintf (stderr, "\n");
809 fprintf (stderr, "No includes\n");
815 cppReader_getIncludePath ()
817 cppReader *pfile = &g_cppState;
818 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
819 cstring res = cstring_undefined;
825 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
831 res = cstring_makeLiteral ("<no include path>");
838 cppReader_addIncludeChain (cppReader *pfile, /*@only@*/ struct file_name_list *dir)
840 struct cppOptions *opts = CPPOPTIONS (pfile);
847 if (opts->include == 0)
853 llassert (opts->last_include->next == NULL);
854 opts->last_include->next = dir;
857 if (opts->first_bracket_include == 0)
859 size_t len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
860 opts->first_bracket_include = dir;
862 if (len > pfile->max_include_len)
864 pfile->max_include_len = len;
869 opts->last_include = dir;
870 /* cppReader_showIncludeChain (pfile); */
873 /* Given a colon-separated list of file names PATH,
874 add all the names to the search path for include files. */
877 path_include (cppReader *pfile, char *path)
884 /* if we have a posix path list, convert to win32 path list */
885 win32temp = (char *) dmalloc /*@i4@*/
886 (cygwin32_posix_to_win32_path_list_buf_size (path));
887 cygwin32_posix_to_win32_path_list (path, win32temp);
897 struct file_name_list *dirtmp;
899 /* Find the end of this name. */
900 while (*q != '\0' && *q != PATH_SEPARATOR)
907 /* An empty name in the path stands for the current directory. */
908 name = (char *) dmalloc ((size_t) 2);
914 /* Otherwise use the directory that is named. */
915 name = (char *) dmalloc (size_fromInt (q - p + 1));
916 memcpy (name, p, size_fromInt (q - p));
920 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
921 dirtmp->next = 0; /* New one goes on the end */
922 dirtmp->control_macro = 0;
923 dirtmp->c_system_include_path = 0;
924 dirtmp->fname = cstring_fromChars (name);
925 dirtmp->got_name_map = 0;
926 cppReader_addIncludeChain (pfile, dirtmp);
928 /* Advance past this name. */
932 /* Skip the colon. */
938 cppOptions_init (cppOptions *opts)
940 memset ((char *) opts, 0, sizeof *opts);
943 opts->in_fname = NULL;
944 opts->out_fname = NULL;
946 /* Initialize is_idchar to allow $. */
947 opts->dollars_in_ident = TRUE;
949 opts->no_line_commands = 0;
950 opts->no_trigraphs = TRUE;
951 opts->put_out_comments = 1;
952 opts->print_include_names = 0;
953 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
957 opts->cplusplus_comments = 1;
962 opts->pedantic_errors = 0;
963 opts->warn_comments = 0;
964 opts->warnings_are_errors = 0;
966 /* Added 2003-07-10: */
967 opts->traditional = FALSE;
969 initialize_char_syntax (opts);
973 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
979 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
980 /*@unused@*/ cppReader *pfile)
986 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
988 hashNode macro = pbuf->hnode;
990 if (macro->type == T_DISABLED)
992 macro->type = T_MACRO;
995 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
1003 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
1005 if (pbuf->buf != NULL)
1012 /* Assuming we have read '/'.
1013 If this is the start of a comment (followed by '*' or '/'),
1014 skip to the end of the comment, and return ' '.
1015 Return EOF if we reached the end of file before the end of the comment.
1016 If not the start of a comment, return '/'. */
1019 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
1023 llassert (pfile->buffer != NULL);
1024 llassert (pfile->buffer->cur != NULL);
1026 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1033 cppReader_forward (pfile, 2);
1036 if (cppReader_peekC (pfile) == '*')
1038 cppReader_forward (pfile, 1);
1043 c = cppReader_getC (pfile);
1050 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1057 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1060 if (prev_c == (int) '*' && c == (int) '/')
1065 if (c == (int) '\n' && (linep != NULL))
1071 else if (cppReader_peekC (pfile) == '/'
1072 && CPPOPTIONS (pfile)->cplusplus_comments)
1075 (void) cppoptgenerror
1076 (FLG_SLASHSLASHCOMMENT,
1077 message ("C++ style // comment"
1081 cppReader_forward (pfile, 1);
1085 c = cppReader_getC (pfile);
1089 /* Allow hash comment to be terminated by EOF. */
1093 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1095 cppReader_forward (pfile, 1);
1096 c = cppReader_getC (pfile);
1104 if (c == (int) '\n')
1106 /* Don't consider final '\n' to be part of comment. */
1107 cppReader_forward (pfile, -1);
1118 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1120 cppSkipHspace (cppReader *pfile)
1128 llassert (pfile->buffer != NULL);
1130 c = cppReader_peekC (pfile);
1134 return 0; /* FIXME */
1137 if (is_hor_space[c])
1139 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1140 cppReader_pedwarn (pfile,
1141 message ("%s in preprocessing directive",
1143 ? cstring_makeLiteralTemp ("formfeed")
1144 : cstring_makeLiteralTemp ("vertical tab")));
1147 cppReader_forward (pfile, 1);
1151 cppReader_forward (pfile, 1);
1152 c = skip_comment (pfile, NULL);
1156 cppReader_forward (pfile, -1);
1159 if (c == EOF || c == '/')
1164 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1166 cppReader_forward (pfile, 2);
1168 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1169 && is_hor_space [cpp_peekN (pfile, 1)])
1171 cppReader_forward (pfile, 2);
1180 /* Read the rest of the current line.
1181 The line is appended to PFILE's output buffer. */
1184 copy_rest_of_line (cppReader *pfile)
1186 struct cppOptions *opts = CPPOPTIONS (pfile);
1193 llassert (pfile->buffer != NULL);
1195 c = cppReader_getC (pfile);
1203 ** Patch from Brian St. Pierre for handling MS-DOS files.
1206 DPRINTF (("Reading directive: %d", (int) c));
1208 if (cppReader_peekC (pfile) == '\n'
1209 || cppReader_peekC (pfile) == '\r')
1211 DPRINTF (("Reading directive..."));
1212 if (cppReader_peekC (pfile) == '\r')
1214 DPRINTF (("Reading directive..."));
1215 cppReader_forward (pfile, 1);
1218 DPRINTF (("Reading directive..."));
1219 cppReader_forward (pfile, 1);
1223 DPRINTF (("Falling..."));
1224 /*@fallthrough@*/ case '\'': case '\"':
1225 goto scan_directive_token;
1228 nextc = cppReader_peekC (pfile);
1231 ** was (opts->cplusplus_comments && nextc == '*')
1236 || (opts->cplusplus_comments && nextc == '/'))
1238 goto scan_directive_token;
1240 /*@switchbreak@*/ break;
1243 if (cppReader_isPedantic (pfile))
1244 cppReader_pedwarn (pfile,
1245 message ("%s in preprocessing directive",
1247 ? cstring_makeLiteralTemp ("formfeed")
1248 : cstring_makeLiteralTemp ("vertical tab")));
1249 /*@switchbreak@*/ break;
1252 cppReader_forward (pfile, -1);
1254 scan_directive_token:
1255 cppReader_forward (pfile, -1);
1256 (void) cpplib_getToken (pfile);
1259 cppReader_putChar (pfile, c);
1262 cppReader_nullTerminate (pfile);
1266 cppReader_skipRestOfLine (cppReader *pfile)
1268 size_t old = cpplib_getWritten (pfile);
1269 copy_rest_of_line (pfile);
1270 cppReader_setWritten (pfile, old);
1273 /* Handle a possible # directive.
1274 '#' has already been read. */
1277 cppReader_handleDirective (cppReader *pfile)
1280 struct directive *kt = NULL;
1282 size_t after_ident = 0;
1284 char *line_end = NULL;
1285 size_t old_written = cpplib_getWritten (pfile);
1286 int nspaces = cppSkipHspace (pfile);
1288 c = cppReader_peekC (pfile);
1290 if (c >= '0' && c <= '9')
1292 /* Handle # followed by a line number. */
1293 if (cppReader_isPedantic (pfile))
1295 cppReader_pedwarnLit
1297 cstring_makeLiteralTemp ("`#' followed by integer"));
1300 (void) do_line (pfile, NULL);
1301 goto done_a_directive;
1305 /* Now find the directive name. */
1307 cppReader_putChar (pfile, '#');
1309 parse_name (pfile, cppReader_getC (pfile));
1311 llassert (pfile->token_buffer != NULL);
1312 ident = pfile->token_buffer + old_written + 1;
1314 ident_length = cpplib_getPWritten (pfile) - ident;
1316 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1318 /* A line of just `#' becomes blank. */
1322 for (kt = directive_table; ; kt++)
1324 if (kt->length <= 0)
1326 return 0; /* goto not_a_directive; */
1329 if (kt->length == ident_length
1330 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1336 if (kt->command_reads_line)
1342 /* Nonzero means do not delete comments within the directive.
1343 #define needs this when -traditional. */
1344 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1345 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1346 CPPOPTIONS (pfile)->put_out_comments = comments;
1347 after_ident = cpplib_getWritten (pfile);
1348 copy_rest_of_line (pfile);
1349 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1353 /* For #pragma and #define, we may want to pass through the directive.
1354 Other directives may create output, but we don't want the directive
1355 itself out, so we pop it now. For example #include may write a #line
1356 command (see comment in do_include), and conditionals may emit
1357 #failed ... #endfailed stuff. But note that popping the buffer
1358 means the parameters to kt->func may point after pfile->limit
1359 so these parameters are invalid as soon as something gets appended
1360 to the token_buffer. */
1362 line_end = cpplib_getPWritten (pfile);
1365 if (!kt->pass_thru && kt->type != T_DEFINE)
1367 cppReader_setWritten (pfile, old_written);
1370 llassert (pfile->token_buffer != NULL);
1372 /* was kt->pass_thru || */
1374 if (kt->type == T_DEFINE
1375 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1377 char *p = pfile->token_buffer + old_written;
1380 ** Still need to record value for preprocessing, so
1381 ** #ifdef's, etc. using the value behave correctly.
1384 (void) do_defineAux (pfile, kt,
1385 pfile->token_buffer + after_ident,
1394 SKIP_WHITE_SPACE (p);
1396 llassert (*p == 'd');
1399 llassert (*p == 'e');
1402 llassert (*p == 'f');
1405 llassert (*p == 'i');
1408 llassert (*p == 'n');
1411 llassert (*p == 'e');
1414 ** This is way-bogus. We use the last char to record the number of
1415 ** spaces. Its too hard to get them back into the input stream.
1418 if (nspaces > 9) nspaces = 9;
1420 *p++ = '0' + nspaces;
1422 return 0; /* not_a_directive */
1424 else if (kt->pass_thru)
1426 /* Just leave the entire #define in the output stack. */
1427 return 0; /* not_a_directive */
1430 else if (kt->type == T_DEFINE
1431 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1433 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1434 SKIP_WHITE_SPACE (p);
1436 while (is_idchar[(int) *p])
1442 cppReader_putChar (pfile, '\n');
1444 else if (kt->type == T_DEFINE)
1446 cppReader_setWritten (pfile, old_written);
1457 llassert (kt->func != NULL);
1458 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1463 /* Pass a directive through to the output file.
1464 BUF points to the contents of the directive, as a contiguous string.
1465 LIMIT points to the first character past the end of the directive.
1466 KEYWORD is the keyword-table entry for the directive. */
1469 pass_thru_directive (char *buf, char *limit,
1471 struct directive *keyword)
1473 int keyword_length = keyword->length;
1475 cpplib_reserve (pfile,
1476 size_fromInt (2 + keyword_length + (limit - buf)));
1477 cppReader_putCharQ (pfile, '#');
1478 /*@-observertrans@*/
1479 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1480 size_fromInt (keyword_length));
1481 /*:=observertrans@*/
1483 if (limit != buf && buf[0] != ' ')
1485 /* Was a bug, since reserve only used 1 + ... */
1486 cppReader_putCharQ (pfile, ' ');
1489 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1492 /* Read a replacement list for a macro with parameters.
1493 Build the DEFINITION structure.
1494 Reads characters of text starting at BUF until END.
1495 ARGLIST specifies the formal parameters to look for
1496 in the text of the definition; NARGS is the number of args
1497 in that list, or -1 for a macro name that wants no argument list.
1498 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1499 and NAMELEN is its length in characters.
1501 Note that comments, backslash-newlines, and leading white space
1502 have already been deleted from the argument. */
1505 collect_expansion (cppReader *pfile, char *buf, char *limit,
1506 int nargs, /*@null@*/ struct arglist *arglist)
1509 char *p, *lastp, *exp_p;
1510 struct reflist *endpat = NULL;
1511 /* Pointer to first nonspace after last ## seen. */
1513 /* Pointer to first nonspace after last single-# seen. */
1514 char *stringify = 0;
1516 char expected_delimiter = '\0';
1519 /* Scan thru the replacement list, ignoring comments and quoted
1520 strings, picking up on the macro calls. It does a linear search
1521 thru the arg list on every potential symbol. Profiling might say
1522 that something smarter should happen. */
1527 /* Find the beginning of the trailing whitespace. */
1530 while (p < limit && is_space[(int) limit[-1]])
1535 /* Allocate space for the text in the macro definition.
1536 Leading and trailing whitespace chars need 2 bytes each.
1537 Each other input char may or may not need 1 byte,
1538 so this is an upper bound. The extra 5 are for invented
1539 leading and trailing newline-marker and final null. */
1540 maxsize = (sizeof (*defn) + (limit - p) + 5);
1542 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1551 defn = (DEFINITION *) dmalloc (maxsize);
1552 defn->noExpand = FALSE;
1554 defn->pattern = NULL;
1555 defn->nargs = nargs;
1556 defn->predefined = NULL;
1558 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1561 defn->rest_args = NULL;
1562 defn->args.argnames = NULL;
1568 /* Add one initial space escape-marker to prevent accidental
1569 token-pasting (often removed by cpplib_macroExpand). */
1573 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1574 cppReader_errorLit (pfile,
1575 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1579 /* Process the main body of the definition. */
1581 int skipped_arg = 0;
1582 register char c = *p++;
1586 if (!cppReader_isTraditional (pfile)) {
1590 if (expected_delimiter != '\0')
1592 if (c == expected_delimiter)
1593 expected_delimiter = '\0';
1597 expected_delimiter = c;
1599 /*@switchbreak@*/ break;
1602 if (p < limit && (expected_delimiter != '\0'))
1604 /* In a string, backslash goes through
1605 and makes next char ordinary. */
1608 /*@switchbreak@*/ break;
1611 /* An '@' in a string or character constant stands for itself,
1612 and does not need to be escaped. */
1613 if (expected_delimiter == '\0')
1618 /*@switchbreak@*/ break;
1621 /* # is ordinary inside a string. */
1622 if (expected_delimiter != '\0')
1624 /*@switchbreak@*/ break;
1627 if (p < limit && *p == '#') {
1628 /* ##: concatenate preceding and following tokens. */
1629 /* Take out the first #, discard preceding whitespace. */
1633 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1639 /* Skip the second #. */
1641 /* Discard following whitespace. */
1642 SKIP_WHITE_SPACE (p);
1646 cppReader_errorLit (pfile,
1647 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1649 } else if (nargs >= 0) {
1650 /* Single #: stringify following argument ref.
1651 Don't leave the # in the expansion. */
1653 SKIP_WHITE_SPACE (p);
1654 if (p == limit || ! is_idstart[(int) *p]
1655 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1656 cppReader_errorLit (pfile,
1657 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1664 /*@switchbreak@*/ break;
1667 /* In -traditional mode, recognize arguments inside strings and
1668 and character constants, and ignore special properties of #.
1669 Arguments inside strings are considered "stringified", but no
1670 extra quote marks are supplied. */
1674 if (expected_delimiter != '\0') {
1675 if (c == expected_delimiter)
1676 expected_delimiter = '\0';
1678 expected_delimiter = c;
1679 /*@switchbreak@*/ break;
1682 /* Backslash quotes delimiters and itself, but not macro args. */
1683 if (expected_delimiter != '\0' && p < limit
1684 && (*p == expected_delimiter || *p == '\\')) {
1688 /*@switchbreak@*/ break;
1691 if (expected_delimiter != '\0') /* No comments inside strings. */
1692 /*@switchbreak@*/ break;
1694 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1695 this must be -traditional. So replace the comment with
1699 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1704 /*@switchbreak@*/ break;
1708 /* Handle the start of a symbol. */
1709 if (is_idchar[(int) c] && nargs > 0) {
1710 char *id_beg = p - 1;
1714 while (p != limit && is_idchar[(int) *p])
1719 id_len = size_fromInt (p - id_beg);
1721 if (is_idstart[(int) c]
1722 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1723 register struct arglist *arg;
1725 for (arg = arglist; arg != NULL; arg = arg->next) {
1726 struct reflist *tpat;
1728 if (arg->name[0] == c
1729 && arg->length == id_len
1730 && strncmp (arg->name, id_beg, id_len) == 0) {
1733 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1734 if (cppReader_isTraditional (pfile)) {
1735 cppReader_warning (pfile,
1736 message ("macro argument `%x' is stringified.",
1737 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1739 cppReader_warning (pfile,
1740 message ("macro arg `%x' would be stringified with -traditional.",
1741 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1744 /* If ANSI, don't actually substitute inside a string. */
1745 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1746 /*@innerbreak@*/ break;
1747 /* make a pat node for this arg and append it to the end of
1749 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1751 tpat->raw_before = (concat == id_beg);
1752 tpat->raw_after = 0;
1753 tpat->rest_args = arg->rest_args;
1754 tpat->stringify = (cppReader_isTraditional (pfile)
1755 ? expected_delimiter != '\0'
1756 : stringify == id_beg);
1760 defn->pattern = tpat;
1764 endpat->next = tpat;
1766 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1770 tpat->argno = arg->argno;
1771 tpat->nchars = exp_p - lastp;
1775 SKIP_WHITE_SPACE (p1);
1777 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1779 tpat->raw_after = 1;
1782 lastp = exp_p; /* place to start copying from next time */
1785 /*@innerbreak@*/ break;
1790 /* If this was not a macro arg, copy it into the expansion. */
1791 if (skipped_arg == 0) {
1792 register char *lim1 = p;
1800 if (stringify == id_beg)
1801 cppReader_errorLit (pfile,
1802 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1807 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1809 /* If ANSI, put in a "@ " marker to prevent token pasting.
1810 But not if "inside a string" (which in ANSI mode
1811 happens only for -D option). */
1818 defn->length = size_fromInt (exp_p - defn->expansion);
1820 /* Crash now if we overrun the allocated size. */
1821 if (defn->length + 1 > maxsize)
1823 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1826 /*@i1@*/ return defn; /* Spurious warning here */
1831 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1835 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1836 int nargs, /*@null@*/ struct arglist *arglist)
1839 char *p, *lastp, *exp_p;
1840 struct reflist *endpat = NULL;
1841 /* Pointer to first nonspace after last ## seen. */
1843 /* Pointer to first nonspace after last single-# seen. */
1844 char *stringify = 0;
1846 char expected_delimiter = '\0';
1849 /* Scan thru the replacement list, ignoring comments and quoted
1850 strings, picking up on the macro calls. It does a linear search
1851 thru the arg list on every potential symbol. Profiling might say
1852 that something smarter should happen. */
1856 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1857 fileloc_unparse (loc)));
1860 /* Find the beginning of the trailing whitespace. */
1863 while (p < limit && is_space[(int) limit[-1]])
1868 /* Allocate space for the text in the macro definition.
1869 Leading and trailing whitespace chars need 2 bytes each.
1870 Each other input char may or may not need 1 byte,
1871 so this is an upper bound. The extra 5 are for invented
1872 leading and trailing newline-marker and final null. */
1873 maxsize = (sizeof (*defn) + (limit - p) + 5);
1875 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1884 defn = (DEFINITION *) dmalloc (maxsize);
1885 defn->noExpand = FALSE;
1887 defn->pattern = NULL;
1888 defn->nargs = nargs;
1889 defn->predefined = NULL;
1890 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1893 defn->rest_args = NULL;
1894 defn->args.argnames = NULL;
1900 /* Add one initial space escape-marker to prevent accidental
1901 token-pasting (often removed by cpplib_macroExpand). */
1905 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1906 voptgenerror (FLG_PREPROC,
1907 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1912 /* Process the main body of the definition. */
1914 int skipped_arg = 0;
1915 register char c = *p++;
1919 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1923 if (expected_delimiter != '\0')
1925 if (c == expected_delimiter)
1926 expected_delimiter = '\0';
1930 expected_delimiter = c;
1932 /*@switchbreak@*/ break;
1935 if (p < limit && (expected_delimiter != '\0'))
1937 /* In a string, backslash goes through
1938 and makes next char ordinary. */
1941 /*@switchbreak@*/ break;
1944 /* An '@' in a string or character constant stands for itself,
1945 and does not need to be escaped. */
1946 if (expected_delimiter == '\0')
1951 /*@switchbreak@*/ break;
1954 /* # is ordinary inside a string. */
1955 if (expected_delimiter != '\0')
1957 /*@switchbreak@*/ break;
1960 if (p < limit && *p == '#') {
1961 /* ##: concatenate preceding and following tokens. */
1962 /* Take out the first #, discard preceding whitespace. */
1966 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1972 /* Skip the second #. */
1974 /* Discard following whitespace. */
1975 SKIP_WHITE_SPACE (p);
1979 voptgenerror (FLG_PREPROC,
1980 cstring_makeLiteral ("`##' at end of macro definition"),
1983 } else if (nargs >= 0) {
1984 /* Single #: stringify following argument ref.
1985 Don't leave the # in the expansion. */
1987 SKIP_WHITE_SPACE (p);
1988 if (p == limit || ! is_idstart[(int) *p]
1989 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1993 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
2002 /*@switchbreak@*/ break;
2005 /* In -traditional mode, recognize arguments inside strings and
2006 and character constants, and ignore special properties of #.
2007 Arguments inside strings are considered "stringified", but no
2008 extra quote marks are supplied. */
2012 if (expected_delimiter != '\0') {
2013 if (c == expected_delimiter)
2014 expected_delimiter = '\0';
2016 expected_delimiter = c;
2017 /*@switchbreak@*/ break;
2020 /* Backslash quotes delimiters and itself, but not macro args. */
2021 if (expected_delimiter != '\0' && p < limit
2022 && (*p == expected_delimiter || *p == '\\')) {
2026 /*@switchbreak@*/ break;
2029 if (expected_delimiter != '\0') /* No comments inside strings. */
2030 /*@switchbreak@*/ break;
2032 /* If we find a comment that wasn't removed by cppReader_handleDirective,
2033 this must be -traditional. So replace the comment with
2037 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2042 /*@switchbreak@*/ break;
2046 /* Handle the start of a symbol. */
2047 if (is_idchar[(int) c] && nargs > 0) {
2048 char *id_beg = p - 1;
2052 while (p != limit && is_idchar[(int) *p])
2057 id_len = size_fromInt (p - id_beg);
2059 if (is_idstart[(int) c]
2060 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2061 register struct arglist *arg;
2063 for (arg = arglist; arg != NULL; arg = arg->next) {
2064 struct reflist *tpat;
2066 if (arg->name[0] == c
2067 && arg->length == id_len
2068 && strncmp (arg->name, id_beg, id_len) == 0) {
2071 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2072 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2073 voptgenerror (FLG_PREPROC,
2074 message ("macro argument `%x' is stringified.",
2075 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2079 voptgenerror (FLG_PREPROC,
2080 message ("Macro arg `%x' would be stringified with -traditional.",
2081 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2086 /* If ANSI, don't actually substitute inside a string. */
2087 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2088 /*@innerbreak@*/ break;
2089 /* make a pat node for this arg and append it to the end of
2091 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2093 tpat->raw_before = (concat == id_beg);
2094 tpat->raw_after = 0;
2095 tpat->rest_args = arg->rest_args;
2096 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2097 ? expected_delimiter != '\0'
2098 : stringify == id_beg);
2102 defn->pattern = tpat;
2106 endpat->next = tpat;
2108 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2112 tpat->argno = arg->argno;
2113 tpat->nchars = exp_p - lastp;
2117 SKIP_WHITE_SPACE (p1);
2119 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2121 tpat->raw_after = 1;
2124 lastp = exp_p; /* place to start copying from next time */
2127 /*@innerbreak@*/ break;
2132 /* If this was not a macro arg, copy it into the expansion. */
2133 if (skipped_arg == 0) {
2134 register char *lim1 = p;
2142 if (stringify == id_beg)
2146 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2153 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2155 /* If ANSI, put in a "@ " marker to prevent token pasting.
2156 But not if "inside a string" (which in ANSI mode
2157 happens only for -D option). */
2164 defn->length = size_fromInt (exp_p - defn->expansion);
2166 /* Crash now if we overrun the allocated size. */
2167 if (defn->length + 1 > maxsize)
2169 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2172 /*@i1@*/ return defn; /* Spurious warning here */
2176 * special extension string that can be added to the last macro argument to
2177 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2178 * #define wow(a, b...) process (b, a, b)
2179 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2180 * { wow (one, two); } -> { process (two, one, two); }
2181 * if this "rest_arg" is used with the concat token '##' and if it is not
2182 * supplied then the token attached to with ## will not be outputted. Ex:
2183 * #define wow (a, b...) process (b ## , a, ## b)
2184 * { wow (1, 2); } -> { process (2, 1, 2); }
2185 * { wow (one); } -> { process (one); {
2188 /*@-readonlytrans@*/
2189 static char rest_extension[] = "...";
2190 /*:=readonlytrans@*/
2193 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2195 /* Create a DEFINITION node from a #define directive. Arguments are
2196 as for do_define. */
2199 static /*@null@*/ macroDef
2200 create_definition (/*@exposed@*/ char *buf, char *limit,
2201 cppReader *pfile, bool predefinition,
2204 char *bp; /* temp ptr into input buffer */
2205 char *symname; /* remember where symbol name starts */
2206 size_t sym_length; /* and how long it is */
2207 int rest_args = 0; /* really int! */
2210 cstring file = (CPPBUFFER (pfile) != NULL)
2211 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2213 int arglengths = 0; /* Accumulate lengths of arg names
2214 plus number of args. */
2218 DPRINTF (("Create definition: %s", buf));
2221 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2225 while (is_hor_space[(int) *bp])
2230 symname = bp; /* remember where it starts */
2232 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2236 /* Lossage will occur if identifiers or control keywords are broken
2237 across lines using backslash. This is not the right place to take
2241 struct arglist *arg_ptrs = NULL;
2244 bp++; /* skip '(' */
2245 SKIP_WHITE_SPACE (bp);
2247 /* Loop over macro argument names. */
2250 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2252 temp->next = arg_ptrs;
2253 temp->argno = argno++;
2254 temp->rest_args = 0;
2260 cppReader_pedwarn (pfile,
2261 message ("another parameter follows `%s'",
2262 cstring_fromChars (rest_extension)));
2265 if (!is_idstart[(int) *bp])
2267 cppReader_pedwarnLit (pfile,
2268 cstring_makeLiteralTemp ("Invalid character in macro parameter name"));
2271 /* Find the end of the arg name. */
2272 while (is_idchar[(int) *bp])
2275 /* do we have a "special" rest-args extension here? */
2276 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2277 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2280 temp->rest_args = 1;
2281 /*@innerbreak@*/ break;
2285 temp->length = size_fromInt (bp - temp->name);
2289 bp += REST_EXTENSION_LENGTH;
2292 arglengths += temp->length + 2;
2293 SKIP_WHITE_SPACE (bp);
2295 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2296 cppReader_errorLit (pfile,
2297 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2303 SKIP_WHITE_SPACE (bp);
2306 cppReader_errorLit (pfile,
2307 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2311 struct arglist *otemp;
2313 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2315 if (temp->length == otemp->length &&
2316 strncmp (temp->name, otemp->name, temp->length) == 0) {
2317 cstring name = cstring_copyLength (temp->name, temp->length);
2318 cppReader_error (pfile,
2319 message ("duplicate argument name `%x' in `#define'", name));
2326 ++bp; /* skip paren */
2327 SKIP_WHITE_SPACE (bp);
2328 /* now everything from bp before limit is the definition. */
2329 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2330 defn->rest_args = rest_args;
2332 /* Now set defn->args.argnames to the result of concatenating
2333 the argument names in reverse order
2334 with comma-space between them. */
2335 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2338 struct arglist *temp;
2340 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2342 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2344 if (temp->next != 0)
2346 defn->args.argnames[i++] = ',';
2347 defn->args.argnames[i++] = ' ';
2351 defn->args.argnames[i] = '\0';
2356 /* Simple expansion or empty definition. */
2360 if (is_hor_space[(int) *bp]) {
2362 SKIP_WHITE_SPACE (bp);
2365 case '!': case '\"': case '#': case '%': case '&': case '\'':
2366 case ')': case '*': case '+': case ',': case '-': case '.':
2367 case '/': case ':': case ';': case '<': case '=': case '>':
2368 case '?': case '[': case '\\': case ']': case '^': case '{':
2369 case '|': case '}': case '~':
2370 cppReader_warning (pfile,
2371 message ("Missing white space after #define %x",
2372 cstring_prefix (cstring_fromChars (symname),
2377 cppReader_pedwarn (pfile,
2378 message ("Missing white space after #define %x",
2379 cstring_prefix (cstring_fromChars (symname),
2385 /* now everything from bp before limit is the definition. */
2386 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2387 defn->args.argnames = mstring_createEmpty ();
2390 defn->noExpand = noExpand;
2391 DPRINTF (("No expand: %d", noExpand));
2395 /* not: llassert (cstring_isUndefined (defn->file)); */
2398 /* OP is null if this is a predefinition */
2399 defn->predefined = predefinition;
2401 mdef.symnam = symname;
2402 mdef.symlen = sym_length;
2413 cpplib_createDefinition (cstring def,
2418 char *buf = cstring_toCharsSafe (def);
2419 char *limit = buf + cstring_length (def);
2420 char *bp; /* temp ptr into input buffer */
2421 char *symname; /* remember where symbol name starts */
2422 size_t sym_length; /* and how long it is */
2423 int rest_args = 0; /* really int! */
2424 int line = fileloc_lineno (loc);
2425 cstring file = fileloc_filename (loc);
2427 int arglengths = 0; /* Accumulate lengths of arg names
2428 plus number of args. */
2433 DPRINTF (("Creating definition: %s", buf));
2435 while (is_hor_space[(int) *bp])
2440 symname = bp; /* remember where it starts */
2442 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2444 DPRINTF (("length: %d", sym_length));
2448 DPRINTF (("Here: %s", bp));
2450 /* Lossage will occur if identifiers or control keywords are broken
2451 across lines using backslash. This is not the right place to take
2455 struct arglist *arg_ptrs = NULL;
2458 bp++; /* skip '(' */
2459 SKIP_WHITE_SPACE (bp);
2461 /* Loop over macro argument names. */
2464 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2466 temp->next = arg_ptrs;
2467 temp->argno = argno++;
2468 temp->rest_args = 0;
2474 voptgenerror (FLG_PREPROC,
2475 message ("Another parameter follows %s",
2476 cstring_fromChars (rest_extension)),
2480 if (!is_idstart[(int) *bp])
2482 voptgenerror (FLG_PREPROC,
2483 message ("Invalid character in macro parameter name: %c", *bp),
2487 /* Find the end of the arg name. */
2488 while (is_idchar[(int) *bp])
2491 /* do we have a "special" rest-args extension here? */
2492 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2493 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2496 temp->rest_args = 1;
2497 /*@innerbreak@*/ break;
2501 temp->length = size_fromInt (bp - temp->name);
2505 bp += REST_EXTENSION_LENGTH;
2508 arglengths += temp->length + 2;
2509 SKIP_WHITE_SPACE (bp);
2511 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2512 voptgenerror (FLG_PREPROC,
2513 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2520 SKIP_WHITE_SPACE (bp);
2523 voptgenerror (FLG_PREPROC,
2524 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2529 struct arglist *otemp;
2531 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2533 if (temp->length == otemp->length &&
2534 strncmp (temp->name, otemp->name, temp->length) == 0) {
2535 cstring name = cstring_copyLength (temp->name, temp->length);
2537 voptgenerror (FLG_PREPROC,
2538 message ("Duplicate argument name in #define: %s", name),
2546 ++bp; /* skip paren */
2547 SKIP_WHITE_SPACE (bp);
2548 /* now everything from bp before limit is the definition. */
2549 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2550 defn->rest_args = rest_args;
2552 /* Now set defn->args.argnames to the result of concatenating
2553 the argument names in reverse order
2554 with comma-space between them. */
2555 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2558 struct arglist *temp;
2560 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2561 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2563 if (temp->next != 0) {
2564 defn->args.argnames[i++] = ',';
2565 defn->args.argnames[i++] = ' ';
2569 defn->args.argnames[i] = '\0';
2574 /* Simple expansion or empty definition. */
2578 if (is_hor_space[(int) *bp]) {
2580 SKIP_WHITE_SPACE (bp);
2583 case '!': case '\"': case '#': case '%': case '&': case '\'':
2584 case ')': case '*': case '+': case ',': case '-': case '.':
2585 case '/': case ':': case ';': case '<': case '=': case '>':
2586 case '?': case '[': case '\\': case ']': case '^': case '{':
2587 case '|': case '}': case '~':
2588 voptgenerror (FLG_PREPROC,
2589 message ("Missing white space after #define %x",
2590 cstring_prefix (cstring_fromChars (symname),
2596 voptgenerror (FLG_PREPROC,
2597 message ("Missing white space after #define %x",
2598 cstring_prefix (cstring_fromChars (symname),
2606 /* now everything from bp before limit is the definition. */
2607 llassert (limit > bp);
2608 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2609 defn->args.argnames = mstring_createEmpty ();
2612 defn->noExpand = noExpand;
2613 DPRINTF (("No expand: %d", noExpand));
2617 /* not: llassert (cstring_isUndefined (defn->file)); */
2620 /* OP is null if this is a predefinition */
2621 defn->predefined = predefinition;
2624 mdef.symnam = symname;
2625 mdef.symlen = sym_length;
2635 /* Check a purported macro name SYMNAME, and yield its length.
2636 USAGE is the kind of name this is intended for. */
2638 size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2643 for (p = symname; is_idchar[(int) *p]; p++)
2648 sym_length = size_fromInt (p - symname);
2651 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2653 cppReader_error (pfile, message ("invalid %s name", usage));
2655 else if (!is_idstart[(int) *symname])
2657 char *msg = (char *) dmalloc (sym_length + 1);
2658 memcpy (msg, symname, sym_length);
2659 msg[sym_length] = '\0';
2660 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2661 cstring_fromChars (msg)));
2666 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2668 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2677 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2680 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2685 for (p = symname; is_idchar[(int) *p]; p++)
2690 sym_length = size_fromInt (p - symname);
2693 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2695 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2696 cstring_fromChars (symname)), loc);
2698 else if (!is_idstart[(int) *symname])
2700 char *msg = (char *) dmalloc (sym_length + 1);
2701 memcpy (msg, symname, sym_length);
2702 msg[sym_length] = '\0';
2703 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2704 cstring_fromChars (msg)),
2710 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2712 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2719 /* Return zero if two DEFINITIONs are isomorphic. */
2722 compare_defs (DEFINITION *d1, DEFINITION *d2)
2724 register struct reflist *a1, *a2;
2725 register char *p1 = d1->expansion;
2726 register char *p2 = d2->expansion;
2729 if (d1->nargs != d2->nargs)
2734 llassert (d1->args.argnames != NULL);
2735 llassert (d2->args.argnames != NULL);
2737 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2742 for (a1 = d1->pattern, a2 = d2->pattern;
2743 (a1 != NULL) && (a2 != NULL);
2744 a1 = a1->next, a2 = a2->next) {
2745 if (!((a1->nchars == a2->nchars
2746 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2747 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2748 || a1->argno != a2->argno
2749 || a1->stringify != a2->stringify
2750 || a1->raw_before != a2->raw_before
2751 || a1->raw_after != a2->raw_after)
2760 if (comp_def_part (first, p1, size_toInt (d1->length - (p1 - d1->expansion)),
2761 p2, size_toInt (d2->length - (p2 - d2->expansion)), 1))
2768 ** Return TRUE if two parts of two macro definitions are effectively different.
2769 ** One of the parts starts at BEG1 and has LEN1 chars;
2770 ** the other has LEN2 chars at BEG2.
2771 ** Any sequence of whitespace matches any other sequence of whitespace.
2772 ** FIRST means these parts are the first of a macro definition;
2773 ** so ignore leading whitespace entirely.
2774 ** LAST means these parts are the last of a macro definition;
2775 ** so ignore trailing whitespace entirely.
2779 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2781 char *end1 = beg1 + len1;
2782 char *end2 = beg2 + len2;
2785 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2786 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2789 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2790 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2792 while (beg1 != end1 && beg2 != end2) {
2793 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2794 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2795 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2796 } else if (*beg1 == *beg2) {
2800 return (beg1 != end1) || (beg2 != end2);
2804 ** Process a #define command.
2805 ** BUF points to the contents of the #define command, as a contiguous string.
2806 ** LIMIT points to the first character past the end of the definition.
2807 ** KEYWORD is the keyword-table entry for #define,
2808 ** or NULL for a "predefined" macro.
2812 do_defineAux (cppReader *pfile, struct directive *keyword,
2813 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2819 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2824 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2826 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2830 /* Redefining a precompiled key is ok. */
2831 if (hp->type == T_PCSTRING)
2833 /* Redefining a macro is ok if the definitions are the same. */
2834 else if (hp->type == T_MACRO)
2835 ok = !compare_defs (mdef.defn, hp->value.defn);
2836 /* Redefining a constant is ok with -D. */
2837 else if (hp->type == T_CONST)
2838 ok = !CPPOPTIONS (pfile)->done_initializing;
2843 /* Print the warning if it's not ok. */
2847 ** If we are passing through #define and #undef directives, do
2848 ** that for this re-definition now.
2851 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2853 /* llassert (keyword != NULL); */
2854 pass_thru_directive (buf, limit, pfile, keyword);
2857 cpp_setLocation (pfile);
2859 if (hp->type == T_MACRO)
2861 if (hp->value.defn->noExpand)
2863 ; /* error will be reported checking macros */
2869 message ("Macro %q already defined",
2870 cstring_copyLength (mdef.symnam, mdef.symlen)),
2871 message ("%q: Previous definition of %q",
2872 fileloc_unparseRaw (hp->value.defn->file,
2873 (int) hp->value.defn->line),
2874 cstring_copyLength (mdef.symnam, mdef.symlen)));
2879 genppllerror (FLG_MACROREDEF,
2880 message ("Macro %q already defined",
2881 cstring_copyLength (mdef.symnam,
2886 /* Replace the old definition. */
2888 hp->value.defn = mdef.defn;
2893 ** If we are passing through #define and #undef directives, do
2894 ** that for this new definition now.
2899 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2901 pass_thru_directive (buf, limit, pfile, keyword);
2904 DPRINTF (("Define macro: %s / %d",
2905 mdef.symnam, mdef.defn->noExpand));
2907 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2909 } /*@=branchstate@*/
2918 do_define (cppReader *pfile, struct directive *keyword,
2919 /*@exposed@*/ char *buf, char *limit)
2921 DPRINTF (("Regular do define"));
2922 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2926 ** This structure represents one parsed argument in a macro call.
2927 ** `raw' points to the argument text as written (`raw_length' is its length).
2928 ** `expanded' points to the argument's macro-expansion
2929 ** (its length is `expand_length').
2930 ** `stringified_length' is the length the argument would have
2932 ** `use_count' is the number of times this macro arg is substituted
2933 ** into the macro. If the actual use count exceeds 10,
2934 ** the value stored is 10.
2937 /* raw and expanded are relative to ARG_BASE */
2939 #define ARG_BASE ((pfile)->token_buffer)
2942 /* Strings relative to pfile->token_buffer */
2948 int stringified_length;
2954 ** Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2955 ** If BUFFER != NULL, then use the LENGTH characters in BUFFER
2956 ** as the new input buffer.
2957 ** Return the new buffer, or NULL on failure.
2960 /*@null@*/ /*@exposed@*/ cppBuffer *
2961 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2963 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2965 if (buf == pfile->buffer_stack)
2967 cppReader_fatalError
2969 message ("%s: macro or `#include' recursion too deep",
2970 (buf->fname != NULL)
2972 : cstring_makeLiteral ("<no name>")));
2973 sfreeEventually (buffer);
2977 llassert (buf != NULL);
2980 memset ((char *) buf, 0, sizeof (*buf));
2981 DPRINTF (("Pushing buffer: %s", cstring_copyLength (buffer, length)));
2982 CPPBUFFER (pfile) = buf;
2984 buf->if_stack = pfile->if_stack;
2985 buf->cleanup = cppReader_nullCleanup;
2986 buf->underflow = cppReader_nullUnderflow;
2988 buf->cur = buf->buf;
2992 buf->alimit = buf->rlimit = buffer + length;
2996 buf->alimit = buf->rlimit = NULL;
3003 cppReader_popBuffer (cppReader *pfile)
3005 cppBuffer *buf = CPPBUFFER (pfile);
3007 llassert (buf != NULL);
3009 (void) (*buf->cleanup) (buf, pfile);
3010 return ++CPPBUFFER (pfile);
3014 ** Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
3015 ** Pop the buffer when done.
3019 cppReader_scanBuffer (cppReader *pfile)
3021 cppBuffer *buffer = CPPBUFFER (pfile);
3024 enum cpp_token token;
3026 token = cpplib_getToken (pfile);
3028 if (token == CPP_EOF) /* Should not happen ... */
3033 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
3035 (void) cppReader_popBuffer (pfile);
3042 * Rescan a string (which may have escape marks) into pfile's buffer.
3043 * Place the result in pfile->token_buffer.
3045 * The input is copied before it is scanned, so it is safe to pass
3046 * it something from the token_buffer that will get overwritten
3047 * (because it follows cpplib_getWritten). This is used by do_include.
3051 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3053 register cppBuffer *ip;
3054 char *limit = buf + length;
3055 char *buf1, *p1, *p2;
3057 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
3059 /* evans - 2001-08-26
3060 ** length is unsigned - this doesn't make sense
3066 /* Set up the input on the input stack. */
3068 buf1 = (char *) dmalloc (length + 1);
3078 buf1[length] = '\0';
3080 ip = cppReader_pushBuffer (pfile, buf1, length);
3085 ip->has_escapes = TRUE;
3087 /* Scan the input, create the output. */
3088 cppReader_scanBuffer (pfile);
3090 cppReader_nullTerminate (pfile);
3094 adjust_position (char *buf, char *limit, int *linep, int *colp)
3100 (*linep)++, (*colp) = 1;
3106 /* Move line_base forward, updating lineno and colno. */
3109 update_position (cppBuffer *pbuf)
3112 char *new_pos = pbuf->cur;
3113 register struct parse_marker *mark;
3115 llassert (pbuf->buf != NULL);
3116 old_pos = pbuf->buf + pbuf->line_base;
3118 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3120 if (pbuf->buf + mark->position < new_pos)
3121 new_pos = pbuf->buf + mark->position;
3123 pbuf->line_base += new_pos - old_pos;
3125 llassert (old_pos != NULL);
3126 llassert (new_pos != NULL);
3128 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3132 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3133 /*@null@*/ /*@out@*/ int *colp)
3141 } /*@=branchstate@*/
3145 *linep = pbuf->lineno;
3146 *colp = pbuf->colno;
3148 llassert (pbuf->buf != NULL);
3149 llassert (pbuf->cur != NULL);
3151 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3160 /* Return the cppBuffer that corresponds to a file (not a macro). */
3162 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3164 cppBuffer *ip = cppReader_getBuffer (pfile);
3167 ip != NULL && ip != cppReader_nullBuffer (pfile);
3168 ip = cppBuffer_prevBuffer (ip))
3170 if (ip->fname != NULL)
3180 count_newlines (char *buf, char *limit)
3182 register long count = 0;
3194 * write out a #line command, for instance, after an #include file.
3195 * If CONDITIONAL is nonzero, we can omit the #line if it would
3196 * appear to be a no-op, and we can output a few newlines instead
3197 * if we want to increase the line number by a small amount.
3198 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3202 output_line_command (cppReader *pfile, bool conditional,
3203 enum file_change_code file_change)
3206 cppBuffer *ip = CPPBUFFER (pfile);
3209 llassert (ip != NULL);
3211 if (ip->fname == NULL)
3214 update_position (ip);
3216 if (CPPOPTIONS (pfile)->no_line_commands
3217 || CPPOPTIONS (pfile)->no_output)
3220 buf = CPPBUFFER (pfile);
3222 llassert (buf != NULL);
3227 llassert (ip->cur != NULL);
3229 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3231 if (CPPOPTIONS (pfile)->no_line_commands)
3235 if (line == pfile->lineno)
3238 /* If the inherited line number is a little too small,
3239 output some newlines instead of a #line command. */
3241 if (line > pfile->lineno && line < pfile->lineno + 8)
3243 cpplib_reserve (pfile, 20);
3244 while (line > pfile->lineno)
3246 cppReader_putCharQ (pfile, '\n');
3254 cpplib_reserve (pfile, 4 * cstring_length (ip->nominal_fname) + 50);
3257 #ifdef OUTPUT_LINE_COMMANDS
3258 static char sharp_line[] = "#line ";
3260 static char sharp_line[] = "# ";
3262 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3265 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3266 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3268 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3270 if (file_change != same_file) {
3271 cppReader_putCharQ (pfile, ' ');
3272 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3274 /* Tell cc1 if following text comes from a system header file. */
3275 if (ip->system_header_p != '\0') {
3276 cppReader_putCharQ (pfile, ' ');
3277 cppReader_putCharQ (pfile, '3');
3279 #ifndef NO_IMPLICIT_EXTERN_C
3280 /* Tell cc1plus if following text should be treated as C. */
3281 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3282 cppReader_putCharQ (pfile, ' ');
3283 cppReader_putCharQ (pfile, '4');
3286 cppReader_putCharQ (pfile, '\n');
3287 pfile->lineno = line;
3292 * Parse a macro argument and append the info on PFILE's token_buffer.
3293 * REST_ARGS means to absorb the rest of the args.
3294 * Return nonzero to indicate a syntax error.
3297 static enum cpp_token
3298 macarg (cppReader *pfile, int rest_args)
3301 enum cpp_token token;
3302 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3303 bool oldexpand = pfile->no_macro_expand;
3304 CPPOPTIONS (pfile)->put_out_comments = 1;
3306 /* Try to parse as much of the argument as exists at this
3307 input stack level. */
3309 pfile->no_macro_expand = TRUE;
3313 token = cpplib_getToken (pfile);
3320 /* If we've hit end of file, it's an error (reported by caller).
3321 Ditto if it's the end of cpp_expand_to_buffer text.
3322 If we've hit end of macro, just continue. */
3323 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3325 /*@switchbreak@*/ break;
3328 /*@switchbreak@*/ break;
3332 /*@switchbreak@*/ break;
3334 /* if we've returned to lowest level and
3335 we aren't absorbing all args */
3336 if (paren == 0 && rest_args == 0)
3338 /*@switchbreak@*/ break;
3340 /* Remove ',' or ')' from argument buffer. */
3341 cppReader_adjustWritten (pfile, -1);
3349 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3350 pfile->no_macro_expand = oldexpand;
3356 /* Turn newlines to spaces in the string of length LENGTH at START,
3357 except inside of string constants.
3358 The string is copied into itself with its beginning staying fixed. */
3361 change_newlines (char *start, int length)
3365 register char *limit;
3369 limit = start + length;
3372 while (ibp < limit) {
3373 *obp++ = c = *ibp++;
3378 /* Notice and skip strings, so that we don't delete newlines in them. */
3381 while (ibp < limit) {
3382 *obp++ = c = *ibp++;
3384 /*@innerbreak@*/ break;
3385 if (c == '\n' && quotec == '\'')
3386 /*@innerbreak@*/ break;
3389 /*@switchbreak@*/ break;
3396 static /*@observer@*/ struct tm *
3397 timestamp (/*@returned@*/ cppReader *pfile)
3399 if (pfile->timebuf == NULL)
3401 time_t t = time ((time_t *) 0);
3402 pfile->timebuf = localtime (&t);
3405 llassert (pfile->timebuf != NULL);
3407 return pfile->timebuf;
3410 static ob_mstring monthnames[] = {
3411 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3412 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3416 * expand things like __FILE__. Place the expansion into the output
3417 * buffer *without* rescanning.
3421 special_symbol (hashNode hp, cppReader *pfile)
3423 cstring buf = cstring_undefined;
3429 int paren = 0; /* For special `defined' keyword */
3431 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3433 if (ip == cppReader_nullBuffer (pfile))
3435 cppReader_errorLit (pfile,
3436 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3437 return; /* the show must go on */
3440 if (ip != NULL && ip->fname != NULL)
3452 if (hp->type == T_BASE_FILE)
3454 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3456 ip = cppBuffer_prevBuffer (ip);
3460 llassert (ip != NULL);
3461 string = cstring_toCharsSafe (ip->nominal_fname);
3468 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3469 quote_string (pfile, string);
3472 case T_FUNC: /* added in ISO C99 */
3474 /* We don't know the actual name of the function, but it doesn't matter */
3475 char *string = "** function-name **";
3476 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3477 quote_string (pfile, string);
3481 case T_INCLUDE_LEVEL:
3483 ip = cppReader_getBuffer (pfile);
3485 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3486 ip = cppBuffer_prevBuffer (ip))
3488 if (ip != NULL && ip->fname != NULL)
3494 buf = message ("%d", true_indepth - 1);
3498 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3501 #ifndef NO_BUILTIN_SIZE_TYPE
3503 buf = cstring_makeLiteral (SIZE_TYPE);
3507 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3508 case T_PTRDIFF_TYPE:
3509 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3514 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3517 case T_USER_LABEL_PREFIX_TYPE:
3518 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3521 case T_REGISTER_PREFIX_TYPE:
3522 buf = cstring_makeLiteral (REGISTER_PREFIX);
3526 buf = message ("%d", hp->value.ival);
3533 int line = ip->lineno;
3534 int col = ip->colno;
3536 llassert (ip->cur != NULL);
3537 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3539 buf = message ("%d", (int) line);
3551 char *sbuf = (char *) dmalloc (20);
3552 timebuf = timestamp (pfile);
3553 if (hp->type == T_DATE)
3555 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3556 timebuf->tm_mday, timebuf->tm_year + 1900);
3560 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3564 buf = cstring_fromCharsNew (sbuf);
3569 case T_SPEC_DEFINED:
3570 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3571 ip = cppReader_getBuffer (pfile);
3572 llassert (ip != NULL);
3573 llassert (ip->cur != NULL);
3574 SKIP_WHITE_SPACE (ip->cur);
3576 if (*ip->cur == '(')
3579 ip->cur++; /* Skip over the paren */
3580 SKIP_WHITE_SPACE (ip->cur);
3583 if (!is_idstart[(int) *ip->cur])
3585 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3588 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3591 buf = cstring_makeLiteral (" 1 ");
3594 while (is_idchar[(int) *ip->cur])
3599 SKIP_WHITE_SPACE (ip->cur);
3603 if (*ip->cur != ')')
3611 cppReader_errorLit (pfile,
3612 cstring_makeLiteralTemp ("`defined' without an identifier"));
3616 cpp_setLocation (pfile);
3617 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3620 len = cstring_length (buf);
3622 cpplib_reserve (pfile, len + 1);
3623 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3624 cppReader_nullTerminateQ (pfile);
3630 /* Write out a #define command for the special named MACRO_NAME
3631 to PFILE's token_buffer. */
3634 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3636 static char define_directive[] = "#define ";
3637 size_t macro_name_length = strlen (macro_name);
3638 output_line_command (pfile, 0, same_file);
3639 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3640 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3641 cppReader_putStrN (pfile, macro_name, macro_name_length);
3642 cppReader_putCharQ (pfile, ' ');
3643 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3644 cppReader_putChar (pfile, '\n');
3647 /* Initialize the built-in macros. */
3650 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3651 int len, enum node_type type,
3652 int ivalue, /*@null@*/ /*@only@*/ char *value,
3655 cstring sname = cstring_fromCharsNew (name);
3657 llassert (usymtab_inGlobalScope ());
3660 ** Be careful here: this is done before the ctype table has
3661 ** been initialized.
3664 if (!usymtab_exists (sname))
3666 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3668 if (ctype_equal (ctyp, ctype_string))
3670 qualList ql = qualList_new ();
3671 ql = qualList_add (ql, qual_createObserver ());
3672 uentry_reflectQualifiers (ue, ql);
3676 usymtab_addGlobalEntry (ue);
3683 (void) cpphash_install (name, len, type, ivalue, value, hash);
3684 cstring_free (sname);
3688 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3689 int len, enum node_type type,
3691 /*@only@*/ /*@null@*/ char *value, int hash)
3693 cstring sname = cstring_fromChars (name);
3694 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3696 llassert (usymtab_inGlobalScope ());
3698 if (!usymtab_existsTypeEither (sname))
3700 uentry ue = uentry_makeDatatype (sname, ctyp,
3701 NO, qual_createConcrete (),
3702 fileloc_createBuiltin ());
3703 llassert (!usymtab_existsEither (sname));
3704 usymtab_addGlobalEntry (ue);
3707 (void) cpphash_install (name, len, type, ivalue, value, hash);
3711 initialize_builtins (cppReader *pfile)
3713 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3714 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3715 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3716 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3717 cpplib_installBuiltin ("__func__", ctype_string, -1, T_FUNC, 0, NULL, -1);
3718 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3719 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3720 #ifndef NO_BUILTIN_SIZE_TYPE
3721 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3723 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3724 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3726 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3727 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3728 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3729 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3731 if (!cppReader_isTraditional (pfile))
3733 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3737 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3741 ** This is supplied using a -D by the compiler driver
3742 ** so that it is present only when truly compiling with GNU C.
3745 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3747 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3748 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3750 /*drl 1/9/2001/ try to define the right symbol for the architecture
3751 We use autoconf to determine the target cpu
3753 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3755 /*drl 1/2/2002 set some flags based on uname
3756 I'd like to be able to do this with autoconf macro instead...
3759 /*Thanks to Nelson Beebe for suggesting possible values for these */
3761 if (! strcmp (UNAME, "Linux"))
3764 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3768 else if(! strcmp (UNAME, "Darwin"))
3770 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3771 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3773 else if(! strcmp (UNAME, "HP-UX"))
3775 cpplib_installBuiltin ("PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3776 cpplib_installBuiltin ("_HIUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3777 cpplib_installBuiltin ("_HPUX_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3778 cpplib_installBuiltin ("_PA_RISC1_1", ctype_int, -1, T_CONST, 2, NULL, -1);
3779 cpplib_installBuiltin ("__PWB", ctype_int, -1, T_CONST, 2, NULL, -1);
3780 cpplib_installBuiltin ("__PWB__", ctype_int, -1, T_CONST, 2, NULL, -1);
3781 cpplib_installBuiltin ("__STDC_EXT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3782 cpplib_installBuiltin ("__hp9000s700", ctype_int, -1, T_CONST, 2, NULL, -1);
3783 cpplib_installBuiltin ("__hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3784 cpplib_installBuiltin ("__hp9000s800__", ctype_int, -1, T_CONST, 2, NULL, -1);
3785 cpplib_installBuiltin ("__hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3786 cpplib_installBuiltin ("__hp9k8__", ctype_int, -1, T_CONST, 2, NULL, -1);
3787 cpplib_installBuiltin ("__hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3788 cpplib_installBuiltin ("__hppa__", ctype_int, -1, T_CONST, 2, NULL, -1);
3789 cpplib_installBuiltin ("__hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3790 cpplib_installBuiltin ("__hpux__", ctype_int, -1, T_CONST, 2, NULL, -1);
3791 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3792 cpplib_installBuiltin ("__unix__", ctype_int, -1, T_CONST, 2, NULL, -1);
3793 cpplib_installBuiltin ("hp9000s800", ctype_int, -1, T_CONST, 2, NULL, -1);
3794 cpplib_installBuiltin ("hp9k8", ctype_int, -1, T_CONST, 2, NULL, -1);
3795 cpplib_installBuiltin ("hppa", ctype_int, -1, T_CONST, 2, NULL, -1);
3796 cpplib_installBuiltin ("hpux", ctype_int, -1, T_CONST, 2, NULL, -1);
3797 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3799 else if(! strcmp (UNAME, "IRIX64"))
3801 cpplib_installBuiltin ("LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3802 cpplib_installBuiltin ("MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3803 cpplib_installBuiltin ("_ABIN32", ctype_int, -1, T_CONST, 2, NULL, -1);
3804 cpplib_installBuiltin ("_COMPILER_VERSION", ctype_int, -1, T_CONST, 730, NULL, -1);
3805 cpplib_installBuiltin ("_LANGUAGE_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3806 cpplib_installBuiltin ("_LONGLONG", ctype_int, -1, T_CONST, 2, NULL, -1);
3807 cpplib_installBuiltin ("_MIPSEB", ctype_int, -1, T_CONST, 2, NULL, -1);
3808 cpplib_installBuiltin ("_MIPS_FPSET", ctype_int, -1, T_CONST, 32, NULL, -1);
3809 cpplib_installBuiltin ("_MIPS_ISA", ctype_int, -1, T_CONST, 3, NULL, -1);
3810 /*_MIPS_SIM=_ABIN32*/
3811 cpplib_installBuiltin ("_MIPS_SIM", ctype_int, -1, T_CONST, 2, NULL , -1);
3812 cpplib_installBuiltin ("_MIPS_SZINT", ctype_int, -1, T_CONST, 32, NULL, -1);
3813 cpplib_installBuiltin ("_MIPS_SZLONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3814 cpplib_installBuiltin ("_MIPS_SZPTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3815 cpplib_installBuiltin ("_MODERN_C", ctype_int, -1, T_CONST, 2, NULL, -1);
3816 cpplib_installBuiltin ("_PIC", ctype_int, -1, T_CONST, 2, NULL, -1);
3817 cpplib_installBuiltin ("_SGI_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3818 cpplib_installBuiltin ("_SIZE_INT", ctype_int, -1, T_CONST, 32, NULL, -1);
3819 cpplib_installBuiltin ("_SIZE_LONG", ctype_int, -1, T_CONST, 32, NULL, -1);
3820 cpplib_installBuiltin ("_SIZE_PTR", ctype_int, -1, T_CONST, 32, NULL, -1);
3821 cpplib_installBuiltin ("_SVR4_SOURCE", ctype_int, -1, T_CONST, 2, NULL, -1);
3822 cpplib_installBuiltin ("_SYSTYPE_SVR4", ctype_int, -1, T_CONST, 2, NULL, -1);
3823 cpplib_installBuiltin ("__DSO__", ctype_int, -1, T_CONST, 2, NULL, -1);
3824 cpplib_installBuiltin ("__EXTENSIONS__", ctype_int, -1, T_CONST, 2, NULL, -1);
3825 cpplib_installBuiltin ("__INLINE_INTRINSICS", ctype_int, -1, T_CONST, 2, NULL, -1);
3826 cpplib_installBuiltin ("__MATH_HAS_NO_SIDE_EFFECTS", ctype_int, -1, T_CONST, 2, NULL, -1);
3827 cpplib_installBuiltin ("__host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3828 cpplib_installBuiltin ("__mips", ctype_int, -1, T_CONST, 3, NULL, -1);
3829 cpplib_installBuiltin ("__sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3830 cpplib_installBuiltin ("__unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3831 cpplib_installBuiltin ("host_mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3832 cpplib_installBuiltin ("mips", ctype_int, -1, T_CONST, 2, NULL, -1);
3833 cpplib_installBuiltin ("sgi", ctype_int, -1, T_CONST, 2, NULL, -1);
3834 cpplib_installBuiltin ("unix", ctype_int, -1, T_CONST, 2, NULL, -1);
3836 else if(! strcmp (UNAME, "OSF1"))
3838 cpplib_installBuiltin ("__alpha", ctype_int, -1, T_CONST, 2, NULL, -1);
3840 else if (!strcmp (UNAME, "Rhapsody"))
3842 cpplib_installBuiltin ("__ppc__", ctype_int, -1, T_CONST, 2, NULL, -1);
3843 cpplib_installBuiltin ("__BIG_ENDIAN__", ctype_int, -1, T_CONST, 2, NULL, -1);
3846 else if (!strcmp (UNAME, "SunOS"))
3848 cpplib_installBuiltin ("__OPEN_MAX", ctype_int, -1, T_CONST, 20, NULL, -1);
3849 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, 2, NULL, -1);
3850 cpplib_installBuiltin ("__sparc", ctype_int, -1, T_CONST, 2, NULL, -1);
3851 /* This define "-Dfileno(f)=0" should be inserted but we're going to stick to deinfe constants for now...*/
3856 types which we have not explictedly handled.
3857 AIX, FreeBSD, IRIX, Mach
3862 if (CPPOPTIONS (pfile)->debug_output)
3864 dump_special_to_buffer (pfile, "__BASE_FILE__");
3865 dump_special_to_buffer (pfile, "__VERSION__");
3866 #ifndef NO_BUILTIN_SIZE_TYPE
3867 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3869 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3870 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3872 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3873 dump_special_to_buffer (pfile, "__DATE__");
3874 dump_special_to_buffer (pfile, "__TIME__");
3875 if (!cppReader_isTraditional (pfile))
3876 dump_special_to_buffer (pfile, "__STDC__");
3881 /* Return 1 iff a token ending in C1 followed directly by a token C2
3882 could cause mis-tokenization. */
3885 unsafe_chars (char c1, char c2)
3890 if (c2 == c1 || c2 == '=')
3894 case '0': case '1': case '2': case '3': case '4':
3895 case '5': case '6': case '7': case '8': case '9':
3896 case 'e': case 'E': case 'p': case 'P':
3897 if (c2 == '-' || c2 == '+')
3898 return 1; /* could extend a pre-processing number */
3901 if (c2 == '\'' || c2 == '\"')
3902 return 1; /* Could turn into L"xxx" or L'xxx'. */
3906 case 'a': case 'b': case 'c': case 'd': case 'f':
3907 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3908 case 'm': case 'n': case 'o': case 'q': case 'r':
3909 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3911 case 'A': case 'B': case 'C': case 'D': case 'F':
3912 case 'G': case 'H': case 'I': case 'J': case 'K':
3913 case 'M': case 'N': case 'O': case 'Q': case 'R':
3914 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3916 /* We're in the middle of either a name or a pre-processing number. */
3917 return (is_idchar[(int) c2] || c2 == '.');
3918 case '<': case '>': case '!': case '%': case '#': case ':':
3919 case '^': case '&': case '|': case '*': case '/': case '=':
3920 return (c2 == c1 || c2 == '=');
3925 /* Expand a macro call.
3926 HP points to the symbol that is the macro being called.
3927 Put the result of expansion onto the input stack
3928 so that subsequent input by our caller will use it.
3930 If macro wants arguments, caller has already verified that
3931 an argument list follows; arguments come from the input stack. */
3934 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3937 DEFINITION *defn = hp->value.defn;
3945 size_t old_written = cpplib_getWritten (pfile);
3949 struct argdata *args = NULL;
3951 pfile->output_escapes++;
3952 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3953 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3955 nargs = defn->nargs;
3959 enum cpp_token token = CPP_EOF;
3961 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3963 for (i = 0; i < nargs; i++)
3965 args[i].expanded = 0;
3967 args[i].raw_length = 0;
3968 args[i].expand_length = args[i].stringified_length = -1;
3969 args[i].use_count = 0;
3973 ** Parse all the macro args that are supplied. I counts them.
3974 ** The first NARGS args are stored in ARGS.
3975 ** The rest are discarded. If rest_args is set then we assume
3976 ** macarg absorbed the rest of the args.
3982 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3990 if (i < nargs || (nargs == 0 && i == 0))
3992 /* if we are working on last arg which absorbs rest of args... */
3993 if (i == nargs - 1 && defn->rest_args)
3998 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3999 token = macarg (pfile, rest_args);
4000 args[i].raw_length = size_toInt (cpplib_getWritten (pfile) - args[i].raw);
4001 args[i].newlines = FALSE; /* FIXME */
4005 token = macarg (pfile, 0);
4008 if (token == CPP_EOF || token == CPP_POP)
4010 cppReader_errorWithLine (pfile, start_line, start_column,
4011 cstring_fromCharsNew ("unterminated macro call"));
4016 } while (token == CPP_COMMA);
4018 /* If we got one arg but it was just whitespace, call that 0 args. */
4026 bp = ARG_BASE + args[0].raw;
4027 lim = bp + args[0].raw_length;
4029 /* cpp.texi says for foo ( ) we provide one argument.
4030 However, if foo wants just 0 arguments, treat this as 0. */
4034 while (bp != lim && is_space[(int) *bp])
4044 /* Don't output an error message if we have already output one for
4045 a parse error above. */
4048 if (nargs == 0 && i > 0)
4050 cppReader_error (pfile,
4051 message ("arguments given to macro `%s'", hp->name));
4055 /* traditional C allows foo() if foo wants one argument. */
4056 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
4060 /* the rest args token is allowed to absorb 0 tokens */
4061 else if (i == nargs - 1 && defn->rest_args)
4064 cppReader_error (pfile,
4065 message ("macro `%s' used without args", hp->name));
4067 cppReader_error (pfile,
4068 message ("macro `%s' used with just one arg", hp->name));
4071 cppReader_error (pfile,
4072 message ("macro `%s' used with only %d args",
4078 cppReader_error (pfile,
4079 message ("macro `%s' used with too many (%d) args", hp->name, i));
4088 ** If the agrument list was multiple lines, need to insert new lines to keep line
4089 ** numbers accurate.
4092 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
4093 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
4095 /* If macro wants zero args, we parsed the arglist for checking only.
4096 Read directly from the macro definition. */
4100 xbuf = defn->expansion;
4101 xbuf_len = defn->length;
4105 char *exp = defn->expansion;
4106 int offset; /* offset in expansion,
4107 copied a piece at a time */
4108 size_t totlen; /* total amount of exp buffer filled so far */
4110 register struct reflist *ap, *last_ap;
4112 assertSet (args); /* args is defined since the nargs > 0 path was taken */
4114 /* Macro really takes args. Compute the expansion of this call. */
4116 /* Compute length in characters of the macro's expansion.
4117 Also count number of times each arg is used. */
4118 xbuf_len = defn->length;
4120 llassert (args != NULL);
4122 for (ap = defn->pattern; ap != NULL; ap = ap->next)
4126 struct argdata *arg = &args[ap->argno];
4128 /* Stringify it it hasn't already been */
4131 if (arg->stringified_length < 0)
4133 int arglen = arg->raw_length;
4134 bool escaped = FALSE;
4135 char in_string = '\0';
4138 /* Initially need_space is -1. Otherwise, 1 means the
4139 previous character was a space, but we suppressed it;
4140 0 means the previous character was a non-space. */
4141 int need_space = -1;
4144 arg->stringified = cpplib_getWritten (pfile);
4145 if (!cppReader_isTraditional (pfile))
4146 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
4147 for (; i < arglen; i++)
4149 c = (ARG_BASE + arg->raw)[i];
4151 if (in_string == '\0')
4153 /* Internal sequences of whitespace are replaced by
4154 one space except within an string or char token.*/
4155 if (is_space[(int) c])
4157 if (cpplib_getWritten (pfile) > arg->stringified
4158 && (cpplib_getPWritten (pfile))[-1] == '@')
4160 /* "@ " escape markers are removed */
4161 cppReader_adjustWritten (pfile, -1);
4162 /*@innercontinue@*/ continue;
4164 if (need_space == 0)
4166 /*@innercontinue@*/ continue;
4168 else if (need_space > 0)
4169 cppReader_putChar (pfile, ' ');
4185 if (in_string != '\0')
4190 else if (c == '\"' || c == '\'')
4200 /* Escape these chars */
4201 if (c == '\"' || (in_string != '\0' && c == '\\'))
4202 cppReader_putChar (pfile, '\\');
4204 cppReader_putChar (pfile, c);
4207 cpplib_reserve (pfile, 4);
4208 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4210 cppReader_adjustWritten (pfile, 4);
4213 if (!cppReader_isTraditional (pfile))
4214 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4215 arg->stringified_length
4216 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4219 xbuf_len += args[ap->argno].stringified_length;
4221 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4223 /* Add 4 for two newline-space markers to prevent token concatenation. */
4224 assertSet (args); /* Splint shouldn't need this */
4225 xbuf_len += args[ap->argno].raw_length + 4;
4229 /* We have an ordinary (expanded) occurrence of the arg.
4230 So compute its expansion, if we have not already. */
4232 assertSet (args); /* shouldn't need this */
4234 if (args[ap->argno].expand_length < 0)
4236 args[ap->argno].expanded = cpplib_getWritten (pfile);
4237 cpp_expand_to_buffer (pfile,
4238 ARG_BASE + args[ap->argno].raw,
4239 size_fromInt (args[ap->argno].raw_length));
4241 args[ap->argno].expand_length
4242 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4245 /* Add 4 for two newline-space markers to prevent
4246 token concatenation. */
4247 xbuf_len += args[ap->argno].expand_length + 4;
4249 if (args[ap->argno].use_count < 10)
4250 args[ap->argno].use_count++;
4253 xbuf = (char *) dmalloc (xbuf_len + 1);
4257 ** Generate in XBUF the complete expansion
4258 ** with arguments substituted in.
4259 ** TOTLEN is the total size generated so far.
4260 ** OFFSET is the index in the definition
4261 ** of where we are copying from.
4267 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4268 last_ap = ap, ap = ap->next)
4270 register struct argdata *arg = &args[ap->argno];
4271 size_t count_before = totlen;
4273 /* Add chars to XBUF. */
4274 for (i = 0; i < ap->nchars; i++, offset++)
4276 xbuf[totlen++] = exp[offset];
4279 /* If followed by an empty rest arg with concatenation,
4280 delete the last run of nonwhite chars. */
4281 if (rest_zero && totlen > count_before
4282 && ((ap->rest_args && ap->raw_before)
4283 || (last_ap != NULL && last_ap->rest_args
4284 && last_ap->raw_after)))
4286 /* Delete final whitespace. */
4287 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4292 /* Delete the nonwhites before them. */
4293 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4299 if (ap->stringify != 0)
4302 memcpy (xbuf + totlen,
4303 ARG_BASE + arg->stringified,
4304 size_fromInt (arg->stringified_length));
4305 totlen += arg->stringified_length;
4307 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4314 p1 = ARG_BASE + arg->raw;
4315 l1 = p1 + arg->raw_length;
4319 while (p1 != l1 && is_space[(int) *p1])
4324 while (p1 != l1 && is_idchar[(int) *p1])
4326 xbuf[totlen++] = *p1++;
4329 /* Delete any no-reexpansion marker that follows
4330 an identifier at the beginning of the argument
4331 if the argument is concatenated with what precedes it. */
4332 if (p1[0] == '@' && p1[1] == '-')
4337 /* Arg is concatenated after: delete trailing whitespace,
4338 whitespace markers, and no-reexpansion markers. */
4341 if (is_space[(int) l1[-1]]) l1--;
4342 else if (l1[-1] == '-')
4345 /* If a `-' is preceded by an odd number of newlines then it
4346 and the last newline are a no-reexpansion marker. */
4347 while (p2 != p1 && p2[-1] == '\n')
4352 if (((l1 - 1 - p2) & 1) != 0)
4358 /*@innerbreak@*/ break;
4363 /*@innerbreak@*/ break;
4368 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4376 expanded = ARG_BASE + arg->expanded;
4378 if (!ap->raw_before && totlen > 0
4379 && (arg->expand_length != 0)
4380 && !cppReader_isTraditional(pfile)
4381 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4383 xbuf[totlen++] = '@';
4384 xbuf[totlen++] = ' ';
4387 memcpy (xbuf + totlen, expanded,
4388 size_fromInt (arg->expand_length));
4389 totlen += arg->expand_length;
4391 if (!ap->raw_after && totlen > 0
4392 && offset < size_toInt (defn->length)
4393 && !cppReader_isTraditional(pfile)
4394 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4396 xbuf[totlen++] = '@';
4397 xbuf[totlen++] = ' ';
4400 /* If a macro argument with newlines is used multiple times,
4401 then only expand the newlines once. This avoids creating
4402 output lines which don't correspond to any input line,
4403 which confuses gdb and gcov. */
4404 if (arg->use_count > 1 && arg->newlines > 0)
4406 /* Don't bother doing change_newlines for subsequent
4410 = change_newlines (expanded, arg->expand_length);
4414 if (totlen > xbuf_len)
4418 /* if there is anything left of the definition
4419 after handling the arg list, copy that in too. */
4421 for (i = offset; i < size_toInt (defn->length); i++)
4423 /* if we've reached the end of the macro */
4426 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4427 && last_ap->raw_after))
4428 xbuf[totlen++] = exp[i];
4431 xbuf[totlen] = '\0';
4435 pfile->output_escapes--;
4437 /* Now put the expansion on the input stack
4438 so our caller will commence reading from it. */
4439 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4441 if (end_line != start_line)
4443 /* xbuf must have enough newlines */
4444 int newlines = end_line - start_line;
4445 int foundnewlines = 0;
4446 char *xbufptr = xbuf;
4448 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4453 if (*xbufptr == '\0')
4459 if (foundnewlines < newlines)
4461 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4463 while (foundnewlines < newlines)
4465 newbuf = cstring_appendChar (newbuf, '\n');
4470 xbuf = cstring_toCharsSafe (newbuf);
4471 xbuf_len = cstring_length (newbuf);
4473 } /*@=branchstate@*/
4476 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4478 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4479 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4480 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4482 /* Pop the space we've used in the token_buffer for argument expansion. */
4483 cppReader_setWritten (pfile, old_written);
4484 DPRINTF (("Done set written"));
4486 /* Recursive macro use sometimes works traditionally.
4487 #define foo(x,y) bar (x (y,0), y)
4490 if (!cppReader_isTraditional (pfile))
4491 hp->type = T_DISABLED;
4497 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4498 /*@dependent@*/ hashNode hp)
4500 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4507 mbuf->cleanup = cppReader_macroCleanup;
4509 llassert (mbuf->hnode == NULL);
4512 /* The first chars of the expansion should be a "@ " added by
4513 collect_expansion. This is to prevent accidental token-pasting
4514 between the text preceding the macro invocation, and the macro
4517 We would like to avoid adding unneeded spaces (for the sake of
4518 tools that use cpp, such as imake). In some common cases we can
4519 tell that it is safe to omit the space.
4521 The character before the macro invocation cannot have been an
4522 idchar (or else it would have been pasted with the idchars of
4523 the macro name). Therefore, if the first non-space character
4524 of the expansion is an idchar, we do not need the extra space
4525 to prevent token pasting.
4527 Also, we don't need the extra space if the first char is '(',
4528 or some other (less common) characters. */
4530 if (xbuf[0] == '@' && xbuf[1] == ' '
4531 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4532 || xbuf[2] == '\"'))
4534 llassert (mbuf->cur != NULL);
4535 DPRINTF (("Eating: %c", xbuf[2]));
4542 /* Like cpplib_getToken, except that it does not read past end-of-line.
4543 Also, horizontal space is skipped, and macros are popped. */
4545 static enum cpp_token
4546 get_directive_token (cppReader *pfile)
4550 size_t old_written = cpplib_getWritten (pfile);
4551 enum cpp_token token;
4552 cppSkipHspace (pfile);
4553 if (cppReader_peekC (pfile) == '\n')
4558 token = cpplib_getToken (pfile);
4563 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4568 cppReader_setWritten (pfile, old_written);
4569 /*@switchbreak@*/ break;
4577 /* Handle #include and #import.
4578 This function expects to see "fname" or <fname> on the input.
4580 The input is normally in part of the output_buffer following
4581 cpplib_getWritten, and will get overwritten by output_line_command.
4582 I.e. in input file specification has been popped by cppReader_handleDirective.
4586 do_include (cppReader *pfile, struct directive *keyword,
4587 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4589 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4591 char *fbeg, *fend; /* Beginning and end of fname */
4592 enum cpp_token token;
4594 /* Chain of dirs to search */
4595 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4596 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4597 struct file_name_list *searchptr = NULL;
4598 size_t old_written = cpplib_getWritten (pfile);
4601 int f; /* file number */
4602 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4603 f= -1; /* JF we iz paranoid! */
4605 pfile->parsing_include_directive++;
4606 token = get_directive_token (pfile);
4607 pfile->parsing_include_directive--;
4609 if (token == CPP_STRING)
4611 /* FIXME - check no trailing garbage */
4612 fbeg = pfile->token_buffer + old_written + 1;
4613 fend = cpplib_getPWritten (pfile) - 1;
4614 if (fbeg[-1] == '<')
4617 /* If -I-, start with the first -I dir after the -I-. */
4618 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4619 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4621 /* If -I- was specified, don't search current dir, only spec'd ones. */
4622 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4624 cppBuffer *fp = CPPBUFFER (pfile);
4625 /* We have "filename". Figure out directory this source
4626 file is coming from and put it on the front of the list. */
4628 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4633 llassert (fp != NULL);
4637 if (cstring_isDefined (fp->nominal_fname))
4639 nam = cstring_toCharsSafe (fp->nominal_fname);
4641 /* Found a named file. Figure out dir of the file,
4642 and put it in front of the search list. */
4643 dsp[0].next = search_start;
4647 ep = strrchr (nam, CONNECTCHAR);
4649 ep = strrchr (nam, ']');
4650 if (ep == NULL) ep = strrchr (nam, '>');
4651 if (ep == NULL) ep = strrchr (nam, ':');
4652 if (ep != NULL) ep++;
4662 /*@-onlytrans@*/ /* This looks like a memory leak... */
4663 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4667 if (n + INCLUDE_LEN_FUDGE > size_toInt (pfile->max_include_len))
4668 pfile->max_include_len = size_fromInt (n + INCLUDE_LEN_FUDGE);
4672 dsp[0].fname = cstring_undefined; /* Current directory */
4675 dsp[0].got_name_map = 0;
4686 else if (token == CPP_NAME)
4689 * Support '#include xyz' like VAX-C to allow for easy use of all the
4690 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4691 * code from case '<' is repeated here) and generates a warning.
4693 cppReader_warning (pfile,
4694 "VAX-C-style include specification found, use '#include <filename.h>' !");
4696 /* If -I-, start with the first -I dir after the -I-. */
4697 if (CPPOPTIONS (pfile)->first_bracket_include)
4698 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4699 fbeg = pfile->token_buffer + old_written;
4700 fend = cpplib_getPWritten (pfile);
4705 cppReader_error (pfile,
4706 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4709 cppReader_setWritten (pfile, old_written);
4710 cppReader_skipRestOfLine (pfile);
4716 token = get_directive_token (pfile);
4717 if (token != CPP_VSPACE)
4719 cppReader_errorLit (pfile,
4720 cstring_makeLiteralTemp ("Junk at end of #include"));
4722 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4724 token = get_directive_token (pfile);
4729 ** For #include_next, skip in the search path
4730 ** past the dir in which the containing file was found.
4735 cppBuffer *fp = CPPBUFFER (pfile);
4737 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4739 llassert (fp != NULL);
4741 if (fp->fname != NULL)
4743 /* fp->dir is null if the containing file was specified with
4744 an absolute file name. In that case, don't skip anything. */
4745 if (fp->dir == SELF_DIR_DUMMY)
4747 search_start = CPPOPTIONS (pfile)->include;
4749 else if (fp->dir != NULL)
4751 search_start = fp->dir->next;
4763 cppReader_setWritten (pfile, old_written);
4765 flen = size_fromInt (fend - fbeg);
4767 DPRINTF (("fbeg: %s", fbeg));
4771 cppReader_error (pfile,
4772 message ("Empty file name in #%s", keyword->name));
4777 ** Allocate this permanently, because it gets stored in the definitions
4781 fname = cstring_undefined;
4783 /* + 2 above for slash and terminating null. */
4784 /* + 2 added for '.h' on VMS (to support '#include filename') */
4786 /* If specified file name is absolute, just open it. */
4788 if (osd_isConnectChar (*fbeg)
4789 # if defined (WIN32) || defined (OS2)
4790 || (*(fbeg + 1) == ':')
4794 fname = cstring_copyLength (fbeg, flen);
4796 if (redundant_include_p (pfile, fname))
4798 cstring_free (fname);
4802 f = open_include_file (pfile, fname, NULL);
4804 if (f == IMPORT_FOUND)
4806 return 0; /* Already included this file */
4811 /* Search directory path, trying to open the file.
4812 Copy each filename tried into FNAME. */
4814 for (searchptr = search_start; searchptr != NULL;
4815 searchptr = searchptr->next)
4817 if (!cstring_isEmpty (searchptr->fname))
4819 /* The empty string in a search path is ignored.
4820 This makes it possible to turn off entirely
4821 a standard piece of the list. */
4822 if (cstring_isEmpty (searchptr->fname))
4825 fname = cstring_copy (searchptr->fname);
4826 fname = cstring_appendChar (fname, CONNECTCHAR);
4827 DPRINTF (("Here: %s", fname));
4834 fname = cstring_concatLength (fname, fbeg, flen);
4836 DPRINTF (("fname: %s", fname));
4838 /* Win32 directory fix from Kay Buschner. */
4839 #if defined (WIN32) || defined (OS2)
4840 /* Fix all unixdir slashes to win dir slashes */
4841 if (searchptr->fname && (searchptr->fname[0] != 0))
4843 cstring_replaceAll (fname, '/', '\\');
4848 /* Change this 1/2 Unix 1/2 VMS file specification into a
4849 full VMS file specification */
4850 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4851 /* Fix up the filename */
4852 hack_vms_include_specification (fname);
4854 /* This is a normal VMS filespec, so use it unchanged. */
4855 strncpy (fname, fbeg, flen);
4857 /* if it's '#include filename', add the missing .h */
4858 if (strchr (fname,'.') == NULL) {
4859 strcat (fname, ".h");
4863 /* ??? There are currently 3 separate mechanisms for avoiding processing
4864 of redundant include files: #import, #pragma once, and
4865 redundant_include_p. It would be nice if they were unified. */
4867 if (redundant_include_p (pfile, fname))
4869 cstring_free (fname);
4873 DPRINTF (("Trying: %s", fname));
4875 f = open_include_file (pfile, fname, searchptr);
4877 if (f == IMPORT_FOUND)
4879 return 0; /* Already included this file */
4882 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4884 cppReader_warning (pfile,
4885 message ("Header file %s exists, but is not readable", fname));
4898 /* A file that was not found. */
4899 fname = cstring_copyLength (fbeg, flen);
4901 if (search_start != NULL)
4903 cppReader_error (pfile,
4904 message ("Cannot find include file %s on search path: %x",
4906 searchPath_unparse (search_start)));
4910 cppReader_error (pfile,
4911 message ("No include path in which to find %s", fname));
4916 ** Check to see if this include file is a once-only include file.
4920 struct file_name_list *ptr;
4922 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4924 if (cstring_equal (ptr->fname, fname))
4926 /* This file was included before. */
4933 /* This is the first time for this file. */
4934 /* Add it to list of files included. */
4936 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4937 ptr->control_macro = NULL;
4938 ptr->c_system_include_path = NULL;
4939 ptr->next = pfile->all_include_files;
4941 ptr->got_name_map = NULL;
4943 DPRINTF (("Including file: %s", fname));
4944 pfile->all_include_files = ptr;
4945 assertSet (pfile->all_include_files);
4948 if (angle_brackets != 0)
4950 pfile->system_include_depth++;
4953 /* Actually process the file */
4954 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4956 cstring_free (fname);
4960 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4961 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4963 output_line_command (pfile, 0, enter_file);
4964 pfile->only_seen_white = 2;
4969 pfile->system_include_depth--;
4972 } /*@=branchstate@*/
4977 /* Return nonzero if there is no need to include file NAME
4978 because it has already been included and it contains a conditional
4979 to make a repeated include do nothing. */
4982 redundant_include_p (cppReader *pfile, cstring name)
4984 struct file_name_list *l = pfile->all_include_files;
4986 for (; l != NULL; l = l->next)
4988 if (cstring_equal (name, l->fname)
4989 && (l->control_macro != NULL)
4990 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4999 /* Return nonzero if the given FILENAME is an absolute pathname which
5000 designates a file within one of the known "system" include file
5001 directories. We assume here that if the given FILENAME looks like
5002 it is the name of a file which resides either directly in a "system"
5003 include file directory, or within any subdirectory thereof, then the
5004 given file must be a "system" include file. This function tells us
5005 if we should suppress pedantic errors/warnings for the given FILENAME.
5007 The value is 2 if the file is a C-language system header file
5008 for which C++ should (on most systems) assume `extern "C"'. */
5011 is_system_include (cppReader *pfile, cstring filename)
5013 struct file_name_list *searchptr;
5015 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
5017 searchptr = searchptr->next)
5019 if (!cstring_isEmpty (searchptr->fname))
5021 cstring sys_dir = searchptr->fname;
5022 size_t length = cstring_length (sys_dir);
5024 if (cstring_equalLen (sys_dir, filename, length)
5025 && osd_isConnectChar (cstring_getChar (filename, length)))
5027 if (searchptr->c_system_include_path)
5038 /* Convert a character string literal into a nul-terminated string.
5039 The input string is [IN ... LIMIT).
5040 The result is placed in RESULT. RESULT can be the same as IN.
5041 The value returned in the end of the string written to RESULT,
5042 or NULL on error. */
5044 static /*@null@*/ char *
5045 convert_string (cppReader *pfile, /*@returned@*/ char *result,
5046 char *in, char *limit, int handle_escapes)
5066 /*@switchbreak@*/ break;
5070 char *bpc = (char *) in;
5071 int i = (char) cppReader_parseEscape (pfile, &bpc);
5074 *result++ = (char) c;
5075 /*@switchbreak@*/ break;
5089 * interpret #line command. Remembers previously seen fnames
5090 * in its very own hash table.
5093 /*@constant int FNAME_HASHSIZE@*/
5094 #define FNAME_HASHSIZE 37
5097 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
5099 cppBuffer *ip = cppReader_getBuffer (pfile);
5101 size_t old_written = cpplib_getWritten (pfile);
5102 enum file_change_code file_change = same_file;
5103 enum cpp_token token;
5105 llassert (ip != NULL);
5106 token = get_directive_token (pfile);
5108 if (token != CPP_NUMBER
5109 || !isdigit(pfile->token_buffer[old_written]))
5111 cppReader_errorLit (pfile,
5112 cstring_makeLiteralTemp ("invalid format `#line' command"));
5114 goto bad_line_directive;
5117 /* The Newline at the end of this line remains to be processed.
5118 To put the next line at the specified line number,
5119 we must store a line number now that is one less. */
5120 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
5121 cppReader_setWritten (pfile, old_written);
5123 /* NEW_LINENO is one less than the actual line number here. */
5124 if (cppReader_isPedantic (pfile) && new_lineno < 0)
5125 cppReader_pedwarnLit (pfile,
5126 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
5128 token = get_directive_token (pfile);
5130 if (token == CPP_STRING) {
5131 char *fname = pfile->token_buffer + old_written;
5133 static hashNode fname_table[FNAME_HASHSIZE];
5135 hashNode *hash_bucket;
5138 size_t fname_length;
5140 /* Turn the file name, which is a character string literal,
5141 into a null-terminated string. Do this in place. */
5142 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
5143 if (end_name == NULL)
5145 cppReader_errorLit (pfile,
5146 cstring_makeLiteralTemp ("invalid format `#line' command"));
5147 goto bad_line_directive;
5150 fname_length = size_fromInt (end_name - fname);
5151 num_start = cpplib_getWritten (pfile);
5153 token = get_directive_token (pfile);
5154 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
5155 p = pfile->token_buffer + num_start;
5156 if (cppReader_isPedantic (pfile))
5157 cppReader_pedwarnLit (pfile,
5158 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
5160 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
5162 cppReader_errorLit (pfile,
5163 cstring_makeLiteralTemp ("invalid format `#line' command"));
5164 goto bad_line_directive;
5167 file_change = enter_file;
5169 file_change = leave_file;
5171 ip->system_header_p = 1;
5172 else /* if (*p == 4) */
5173 ip->system_header_p = 2;
5175 cppReader_setWritten (pfile, num_start);
5176 token = get_directive_token (pfile);
5177 p = pfile->token_buffer + num_start;
5178 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
5179 ip->system_header_p = *p == 3 ? 1 : 2;
5180 token = get_directive_token (pfile);
5182 if (token != CPP_VSPACE) {
5183 cppReader_errorLit (pfile,
5184 cstring_makeLiteralTemp ("invalid format `#line' command"));
5186 goto bad_line_directive;
5191 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
5193 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
5195 if (hp->length == fname_length)
5197 llassert (hp->value.cpval != NULL);
5199 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
5201 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5208 /* Didn't find it; cons up a new one. */
5209 hp = (hashNode) dmalloc (sizeof (*hp));
5212 hp->bucket_hdr = NULL;
5214 hp->name = cstring_undefined;
5215 hp->next = *hash_bucket;
5219 hp->length = fname_length;
5220 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5221 memcpy (hp->value.cpval, fname, fname_length);
5222 hp->value.cpval[fname_length] = '\0';
5223 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5226 else if (token != CPP_VSPACE && token != CPP_EOF)
5228 cppReader_errorLit (pfile,
5229 cstring_makeLiteralTemp ("invalid format `#line' command"));
5230 goto bad_line_directive;
5237 ip->lineno = new_lineno;
5239 cppReader_skipRestOfLine (pfile);
5240 cppReader_setWritten (pfile, old_written);
5241 output_line_command (pfile, 0, file_change);
5246 * remove the definition of a symbol from the symbol table.
5247 * according to un*x /lib/cpp, it is not an error to undef
5248 * something that has no definitions, so it isn't one here either.
5252 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5257 char *orig_buf = buf;
5259 SKIP_WHITE_SPACE (buf);
5261 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5263 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
5265 /* If we are generating additional info for debugging (with -g) we
5266 need to pass through all effective #undef commands. */
5267 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5269 pass_thru_directive (orig_buf, limit, pfile, keyword);
5272 if (hp->type != T_MACRO)
5274 cppReader_warning (pfile,
5275 message ("Undefining preprocessor builtin: %s",
5279 cppReader_deleteMacro (hp);
5282 if (cppReader_isPedantic (pfile)) {
5284 SKIP_WHITE_SPACE (buf);
5287 cppReader_pedwarnLit (pfile,
5288 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5297 * Report an error detected by the program we are processing.
5298 * Use the text of the line in the error message.
5299 * (We use error because it prints the filename & line#.)
5303 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5304 char *buf, char *limit)
5306 size_t length = size_fromInt (limit - buf);
5307 cstring copy = cstring_copyLength (buf, length);
5308 cstring adv = cstring_advanceWhiteSpace (copy);
5310 cppReader_error (pfile, message ("#error %s", adv));
5311 cstring_free (copy);
5316 * Report a warning detected by the program we are processing.
5317 * Use the text of the line in the warning message, then continue.
5318 * (We use error because it prints the filename & line#.)
5322 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5323 char *buf, char *limit)
5325 size_t length = size_fromInt (limit - buf);
5326 cstring copy = cstring_copyLength (buf, length);
5327 cstring adv = cstring_advanceWhiteSpace (copy);
5328 cppReader_warning (pfile, message ("#warning %s", adv));
5329 cstring_free (copy);
5334 /* #ident has already been copied to the output file, so just ignore it. */
5337 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5338 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5340 /* Allow #ident in system headers, since that's not user's fault. */
5341 if (cppReader_isPedantic (pfile)
5342 && !cppReader_getBufferSafe (pfile)->system_header_p)
5343 cppReader_pedwarnLit (pfile,
5344 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5346 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5351 /* #pragma and its argument line have already been copied to the output file.
5352 Just check for some recognized pragmas that need validation here. */
5355 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5356 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5358 while (*buf == ' ' || *buf == '\t')
5363 if (!strncmp (buf, "implementation", 14)) {
5364 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5365 been included yet. */
5366 struct file_name_list *ptr;
5367 char *p = buf + 14, *fname, *inc_fname;
5369 SKIP_WHITE_SPACE (p);
5370 if (*p == '\n' || *p != '\"')
5374 p = (char *) strchr (fname, '\"');
5375 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5377 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5379 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5380 inc_fname = (inc_fname != NULL)
5381 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5383 if ((inc_fname != NULL)
5384 && (strncmp (inc_fname, fname, fname_len) == 0))
5386 cpp_setLocation (pfile);
5388 ppllerror (message ("`#pragma implementation' for `%s' appears "
5389 "after file is included",
5390 cstring_fromChars (fname)));
5399 * handle #if command by
5400 * 1) inserting special `defined' keyword into the hash table
5401 * that gets turned into 0 or 1 by special_symbol (thus,
5402 * if the luser has a symbol called `defined' already, it won't
5403 * work inside the #if command)
5404 * 2) rescan the input into a temporary output buffer
5405 * 3) pass the output buffer to the yacc parser and collect a value
5406 * 4) clean up the mess left from steps 1 and 2.
5407 * 5) call conditional_skip to skip til the next #endif (etc.),
5408 * or not, depending on the value from step 3.
5412 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5413 char *buf, char *limit)
5415 HOST_WIDE_INT value;
5416 DPRINTF (("Do if: %s", buf));
5417 value = eval_if_expression (pfile, buf, limit - buf);
5418 conditional_skip (pfile, value == 0, T_IF, NULL);
5423 * handle a #elif directive by not changing if_stack either.
5424 * see the comment above do_else.
5427 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5428 char *buf, char *limit)
5430 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5432 cppReader_errorLit (pfile,
5433 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5438 llassert (pfile->if_stack != NULL);
5440 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5442 cppReader_errorLit (pfile,
5443 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5445 if (pfile->if_stack->fname != NULL
5446 && cppReader_getBufferSafe (pfile)->fname != NULL
5447 && !cstring_equal (pfile->if_stack->fname,
5448 cppReader_getBufferSafe (pfile)->nominal_fname))
5449 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5450 fprintf (stderr, ")\n");
5452 pfile->if_stack->type = T_ELIF;
5455 if (pfile->if_stack->if_succeeded)
5457 skip_if_group (pfile, 0);
5461 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5463 skip_if_group (pfile, 0);
5466 ++pfile->if_stack->if_succeeded; /* continue processing input */
5467 output_line_command (pfile, 1, same_file);
5475 * evaluate a #if expression in BUF, of length LENGTH,
5476 * then parse the result as a C expression and return the value as an int.
5479 static HOST_WIDE_INT
5480 eval_if_expression (cppReader *pfile,
5481 /*@unused@*/ char *buf,
5482 /*@unused@*/ int length)
5484 hashNode save_defined;
5485 HOST_WIDE_INT value;
5486 size_t old_written = cpplib_getWritten (pfile);
5488 DPRINTF (("Saving defined..."));
5489 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5490 pfile->pcp_inside_if = 1;
5492 value = cppReader_parseExpression (pfile);
5493 pfile->pcp_inside_if = 0;
5495 /* Clean up special symbol */
5496 DPRINTF (("Removing defined..."));
5497 cppReader_deleteMacro (save_defined);
5498 cppReader_setWritten (pfile, old_written); /* Pop */
5504 * routine to handle ifdef/ifndef. Try to look up the symbol,
5505 * then do or don't skip to the #endif/#else/#elif depending
5506 * on what directive is actually being processed.
5510 do_xifdef (cppReader *pfile, struct directive *keyword,
5511 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5514 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5516 size_t ident_length;
5517 enum cpp_token token;
5518 int start_of_file = 0;
5519 char *control_macro = 0;
5520 size_t old_written = cpplib_getWritten (pfile);
5522 DPRINTF (("do xifdef: %d",
5523 keyword->type == T_IFNDEF));
5525 /* Detect a #ifndef at start of file (not counting comments). */
5526 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5528 start_of_file = pfile->only_seen_white == 2;
5531 pfile->no_macro_expand++;
5532 token = get_directive_token (pfile);
5533 pfile->no_macro_expand--;
5535 ident = pfile->token_buffer + old_written;
5536 DPRINTF (("Ident: %s", ident));
5538 ident_length = cpplib_getWritten (pfile) - old_written;
5539 cppReader_setWritten (pfile, old_written); /* Pop */
5541 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5543 skip = (keyword->type == T_IFDEF);
5544 if (! cppReader_isTraditional (pfile))
5546 cppReader_pedwarn (pfile,
5547 message ("`#%s' with no argument", keyword->name));
5550 else if (token == CPP_NAME)
5552 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5554 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5556 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5558 if (start_of_file && !skip)
5560 DPRINTF (("Not skipping!"));
5561 control_macro = (char *) dmalloc (ident_length + 1);
5562 memcpy (control_macro, ident, ident_length + 1);
5567 skip = (keyword->type == T_IFDEF);
5568 if (! cppReader_isTraditional (pfile))
5570 cppReader_error (pfile,
5571 message ("`#%s' with invalid argument", keyword->name));
5575 if (!cppReader_isTraditional (pfile))
5578 cppSkipHspace (pfile);
5579 c = cppReader_peekC (pfile);
5580 if (c != EOF && c != '\n')
5582 cppReader_pedwarn (pfile,
5583 message ("garbage at end of `#%s' argument", keyword->name));
5587 cppReader_skipRestOfLine (pfile);
5589 DPRINTF (("Conditional skip: %d", skip));
5590 conditional_skip (pfile, skip, T_IF, control_macro);
5594 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5595 If this is a #ifndef starting at the beginning of a file,
5596 CONTROL_MACRO is the macro name tested by the #ifndef.
5597 Otherwise, CONTROL_MACRO is 0. */
5600 conditional_skip (cppReader *pfile, int skip,
5601 enum node_type type,
5602 /*@dependent@*/ char *control_macro)
5604 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5606 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5607 temp->next = pfile->if_stack;
5608 temp->control_macro = control_macro;
5610 temp->if_succeeded = 0;
5612 pfile->if_stack = temp;
5613 pfile->if_stack->type = type;
5617 skip_if_group (pfile, 0);
5622 ++pfile->if_stack->if_succeeded;
5623 output_line_command (pfile, 1, same_file);
5628 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5629 * leaves input ptr at the sharp sign found.
5630 * If ANY is nonzero, return at next directive of any sort.
5634 skip_if_group (cppReader *pfile, int any)
5637 struct directive *kt;
5638 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5639 register int ident_length;
5641 struct parse_marker line_start_mark;
5643 parseSetMark (&line_start_mark, pfile);
5645 if (CPPOPTIONS (pfile)->output_conditionals) {
5646 static char failed[] = "#failed\n";
5647 cppReader_puts (pfile, failed, sizeof(failed)-1);
5649 output_line_command (pfile, 1, same_file);
5653 if (CPPOPTIONS (pfile)->output_conditionals)
5655 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5658 llassert (pbuf->buf != NULL);
5660 start_line = pbuf->buf + line_start_mark.position;
5661 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5664 parseMoveMark (&line_start_mark, pfile);
5666 if (!cppReader_isTraditional (pfile))
5668 cppSkipHspace (pfile);
5671 c = cppReader_getC (pfile);
5674 size_t old_written = cpplib_getWritten (pfile);
5675 cppSkipHspace (pfile);
5677 parse_name (pfile, cppReader_getC (pfile));
5678 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5679 ident = pfile->token_buffer + old_written;
5680 pfile->limit = ident;
5682 for (kt = directive_table; kt->length >= 0; kt++)
5684 cppIfStackFrame *temp;
5685 if (ident_length == kt->length
5686 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5688 /* If we are asked to return on next directive, do so now. */
5699 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5700 temp->next = pfile->if_stack;
5701 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5702 temp->type = kt->type;
5704 temp->if_succeeded = 0;
5705 temp->control_macro = NULL;
5707 pfile->if_stack = temp;
5708 /*@switchbreak@*/ break;
5711 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5712 validate_else (pfile,
5713 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5716 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5718 cppReader_error (pfile,
5719 message ("Preprocessor command #%s is not within a conditional", kt->name));
5720 /*@switchbreak@*/ break;
5722 else if (pfile->if_stack == save_if_stack)
5724 goto done; /* found what we came for */
5731 if (kt->type != T_ENDIF)
5733 llassert (pfile->if_stack != NULL);
5735 if (pfile->if_stack->type == T_ELSE)
5737 cppReader_errorLit (pfile,
5738 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5741 pfile->if_stack->type = kt->type;
5742 /*@switchbreak@*/ break;
5745 temp = pfile->if_stack;
5746 llassert (temp != NULL);
5747 pfile->if_stack = temp->next;
5749 /*@switchbreak@*/ break;
5752 #if defined (OS2) && defined (__IBMC__)
5753 /* Dummy code to eleminate optimization problems with icc */
5762 /* Don't let erroneous code go by. */
5764 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5765 && cppReader_isPedantic (pfile))
5767 cppReader_pedwarnLit (pfile,
5768 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5772 c = cppReader_getC (pfile);
5774 /* We're in the middle of a line. Skip the rest of it. */
5782 case '/': /* possible comment */
5783 c = skip_comment (pfile, NULL);
5786 /*@switchbreak@*/ break;
5789 cppReader_forward (pfile, -1);
5790 old = cpplib_getWritten (pfile);
5791 (void) cpplib_getToken (pfile);
5792 cppReader_setWritten (pfile, old);
5793 /*@switchbreak@*/ break;
5795 /* Char after backslash loses its special meaning. */
5796 if (cppReader_peekC (pfile) == '\n')
5798 cppReader_forward (pfile, 1);
5801 /*@switchbreak@*/ break;
5805 c = cppReader_getC (pfile);
5808 if (CPPOPTIONS (pfile)->output_conditionals) {
5809 static char end_failed[] = "#endfailed\n";
5810 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5813 pfile->only_seen_white = 1;
5815 parseGotoMark (&line_start_mark, pfile);
5816 parseClearMark (&line_start_mark);
5820 * handle a #else directive. Do this by just continuing processing
5821 * without changing if_stack ; this is so that the error message
5822 * for missing #endif's etc. will point to the original #if. It
5823 * is possible that something different would be better.
5827 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5828 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5830 if (cppReader_isPedantic (pfile))
5832 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5835 cppReader_skipRestOfLine (pfile);
5837 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5838 cppReader_errorLit (pfile,
5839 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5842 /* #ifndef can't have its special treatment for containing the whole file
5843 if it has a #else clause. */
5845 llassert (pfile->if_stack != NULL);
5847 pfile->if_stack->control_macro = 0;
5849 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5851 cpp_setLocation (pfile);
5852 genppllerrorhint (FLG_PREPROC,
5853 message ("Pre-processor directive #else after #else"),
5854 message ("%q: Location of match",
5855 fileloc_unparseRaw (pfile->if_stack->fname,
5856 pfile->if_stack->lineno)));
5859 pfile->if_stack->type = T_ELSE;
5862 if (pfile->if_stack->if_succeeded)
5863 skip_if_group (pfile, 0);
5865 ++pfile->if_stack->if_succeeded; /* continue processing input */
5866 output_line_command (pfile, 1, same_file);
5873 * unstack after #endif command
5877 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5878 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5880 if (cppReader_isPedantic (pfile))
5882 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5885 cppReader_skipRestOfLine (pfile);
5887 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5889 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5893 cppIfStackFrame *temp = pfile->if_stack;
5895 llassert (temp != NULL);
5897 pfile->if_stack = temp->next;
5898 if (temp->control_macro != 0)
5900 /* This #endif matched a #ifndef at the start of the file.
5901 See if it is at the end of the file. */
5902 struct parse_marker start_mark;
5905 parseSetMark (&start_mark, pfile);
5909 cppSkipHspace (pfile);
5910 c = cppReader_getC (pfile);
5916 parseGotoMark (&start_mark, pfile);
5917 parseClearMark (&start_mark);
5921 /* If we get here, this #endif ends a #ifndef
5922 that contains all of the file (aside from whitespace).
5923 Arrange not to include the file again
5924 if the macro that was tested is defined.
5926 Do not do this for the top-level file in a -include or any
5927 file in a -imacros. */
5928 struct file_name_list *ifile = pfile->all_include_files;
5930 for ( ; ifile != NULL; ifile = ifile->next)
5932 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5934 ifile->control_macro = temp->control_macro;
5942 output_line_command (pfile, 1, same_file);
5947 /* When an #else or #endif is found while skipping failed conditional,
5948 if -pedantic was specified, this is called to warn about text after
5949 the command name. P points to the first char after the command name. */
5952 validate_else (cppReader *pfile, cstring directive)
5955 cppSkipHspace (pfile);
5956 c = cppReader_peekC (pfile);
5957 if (c != EOF && c != '\n')
5959 cppReader_pedwarn (pfile,
5960 message ("text following `%s' violates ANSI standard", directive));
5965 ** Get the next token, and add it to the text in pfile->token_buffer.
5966 ** Return the kind of token we got.
5970 cpplib_getToken (cppReader *pfile)
5972 return cpplib_getTokenAux (pfile, FALSE);
5976 cpplib_getTokenForceExpand (cppReader *pfile)
5978 return cpplib_getTokenAux (pfile, TRUE);
5982 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5985 size_t old_written = 0;
5986 int start_line, start_column;
5987 enum cpp_token token;
5988 struct cppOptions *opts = CPPOPTIONS (pfile);
5989 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5992 c = cppReader_getC (pfile);
5993 DPRINTF (("Get next token: %c", c));
5998 if (cppReader_getBufferSafe (pfile)->seen_eof)
6000 cppBuffer *buf = cppReader_popBuffer (pfile);
6002 if (buf != cppReader_nullBuffer (pfile))
6013 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6014 cppReader_getBufferSafe (pfile)->seen_eof = 1;
6016 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
6017 && next_buf != cppReader_nullBuffer (pfile))
6019 /* We're about to return from an #include file.
6020 Emit #line information now (as part of the CPP_POP) result.
6021 But the #line refers to the file we will pop to. */
6022 cppBuffer *cur_buffer = CPPBUFFER (pfile);
6023 CPPBUFFER (pfile) = next_buf;
6024 pfile->input_stack_listing_current = 0;
6025 output_line_command (pfile, 0, leave_file);
6026 CPPBUFFER (pfile) = cur_buffer;
6034 struct parse_marker start_mark;
6039 if (cppReader_peekC (pfile) == '=')
6044 if (opts->put_out_comments)
6046 parseSetMark (&start_mark, pfile);
6050 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6051 &start_line, &start_column);
6052 c = skip_comment (pfile, &newlines);
6053 DPRINTF (("c = %c", c));
6054 if (opts->put_out_comments && (c == '/' || c == EOF))
6056 assertSet (&start_mark);
6057 parseClearMark (&start_mark);
6064 cppReader_errorWithLine (pfile, start_line, start_column,
6065 cstring_makeLiteral ("Unterminated comment"));
6068 c = '/'; /* Initial letter of comment. */
6070 /* Comments are equivalent to spaces.
6071 For -traditional, a comment is equivalent to nothing. */
6073 if (opts->put_out_comments)
6077 assertSet (&start_mark);
6078 res = cpp_handleComment (pfile, &start_mark);
6079 pfile->lineno += newlines;
6082 else if (cppReader_isTraditional (pfile))
6088 cpplib_reserve(pfile, 1);
6089 cppReader_putCharQ (pfile, ' ');
6094 if (!pfile->only_seen_white)
6099 if (cppReader_handleDirective (pfile))
6101 return CPP_DIRECTIVE;
6104 pfile->only_seen_white = 0;
6109 /* A single quoted string is treated like a double -- some
6110 programs (e.g., troff) are perverse this way */
6111 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
6112 &start_line, &start_column);
6113 old_written = cpplib_getWritten (pfile);
6115 DPRINTF (("Reading string: %c", c));
6116 cppReader_putChar (pfile, c);
6120 ** Because of ISO8859-1 characters in string literals, we need a special test here.
6123 if (cppReader_reachedEOF (pfile))
6126 DPRINTF (("Matches EOF!"));
6127 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
6129 /* try harder: this string crosses a macro expansion
6130 boundary. This can happen naturally if -traditional.
6131 Otherwise, only -D can make a macro with an unmatched
6134 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6135 (*cppReader_getBufferSafe (pfile)->cleanup)
6136 (cppReader_getBufferSafe (pfile), pfile);
6137 CPPBUFFER (pfile) = next_buf;
6141 if (!cppReader_isTraditional (pfile))
6143 cpp_setLocation (pfile);
6145 setLine (long_toInt (start_line));
6146 setColumn (long_toInt (start_column));
6148 if (pfile->multiline_string_line != long_toInt (start_line)
6149 && pfile->multiline_string_line != 0)
6153 message ("Unterminated string or character constant"),
6154 message ("%q: Possible real start of unterminated constant",
6156 (fileloc_filename (g_currentloc),
6157 pfile->multiline_string_line)));
6158 pfile->multiline_string_line = 0;
6164 message ("Unterminated string or character constant"));
6167 /*@loopbreak@*/ break;
6171 int cc = cppReader_getC (pfile);
6172 DPRINTF (("cc: %c [%d] [%d]", cc, cc, EOF));
6173 DPRINTF (("putting char: %c", cc));
6174 cppReader_putChar (pfile, cc);
6178 /* Traditionally, end of line ends a string constant with
6179 no error. So exit the loop and record the new line. */
6180 if (cppReader_isTraditional (pfile))
6186 if (cppReader_isPedantic (pfile)
6187 && pfile->multiline_string_line == 0)
6189 cppReader_pedwarnWithLine
6190 (pfile, long_toInt (start_line),
6191 long_toInt (start_column),
6192 cstring_makeLiteral ("String constant runs past end of line"));
6194 if (pfile->multiline_string_line == 0)
6196 pfile->multiline_string_line = start_line;
6199 /*@switchbreak@*/ break;
6202 cc = cppReader_getC (pfile);
6205 /* Backslash newline is replaced by nothing at all. */
6206 cppReader_adjustWritten (pfile, -1);
6211 /* ANSI stupidly requires that in \\ the second \
6212 is *not* prevented from combining with a newline. */
6215 cppReader_putChar (pfile, cc);
6217 /*@switchbreak@*/ break;
6223 /*@switchbreak@*/ break;
6228 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6229 cpplib_getPWritten (pfile));
6230 pfile->only_seen_white = 0;
6231 return c == '\'' ? CPP_CHAR : CPP_STRING;
6234 if (!opts->dollars_in_ident)
6239 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6247 c2 = cppReader_peekC (pfile);
6248 if (c2 == c || c2 == '=')
6258 if (cppReader_peekC (pfile) == '=')
6264 c2 = cppReader_peekC (pfile);
6265 if (c2 == '-' && opts->chill)
6267 /* Chill style comment */
6268 if (opts->put_out_comments)
6270 parseSetMark (&start_mark, pfile);
6273 cppReader_forward (pfile, 1); /* Skip second '-'. */
6277 c = cppReader_getC (pfile);
6279 /*@loopbreak@*/ break;
6282 /* Don't consider final '\n' to be part of comment. */
6283 cppReader_forward (pfile, -1);
6284 /*@loopbreak@*/ break;
6288 goto return_comment;
6290 if (c2 == '-' || c2 == '=' || c2 == '>')
6295 if (pfile->parsing_include_directive)
6299 cppReader_putChar (pfile, c);
6301 /*@loopbreak@*/ break;
6302 c = cppReader_getC (pfile);
6304 if (c == '\n' || c == EOF)
6306 cppReader_errorLit (pfile,
6307 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6308 /*@loopbreak@*/ break;
6316 c2 = cppReader_peekC (pfile);
6321 cppReader_forward (pfile, 1);
6322 cpplib_reserve (pfile, 4);
6323 cppReader_putChar (pfile, c);
6324 cppReader_putChar (pfile, c2);
6326 c3 = cppReader_peekC (pfile);
6328 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6329 cppReader_nullTerminateQ (pfile);
6330 pfile->only_seen_white = 0;
6334 DPRINTF (("Macro @!"));
6335 if (cppReader_getBufferSafe (pfile)->has_escapes)
6337 c = cppReader_getC (pfile);
6338 DPRINTF (("got c: %c", c));
6341 if (pfile->output_escapes)
6342 cppReader_puts (pfile, "@-", 2);
6343 parse_name (pfile, cppReader_getC (pfile));
6346 else if (is_space [c])
6348 cpplib_reserve (pfile, 2);
6349 if (pfile->output_escapes)
6350 cppReader_putCharQ (pfile, '@');
6351 cppReader_putCharQ (pfile, c);
6359 if (pfile->output_escapes)
6361 cppReader_puts (pfile, "@@", 2);
6367 c2 = cppReader_peekC (pfile);
6370 cpplib_reserve(pfile, 2);
6371 cppReader_putCharQ (pfile, '.');
6372 c = cppReader_getC (pfile);
6376 /* FIXME - misses the case "..\\\n." */
6377 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6379 cpplib_reserve(pfile, 4);
6380 cppReader_putCharQ (pfile, '.');
6381 cppReader_putCharQ (pfile, '.');
6382 cppReader_putCharQ (pfile, '.');
6383 cppReader_forward (pfile, 2);
6384 cppReader_nullTerminateQ (pfile);
6385 pfile->only_seen_white = 0;
6391 pfile->only_seen_white = 0;
6392 op2any: /* jumped to for \ continuations */
6393 cpplib_reserve(pfile, 3);
6394 cppReader_putCharQ (pfile, c);
6396 /* evans 2003-08-24: This is a hack to fix line output for \
6397 continuations. Someday I really should get a decent pre-processor!
6401 (void) cppReader_getC (pfile); /* skip the newline to avoid extra lines */
6403 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6406 cppReader_nullTerminateQ (pfile);
6411 c2 = cppReader_peekC (pfile);
6412 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6414 cppReader_putChar (pfile, c);
6415 c = cppReader_getC (pfile);
6420 case '0': case '1': case '2': case '3': case '4':
6421 case '5': case '6': case '7': case '8': case '9':
6426 cpplib_reserve (pfile, 2);
6427 cppReader_putCharQ (pfile, c);
6429 c = cppReader_peekC (pfile);
6431 /*@loopbreak@*/ break;
6432 if (!is_idchar[c] && c != '.'
6433 && ((c2 != 'e' && c2 != 'E'
6434 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6435 || (c != '+' && c != '-')))
6436 /*@loopbreak@*/ break;
6437 cppReader_forward (pfile, 1);
6441 cppReader_nullTerminateQ (pfile);
6442 pfile->only_seen_white = 0;
6445 case 'b': case 'c': case 'd': case 'h': case 'o':
6446 case 'B': case 'C': case 'D': case 'H': case 'O':
6447 if (opts->chill && cppReader_peekC (pfile) == '\'')
6449 pfile->only_seen_white = 0;
6450 cpplib_reserve (pfile, 2);
6451 cppReader_putCharQ (pfile, c);
6452 cppReader_putCharQ (pfile, '\'');
6453 cppReader_forward (pfile, 1);
6456 c = cppReader_getC (pfile);
6458 goto chill_number_eof;
6461 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6463 cppReader_forward (pfile, 2);
6466 /*@loopbreak@*/ break;
6468 cppReader_putChar (pfile, c);
6472 cpplib_reserve (pfile, 2);
6473 cppReader_putCharQ (pfile, c);
6474 cppReader_nullTerminateQ (pfile);
6479 cppReader_forward (pfile, -1);
6481 cppReader_nullTerminate (pfile);
6488 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6489 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6490 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6491 case 'x': case 'y': case 'z':
6492 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6493 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6494 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6500 size_t before_name_written = cpplib_getWritten (pfile);
6502 parse_name (pfile, c);
6503 pfile->only_seen_white = 0;
6505 if (pfile->no_macro_expand)
6507 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6511 ident = pfile->token_buffer + before_name_written;
6512 DPRINTF (("Ident: %s", ident));
6514 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6516 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6520 DPRINTF (("No expand: %s %d", ident, ident_len));
6524 if (hp->type == T_DISABLED)
6526 DPRINTF (("Disabled!"));
6528 if (pfile->output_escapes)
6529 { /* Return "@-IDENT", followed by '\0'. */
6531 cpplib_reserve (pfile, 3);
6532 ident = pfile->token_buffer + before_name_written;
6533 cppReader_adjustWritten (pfile, 2);
6535 for (i = size_toInt (ident_len); i >= 0; i--)
6537 ident[i+2] = ident[i];
6547 ** If macro wants an arglist, verify that a '(' follows.
6548 ** first skip all whitespace, copying it to the output
6549 ** after the macro name. Then, if there is no '(',
6550 ** decide this is not a macro call and leave things that way.
6553 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6555 struct parse_marker macro_mark;
6558 DPRINTF (("Arglist macro!"));
6561 ** evans 2002-07-03: Moved this here (from below).
6562 ** This bug caused necessary whitespace to be lost
6563 ** when parsing parameterized macros without parameters.
6566 parseSetMark (¯o_mark, pfile);
6568 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6570 cppBuffer *next_buf;
6571 cppSkipHspace (pfile);
6572 if (cppReader_peekC (pfile) != EOF)
6574 DPRINTF (("Peeking!"));
6575 /*@loopbreak@*/ break;
6578 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6579 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6580 CPPBUFFER (pfile) = next_buf;
6583 /* parseSetMark (¯o_mark, pfile); */
6587 cppSkipHspace (pfile);
6588 c = cppReader_peekC (pfile);
6589 DPRINTF (("c: %c", c));
6590 is_macro_call = c == '(';
6592 /*@loopbreak@*/ break;
6593 cppReader_forward (pfile, 1);
6598 parseGotoMark (¯o_mark, pfile);
6601 parseClearMark (¯o_mark);
6605 DPRINTF (("not macro call!"));
6610 /* This is now known to be a macro call. */
6612 /* it might not actually be a macro. */
6613 if (hp->type != T_MACRO)
6618 cppReader_setWritten (pfile, before_name_written);
6619 special_symbol (hp, pfile);
6620 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6621 xbuf = (char *) dmalloc (xbuf_len + 1);
6622 cppReader_setWritten (pfile, before_name_written);
6623 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6624 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6629 ** Expand the macro, reading arguments as needed,
6630 ** and push the expansion on the input stack.
6633 cpplib_macroExpand (pfile, hp);
6634 cppReader_setWritten (pfile, before_name_written);
6637 /* An extra "@ " is added to the end of a macro expansion
6638 to prevent accidental token pasting. We prefer to avoid
6639 unneeded extra spaces (for the sake of cpp-using tools like
6640 imake). Here we remove the space if it is safe to do so. */
6642 llassert (pfile->buffer->rlimit != NULL);
6644 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6645 && pfile->buffer->rlimit[-2] == '@'
6646 && pfile->buffer->rlimit[-1] == ' ')
6648 int c1 = pfile->buffer->rlimit[-3];
6649 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6651 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6652 pfile->buffer->rlimit -= 2;
6658 case ' ': case '\t': case '\v': case '\r':
6661 cppReader_putChar (pfile, c);
6662 c = cppReader_peekC (pfile);
6663 if (c == EOF || !is_hor_space[c])
6664 /*@loopbreak@*/ break;
6665 cppReader_forward (pfile, 1);
6670 c2 = cppReader_peekC (pfile);
6671 /* allow other stuff here if a flag is set? */
6672 DPRINTF (("Got continuation!"));
6679 cppReader_putChar (pfile, c);
6680 if (pfile->only_seen_white == 0)
6681 pfile->only_seen_white = 1;
6683 output_line_command (pfile, 1, same_file);
6686 case '(': token = CPP_LPAREN; goto char1;
6687 case ')': token = CPP_RPAREN; goto char1;
6688 case '{': token = CPP_LBRACE; goto char1;
6689 case '}': token = CPP_RBRACE; goto char1;
6690 case ',': token = CPP_COMMA; goto char1;
6691 case ';': token = CPP_SEMICOLON; goto char1;
6697 pfile->only_seen_white = 0;
6698 cppReader_putChar (pfile, c);
6707 /* Parse an identifier starting with C. */
6710 parse_name (cppReader *pfile, int c)
6716 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6718 cppReader_forward (pfile, 2);
6722 cppReader_forward (pfile, -1);
6726 if (c == '$' && cppReader_isPedantic (pfile))
6728 cppReader_pedwarnLit (pfile,
6729 cstring_makeLiteralTemp ("`$' in identifier"));
6732 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6733 cppReader_putCharQ (pfile, c);
6734 c = cppReader_getC (pfile);
6740 cppReader_nullTerminateQ (pfile);
6743 /* The file_name_map structure holds a mapping of file names for a
6744 particular directory. This mapping is read from the file named
6745 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6746 map filenames on a file system with severe filename restrictions,
6747 such as DOS. The format of the file name map file is just a series
6748 of lines with two tokens on each line. The first token is the name
6749 to map, and the second token is the actual name to use. */
6751 struct file_name_map
6753 struct file_name_map *map_next;
6758 /*@constant observer char *FILE_NAME_MAP_FILE*/
6759 #define FILE_NAME_MAP_FILE "header.gcc"
6761 /* Read a space delimited string of unlimited length from a stdio
6764 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6770 set = alloc = dmalloc (len + 1);
6775 while ((ch = getc (f)) != EOF && ! is_space[ch])
6777 if (set - alloc == size_toInt (len))
6780 alloc = drealloc (alloc, len + 1);
6781 set = alloc + len / 2;
6782 /*@-branchstate@*/ }
6785 } /*@=branchstate@*/
6788 check (ungetc (ch, f) != EOF);
6790 return cstring_fromChars (alloc);
6793 /* This structure holds a linked list of file name maps, one per directory. */
6795 struct file_name_map_list
6797 /*@only@*/ struct file_name_map_list *map_list_next;
6798 /*@only@*/ cstring map_list_name;
6799 /*@null@*/ struct file_name_map *map_list_map;
6802 /* Read the file name map file for DIRNAME. */
6804 static struct file_name_map *
6805 read_name_map (cppReader *pfile, cstring dirname)
6807 struct file_name_map_list *map_list_ptr;
6811 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6812 map_list_ptr != NULL;
6813 map_list_ptr = map_list_ptr->map_list_next)
6815 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6817 return map_list_ptr->map_list_map;
6821 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6822 map_list_ptr->map_list_name = cstring_copy (dirname);
6823 map_list_ptr->map_list_map = NULL;
6825 name = cstring_copy (dirname);
6827 if (cstring_length (dirname) > 0)
6829 name = cstring_appendChar (name, CONNECTCHAR);
6832 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6834 f = fileTable_openReadFile (context_fileTable (), name);
6835 cstring_free (name);
6839 map_list_ptr->map_list_map = NULL;
6845 while ((ch = getc (f)) != EOF)
6848 struct file_name_map *ptr;
6855 from = read_filename_string (ch, f);
6856 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6861 to = read_filename_string (ch, f);
6863 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6864 ptr->map_from = from;
6866 /* Make the real filename absolute. */
6867 if (cstring_length (to) > 1
6868 && osd_isConnectChar (cstring_firstChar (to)))
6874 ptr->map_to = cstring_copy (dirname);
6875 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6876 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6879 ptr->map_next = map_list_ptr->map_list_map;
6880 map_list_ptr->map_list_map = ptr;
6882 while ((ch = getc (f)) != '\n')
6886 /*@innerbreak@*/ break;
6891 assertSet (map_list_ptr->map_list_map);
6892 check (fileTable_closeFile (context_fileTable (),f) == 0);
6895 map_list_ptr->map_list_next = pfile->opts->map_list;
6896 pfile->opts->map_list = map_list_ptr;
6898 return map_list_ptr->map_list_map;
6901 /* Try to open include file FILENAME. SEARCHPTR is the directory
6902 being tried from the include file search path. This function maps
6903 filenames on file systems based on information read by
6907 open_include_file (cppReader *pfile,
6909 struct file_name_list *searchptr)
6911 char *filename = cstring_toCharsSafe (fname);
6912 struct file_name_map *map;
6916 cstring_markOwned (fname);
6918 cpp_setLocation (pfile);
6920 if (context_getFlag (FLG_NEVERINCLUDE))
6922 if (isHeaderFile (fname))
6924 return SKIP_INCLUDE;
6928 if ((searchptr != NULL) && ! searchptr->got_name_map)
6930 searchptr->name_map = read_name_map (pfile,
6931 !cstring_isEmpty (searchptr->fname)
6932 ? searchptr->fname :
6933 cstring_makeLiteralTemp ("."));
6934 searchptr->got_name_map = 1;
6937 /* First check the mapping for the directory we are using. */
6939 if ((searchptr != NULL)
6940 && (searchptr->name_map != NULL))
6944 if (!cstring_isEmpty (searchptr->fname))
6946 from += cstring_length (searchptr->fname) + 1;
6949 for (map = searchptr->name_map;
6951 map = map->map_next)
6953 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6956 ** Found a match. Check if the file should be skipped
6959 if (cpp_skipIncludeFile (map->map_to))
6961 return SKIP_INCLUDE;
6965 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6972 ** Try to find a mapping file for the particular directory we are
6973 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6974 ** in /usr/include/header.gcc and look up types.h in
6975 ** /usr/include/sys/header.gcc.
6978 p = strrchr (filename, CONNECTCHAR);
6985 if ((searchptr != NULL)
6986 && (cstring_isDefined (searchptr->fname))
6987 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6988 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6990 size_fromInt (p - filename)))
6992 /* filename is in SEARCHPTR, which we've already checked. */
6994 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6996 return SKIP_INCLUDE;
7000 return cpp_openIncludeFile (filename);
7006 dir = mstring_copy (".");
7011 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
7012 memcpy (dir, filename, size_fromInt (p - filename));
7013 dir[p - filename] = '\0';
7017 for (map = read_name_map (pfile, cstring_fromChars (dir));
7019 map = map->map_next)
7021 if (cstring_equal (map->map_from, cstring_fromChars (from)))
7025 if (cpp_skipIncludeFile (map->map_to))
7027 return SKIP_INCLUDE;
7031 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
7038 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
7040 return SKIP_INCLUDE;
7044 return cpp_openIncludeFile (filename);
7048 /* Process the contents of include file FNAME, already open on descriptor F,
7050 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
7051 "system" include directories (as decided by the `is_system_include'
7053 DIRPTR is the link in the dir path through which this file was found,
7054 or 0 if the file name was absolute or via the current directory.
7055 Return 1 on success, 0 on failure.
7057 The caller is responsible for the cppReader_pushBuffer. */
7060 finclude (cppReader *pfile, int f,
7062 bool system_header_p,
7063 /*@dependent@*/ struct file_name_list *dirptr)
7069 cppBuffer *fp; /* For input stack frame */
7071 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
7073 cppReader_perrorWithName (pfile, fname);
7074 check (close (f) == 0);
7075 (void) cppReader_popBuffer (pfile);
7081 fp = cppReader_getBufferSafe (pfile);
7083 /*@-temptrans@*/ /* fname shouldn't really be temp */
7084 fp->nominal_fname = fp->fname = fname;
7088 fp->system_header_p = system_header_p;
7091 fp->cleanup = cppReader_fileCleanup;
7093 if (S_ISREG (st_mode))
7096 fp->buf = (char *) dmalloc (st_size + 2);
7097 fp->alimit = fp->buf + st_size + 2;
7100 /* Read the file contents, knowing that st_size is an upper bound
7101 on the number of bytes we can read. */
7102 length = safe_read (f, fp->buf, size_toInt (st_size));
7103 fp->rlimit = fp->buf + length;
7104 if (length < 0) goto nope;
7106 else if (S_ISDIR (st_mode))
7108 cppReader_error (pfile,
7109 message ("Directory specified where file is expected: %s", fname));
7110 check (close (f) == 0);
7116 ** Cannot count its file size before reading.
7117 ** First read the entire file into heap and
7118 ** copy them into buffer on stack.
7121 size_t bsize = 2000;
7126 fp->buf = (char *) dmalloc (bsize + 2);
7129 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
7132 goto nope; /* error! */
7135 if (st_size != bsize)
7137 break; /* End of file */
7141 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
7145 length = size_toInt (st_size);
7148 if ((length > 0 && fp->buf[length - 1] != '\n')
7149 /* Backslash-newline at end is not good enough. */
7150 || (length > 1 && fp->buf[length - 2] == '\\')) {
7151 fp->buf[length++] = '\n';
7154 fp->buf[length] = '\0';
7155 fp->rlimit = fp->buf + length;
7157 /* Close descriptor now, so nesting does not use lots of descriptors. */
7158 check (close (f) == 0);
7160 /* Must do this before calling trigraph_pcp, so that the correct file name
7161 will be printed in warning messages. */
7163 pfile->input_stack_listing_current = 0;
7168 cppReader_perrorWithName (pfile, fname);
7169 check (close (f) == 0);
7175 cpplib_init (cppReader *pfile)
7177 memset ((char *) pfile, 0, sizeof (*pfile));
7179 pfile->get_token = cpplib_getToken;
7180 pfile->token_buffer_size = 200;
7181 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
7182 pfile->all_include_files = NULL;
7186 cppReader_setWritten (pfile, 0);
7188 pfile->system_include_depth = 0;
7189 pfile->max_include_len = 0;
7190 pfile->timebuf = NULL;
7191 pfile->only_seen_white = 1;
7193 pfile->buffer = cppReader_nullBuffer (pfile);
7197 cppReader_finish (/*@unused@*/ cppReader *pfile)
7202 /* Free resources used by PFILE.
7203 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
7206 cppCleanup (/*@special@*/ cppReader *pfile)
7208 /*@releases pfile@*/
7210 DPRINTF (("cppCleanup!"));
7212 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
7214 (void) cppReader_popBuffer (pfile);
7217 if (pfile->token_buffer != NULL)
7219 sfree (pfile->token_buffer);
7220 pfile->token_buffer = NULL;
7223 while (pfile->if_stack != NULL)
7225 cppIfStackFrame *temp = pfile->if_stack;
7226 pfile->if_stack = temp->next;
7230 while (pfile->all_include_files != NULL)
7232 struct file_name_list *temp = pfile->all_include_files;
7233 pfile->all_include_files = temp->next;
7234 /*@-dependenttrans@*/
7235 cstring_free (temp->fname);
7236 /*@=dependenttrans@*/
7240 /* evans 2002-07-12 */
7241 while (pfile->opts->map_list != NULL)
7243 struct file_name_map_list *temp = pfile->opts->map_list;
7244 pfile->opts->map_list = pfile->opts->map_list->map_list_next;
7245 cstring_free (temp->map_list_name);
7249 while (pfile->opts->include != NULL)
7251 struct file_name_list *temp = pfile->opts->include;
7252 pfile->opts->include = pfile->opts->include->next;
7253 /* cstring_free (temp->fname); */
7257 sfree (pfile->opts);
7259 cppReader_hashCleanup ();
7263 ** Get the file-mode and data size of the file open on FD
7264 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7268 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7272 if (fstat (fd, &sbuf) < 0) {
7278 if (mode_pointer != NULL)
7280 *mode_pointer = sbuf.st_mode;
7283 if (size_pointer != NULL)
7285 *size_pointer = (size_t) sbuf.st_size;
7291 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7292 retrying if necessary. Return a negative value if an error occurs,
7293 otherwise return the actual number of bytes read,
7294 which must be LEN unless end-of-file was reached. */
7296 static int safe_read (int desc, char *ptr, int len)
7302 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7303 /*@-compdef@*/ /* ptr is an out parameter */
7304 int nchars = _read (desc, ptr, (unsigned) left);
7307 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7316 return (int) nchars;
7330 /* Initialize PMARK to remember the current position of PFILE. */
7333 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7335 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7337 pmark->next = pbuf->marks;
7339 pbuf->marks = pmark;
7343 pmark->position = pbuf->cur - pbuf->buf;
7344 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
7347 /* Cleanup PMARK - we no longer need it. */
7349 void parseClearMark (struct parse_marker *pmark)
7351 struct parse_marker **pp = &pmark->buf->marks;
7353 for (; ; pp = &(*pp)->next)
7355 llassert (*pp != NULL);
7356 if (*pp == pmark) break;
7362 /* Backup the current position of PFILE to that saved in PMARK. */
7365 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7367 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7369 if (pbuf != pmark->buf)
7371 cpp_setLocation (pfile);
7372 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7375 llassert (pbuf->buf != NULL);
7376 pbuf->cur = pbuf->buf + pmark->position;
7377 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
7380 /* Reset PMARK to point to the current position of PFILE. (Same
7381 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7384 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7386 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7388 if (pbuf != pmark->buf)
7390 cpp_setLocation (pfile);
7391 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7394 pmark->position = pbuf->cur - pbuf->buf;
7395 DPRINTF (("move mark: %s", pmark->position));
7398 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7400 struct cppOptions *opts = CPPOPTIONS (pfile);
7403 /* The code looks at the defaults through this pointer, rather than through
7404 the constant structure above. This pointer gets changed if an environment
7405 variable specifies other defaults. */
7407 struct default_include *include_defaults = include_defaults_array;
7409 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7410 /* There seems to be confusion about what CPATH should do,
7411 so for the moment it is not documented. */
7412 /* Some people say that CPATH should replace the standard include dirs,
7413 but that seems pointless: it comes before them, so it overrides them
7416 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7418 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7420 path_include (pfile, cstring_toCharsSafe (xp));
7423 /* Now that dollars_in_ident is known, initialize is_idchar. */
7424 initialize_char_syntax (opts);
7426 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7427 and option processing. */
7429 initialize_builtins (pfile);
7431 /* Do standard #defines and assertions
7432 that identify system and machine type. */
7434 if (!opts->inhibit_predefs) {
7435 char *p = (char *) dmalloc (strlen (predefs) + 1);
7436 strcpy (p, predefs);
7442 while (*p == ' ' || *p == '\t')
7447 /* Handle -D options. */
7448 if (p[0] == '-' && p[1] == 'D')
7452 while (*p && *p != ' ' && *p != '\t')
7462 if (opts->debug_output)
7464 output_line_command (pfile, 0, same_file);
7467 cppReader_define (pfile, q);
7469 while (*p == ' ' || *p == '\t')
7483 opts->done_initializing = 1;
7485 { /* Read the appropriate environment variable and if it exists
7486 replace include_defaults with the listed path. */
7490 int win32_buf_size = 0; /* memory we need to allocate */
7493 if (opts->cplusplus)
7495 epath = getenv ("CPLUS_INCLUDE_PATH");
7499 epath = getenv ("C_INCLUDE_PATH");
7503 ** If the environment var for this language is set,
7504 ** add to the default list of include directories.
7507 if (epath != NULL) {
7508 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7510 char *startp, *endp;
7513 /* if we have a posix path list, convert to win32 path list */
7514 if (cygwin32_posix_path_list_p (epath))
7516 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7517 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7518 cygwin32_posix_to_win32_path_list (epath, win32epath);
7522 for (num_dirs = 1, startp = epath; *startp; startp++)
7524 if (*startp == PATH_SEPARATOR)
7530 = (struct default_include *) dmalloc ((num_dirs
7531 * sizeof (struct default_include))
7532 + sizeof (include_defaults_array));
7535 startp = endp = epath;
7538 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7539 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7541 strncpy (nstore, startp, size_fromInt (endp - startp));
7544 strcpy (nstore, ".");
7548 nstore[endp-startp] = '\0';
7551 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7552 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7553 include_defaults[num_dirs].cxx_aware = 1;
7560 endp = startp = endp + 1;
7567 /* Put the usual defaults back in at the end. */
7568 memcpy ((char *) &include_defaults[num_dirs],
7569 (char *) include_defaults_array,
7570 sizeof (include_defaults_array));
7573 /*@-branchstate@*/ } /*@=branchstate@*/
7576 cppReader_appendIncludeChain (pfile, opts->before_system,
7577 opts->last_before_system);
7579 opts->first_system_include = opts->before_system;
7581 /* Unless -fnostdinc,
7582 tack on the standard include file dirs to the specified list */
7584 if (!opts->no_standard_includes) {
7585 struct default_include *p = include_defaults;
7586 char *specd_prefix = opts->include_prefix;
7587 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7588 size_t default_len = 0;
7590 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7591 if (default_prefix != NULL) {
7592 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7593 default_len = strlen (default_prefix) - 7;
7594 default_prefix[default_len] = 0;
7598 /* Search "translated" versions of GNU directories.
7599 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7600 if (specd_prefix != 0 && default_len != 0)
7601 for (p = include_defaults; p->fname != NULL; p++) {
7602 /* Some standard dirs are only for C++. */
7604 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7605 /* Does this dir start with the prefix? */
7606 llassert (default_prefix != NULL);
7608 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix, default_len))
7610 /* Yes; change prefix and add to search list. */
7611 struct file_name_list *nlist
7612 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7613 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7614 char *str = (char *) dmalloc (this_len + 1);
7615 strcpy (str, specd_prefix);
7616 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7619 nlist->fname = cstring_fromChars (str);
7620 nlist->control_macro = 0;
7621 nlist->c_system_include_path = !p->cxx_aware;
7622 nlist->got_name_map = 0;
7624 if (opts->first_system_include == 0)
7626 opts->first_system_include = nlist;
7629 cppReader_addIncludeChain (pfile, nlist);
7634 /* Search ordinary names for GNU include directories. */
7636 for (p = include_defaults; p->fname != NULL; p++)
7638 /* Some standard dirs are only for C++. */
7640 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7642 struct file_name_list *nlist
7643 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7644 nlist->control_macro = 0;
7645 nlist->c_system_include_path = !p->cxx_aware;
7646 nlist->fname = p->fname;
7647 nlist->got_name_map = 0;
7650 /* Spurious warning reported for opts->first_system_include */
7651 /*@-usereleased@*/ if (opts->first_system_include == NULL)
7653 opts->first_system_include = nlist;
7657 cppReader_addIncludeChain (pfile, nlist);
7660 sfree (default_prefix);
7663 /* Tack the after_include chain at the end of the include chain. */
7664 cppReader_appendIncludeChain (pfile, opts->after_include,
7665 opts->last_after_include);
7667 /* Spurious warnings for opts->first_system_include */
7669 if (opts->first_system_include == NULL)
7671 opts->first_system_include = opts->after_include;
7675 /* With -v, print the list of dirs to search. */
7676 if (opts->verbose) {
7677 struct file_name_list *p;
7678 fprintf (stderr, "#include \"...\" search starts here:\n");
7680 for (p = opts->include; p != NULL; p = p->next) {
7681 if (p == opts->first_bracket_include)
7682 fprintf (stderr, "#include <...> search starts here:\n");
7684 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7686 fprintf (stderr, "End of search list.\n");
7690 int cppReader_startProcess (cppReader *pfile, cstring fname)
7694 struct cppOptions *opts = CPPOPTIONS (pfile);
7696 fp = cppReader_pushBuffer (pfile, NULL, 0);
7703 if (opts->in_fname == NULL)
7705 opts->in_fname = cstring_makeLiteralTemp ("");
7708 fp->fname = opts->in_fname;
7709 fp->nominal_fname = fp->fname;
7712 /* Copy the entire contents of the main input file into
7713 the stacked input buffer previously allocated for it. */
7715 if (cstring_isEmpty (fname))
7717 fname = cstring_makeLiteralTemp ("");
7720 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7722 cppReader_error (pfile,
7723 message ("Error opening %s for reading: %s",
7724 fname, lldecodeerror (errno)));
7733 if (finclude (pfile, f, fname, 0, NULL))
7735 output_line_command (pfile, 0, same_file);
7741 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7743 return pfile->buffer;
7746 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7748 llassert (pfile->buffer != NULL);
7749 return pfile->buffer;
7752 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7754 llassert (buf->buf != NULL);
7755 return (buf->buf + buf->line_base);
7758 int cpplib_bufPeek (cppBuffer *buf)
7760 if (buf->cur == NULL || buf->rlimit == NULL) {
7764 if (buf->cur < buf->rlimit) {
7771 bool cppBuffer_isMacro (cppBuffer *buf)
7775 return (buf->cleanup == cppReader_macroCleanup);
7782 ** Returns true if the macro should be checked, false
7783 ** if it should be expanded normally.
7786 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7787 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7788 static bool expectiter = FALSE; /* preceeded by @iter@ */
7789 static bool expectenditer = FALSE; /* second after @iter@ */
7790 static bool expectfunction = FALSE; /* preceeded by @function@ */
7791 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7792 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7794 static void cpp_setLocation (cppReader *pfile)
7799 if (pfile->buffer != NULL)
7801 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7803 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7805 DPRINTF (("Looking up: %s", fname));
7807 if (fileTable_exists (context_fileTable (), fname))
7809 fid = fileTable_lookup (context_fileTable (), fname);
7813 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7815 fid = fileTable_lookup (context_fileTable (),
7816 cppReader_getBufferSafe (pfile)->fname);
7821 fid = fileTable_lookup (context_fileTable (),
7822 cppReader_getBufferSafe (pfile)->fname);
7825 line = cppReader_getBufferSafe (pfile)->lineno;
7826 fileloc_free (g_currentloc);
7828 if (fileId_isValid (fid))
7830 g_currentloc = fileloc_create (fid, line, 1);
7834 g_currentloc = fileloc_createBuiltin ();
7839 fileloc_free (g_currentloc);
7840 g_currentloc = fileloc_createBuiltin ();
7844 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7846 bool checkmacro = FALSE;
7847 bool hasParams = FALSE;
7848 bool noexpand = FALSE;
7852 cpp_setLocation (pfile);
7854 DPRINTF (("Should check macro? %s", p));
7856 if (expectiter || expectconstant || expectenditer)
7861 expectenditer = TRUE;
7866 expectconstant = FALSE;
7867 expectenditer = FALSE;
7870 if (notfunction || notparseable)
7872 notfunction = FALSE;
7873 notparseable = FALSE;
7882 llassert (*p == '#');
7885 while (*p == ' ' || *p == '\t')
7890 llassert (*p == 'd'); /* define starts */
7894 while (*p == ' ' || *p == '\t')
7899 sname = cstring_fromChars (p);
7900 DPRINTF (("Check macro: %s", sname));
7902 while (((c = *p) != ' ')
7903 && c != '\0' && c != '('
7904 && c != '\t' && c != '\\' && c != '\n'
7910 hasParams = (c == '(');
7915 notparseable = FALSE;
7917 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7919 DPRINTF (("Clear notfunction"));
7920 notfunction = FALSE;
7935 if (usymtab_existsReal (sname))
7937 uentry ue = usymtab_lookup (sname);
7939 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7941 if (fileloc_isPreproc (uentry_whereLast (ue)))
7947 if (uentry_isSpecified (ue))
7949 checkmacro = context_getFlag (FLG_SPECMACROS);
7955 checkmacro = context_getFlag (FLG_LIBMACROS)
7956 || context_getFlag (FLG_FCNMACROS);
7964 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7966 if (fileloc_isSystemFile (g_currentloc)
7967 && context_getFlag (FLG_SYSTEMDIREXPAND))
7969 ; /* don't check this macro */
7970 DPRINTF (("Don't check 1"));
7978 DPRINTF (("Has params..."));
7980 if (context_getFlag (FLG_FCNMACROS))
7982 if (usymtab_exists (sname))
7985 ** only get here is macro is redefined
7986 ** error reported elsewhere
7989 DPRINTF (("It exists!"));
7994 ** We make it a forward function, since it might be declared elsewhere.
7995 ** After all headers have been processed, we should check the forward
7999 fileloc loc = fileloc_makePreproc (g_currentloc);
8001 /* the line is off-by-one, since the newline was already read */
8006 expectfunction = FALSE;
8009 le = uentry_makeForwardFunction (sname,
8010 typeId_invalid, loc);
8016 /* Do not define here! */
8018 (void) usymtab_addEntry (le);
8022 DPRINTF (("Check: TRUE"));
8026 DPRINTF (("Flag FCN_MACROS not set!"));
8031 DPRINTF (("No params"));
8033 if (context_getFlag (FLG_CONSTMACROS))
8035 bool nocontent = FALSE;
8048 ** Check if there is nothing after the define.
8051 while ((*rest) != '\0' && isspace (*rest))
8058 nocontent = TRUE; /* empty macro, don't check */
8063 if (usymtab_exists (sname))
8069 fileloc loc = fileloc_makePreproc (g_currentloc);
8070 DPRINTF (("Make constant: %s", sname));
8071 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
8072 (void) usymtab_addEntry (le);
8075 checkmacro = !nocontent;
8080 if (checkmacro && usymtab_existsType (sname))
8082 DPRINTF (("Making false..."));
8084 ppllerror (message ("Specified type implemented as macro: %s", sname));
8094 if (usymtab_exists (sname))
8096 uentry ue = usymtab_lookupExpose (sname);
8097 fileloc tloc = fileloc_makePreproc (g_currentloc);
8099 uentry_setDefined (ue, tloc);
8100 fileloc_free (tloc);
8101 uentry_setUsed (ue, fileloc_undefined);
8105 fileloc tloc = fileloc_makePreproc (g_currentloc);
8106 uentry ue = uentry_makeExpandedMacro (sname, tloc);
8107 DPRINTF (("Make expanded macro: %s", sname));
8108 DPRINTF (("Not in symbol table: %s", sname));
8110 (void) usymtab_addGlobalEntry (ue);
8111 fileloc_free (tloc);
8116 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
8120 static enum cpp_token
8121 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
8123 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
8127 bool eliminateComment = FALSE;
8129 llassert (pbuf->buf != NULL);
8131 start = pbuf->buf + smark->position;
8133 llassert (pbuf->cur != NULL);
8134 len = pbuf->cur - start;
8137 && start[1] == context_getCommentMarkerChar ())
8141 char *scomment = start + 2;
8142 char savec = start[len];
8144 cpp_setLocation (pfile);
8145 loc = fileloc_copy (g_currentloc);
8147 start[0] = BEFORE_COMMENT_MARKER[0];
8148 start[1] = BEFORE_COMMENT_MARKER[1];
8150 llassert (start[len - 2] == '*');
8151 start[len - 2] = AFTER_COMMENT_MARKER[0];
8153 llassert (start[len - 1] == '/');
8154 start[len - 1] = AFTER_COMMENT_MARKER[1];
8156 cpplib_reserve(pfile, size_fromInt (1 + len));
8157 cppReader_putCharQ (pfile, c);
8159 cpp_setLocation (pfile);
8163 if (mstring_containsString (scomment, "/*"))
8165 (void) cppoptgenerror
8167 message ("Comment starts inside syntactic comment: %s",
8168 cstring_fromChars (scomment)),
8174 if (mstring_equalPrefix (scomment, "ignore"))
8176 if (!context_getFlag (FLG_NOCOMMENTS))
8178 context_enterSuppressRegion (loc);
8181 else if (mstring_equalPrefix (scomment, "end"))
8183 if (!context_getFlag (FLG_NOCOMMENTS))
8185 context_exitSuppressRegion (loc);
8188 else if (mstring_equalPrefix (scomment, "notparseable"))
8190 notparseable = TRUE;
8192 eliminateComment = TRUE;
8194 else if (mstring_equalPrefix (scomment, "notfunction"))
8198 eliminateComment = TRUE;
8200 else if (mstring_equalPrefix (scomment, "iter"))
8204 else if (mstring_equalPrefix (scomment, "function"))
8206 expectfunction = TRUE;
8208 else if (mstring_equalPrefix (scomment, "constant"))
8210 expectconstant = TRUE;
8214 char sChar = *scomment;
8220 char *rest = scomment + 1;
8222 if (mstring_equalPrefix (rest, "commentchar"))
8224 eliminateComment = TRUE;
8228 ppllerror (cstring_makeLiteral
8229 ("Cannot restore commentchar"));
8233 char *next = scomment + 12; /* strlen commentchar = 12 */
8235 if (*next != ' ' && *next != '\t' && *next != '\n')
8239 ("Syntactic commentchar comment is not followed by a "
8240 "whitespace character: %c",
8245 char cchar = *(next + 1);
8250 (cstring_makeLiteral
8251 ("Cannot set commentchar to NUL"));
8255 context_setCommentMarkerChar (cchar);
8256 /* setComment = TRUE; */
8261 else if (mstring_equalPrefix (scomment, "nestcomment"))
8263 /* fix from Mike Miller <MikeM@xata.com> */
8264 context_fileSetFlag (FLG_NESTCOMMENT,
8265 ynm_fromCodeChar (sChar),
8268 else if (mstring_equalPrefix (rest, "namechecks"))
8270 context_fileSetFlag (FLG_NAMECHECKS,
8271 ynm_fromCodeChar (sChar),
8274 else if (mstring_equalPrefix (rest, "macroredef"))
8276 context_fileSetFlag (FLG_MACROREDEF,
8277 ynm_fromCodeChar (sChar),
8280 else if (mstring_equalPrefix (rest, "usevarargs"))
8282 context_fileSetFlag (FLG_USEVARARGS,
8283 ynm_fromCodeChar (sChar),
8286 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8288 context_fileSetFlag (FLG_MACRONEXTLINE,
8289 ynm_fromCodeChar (sChar),
8292 else if (mstring_equalPrefix (rest, "allmacros")
8293 || mstring_equalPrefix (rest, "fcnmacros")
8294 || mstring_equalPrefix (rest, "constmacros"))
8298 if (mstring_equalPrefix (rest, "allmacros"))
8302 else if (mstring_equalPrefix (rest, "fcnmacros"))
8308 llassert (mstring_equalPrefix (rest, "constmacros"));
8309 fl = FLG_CONSTMACROS;
8312 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
8313 notfunction = FALSE;
8326 if (eliminateComment)
8331 /* Replaces comment char's in start with spaces */
8333 for (i = 2; i < len - 2; i++)
8335 if (start[i] == BEFORE_COMMENT_MARKER[0]
8336 || start[i] == BEFORE_COMMENT_MARKER[1]
8337 || start[i] == context_getCommentMarkerChar ())
8343 cppReader_putStrN (pfile, start, size_fromInt (len));
8344 parseClearMark (smark);
8354 ** Output the comment as all spaces so line/column
8355 ** in output file is still correct.
8359 cstring lintcomment = cstring_undefined;
8361 if (context_getFlag (FLG_LINTCOMMENTS))
8363 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8365 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8367 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8369 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8371 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8373 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8375 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8377 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8379 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8381 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8385 lintcomment = cstring_undefined;
8390 lintcomment = cstring_undefined;
8393 if (cstring_isDefined (lintcomment))
8395 c = BEFORE_COMMENT_MARKER[0];
8396 start[0] = BEFORE_COMMENT_MARKER[1];
8398 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
8400 for (i = 1; i < len - 2; i++)
8402 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
8405 start[len - 2] = AFTER_COMMENT_MARKER[0];
8406 start[len - 1] = AFTER_COMMENT_MARKER[1];
8410 /* Replaces char's in start with spaces */
8411 for (i = 0; i < len; i++)
8415 && start[i + 1] == '*') {
8416 (void) cppoptgenerror
8418 message ("Comment starts inside comment"),
8422 if (start[i] != '\n')
8429 cpplib_reserve (pfile, size_fromInt (1 + len));
8430 cppReader_putCharQ (pfile, c);
8431 cppReader_putStrN (pfile, start, size_fromInt (len));
8432 parseClearMark (smark);
8438 static int cpp_openIncludeFile (char *filename)
8440 int res = open (filename, O_RDONLY, 0666);
8442 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8445 if (!fileTable_exists (context_fileTable (),
8446 cstring_fromChars (filename)))
8448 if (fileloc_isXHFile (g_currentloc))
8451 ** Files includes by XH files are also XH files
8454 (void) fileTable_addXHFile (context_fileTable (),
8455 cstring_fromChars (filename));
8459 (void) fileTable_addHeaderFile (context_fileTable (),
8460 cstring_fromChars (filename));
8465 DPRINTF (("File already exists: %s", filename));
8472 static bool cpp_skipIncludeFile (cstring fname)
8474 if (context_isSystemDir (fname))
8476 DPRINTF (("System dir: %s", fname));
8478 if (lcllib_isSkipHeader (fname))
8480 DPRINTF (("Skip include TRUE: %s", fname));
8484 if (context_getFlag (FLG_SKIPSYSHEADERS))
8487 ** 2003-04-18: Patch from Randal Parsons
8491 ** Don't skip include file unless the file actually exists.
8492 ** It may be in a different directory.
8495 int f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666);
8499 check (close (f) == 0);
8500 DPRINTF (("Skip include TRUE: %s", fname));
8505 /* Keep looking... */
8510 if (context_getFlag (FLG_SINGLEINCLUDE))
8512 fname = removePreDirs (fname);
8514 # if defined (WIN32) || defined (OS2)
8515 cstring_replaceAll (fname, '\\', '/');
8518 if (fileTable_exists (context_fileTable (), fname))
8520 DPRINTF (("Skip include TRUE: %s", fname));
8525 DPRINTF (("Skip include FALSE: %s", fname));
8529 static int cpp_peekN (cppReader *pfile, int n)
8531 cppBuffer *buf = cppReader_getBufferSafe (pfile);
8533 llassert (buf->cur != NULL);
8535 return (buf->rlimit - buf->cur >= (n)
8540 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8545 void cppBuffer_forward (cppBuffer *buf, int n)
8547 llassert (buf->cur != NULL);
8551 /*@=bufferoverflowhigh@*/