2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
29 Contributed by Per Bothner, 1994-95.
30 Based on CCCP program by Paul Rubin, June 1986
31 Adapted to ANSI C, Richard Stallman, Jan 1987
33 This program is free software; you can redistribute it and/or modify it
34 under the terms of the GNU General Public License as published by the
35 Free Software Foundation; either version 2, or (at your option) any
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 In other words, you are welcome to use, share and improve this program.
48 You are forbidden to forbid anyone else to use, share and improve
49 what you give them. Help stamp out software-hoarding! */
53 * - OS2 drive specs like WIN32
54 * - Includes for IBMs OS/2 compiler
66 # if !(defined (WIN32) || defined (OS2) && defined (__IBMC__))
70 # include <sys/types.h>
71 # include <sys/stat.h>
74 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
76 # include <sys/utime.h> /* for __DATE__ and __TIME__ */
81 # include <time.h> /* Reported by Paul Smith */
82 # include <sys/time.h>
83 # include <sys/resource.h>
85 # include <sys/times.h>
90 # endif /* not WIN32 */
92 /* This defines "errno" properly for VMS, and gives us EACCES. */
95 # include "splintMacros.nf"
99 # include "cpperror.h"
100 # include "cpphash.h"
102 # include "version.h"
107 ** This is really kludgey code...
113 #define NO_SHORTNAMES
121 /*@constant int IMPORT_FOUND@*/
122 # define IMPORT_FOUND -2
124 /*@constant int SKIP_INCLUDE@*/
125 # define SKIP_INCLUDE IMPORT_FOUND
127 /*@constant unused int IMPORT_NOT_FOUND@*/
128 # define IMPORT_NOT_FOUND -1
131 /*@constant unused int STDC_VALUE@*/
135 /* By default, colon separates directories in a path. */
136 #ifndef PATH_SEPARATOR
137 /*@constant char PATH_SEPARATOR@*/
138 #define PATH_SEPARATOR ':'
141 static void parse_name (cppReader *, int);
143 static int cpp_openIncludeFile (char *p_filename)
144 /*@modifies fileSystem @*/ ;
146 static void cpp_setLocation (cppReader *p_pfile)
147 /*@modifies g_currentloc@*/ ;
149 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
150 struct parse_marker *p_smark)
151 /*@modifies p_pfile, p_smark@*/;
153 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
155 static int cppReader_checkMacroNameLoc (fileloc p_loc,
159 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
165 /* Symbols to predefine. */
167 #ifdef CPP_PREDEFINES
168 static /*@observer@*/ char *predefs = CPP_PREDEFINES;
170 static /*@observer@*/ char *predefs = "";
173 /* We let tm.h override the types used here, to handle trivial differences
174 such as the choice of unsigned int or long unsigned int for size_t.
175 When machines start needing nontrivial differences in the size type,
176 it would be best to do something here to figure out automatically
177 from other information what type to use. */
179 /* The string value for __SIZE_TYPE__. */
182 /*@constant observer char *SIZE_TYPE@*/
183 #define SIZE_TYPE "long unsigned int"
186 /* The string value for __PTRDIFF_TYPE__. */
189 /*@constant observer char *PTRDIFF_TYPE@*/
190 #define PTRDIFF_TYPE "long int"
193 /* The string value for __WCHAR_TYPE__. */
196 /*@constant observer char *WCHAR_TYPE@*/
197 #define WCHAR_TYPE "int"
200 /* The string value for __USER_LABEL_PREFIX__ */
202 #ifndef USER_LABEL_PREFIX
203 /*@constant observer char *USER_LABEL_PREFIX@*/
204 #define USER_LABEL_PREFIX ""
207 /* The string value for __REGISTER_PREFIX__ */
209 #ifndef REGISTER_PREFIX
210 /*@constant observer char *REGISTER_PREFIX@*/
211 #define REGISTER_PREFIX ""
214 /* table to tell if char can be part of a C identifier. */
215 static bool is_idchar[256];
216 /* table to tell if char can be first char of a c identifier. */
217 static bool is_idstart[256];
218 /* table to tell if c is horizontal space. */
219 static bool is_hor_space[256];
220 /* table to tell if c is horizontal or vertical space. */
221 static bool is_space[256];
223 static /*@exposed@*/ /*@null@*/ cppBuffer *
224 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
225 /*@uses p_pfile->buffer@*/
226 /*@modifies nothing@*/ ;
229 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
232 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
234 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
236 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
237 # define cppBuffer_get(BUFFER) \
238 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
240 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
241 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
242 modifies *p_file; @*/
243 # define cppReader_puts(PFILE, STR, N) \
244 cpplib_reserve(PFILE, N), cppReader_putStrN (PFILE, STR,N)
246 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
248 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
249 modifies *p_file; @*/
250 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
252 /* Append character CH to PFILE's output buffer. Make space if need be. */
254 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
255 modifies *p_file; @*/
256 #define cppReader_putChar(PFILE, CH) (cpplib_reserve (PFILE, (size_t) 1), cppReader_putCharQ (PFILE, CH))
258 /* Make sure PFILE->limit is followed by '\0'. */
259 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
260 modifies *p_file; @*/
262 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
264 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
265 modifies *p_file; @*/
266 # define cppReader_nullTerminate(PFILE) \
267 (cpplib_reserve (PFILE, (size_t) 1), *(PFILE)->limit = 0)
269 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
270 modifies *p_file; @*/
271 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
273 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
274 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
276 /*@function static observer char *cppReader_wcharType (cppReader *)
277 modifies nothing; @*/
279 # define cppReader_wcharType(PFILE) \
280 (CPPOPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
282 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
284 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
285 # define cppReader_forward(pfile, N) \
286 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
288 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
289 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
291 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
292 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
294 /* Move all backslash-newline pairs out of embarrassing places.
295 Exchange all such pairs following BP
296 with any potentially-embarrassing characters that follow them.
297 Potentially-embarrassing characters are / and *
298 (because a backslash-newline inside a comment delimiter
299 would cause it not to be recognized). */
302 # define NEWLINE_FIX \
303 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
305 /* Same, but assume we've already read the potential '\\' into C. */
307 # define NEWLINE_FIX1(C) do { \
308 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
311 static void parseSetMark (/*@out@*/ struct parse_marker *,
313 static void parseClearMark (struct parse_marker *);
314 static void parseGotoMark (struct parse_marker *, cppReader *);
315 static void parseMoveMark (struct parse_marker *, cppReader *);
317 /* If we have a huge buffer, may need to cache more recent counts */
318 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
320 static /*@exposed@*/ /*@null@*/ cppBuffer *
321 cppReader_pushBuffer (cppReader *p_pfile,
322 /*@owned@*/ /*@null@*/ char *, size_t)
323 /*@modifies p_pfile@*/ ;
325 static void cppReader_appendIncludeChain
327 /*@keep@*/ struct file_name_list *p_first,
328 /*@dependent@*/ struct file_name_list *p_last);
330 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
331 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
333 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
334 /*@unused@*/ cppReader *p_pfile);
336 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
337 /*@unused@*/ cppReader *p_pfile);
339 static int cppReader_handleDirective (cppReader *p_pfile);
341 static void cppReader_scanBuffer (cppReader *p_pfile);
343 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
346 ** WIN32 (at least the VC++ include files) does not define mode_t.
349 /*@-incondefs@*/ /*@-czechtypes@*/
350 typedef unsigned int mode_t;
351 /*@=incondefs@*/ /*@=czechtypes@*/
355 static int file_size_and_mode (int p_fd, /*@out@*/ mode_t *p_mode_pointer,
356 /*@out@*/ size_t *p_size_pointer);
357 static int safe_read (int p_desc, /*@out@*/ char *p_ptr, int p_len);
361 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
362 ** (Note that it is false while we're expanding marco *arguments*.)
365 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
367 static void path_include (cppReader *p_pfile, char *p_path)
368 /*@modifies p_pfile@*/ ;
370 static void initialize_builtins (cppReader *p_pfile)
371 /*@modifies p_pfile@*/ ;
373 static void initialize_char_syntax (struct cppOptions *p_opts) ;
375 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
377 bool p_system_header_p,
378 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
380 static void validate_else (cppReader *p_pfile, cstring p_directive);
382 static void conditional_skip (cppReader *p_pfile, int p_skip,
383 enum node_type p_type,
384 /*@dependent@*/ /*@null@*/ char *p_control_macro);
386 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
390 static void skip_if_group (cppReader *p_pfile, int p_any);
392 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
393 char *p_beg2, int p_len2, bool p_last);
396 extern void fancy_abort ();
399 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
400 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
402 static /*@observer@*/ /*@null@*/ struct file_name_map *
403 read_name_map (cppReader *p_pfile, cstring p_dirname);
405 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
407 static int open_include_file (cppReader *p_pfile,
408 /*@owned@*/ cstring p_fname,
409 /*@null@*/ struct file_name_list *p_searchptr);
411 static void push_macro_expansion (cppReader *,
412 /*@owned@*/ char *, size_t,
413 /*@dependent@*/ hashNode);
415 /* Last arg to output_line_command. */
416 enum file_change_code {
417 same_file, enter_file, leave_file
420 /* `struct directive' defines one #-directive, including how to handle it. */
423 int length; /* Length of name */
424 /*@null@*/ int (*func)(); /* Function to handle directive */
425 /*@observer@*/ cstring name; /* Name of directive */
426 enum node_type type; /* Code which describes which directive. */
427 bool command_reads_line; /* One if rest of line is read by func. */
428 bool traditional_comments; /* Nonzero: keep comments if -traditional. */
429 bool pass_thru; /* Copy preprocessed directive to output file.*/
432 /* These functions are declared to return int instead of void since they
433 are going to be placed in a table and some old compilers have trouble with
434 pointers to functions returning void. */
436 static int do_define (cppReader *, /*@null@*/ struct directive *,
437 /*@exposed@*/ char *, char *);
438 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
439 /*@exposed@*/ char *, char *, bool);
441 static int do_line (cppReader *, /*@null@*/ struct directive *);
442 static int do_include (cppReader *, struct directive *, char *, char *);
443 static int do_undef (cppReader *, struct directive *, char *, char *);
444 static int do_error (cppReader *, struct directive *, char *, char *);
445 static int do_pragma (cppReader *, struct directive *, char *, char *);
446 static int do_ident (cppReader *, struct directive *, char *, char *);
447 static int do_if (cppReader *, struct directive *, char *, char *);
448 static int do_xifdef (cppReader *, struct directive *, char *, char *);
449 static int do_else (cppReader *, struct directive *, char *, char *);
450 static int do_elif (cppReader *, struct directive *, char *, char *);
451 static int do_endif (cppReader *, struct directive *, char *, char *);
452 static int do_warning (cppReader *, struct directive *, char *, char *);
454 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
455 via the same directory as the file that #included it. */
457 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
458 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
460 /* #include "file" looks in source file dir, then stack. */
461 /* #include <file> just looks in the stack. */
462 /* -I directories are added to the end, then the defaults are added. */
466 static struct default_include {
467 /*@dependent@*/ /*@observer@*/ cstring fname; /* The name of the directory. */
468 int cplusplus; /* Only look here if we're compiling C++. */
469 int cxx_aware; /* Includes in this directory don't need to
470 be wrapped in extern "C" when compiling
472 } include_defaults_array[]
474 /* This is the dir for fixincludes. Put it just before
475 the files that we fix. */
476 { GCC_INCLUDE_DIR, 0, 0 },
477 { GCC_INCLUDE_DIR2, 0, 0 },
478 { cstring_undefined, 0, 0 }
481 /*@noaccess cstring@*/
483 /* Here is the actual list of #-directives, most-often-used first.
484 The initialize_builtins function assumes #define is the very first. */
488 static struct directive directive_table[] = {
489 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
490 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
491 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
492 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
493 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
494 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
495 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
496 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
497 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
498 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
499 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
500 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
501 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
502 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
503 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
504 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
506 /*@noaccess cstring@*/
508 static cstring searchPath_unparse (struct file_name_list *search_start)
510 cstring res = cstring_newEmpty ();
511 struct file_name_list *searchptr = NULL;
513 for (searchptr = search_start; searchptr != NULL;
514 searchptr = searchptr->next)
516 if (!cstring_isEmpty (searchptr->fname)) {
517 res = cstring_concatFree1 (res, searchptr->fname);
518 if (searchptr->next != NULL) {
519 res = cstring_appendChar (res, ';');
529 initialize_char_syntax (struct cppOptions *opts)
534 * Set up is_idchar and is_idstart tables. These should be
535 * faster than saying (is_alpha (c) || c == '_'), etc.
536 * Set up these things before calling any routines tthat
540 for (i = 'a'; i <= 'z'; i++) {
541 is_idchar[i - 'a' + 'A'] = TRUE;
542 is_idchar[(int) i] = TRUE;
543 is_idstart[i - 'a' + 'A'] = TRUE;
544 is_idstart[(int) i] = TRUE;
547 for (i = '0'; i <= '9'; i++)
549 is_idchar[(int) i] = TRUE;
552 is_idchar['_'] = TRUE;
553 is_idstart['_'] = TRUE;
554 is_idchar['$'] = opts->dollars_in_ident;
555 is_idstart['$'] = opts->dollars_in_ident;
557 /* horizontal space table */
558 is_hor_space[' '] = TRUE;
559 is_hor_space['\t'] = TRUE;
560 is_hor_space['\v'] = TRUE;
561 is_hor_space['\f'] = TRUE;
562 is_hor_space['\r'] = TRUE;
564 is_space[' '] = TRUE;
565 is_space['\t'] = TRUE;
566 is_space['\v'] = TRUE;
567 is_space['\f'] = TRUE;
568 is_space['\n'] = TRUE;
569 is_space['\r'] = TRUE;
572 bool isIdentifierChar (char c)
574 return is_idchar[(int) c];
577 /* Place into P_PFILE a quoted string representing the string SRC.
578 Caller must reserve enough space in pfile->token_buffer. */
581 quote_string (cppReader *pfile, char *src)
585 cppReader_putCharQ (pfile, '\"');
588 switch ((c = *src++))
592 cppReader_putCharQ (pfile, c);
595 sprintf (cpplib_getPWritten (pfile), "\\%03o",
597 cppReader_adjustWritten (pfile, (size_t) 4);
599 /*@switchbreak@*/ break;
603 cppReader_putCharQ (pfile, '\\');
604 cppReader_putCharQ (pfile, c);
605 /*@switchbreak@*/ break;
608 cppReader_putCharQ (pfile, '\"');
609 cppReader_nullTerminateQ (pfile);
615 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
618 cppReader_growBuffer (cppReader *pfile, size_t n)
620 size_t old_written = cpplib_getWritten (pfile);
621 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
622 pfile->token_buffer = (char *)
623 drealloc (pfile->token_buffer, pfile->token_buffer_size);
624 cppReader_setWritten (pfile, old_written);
628 * process a given definition string, for initialization
629 * If STR is just an identifier, define it with value 1.
630 * If STR has anything after the identifier, then it should
631 * be identifier=definition.
635 cppReader_define (cppReader *pfile, char *str)
640 DPRINTF (("Cpp reader define: %s", str));
642 if (!is_idstart[(int) *p])
644 DPRINTF (("ERROR 1"));
645 cppReader_error (pfile,
646 message ("Malformed option `-D%s'",
647 cstring_fromChars (str)));
654 DPRINTF (("Here 2"));
656 while (is_idchar[(int) *p])
663 while (*p != ')' && *p != '\0') {
672 message ("Malformed option: -D%s (no closing parenthesis)",
673 cstring_fromChars (str)));
677 DPRINTF (("Here 2"));
681 buf = (char *) dmalloc (size_fromInt (p - str + 4));
682 strcpy ((char *) buf, str);
683 strcat ((char *) buf, " 1");
687 DPRINTF (("ERROR 2"));
688 cppReader_error (pfile,
689 message ("Malformed option: -D%s (expected '=', found '%c')",
690 cstring_fromChars (str),
697 /* Copy the entire option so we can modify it. */
698 DPRINTF (("Copying..."));
699 buf = (char *) dmalloc (2 * strlen (str) + 1);
700 strncpy (buf, str, size_fromInt (p - str));
702 /* Change the = to a space. */
704 /* Scan for any backslash-newline and remove it. */
710 if (*p == '\\' && p[1] == '\n')
716 DPRINTF (("Here we are..."));
720 llassert (buf != NULL);
721 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
722 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
726 /* Append a chain of `struct file_name_list's
727 to the end of the main include chain.
728 FIRST is gthe beginning of the chain to append, and LAST is the end. */
731 cppReader_appendIncludeChain (cppReader *pfile,
732 struct file_name_list *first,
733 struct file_name_list *last)
735 struct cppOptions *opts = CPPOPTIONS (pfile);
736 struct file_name_list *dir;
738 if (first == NULL || last == NULL)
743 if (opts->include == 0)
745 opts->include = first;
749 llassert (opts->last_include->next == NULL);
750 opts->last_include->next = first;
753 if (opts->first_bracket_include == 0)
755 opts->first_bracket_include = first;
757 for (dir = first; ; dir = dir->next) {
758 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
759 if (len > pfile->max_include_len)
760 pfile->max_include_len = len;
766 llassert (last->next == NULL);
767 /* last->next = NULL; */
768 opts->last_include = last;
772 static /*@unused@*/ void
773 cppReader_showIncludeChain (cppReader *pfile)
775 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
781 fprintf (stderr, "*%s*:", cstring_toCharsSafe (dirs->fname));
785 fprintf (stderr, "\n");
789 fprintf (stderr, "No includes\n");
795 cppReader_getIncludePath ()
797 cppReader *pfile = &g_cppState;
798 struct file_name_list *dirs = CPPOPTIONS (pfile)->include;
799 cstring res = cstring_undefined;
805 res = message ("%q%c%s", res, PATH_SEPARATOR, dirs->fname);
811 res = cstring_makeLiteral ("<no include path>");
818 cppReader_addIncludeChain (cppReader *pfile, struct file_name_list *dir)
820 struct cppOptions *opts = CPPOPTIONS (pfile);
827 if (opts->include == 0)
833 llassert (opts->last_include->next == NULL);
834 opts->last_include->next = dir;
837 if (opts->first_bracket_include == 0)
839 int len = cstring_length (dir->fname) + INCLUDE_LEN_FUDGE;
840 opts->first_bracket_include = dir;
841 if (len > pfile->max_include_len)
843 pfile->max_include_len = len;
848 opts->last_include = dir;
849 /* cppReader_showIncludeChain (pfile); */
852 /* Given a colon-separated list of file names PATH,
853 add all the names to the search path for include files. */
856 path_include (cppReader *pfile, char *path)
863 /* if we have a posix path list, convert to win32 path list */
864 win32temp = (char *) dmalloc /*@i4@*/
865 (cygwin32_posix_to_win32_path_list_buf_size (path));
866 cygwin32_posix_to_win32_path_list (path, win32temp);
876 struct file_name_list *dirtmp;
878 /* Find the end of this name. */
879 while (*q != '\0' && *q != PATH_SEPARATOR)
886 /* An empty name in the path stands for the current directory. */
887 name = (char *) dmalloc ((size_t) 2);
893 /* Otherwise use the directory that is named. */
894 name = (char *) dmalloc (size_fromInt (q - p + 1));
895 memcpy (name, p, size_fromInt (q - p));
899 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
900 dirtmp->next = 0; /* New one goes on the end */
901 dirtmp->control_macro = 0;
902 dirtmp->c_system_include_path = 0;
903 dirtmp->fname = cstring_fromChars (name);
904 dirtmp->got_name_map = 0;
905 cppReader_addIncludeChain (pfile, dirtmp);
907 /* Advance past this name. */
911 /* Skip the colon. */
917 cppOptions_init (cppOptions *opts)
919 memset ((char *) opts, 0, sizeof *opts);
922 opts->in_fname = NULL;
923 opts->out_fname = NULL;
925 /* Initialize is_idchar to allow $. */
926 opts->dollars_in_ident = TRUE;
928 opts->no_line_commands = 0;
929 opts->no_trigraphs = TRUE;
930 opts->put_out_comments = 1;
931 opts->print_include_names = 0;
932 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
936 opts->cplusplus_comments = 1;
941 opts->pedantic_errors = 0;
942 opts->warn_comments = 0;
943 opts->warnings_are_errors = 0;
945 initialize_char_syntax (opts);
949 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
955 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
956 /*@unused@*/ cppReader *pfile)
962 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
964 hashNode macro = pbuf->hnode;
966 if (macro->type == T_DISABLED)
968 macro->type = T_MACRO;
971 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
979 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
981 if (pbuf->buf != NULL)
988 /* Assuming we have read '/'.
989 If this is the start of a comment (followed by '*' or '/'),
990 skip to the end of the comment, and return ' '.
991 Return EOF if we reached the end of file before the end of the comment.
992 If not the start of a comment, return '/'. */
995 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
999 llassert (pfile->buffer != NULL);
1000 llassert (pfile->buffer->cur != NULL);
1002 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
1009 cppReader_forward (pfile, 2);
1012 if (cppReader_peekC (pfile) == '*')
1014 cppReader_forward (pfile, 1);
1019 c = cppReader_getC (pfile);
1026 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
1033 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
1036 if (prev_c == (int) '*' && c == (int) '/')
1041 if (c == (int) '\n' && (linep != NULL))
1047 else if (cppReader_peekC (pfile) == '/'
1048 && CPPOPTIONS (pfile)->cplusplus_comments)
1051 (void) cppoptgenerror
1052 (FLG_SLASHSLASHCOMMENT,
1053 message ("C++ style // comment"
1057 cppReader_forward (pfile, 1);
1061 c = cppReader_getC (pfile);
1065 /* Allow hash comment to be terminated by EOF. */
1069 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
1071 cppReader_forward (pfile, 1);
1072 c = cppReader_getC (pfile);
1080 if (c == (int) '\n')
1082 /* Don't consider final '\n' to be part of comment. */
1083 cppReader_forward (pfile, -1);
1094 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1096 cppSkipHspace (cppReader *pfile)
1104 llassert (pfile->buffer != NULL);
1106 c = cppReader_peekC (pfile);
1110 return 0; /* FIXME */
1113 if (is_hor_space[c])
1115 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1116 cppReader_pedwarn (pfile,
1117 message ("%s in preprocessing directive",
1119 ? cstring_makeLiteralTemp ("formfeed")
1120 : cstring_makeLiteralTemp ("vertical tab")));
1123 cppReader_forward (pfile, 1);
1127 cppReader_forward (pfile, 1);
1128 c = skip_comment (pfile, NULL);
1132 cppReader_forward (pfile, -1);
1135 if (c == EOF || c == '/')
1140 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1142 cppReader_forward (pfile, 2);
1144 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1145 && is_hor_space [cpp_peekN (pfile, 1)])
1147 cppReader_forward (pfile, 2);
1156 /* Read the rest of the current line.
1157 The line is appended to PFILE's output buffer. */
1160 copy_rest_of_line (cppReader *pfile)
1162 struct cppOptions *opts = CPPOPTIONS (pfile);
1169 llassert (pfile->buffer != NULL);
1171 c = cppReader_getC (pfile);
1177 if (cppReader_peekC (pfile) == '\n')
1179 cppReader_forward (pfile, 1);
1183 /*@fallthrough@*/ case '\'': case '\"':
1184 goto scan_directive_token;
1187 nextc = cppReader_peekC (pfile);
1190 ** was (opts->cplusplus_comments && nextc == '*')
1195 || (opts->cplusplus_comments && nextc == '/'))
1197 goto scan_directive_token;
1199 /*@switchbreak@*/ break;
1202 if (cppReader_isPedantic (pfile))
1203 cppReader_pedwarn (pfile,
1204 message ("%s in preprocessing directive",
1206 ? cstring_makeLiteralTemp ("formfeed")
1207 : cstring_makeLiteralTemp ("vertical tab")));
1208 /*@switchbreak@*/ break;
1211 cppReader_forward (pfile, -1);
1213 scan_directive_token:
1214 cppReader_forward (pfile, -1);
1215 (void) cpplib_getToken (pfile);
1218 cppReader_putChar (pfile, c);
1221 cppReader_nullTerminate (pfile);
1225 cppReader_skipRestOfLine (cppReader *pfile)
1227 size_t old = cpplib_getWritten (pfile);
1228 copy_rest_of_line (pfile);
1229 cppReader_setWritten (pfile, old);
1232 /* Handle a possible # directive.
1233 '#' has already been read. */
1236 cppReader_handleDirective (cppReader *pfile)
1239 struct directive *kt = NULL;
1241 size_t after_ident = 0;
1243 char *line_end = NULL;
1244 size_t old_written = cpplib_getWritten (pfile);
1245 int nspaces = cppSkipHspace (pfile);
1247 c = cppReader_peekC (pfile);
1249 if (c >= '0' && c <= '9')
1251 /* Handle # followed by a line number. */
1252 if (cppReader_isPedantic (pfile))
1254 cppReader_pedwarnLit
1256 cstring_makeLiteralTemp ("`#' followed by integer"));
1259 (void) do_line (pfile, NULL);
1260 goto done_a_directive;
1264 /* Now find the directive name. */
1266 cppReader_putChar (pfile, '#');
1268 parse_name (pfile, cppReader_getC (pfile));
1270 llassert (pfile->token_buffer != NULL);
1271 ident = pfile->token_buffer + old_written + 1;
1273 ident_length = cpplib_getPWritten (pfile) - ident;
1275 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1277 /* A line of just `#' becomes blank. */
1281 for (kt = directive_table; ; kt++)
1283 if (kt->length <= 0)
1285 return 0; /* goto not_a_directive; */
1288 if (kt->length == ident_length
1289 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1295 if (kt->command_reads_line)
1301 /* Nonzero means do not delete comments within the directive.
1302 #define needs this when -traditional. */
1303 bool comments = 1; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1304 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1305 CPPOPTIONS (pfile)->put_out_comments = comments;
1306 after_ident = cpplib_getWritten (pfile);
1307 copy_rest_of_line (pfile);
1308 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1312 /* For #pragma and #define, we may want to pass through the directive.
1313 Other directives may create output, but we don't want the directive
1314 itself out, so we pop it now. For example #include may write a #line
1315 command (see comment in do_include), and conditionals may emit
1316 #failed ... #endfailed stuff. But note that popping the buffer
1317 means the parameters to kt->func may point after pfile->limit
1318 so these parameters are invalid as soon as something gets appended
1319 to the token_buffer. */
1321 line_end = cpplib_getPWritten (pfile);
1324 if (!kt->pass_thru && kt->type != T_DEFINE)
1326 cppReader_setWritten (pfile, old_written);
1329 llassert (pfile->token_buffer != NULL);
1331 /* was kt->pass_thru || */
1333 if (kt->type == T_DEFINE
1334 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1336 char *p = pfile->token_buffer + old_written;
1339 ** Still need to record value for preprocessing, so
1340 ** #ifdef's, etc. using the value behave correctly.
1343 (void) do_defineAux (pfile, kt,
1344 pfile->token_buffer + after_ident,
1353 SKIP_WHITE_SPACE (p);
1355 llassert (*p == 'd');
1358 llassert (*p == 'e');
1361 llassert (*p == 'f');
1364 llassert (*p == 'i');
1367 llassert (*p == 'n');
1370 llassert (*p == 'e');
1373 ** This is way-bogus. We use the last char to record the number of
1374 ** spaces. Its too hard to get them back into the input stream.
1377 if (nspaces > 9) nspaces = 9;
1379 *p++ = '0' + nspaces;
1381 return 0; /* not_a_directive */
1383 else if (kt->pass_thru)
1385 /* Just leave the entire #define in the output stack. */
1386 return 0; /* not_a_directive */
1389 else if (kt->type == T_DEFINE
1390 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1392 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1393 SKIP_WHITE_SPACE (p);
1395 while (is_idchar[(int) *p])
1401 cppReader_putChar (pfile, '\n');
1403 else if (kt->type == T_DEFINE)
1405 cppReader_setWritten (pfile, old_written);
1416 llassert (kt->func != NULL);
1417 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1422 /* Pass a directive through to the output file.
1423 BUF points to the contents of the directive, as a contiguous string.
1424 LIMIT points to the first character past the end of the directive.
1425 KEYWORD is the keyword-table entry for the directive. */
1428 pass_thru_directive (char *buf, char *limit,
1430 struct directive *keyword)
1432 int keyword_length = keyword->length;
1434 cpplib_reserve (pfile,
1435 size_fromInt (2 + keyword_length + (limit - buf)));
1436 cppReader_putCharQ (pfile, '#');
1437 /*@-observertrans@*/
1438 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1439 size_fromInt (keyword_length));
1440 /*:=observertrans@*/
1442 if (limit != buf && buf[0] != ' ')
1444 /* Was a bug, since reserve only used 1 + ... */
1445 cppReader_putCharQ (pfile, ' ');
1448 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1451 /* Read a replacement list for a macro with parameters.
1452 Build the DEFINITION structure.
1453 Reads characters of text starting at BUF until END.
1454 ARGLIST specifies the formal parameters to look for
1455 in the text of the definition; NARGS is the number of args
1456 in that list, or -1 for a macro name that wants no argument list.
1457 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1458 and NAMELEN is its length in characters.
1460 Note that comments, backslash-newlines, and leading white space
1461 have already been deleted from the argument. */
1464 collect_expansion (cppReader *pfile, char *buf, char *limit,
1465 int nargs, /*@null@*/ struct arglist *arglist)
1468 char *p, *lastp, *exp_p;
1469 struct reflist *endpat = NULL;
1470 /* Pointer to first nonspace after last ## seen. */
1472 /* Pointer to first nonspace after last single-# seen. */
1473 char *stringify = 0;
1475 char expected_delimiter = '\0';
1478 /* Scan thru the replacement list, ignoring comments and quoted
1479 strings, picking up on the macro calls. It does a linear search
1480 thru the arg list on every potential symbol. Profiling might say
1481 that something smarter should happen. */
1486 /* Find the beginning of the trailing whitespace. */
1489 while (p < limit && is_space[(int) limit[-1]])
1494 /* Allocate space for the text in the macro definition.
1495 Leading and trailing whitespace chars need 2 bytes each.
1496 Each other input char may or may not need 1 byte,
1497 so this is an upper bound. The extra 5 are for invented
1498 leading and trailing newline-marker and final null. */
1499 maxsize = (sizeof (*defn) + (limit - p) + 5);
1501 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1510 defn = (DEFINITION *) dmalloc (maxsize);
1511 defn->noExpand = FALSE;
1513 defn->pattern = NULL;
1514 defn->nargs = nargs;
1515 defn->predefined = NULL;
1517 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1520 defn->rest_args = NULL;
1521 defn->args.argnames = NULL;
1527 /* Add one initial space escape-marker to prevent accidental
1528 token-pasting (often removed by cpplib_macroExpand). */
1532 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1533 cppReader_errorLit (pfile,
1534 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1538 /* Process the main body of the definition. */
1540 int skipped_arg = 0;
1541 register char c = *p++;
1545 if (!cppReader_isTraditional (pfile)) {
1549 if (expected_delimiter != '\0')
1551 if (c == expected_delimiter)
1552 expected_delimiter = '\0';
1556 expected_delimiter = c;
1558 /*@switchbreak@*/ break;
1561 if (p < limit && (expected_delimiter != '\0'))
1563 /* In a string, backslash goes through
1564 and makes next char ordinary. */
1567 /*@switchbreak@*/ break;
1570 /* An '@' in a string or character constant stands for itself,
1571 and does not need to be escaped. */
1572 if (expected_delimiter == '\0')
1577 /*@switchbreak@*/ break;
1580 /* # is ordinary inside a string. */
1581 if (expected_delimiter != '\0')
1583 /*@switchbreak@*/ break;
1586 if (p < limit && *p == '#') {
1587 /* ##: concatenate preceding and following tokens. */
1588 /* Take out the first #, discard preceding whitespace. */
1592 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1598 /* Skip the second #. */
1600 /* Discard following whitespace. */
1601 SKIP_WHITE_SPACE (p);
1605 cppReader_errorLit (pfile,
1606 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1608 } else if (nargs >= 0) {
1609 /* Single #: stringify following argument ref.
1610 Don't leave the # in the expansion. */
1612 SKIP_WHITE_SPACE (p);
1613 if (p == limit || ! is_idstart[(int) *p]
1614 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1615 cppReader_errorLit (pfile,
1616 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1623 /*@switchbreak@*/ break;
1626 /* In -traditional mode, recognize arguments inside strings and
1627 and character constants, and ignore special properties of #.
1628 Arguments inside strings are considered "stringified", but no
1629 extra quote marks are supplied. */
1633 if (expected_delimiter != '\0') {
1634 if (c == expected_delimiter)
1635 expected_delimiter = '\0';
1637 expected_delimiter = c;
1638 /*@switchbreak@*/ break;
1641 /* Backslash quotes delimiters and itself, but not macro args. */
1642 if (expected_delimiter != '\0' && p < limit
1643 && (*p == expected_delimiter || *p == '\\')) {
1647 /*@switchbreak@*/ break;
1650 if (expected_delimiter != '\0') /* No comments inside strings. */
1651 /*@switchbreak@*/ break;
1653 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1654 this must be -traditional. So replace the comment with
1658 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1663 /*@switchbreak@*/ break;
1667 /* Handle the start of a symbol. */
1668 if (is_idchar[(int) c] && nargs > 0) {
1669 char *id_beg = p - 1;
1673 while (p != limit && is_idchar[(int) *p])
1678 id_len = p - id_beg;
1680 if (is_idstart[(int) c]
1681 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1682 register struct arglist *arg;
1684 for (arg = arglist; arg != NULL; arg = arg->next) {
1685 struct reflist *tpat;
1687 if (arg->name[0] == c
1688 && arg->length == id_len
1689 && strncmp (arg->name, id_beg, size_fromInt (id_len)) == 0) {
1692 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1693 if (cppReader_isTraditional (pfile)) {
1694 cppReader_warning (pfile,
1695 message ("macro argument `%x' is stringified.",
1696 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1698 cppReader_warning (pfile,
1699 message ("macro arg `%x' would be stringified with -traditional.",
1700 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1703 /* If ANSI, don't actually substitute inside a string. */
1704 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1705 /*@innerbreak@*/ break;
1706 /* make a pat node for this arg and append it to the end of
1708 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1710 tpat->raw_before = (concat == id_beg);
1711 tpat->raw_after = 0;
1712 tpat->rest_args = arg->rest_args;
1713 tpat->stringify = (cppReader_isTraditional (pfile)
1714 ? expected_delimiter != '\0'
1715 : stringify == id_beg);
1719 defn->pattern = tpat;
1723 endpat->next = tpat;
1725 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1729 tpat->argno = arg->argno;
1730 tpat->nchars = exp_p - lastp;
1734 SKIP_WHITE_SPACE (p1);
1736 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1738 tpat->raw_after = 1;
1741 lastp = exp_p; /* place to start copying from next time */
1744 /*@innerbreak@*/ break;
1749 /* If this was not a macro arg, copy it into the expansion. */
1750 if (skipped_arg == 0) {
1751 register char *lim1 = p;
1759 if (stringify == id_beg)
1760 cppReader_errorLit (pfile,
1761 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1766 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1768 /* If ANSI, put in a "@ " marker to prevent token pasting.
1769 But not if "inside a string" (which in ANSI mode
1770 happens only for -D option). */
1777 defn->length = size_fromInt (exp_p - defn->expansion);
1779 /* Crash now if we overrun the allocated size. */
1780 if (defn->length + 1 > maxsize)
1782 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1790 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1794 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1795 int nargs, /*@null@*/ struct arglist *arglist)
1798 char *p, *lastp, *exp_p;
1799 struct reflist *endpat = NULL;
1800 /* Pointer to first nonspace after last ## seen. */
1802 /* Pointer to first nonspace after last single-# seen. */
1803 char *stringify = 0;
1805 char expected_delimiter = '\0';
1808 /* Scan thru the replacement list, ignoring comments and quoted
1809 strings, picking up on the macro calls. It does a linear search
1810 thru the arg list on every potential symbol. Profiling might say
1811 that something smarter should happen. */
1815 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1816 fileloc_unparse (loc)));
1819 /* Find the beginning of the trailing whitespace. */
1822 while (p < limit && is_space[(int) limit[-1]])
1827 /* Allocate space for the text in the macro definition.
1828 Leading and trailing whitespace chars need 2 bytes each.
1829 Each other input char may or may not need 1 byte,
1830 so this is an upper bound. The extra 5 are for invented
1831 leading and trailing newline-marker and final null. */
1832 maxsize = (sizeof (*defn) + (limit - p) + 5);
1834 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1843 defn = (DEFINITION *) dmalloc (maxsize);
1844 defn->noExpand = FALSE;
1846 defn->pattern = NULL;
1847 defn->nargs = nargs;
1848 defn->predefined = NULL;
1850 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1853 defn->rest_args = NULL;
1854 defn->args.argnames = NULL;
1860 /* Add one initial space escape-marker to prevent accidental
1861 token-pasting (often removed by cpplib_macroExpand). */
1865 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1866 voptgenerror (FLG_PREPROC,
1867 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1872 /* Process the main body of the definition. */
1874 int skipped_arg = 0;
1875 register char c = *p++;
1879 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1883 if (expected_delimiter != '\0')
1885 if (c == expected_delimiter)
1886 expected_delimiter = '\0';
1890 expected_delimiter = c;
1892 /*@switchbreak@*/ break;
1895 if (p < limit && (expected_delimiter != '\0'))
1897 /* In a string, backslash goes through
1898 and makes next char ordinary. */
1901 /*@switchbreak@*/ break;
1904 /* An '@' in a string or character constant stands for itself,
1905 and does not need to be escaped. */
1906 if (expected_delimiter == '\0')
1911 /*@switchbreak@*/ break;
1914 /* # is ordinary inside a string. */
1915 if (expected_delimiter != '\0')
1917 /*@switchbreak@*/ break;
1920 if (p < limit && *p == '#') {
1921 /* ##: concatenate preceding and following tokens. */
1922 /* Take out the first #, discard preceding whitespace. */
1926 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1932 /* Skip the second #. */
1934 /* Discard following whitespace. */
1935 SKIP_WHITE_SPACE (p);
1939 voptgenerror (FLG_PREPROC,
1940 cstring_makeLiteral ("`##' at end of macro definition"),
1943 } else if (nargs >= 0) {
1944 /* Single #: stringify following argument ref.
1945 Don't leave the # in the expansion. */
1947 SKIP_WHITE_SPACE (p);
1948 if (p == limit || ! is_idstart[(int) *p]
1949 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1953 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1962 /*@switchbreak@*/ break;
1965 /* In -traditional mode, recognize arguments inside strings and
1966 and character constants, and ignore special properties of #.
1967 Arguments inside strings are considered "stringified", but no
1968 extra quote marks are supplied. */
1972 if (expected_delimiter != '\0') {
1973 if (c == expected_delimiter)
1974 expected_delimiter = '\0';
1976 expected_delimiter = c;
1977 /*@switchbreak@*/ break;
1980 /* Backslash quotes delimiters and itself, but not macro args. */
1981 if (expected_delimiter != '\0' && p < limit
1982 && (*p == expected_delimiter || *p == '\\')) {
1986 /*@switchbreak@*/ break;
1989 if (expected_delimiter != '\0') /* No comments inside strings. */
1990 /*@switchbreak@*/ break;
1992 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1993 this must be -traditional. So replace the comment with
1997 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2002 /*@switchbreak@*/ break;
2006 /* Handle the start of a symbol. */
2007 if (is_idchar[(int) c] && nargs > 0) {
2008 char *id_beg = p - 1;
2012 while (p != limit && is_idchar[(int) *p])
2017 id_len = p - id_beg;
2019 if (is_idstart[(int) c]
2020 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
2021 register struct arglist *arg;
2023 for (arg = arglist; arg != NULL; arg = arg->next) {
2024 struct reflist *tpat;
2026 if (arg->name[0] == c
2027 && arg->length == id_len
2028 && strncmp (arg->name, id_beg, size_fromInt (id_len)) == 0) {
2031 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
2032 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
2033 voptgenerror (FLG_PREPROC,
2034 message ("macro argument `%x' is stringified.",
2035 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2039 voptgenerror (FLG_PREPROC,
2040 message ("Macro arg `%x' would be stringified with -traditional.",
2041 cstring_prefix (cstring_fromChars (arg->name), id_len)),
2046 /* If ANSI, don't actually substitute inside a string. */
2047 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
2048 /*@innerbreak@*/ break;
2049 /* make a pat node for this arg and append it to the end of
2051 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
2053 tpat->raw_before = (concat == id_beg);
2054 tpat->raw_after = 0;
2055 tpat->rest_args = arg->rest_args;
2056 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
2057 ? expected_delimiter != '\0'
2058 : stringify == id_beg);
2062 defn->pattern = tpat;
2066 endpat->next = tpat;
2068 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2072 tpat->argno = arg->argno;
2073 tpat->nchars = exp_p - lastp;
2077 SKIP_WHITE_SPACE (p1);
2079 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2081 tpat->raw_after = 1;
2084 lastp = exp_p; /* place to start copying from next time */
2087 /*@innerbreak@*/ break;
2092 /* If this was not a macro arg, copy it into the expansion. */
2093 if (skipped_arg == 0) {
2094 register char *lim1 = p;
2102 if (stringify == id_beg)
2106 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2113 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2115 /* If ANSI, put in a "@ " marker to prevent token pasting.
2116 But not if "inside a string" (which in ANSI mode
2117 happens only for -D option). */
2124 defn->length = size_fromInt (exp_p - defn->expansion);
2126 /* Crash now if we overrun the allocated size. */
2127 if (defn->length + 1 > maxsize)
2129 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2136 * special extension string that can be added to the last macro argument to
2137 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2138 * #define wow(a, b...) process (b, a, b)
2139 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2140 * { wow (one, two); } -> { process (two, one, two); }
2141 * if this "rest_arg" is used with the concat token '##' and if it is not
2142 * supplied then the token attached to with ## will not be outputted. Ex:
2143 * #define wow (a, b...) process (b ## , a, ## b)
2144 * { wow (1, 2); } -> { process (2, 1, 2); }
2145 * { wow (one); } -> { process (one); {
2148 /*@-readonlytrans@*/
2149 static char rest_extension[] = "...";
2150 /*:=readonlytrans@*/
2153 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2155 /* Create a DEFINITION node from a #define directive. Arguments are
2156 as for do_define. */
2159 static /*@null@*/ macroDef
2160 create_definition (/*@exposed@*/ char *buf, char *limit,
2161 cppReader *pfile, bool predefinition,
2164 char *bp; /* temp ptr into input buffer */
2165 char *symname; /* remember where symbol name starts */
2166 int sym_length; /* and how long it is */
2167 int rest_args = 0; /* really int! */
2170 cstring file = (CPPBUFFER (pfile) != NULL)
2171 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2173 int arglengths = 0; /* Accumulate lengths of arg names
2174 plus number of args. */
2178 DPRINTF (("Create definition: %s", buf));
2181 cppBuffer_lineAndColumn (CPPBUFFER (pfile), &line, &col);
2185 while (is_hor_space[(int) *bp])
2190 symname = bp; /* remember where it starts */
2192 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2196 /* Lossage will occur if identifiers or control keywords are broken
2197 across lines using backslash. This is not the right place to take
2201 struct arglist *arg_ptrs = NULL;
2204 bp++; /* skip '(' */
2205 SKIP_WHITE_SPACE (bp);
2207 /* Loop over macro argument names. */
2210 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2212 temp->next = arg_ptrs;
2213 temp->argno = argno++;
2214 temp->rest_args = 0;
2220 cppReader_pedwarn (pfile,
2221 message ("another parameter follows `%s'",
2222 cstring_fromChars (rest_extension)));
2225 if (!is_idstart[(int) *bp])
2227 cppReader_pedwarnLit (pfile,
2228 cstring_makeLiteralTemp ("invalid character in macro parameter name"));
2231 /* Find the end of the arg name. */
2232 while (is_idchar[(int) *bp])
2235 /* do we have a "special" rest-args extension here? */
2236 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2237 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2240 temp->rest_args = 1;
2241 /*@innerbreak@*/ break;
2245 temp->length = bp - temp->name;
2249 bp += REST_EXTENSION_LENGTH;
2252 arglengths += temp->length + 2;
2253 SKIP_WHITE_SPACE (bp);
2255 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2256 cppReader_errorLit (pfile,
2257 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2263 SKIP_WHITE_SPACE (bp);
2266 cppReader_errorLit (pfile,
2267 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2271 struct arglist *otemp;
2273 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2275 if (temp->length == otemp->length &&
2276 strncmp (temp->name, otemp->name, size_fromInt (temp->length)) == 0) {
2277 cstring name = cstring_copyLength (temp->name, temp->length);
2278 cppReader_error (pfile,
2279 message ("duplicate argument name `%x' in `#define'", name));
2286 ++bp; /* skip paren */
2287 SKIP_WHITE_SPACE (bp);
2288 /* now everything from bp before limit is the definition. */
2289 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2290 defn->rest_args = rest_args;
2292 /* Now set defn->args.argnames to the result of concatenating
2293 the argument names in reverse order
2294 with comma-space between them. */
2295 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2298 struct arglist *temp;
2300 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2301 memcpy (&defn->args.argnames[i], temp->name, size_fromInt (temp->length));
2303 if (temp->next != 0) {
2304 defn->args.argnames[i++] = ',';
2305 defn->args.argnames[i++] = ' ';
2309 defn->args.argnames[i] = '\0';
2314 /* Simple expansion or empty definition. */
2318 if (is_hor_space[(int) *bp]) {
2320 SKIP_WHITE_SPACE (bp);
2323 case '!': case '\"': case '#': case '%': case '&': case '\'':
2324 case ')': case '*': case '+': case ',': case '-': case '.':
2325 case '/': case ':': case ';': case '<': case '=': case '>':
2326 case '?': case '[': case '\\': case ']': case '^': case '{':
2327 case '|': case '}': case '~':
2328 cppReader_warning (pfile,
2329 message ("Missing white space after #define %x",
2330 cstring_prefix (cstring_fromChars (symname),
2335 cppReader_pedwarn (pfile,
2336 message ("Missing white space after #define %x",
2337 cstring_prefix (cstring_fromChars (symname),
2343 /* now everything from bp before limit is the definition. */
2344 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2345 defn->args.argnames = mstring_createEmpty ();
2348 defn->noExpand = noExpand;
2349 DPRINTF (("No expand: %d", noExpand));
2353 /* not: llassert (cstring_isUndefined (defn->file)); */
2356 /* OP is null if this is a predefinition */
2357 defn->predefined = predefinition;
2359 mdef.symnam = symname;
2360 mdef.symlen = sym_length;
2371 cpplib_createDefinition (cstring def,
2376 char *buf = cstring_toCharsSafe (def);
2377 char *limit = buf + cstring_length (def);
2378 char *bp; /* temp ptr into input buffer */
2379 char *symname; /* remember where symbol name starts */
2380 int sym_length; /* and how long it is */
2381 int rest_args = 0; /* really int! */
2382 int line = fileloc_lineno (loc);
2383 cstring file = fileloc_filename (loc);
2385 int arglengths = 0; /* Accumulate lengths of arg names
2386 plus number of args. */
2391 DPRINTF (("Creating definition: %s", buf));
2393 while (is_hor_space[(int) *bp])
2398 symname = bp; /* remember where it starts */
2400 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2402 DPRINTF (("length: %d", sym_length));
2406 DPRINTF (("Here: %s", bp));
2408 /* Lossage will occur if identifiers or control keywords are broken
2409 across lines using backslash. This is not the right place to take
2413 struct arglist *arg_ptrs = NULL;
2416 bp++; /* skip '(' */
2417 SKIP_WHITE_SPACE (bp);
2419 /* Loop over macro argument names. */
2422 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2424 temp->next = arg_ptrs;
2425 temp->argno = argno++;
2426 temp->rest_args = 0;
2432 voptgenerror (FLG_PREPROC,
2433 message ("Another parameter follows %s",
2434 cstring_fromChars (rest_extension)),
2438 if (!is_idstart[(int) *bp])
2440 voptgenerror (FLG_PREPROC,
2441 message ("Invalid character in macro parameter name: %c", *bp),
2445 /* Find the end of the arg name. */
2446 while (is_idchar[(int) *bp])
2449 /* do we have a "special" rest-args extension here? */
2450 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2451 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2454 temp->rest_args = 1;
2455 /*@innerbreak@*/ break;
2459 temp->length = bp - temp->name;
2463 bp += REST_EXTENSION_LENGTH;
2466 arglengths += temp->length + 2;
2467 SKIP_WHITE_SPACE (bp);
2469 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2470 voptgenerror (FLG_PREPROC,
2471 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2478 SKIP_WHITE_SPACE (bp);
2481 voptgenerror (FLG_PREPROC,
2482 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2487 struct arglist *otemp;
2489 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2491 if (temp->length == otemp->length &&
2492 strncmp (temp->name, otemp->name, size_fromInt (temp->length)) == 0) {
2493 cstring name = cstring_copyLength (temp->name, temp->length);
2495 voptgenerror (FLG_PREPROC,
2496 message ("Duplicate argument name in #define: %s", name),
2504 ++bp; /* skip paren */
2505 SKIP_WHITE_SPACE (bp);
2506 /* now everything from bp before limit is the definition. */
2507 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2508 defn->rest_args = rest_args;
2510 /* Now set defn->args.argnames to the result of concatenating
2511 the argument names in reverse order
2512 with comma-space between them. */
2513 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2516 struct arglist *temp;
2518 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2519 memcpy (&defn->args.argnames[i], temp->name, size_fromInt (temp->length));
2521 if (temp->next != 0) {
2522 defn->args.argnames[i++] = ',';
2523 defn->args.argnames[i++] = ' ';
2527 defn->args.argnames[i] = '\0';
2532 /* Simple expansion or empty definition. */
2536 if (is_hor_space[(int) *bp]) {
2538 SKIP_WHITE_SPACE (bp);
2541 case '!': case '\"': case '#': case '%': case '&': case '\'':
2542 case ')': case '*': case '+': case ',': case '-': case '.':
2543 case '/': case ':': case ';': case '<': case '=': case '>':
2544 case '?': case '[': case '\\': case ']': case '^': case '{':
2545 case '|': case '}': case '~':
2546 voptgenerror (FLG_PREPROC,
2547 message ("Missing white space after #define %x",
2548 cstring_prefix (cstring_fromChars (symname),
2554 voptgenerror (FLG_PREPROC,
2555 message ("Missing white space after #define %x",
2556 cstring_prefix (cstring_fromChars (symname),
2564 /* now everything from bp before limit is the definition. */
2565 llassert (limit > bp);
2566 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2567 defn->args.argnames = mstring_createEmpty ();
2570 defn->noExpand = noExpand;
2571 DPRINTF (("No expand: %d", noExpand));
2575 /* not: llassert (cstring_isUndefined (defn->file)); */
2578 /* OP is null if this is a predefinition */
2579 defn->predefined = predefinition;
2582 mdef.symnam = symname;
2583 mdef.symlen = sym_length;
2593 /* Check a purported macro name SYMNAME, and yield its length.
2594 USAGE is the kind of name this is intended for. */
2596 int cppReader_checkMacroName (cppReader *pfile,
2603 for (p = symname; is_idchar[(int) *p]; p++)
2608 sym_length = size_fromInt (p - symname);
2611 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2613 cppReader_error (pfile, message ("invalid %s name", usage));
2615 else if (!is_idstart[(int) *symname])
2617 char *msg = (char *) dmalloc (sym_length + 1);
2618 memcpy (msg, symname, sym_length);
2619 msg[sym_length] = '\0';
2620 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2621 cstring_fromChars (msg)));
2626 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2628 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2632 return size_toInt (sym_length);
2637 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2640 int cppReader_checkMacroNameLoc (fileloc loc,
2647 for (p = symname; is_idchar[(int) *p]; p++)
2652 sym_length = size_fromInt (p - symname);
2655 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2657 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2658 cstring_fromChars (symname)), loc);
2660 else if (!is_idstart[(int) *symname])
2662 char *msg = (char *) dmalloc (sym_length + 1);
2663 memcpy (msg, symname, sym_length);
2664 msg[sym_length] = '\0';
2665 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2666 cstring_fromChars (msg)),
2672 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2674 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2678 return size_toInt (sym_length);
2681 /* Return zero if two DEFINITIONs are isomorphic. */
2684 compare_defs (DEFINITION *d1, DEFINITION *d2)
2686 register struct reflist *a1, *a2;
2687 register char *p1 = d1->expansion;
2688 register char *p2 = d2->expansion;
2691 if (d1->nargs != d2->nargs)
2696 llassert (d1->args.argnames != NULL);
2697 llassert (d2->args.argnames != NULL);
2699 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2704 for (a1 = d1->pattern, a2 = d2->pattern;
2705 (a1 != NULL) && (a2 != NULL);
2706 a1 = a1->next, a2 = a2->next) {
2707 if (!((a1->nchars == a2->nchars
2708 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2709 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2710 || a1->argno != a2->argno
2711 || a1->stringify != a2->stringify
2712 || a1->raw_before != a2->raw_before
2713 || a1->raw_after != a2->raw_after)
2722 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2723 p2, d2->length - (p2 - d2->expansion), 1))
2729 /* Return TRUE if two parts of two macro definitions are effectively different.
2730 One of the parts starts at BEG1 and has LEN1 chars;
2731 the other has LEN2 chars at BEG2.
2732 Any sequence of whitespace matches any other sequence of whitespace.
2733 FIRST means these parts are the first of a macro definition;
2734 so ignore leading whitespace entirely.
2735 LAST means these parts are the last of a macro definition;
2736 so ignore trailing whitespace entirely. */
2739 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2741 char *end1 = beg1 + len1;
2742 char *end2 = beg2 + len2;
2745 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2746 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2749 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2750 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2752 while (beg1 != end1 && beg2 != end2) {
2753 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2754 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2755 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2756 } else if (*beg1 == *beg2) {
2760 return (beg1 != end1) || (beg2 != end2);
2763 /* Process a #define command.
2764 BUF points to the contents of the #define command, as a contiguous string.
2765 LIMIT points to the first character past the end of the definition.
2766 KEYWORD is the keyword-table entry for #define,
2767 or NULL for a "predefined" macro. */
2770 do_defineAux (cppReader *pfile, struct directive *keyword,
2771 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2777 DPRINTF (("Define aux: %d", noExpand));
2779 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2784 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2786 DPRINTF (("Macro: %s / %s",
2787 cstring_copyLength (mdef.symnam, mdef.symlen),
2788 bool_unparse (noExpand)));
2790 if ((hp = cpphash_lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL)
2794 /* Redefining a precompiled key is ok. */
2795 if (hp->type == T_PCSTRING)
2797 /* Redefining a macro is ok if the definitions are the same. */
2798 else if (hp->type == T_MACRO)
2799 ok = !compare_defs (mdef.defn, hp->value.defn);
2800 /* Redefining a constant is ok with -D. */
2801 else if (hp->type == T_CONST)
2802 ok = !CPPOPTIONS (pfile)->done_initializing;
2807 /* Print the warning if it's not ok. */
2811 ** If we are passing through #define and #undef directives, do
2812 ** that for this re-definition now.
2815 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2817 /* llassert (keyword != NULL); */
2818 pass_thru_directive (buf, limit, pfile, keyword);
2821 cpp_setLocation (pfile);
2823 if (hp->type == T_MACRO)
2825 if (hp->value.defn->noExpand)
2827 ; /* error will be reported checking macros */
2833 message ("Macro %q already defined",
2834 cstring_copyLength (mdef.symnam, mdef.symlen)),
2835 message ("%q: Previous definition of %q",
2836 fileloc_unparseRaw (hp->value.defn->file,
2837 (int) hp->value.defn->line),
2838 cstring_copyLength (mdef.symnam, mdef.symlen)));
2843 genppllerror (FLG_MACROREDEF,
2844 message ("Macro %q already defined",
2845 cstring_copyLength (mdef.symnam,
2851 /* Replace the old definition. */
2853 hp->value.defn = mdef.defn;
2858 ** If we are passing through #define and #undef directives, do
2859 ** that for this new definition now.
2864 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2866 pass_thru_directive (buf, limit, pfile, keyword);
2869 DPRINTF (("Define macro: %s / %d",
2870 mdef.symnam, mdef.defn->noExpand));
2872 hn = cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2874 } /*@=branchstate@*/
2884 do_define (cppReader *pfile, struct directive *keyword,
2885 /*@exposed@*/ char *buf, char *limit)
2887 DPRINTF (("Regular do define"));
2888 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2891 /* This structure represents one parsed argument in a macro call.
2892 `raw' points to the argument text as written (`raw_length' is its length).
2893 `expanded' points to the argument's macro-expansion
2894 (its length is `expand_length').
2895 `stringified_length' is the length the argument would have
2897 `use_count' is the number of times this macro arg is substituted
2898 into the macro. If the actual use count exceeds 10,
2899 the value stored is 10. */
2901 /* raw and expanded are relative to ARG_BASE */
2903 #define ARG_BASE ((pfile)->token_buffer)
2906 /* Strings relative to pfile->token_buffer */
2912 int stringified_length;
2917 /* Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2918 If BUFFER != NULL, then use the LENGTH characters in BUFFER
2919 as the new input buffer.
2920 Return the new buffer, or NULL on failure. */
2922 /*@null@*/ /*@exposed@*/ cppBuffer *
2923 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2925 cppBuffer *buf = cppReader_getBuffer (pfile);
2927 if (buf == pfile->buffer_stack)
2929 cppReader_fatalError
2931 message ("%s: macro or `#include' recursion too deep",
2932 (buf->fname != NULL)
2934 : cstring_makeLiteral ("<no name>")));
2935 sfreeEventually (buffer);
2939 llassert (buf != NULL);
2942 memset ((char *) buf, 0, sizeof (*buf));
2943 CPPBUFFER (pfile) = buf;
2945 buf->if_stack = pfile->if_stack;
2946 buf->cleanup = cppReader_nullCleanup;
2947 buf->underflow = cppReader_nullUnderflow;
2949 buf->cur = buf->buf;
2953 buf->alimit = buf->rlimit = buffer + length;
2957 buf->alimit = buf->rlimit = NULL;
2964 cppReader_popBuffer (cppReader *pfile)
2966 cppBuffer *buf = CPPBUFFER (pfile);
2968 llassert (buf != NULL);
2970 (void) (*buf->cleanup) (buf, pfile);
2971 return ++CPPBUFFER (pfile);
2974 /* Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2975 Pop the buffer when done. */
2978 cppReader_scanBuffer (cppReader *pfile)
2980 cppBuffer *buffer = CPPBUFFER (pfile);
2983 enum cpp_token token;
2985 token = cpplib_getToken (pfile);
2987 if (token == CPP_EOF) /* Should not happen ... */
2992 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2994 (void) cppReader_popBuffer (pfile);
3001 * Rescan a string (which may have escape marks) into pfile's buffer.
3002 * Place the result in pfile->token_buffer.
3004 * The input is copied before it is scanned, so it is safe to pass
3005 * it something from the token_buffer that will get overwritten
3006 * (because it follows cpplib_getWritten). This is used by do_include.
3010 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
3012 register cppBuffer *ip;
3013 char *limit = buf + length;
3014 char *buf1, *p1, *p2;
3016 /* evans - 2001-08-26
3017 ** length is unsigned - this doesn't make sense
3023 /* Set up the input on the input stack. */
3025 buf1 = (char *) dmalloc (length + 1);
3035 buf1[length] = '\0';
3037 ip = cppReader_pushBuffer (pfile, buf1, length);
3042 ip->has_escapes = TRUE;
3044 /* Scan the input, create the output. */
3045 cppReader_scanBuffer (pfile);
3047 cppReader_nullTerminate (pfile);
3051 adjust_position (char *buf, char *limit, int *linep, int *colp)
3057 (*linep)++, (*colp) = 1;
3063 /* Move line_base forward, updating lineno and colno. */
3066 update_position (cppBuffer *pbuf)
3069 char *new_pos = pbuf->cur;
3070 register struct parse_marker *mark;
3072 llassert (pbuf->buf != NULL);
3073 old_pos = pbuf->buf + pbuf->line_base;
3075 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3077 if (pbuf->buf + mark->position < new_pos)
3078 new_pos = pbuf->buf + mark->position;
3080 pbuf->line_base += new_pos - old_pos;
3082 llassert (old_pos != NULL);
3083 llassert (new_pos != NULL);
3085 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3089 cppBuffer_lineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3090 /*@null@*/ /*@out@*/ int *colp)
3098 } /*@=branchstate@*/
3102 *linep = pbuf->lineno;
3103 *colp = pbuf->colno;
3105 llassert (pbuf->buf != NULL);
3106 llassert (pbuf->cur != NULL);
3108 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3117 /* Return the cppBuffer that corresponds to a file (not a macro). */
3119 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3121 cppBuffer *ip = cppReader_getBuffer (pfile);
3124 ip != NULL && ip != cppReader_nullBuffer (pfile);
3125 ip = cppBuffer_prevBuffer (ip))
3127 if (ip->fname != NULL)
3137 count_newlines (char *buf, char *limit)
3139 register long count = 0;
3151 * write out a #line command, for instance, after an #include file.
3152 * If CONDITIONAL is nonzero, we can omit the #line if it would
3153 * appear to be a no-op, and we can output a few newlines instead
3154 * if we want to increase the line number by a small amount.
3155 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3159 output_line_command (cppReader *pfile, bool conditional,
3160 enum file_change_code file_change)
3163 cppBuffer *ip = CPPBUFFER (pfile);
3166 llassert (ip != NULL);
3168 if (ip->fname == NULL)
3171 update_position (ip);
3173 if (CPPOPTIONS (pfile)->no_line_commands
3174 || CPPOPTIONS (pfile)->no_output)
3177 buf = CPPBUFFER (pfile);
3179 llassert (buf != NULL);
3184 llassert (ip->cur != NULL);
3186 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3188 if (CPPOPTIONS (pfile)->no_line_commands)
3192 if (line == pfile->lineno)
3195 /* If the inherited line number is a little too small,
3196 output some newlines instead of a #line command. */
3198 if (line > pfile->lineno && line < pfile->lineno + 8)
3200 cpplib_reserve (pfile, 20);
3201 while (line > pfile->lineno)
3203 cppReader_putCharQ (pfile, '\n');
3211 cpplib_reserve (pfile,
3212 size_fromInt (4 * cstring_length (ip->nominal_fname) + 50));
3215 #ifdef OUTPUT_LINE_COMMANDS
3216 static char sharp_line[] = "#line ";
3218 static char sharp_line[] = "# ";
3220 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3223 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3224 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3226 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3228 if (file_change != same_file) {
3229 cppReader_putCharQ (pfile, ' ');
3230 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3232 /* Tell cc1 if following text comes from a system header file. */
3233 if (ip->system_header_p != '\0') {
3234 cppReader_putCharQ (pfile, ' ');
3235 cppReader_putCharQ (pfile, '3');
3237 #ifndef NO_IMPLICIT_EXTERN_C
3238 /* Tell cc1plus if following text should be treated as C. */
3239 if (ip->system_header_p == (char) 2 && CPPOPTIONS (pfile)->cplusplus) {
3240 cppReader_putCharQ (pfile, ' ');
3241 cppReader_putCharQ (pfile, '4');
3244 cppReader_putCharQ (pfile, '\n');
3245 pfile->lineno = line;
3250 * Parse a macro argument and append the info on PFILE's token_buffer.
3251 * REST_ARGS means to absorb the rest of the args.
3252 * Return nonzero to indicate a syntax error.
3255 static enum cpp_token
3256 macarg (cppReader *pfile, int rest_args)
3259 enum cpp_token token;
3260 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3261 bool oldexpand = pfile->no_macro_expand;
3262 CPPOPTIONS (pfile)->put_out_comments = 1;
3264 /* Try to parse as much of the argument as exists at this
3265 input stack level. */
3267 pfile->no_macro_expand = TRUE;
3271 token = cpplib_getToken (pfile);
3278 /* If we've hit end of file, it's an error (reported by caller).
3279 Ditto if it's the end of cpp_expand_to_buffer text.
3280 If we've hit end of macro, just continue. */
3281 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3283 /*@switchbreak@*/ break;
3286 /*@switchbreak@*/ break;
3290 /*@switchbreak@*/ break;
3292 /* if we've returned to lowest level and
3293 we aren't absorbing all args */
3294 if (paren == 0 && rest_args == 0)
3296 /*@switchbreak@*/ break;
3298 /* Remove ',' or ')' from argument buffer. */
3299 cppReader_adjustWritten (pfile, -1);
3307 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3308 pfile->no_macro_expand = oldexpand;
3314 /* Turn newlines to spaces in the string of length LENGTH at START,
3315 except inside of string constants.
3316 The string is copied into itself with its beginning staying fixed. */
3319 change_newlines (char *start, int length)
3323 register char *limit;
3327 limit = start + length;
3330 while (ibp < limit) {
3331 *obp++ = c = *ibp++;
3336 /* Notice and skip strings, so that we don't delete newlines in them. */
3339 while (ibp < limit) {
3340 *obp++ = c = *ibp++;
3342 /*@innerbreak@*/ break;
3343 if (c == '\n' && quotec == '\'')
3344 /*@innerbreak@*/ break;
3347 /*@switchbreak@*/ break;
3354 static /*@observer@*/ struct tm *
3355 timestamp (/*@returned@*/ cppReader *pfile)
3357 if (pfile->timebuf == NULL)
3359 time_t t = time ((time_t *) 0);
3360 pfile->timebuf = localtime (&t);
3363 llassert (pfile->timebuf != NULL);
3365 return pfile->timebuf;
3368 static ob_mstring monthnames[] = {
3369 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3370 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3374 * expand things like __FILE__. Place the expansion into the output
3375 * buffer *without* rescanning.
3379 special_symbol (hashNode hp, cppReader *pfile)
3381 cstring buf = cstring_undefined;
3387 int paren = 0; /* For special `defined' keyword */
3389 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3391 if (ip == cppReader_nullBuffer (pfile))
3393 cppReader_errorLit (pfile,
3394 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3395 return; /* the show must go on */
3398 if (ip != NULL && ip->fname != NULL)
3410 if (hp->type == T_BASE_FILE)
3412 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3414 ip = cppBuffer_prevBuffer (ip);
3418 llassert (ip != NULL);
3419 string = cstring_toCharsSafe (ip->nominal_fname);
3426 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3427 quote_string (pfile, string);
3431 case T_INCLUDE_LEVEL:
3433 ip = cppReader_getBuffer (pfile);
3435 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3436 ip = cppBuffer_prevBuffer (ip))
3438 if (ip != NULL && ip->fname != NULL)
3444 buf = message ("%d", true_indepth - 1);
3448 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3451 #ifndef NO_BUILTIN_SIZE_TYPE
3453 buf = cstring_makeLiteral (SIZE_TYPE);
3457 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3458 case T_PTRDIFF_TYPE:
3459 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3464 buf = cstring_makeLiteral (cppReader_wcharType (pfile));
3467 case T_USER_LABEL_PREFIX_TYPE:
3468 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3471 case T_REGISTER_PREFIX_TYPE:
3472 buf = cstring_makeLiteral (REGISTER_PREFIX);
3476 buf = message ("%d", hp->value.ival);
3483 int line = ip->lineno;
3484 int col = ip->colno;
3486 llassert (ip->cur != NULL);
3487 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3489 buf = message ("%d", (int) line);
3501 char *sbuf = (char *) dmalloc (20);
3502 timebuf = timestamp (pfile);
3503 if (hp->type == T_DATE)
3505 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3506 timebuf->tm_mday, timebuf->tm_year + 1900);
3510 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3514 buf = cstring_fromCharsNew (sbuf);
3519 case T_SPEC_DEFINED:
3520 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3521 ip = cppReader_getBuffer (pfile);
3523 llassert (ip->cur != NULL);
3524 SKIP_WHITE_SPACE (ip->cur);
3526 if (*ip->cur == '(')
3529 ip->cur++; /* Skip over the paren */
3530 SKIP_WHITE_SPACE (ip->cur);
3533 if (!is_idstart[(int) *ip->cur])
3535 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3538 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3541 buf = cstring_makeLiteral (" 1 ");
3544 while (is_idchar[(int) *ip->cur])
3549 SKIP_WHITE_SPACE (ip->cur);
3553 if (*ip->cur != ')')
3561 cppReader_errorLit (pfile,
3562 cstring_makeLiteralTemp ("`defined' without an identifier"));
3566 cpp_setLocation (pfile);
3567 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3570 len = size_fromInt (cstring_length (buf));
3572 cpplib_reserve (pfile, len + 1);
3573 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3574 cppReader_nullTerminateQ (pfile);
3580 /* Write out a #define command for the special named MACRO_NAME
3581 to PFILE's token_buffer. */
3584 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3586 static char define_directive[] = "#define ";
3587 size_t macro_name_length = strlen (macro_name);
3588 output_line_command (pfile, 0, same_file);
3589 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3590 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3591 cppReader_putStrN (pfile, macro_name, macro_name_length);
3592 cppReader_putCharQ (pfile, ' ');
3593 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3594 cppReader_putChar (pfile, '\n');
3597 /* Initialize the built-in macros. */
3600 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3601 int len, enum node_type type,
3602 int ivalue, /*@null@*/ /*@only@*/ char *value,
3605 cstring sname = cstring_fromCharsNew (name);
3607 llassert (usymtab_inGlobalScope ());
3610 ** Be careful here: this is done before the ctype table has
3611 ** been initialized.
3614 if (!usymtab_exists (sname))
3616 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3618 if (ctype_equal (ctyp, ctype_string))
3620 qualList ql = qualList_new ();
3621 ql = qualList_add (ql, qual_createObserver ());
3622 uentry_reflectQualifiers (ue, ql);
3626 usymtab_addGlobalEntry (ue);
3633 (void) cpphash_install (name, len, type, ivalue, value, hash);
3634 cstring_free (sname);
3638 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3639 int len, enum node_type type,
3641 /*@only@*/ /*@null@*/ char *value, int hash)
3643 cstring sname = cstring_fromChars (name);
3644 /* evs 2000 07 10 - removed a memory leak, detected by splint */
3646 llassert (usymtab_inGlobalScope ());
3648 if (!usymtab_existsTypeEither (sname))
3650 uentry ue = uentry_makeDatatype (sname, ctyp,
3652 fileloc_createBuiltin ());
3653 llassert (!usymtab_existsEither (sname));
3654 usymtab_addGlobalEntry (ue);
3657 (void) cpphash_install (name, len, type, ivalue, value, hash);
3661 initialize_builtins (cppReader *pfile)
3663 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3664 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3665 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3666 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3667 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3668 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3669 #ifndef NO_BUILTIN_SIZE_TYPE
3670 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3672 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3673 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3675 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3676 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3677 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3678 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3681 ** No, don't define __STDC__
3684 if (!cppReader_isTraditional (pfile))
3686 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3693 cpplib_installBuiltin ("_WIN32", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3697 ** This is supplied using a -D by the compiler driver
3698 ** so that it is present only when truly compiling with GNU C.
3701 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3703 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3704 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3706 /*drl 1/9/2001/ try to define the right symbol for the architecture
3707 We use autoconf to determine the target cpu
3709 cpplib_installBuiltin ("__" TARGET_CPU, ctype_int, -1, T_CONST, 2, NULL, -1);
3712 if (CPPOPTIONS (pfile)->debug_output)
3714 dump_special_to_buffer (pfile, "__BASE_FILE__");
3715 dump_special_to_buffer (pfile, "__VERSION__");
3716 #ifndef NO_BUILTIN_SIZE_TYPE
3717 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3719 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3720 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3722 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3723 dump_special_to_buffer (pfile, "__DATE__");
3724 dump_special_to_buffer (pfile, "__TIME__");
3725 if (!cppReader_isTraditional (pfile))
3726 dump_special_to_buffer (pfile, "__STDC__");
3731 /* Return 1 iff a token ending in C1 followed directly by a token C2
3732 could cause mis-tokenization. */
3735 unsafe_chars (char c1, char c2)
3740 if (c2 == c1 || c2 == '=')
3744 case '0': case '1': case '2': case '3': case '4':
3745 case '5': case '6': case '7': case '8': case '9':
3746 case 'e': case 'E': case 'p': case 'P':
3747 if (c2 == '-' || c2 == '+')
3748 return 1; /* could extend a pre-processing number */
3751 if (c2 == '\'' || c2 == '\"')
3752 return 1; /* Could turn into L"xxx" or L'xxx'. */
3756 case 'a': case 'b': case 'c': case 'd': case 'f':
3757 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3758 case 'm': case 'n': case 'o': case 'q': case 'r':
3759 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3761 case 'A': case 'B': case 'C': case 'D': case 'F':
3762 case 'G': case 'H': case 'I': case 'J': case 'K':
3763 case 'M': case 'N': case 'O': case 'Q': case 'R':
3764 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3766 /* We're in the middle of either a name or a pre-processing number. */
3767 return (is_idchar[(int) c2] || c2 == '.');
3768 case '<': case '>': case '!': case '%': case '#': case ':':
3769 case '^': case '&': case '|': case '*': case '/': case '=':
3770 return (c2 == c1 || c2 == '=');
3775 /* Expand a macro call.
3776 HP points to the symbol that is the macro being called.
3777 Put the result of expansion onto the input stack
3778 so that subsequent input by our caller will use it.
3780 If macro wants arguments, caller has already verified that
3781 an argument list follows; arguments come from the input stack. */
3784 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3787 DEFINITION *defn = hp->value.defn;
3793 size_t old_written = cpplib_getWritten (pfile);
3797 struct argdata *args = NULL;
3799 pfile->output_escapes++;
3801 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3803 nargs = defn->nargs;
3807 enum cpp_token token = CPP_EOF;
3809 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3811 for (i = 0; i < nargs; i++)
3813 args[i].expanded = 0;
3815 args[i].raw_length = 0;
3816 args[i].expand_length = args[i].stringified_length = -1;
3817 args[i].use_count = 0;
3821 ** Parse all the macro args that are supplied. I counts them.
3822 ** The first NARGS args are stored in ARGS.
3823 ** The rest are discarded. If rest_args is set then we assume
3824 ** macarg absorbed the rest of the args.
3830 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3838 if (i < nargs || (nargs == 0 && i == 0))
3840 /* if we are working on last arg which absorbs rest of args... */
3841 if (i == nargs - 1 && defn->rest_args)
3846 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3847 token = macarg (pfile, rest_args);
3848 args[i].raw_length = cpplib_getWritten (pfile) - args[i].raw;
3849 args[i].newlines = FALSE; /* FIXME */
3853 token = macarg (pfile, 0);
3856 if (token == CPP_EOF || token == CPP_POP)
3858 cppReader_errorWithLine (pfile, start_line, start_column,
3859 cstring_fromCharsNew ("unterminated macro call"));
3864 } while (token == CPP_COMMA);
3866 /* If we got one arg but it was just whitespace, call that 0 args. */
3874 bp = ARG_BASE + args[0].raw;
3875 lim = bp + args[0].raw_length;
3877 /* cpp.texi says for foo ( ) we provide one argument.
3878 However, if foo wants just 0 arguments, treat this as 0. */
3882 while (bp != lim && is_space[(int) *bp])
3892 /* Don't output an error message if we have already output one for
3893 a parse error above. */
3896 if (nargs == 0 && i > 0)
3898 cppReader_error (pfile,
3899 message ("arguments given to macro `%s'", hp->name));
3903 /* traditional C allows foo() if foo wants one argument. */
3904 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
3908 /* the rest args token is allowed to absorb 0 tokens */
3909 else if (i == nargs - 1 && defn->rest_args)
3912 cppReader_error (pfile,
3913 message ("macro `%s' used without args", hp->name));
3915 cppReader_error (pfile,
3916 message ("macro `%s' used with just one arg", hp->name));
3919 cppReader_error (pfile,
3920 message ("macro `%s' used with only %d args",
3926 cppReader_error (pfile,
3927 message ("macro `%s' used with too many (%d) args", hp->name, i));
3935 /* If macro wants zero args, we parsed the arglist for checking only.
3936 Read directly from the macro definition. */
3940 xbuf = defn->expansion;
3941 xbuf_len = defn->length;
3945 char *exp = defn->expansion;
3946 int offset; /* offset in expansion,
3947 copied a piece at a time */
3948 size_t totlen; /* total amount of exp buffer filled so far */
3950 register struct reflist *ap, *last_ap;
3952 assertSet (args); /* args is defined since the nargs > 0 path was taken */
3954 /* Macro really takes args. Compute the expansion of this call. */
3956 /* Compute length in characters of the macro's expansion.
3957 Also count number of times each arg is used. */
3958 xbuf_len = defn->length;
3960 llassert (args != NULL);
3962 for (ap = defn->pattern; ap != NULL; ap = ap->next)
3966 struct argdata *arg = &args[ap->argno];
3968 /* Stringify it it hasn't already been */
3971 if (arg->stringified_length < 0)
3973 int arglen = arg->raw_length;
3974 bool escaped = FALSE;
3975 char in_string = '\0';
3978 /* Initially need_space is -1. Otherwise, 1 means the
3979 previous character was a space, but we suppressed it;
3980 0 means the previous character was a non-space. */
3981 int need_space = -1;
3984 arg->stringified = cpplib_getWritten (pfile);
3985 if (!cppReader_isTraditional (pfile))
3986 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
3987 for (; i < arglen; i++)
3989 c = (ARG_BASE + arg->raw)[i];
3991 if (in_string == '\0')
3993 /* Internal sequences of whitespace are replaced by
3994 one space except within an string or char token.*/
3995 if (is_space[(int) c])
3997 if (cpplib_getWritten (pfile) > arg->stringified
3998 && (cpplib_getPWritten (pfile))[-1] == '@')
4000 /* "@ " escape markers are removed */
4001 cppReader_adjustWritten (pfile, -1);
4002 /*@innercontinue@*/ continue;
4004 if (need_space == 0)
4006 /*@innercontinue@*/ continue;
4008 else if (need_space > 0)
4009 cppReader_putChar (pfile, ' ');
4025 if (in_string != '\0')
4030 else if (c == '\"' || c == '\'')
4040 /* Escape these chars */
4041 if (c == '\"' || (in_string != '\0' && c == '\\'))
4042 cppReader_putChar (pfile, '\\');
4044 cppReader_putChar (pfile, c);
4047 cpplib_reserve (pfile, 4);
4048 sprintf (cpplib_getPWritten (pfile), "\\%03o",
4050 cppReader_adjustWritten (pfile, 4);
4053 if (!cppReader_isTraditional (pfile))
4054 cppReader_putChar (pfile, '\"'); /* insert ending quote */
4055 arg->stringified_length
4056 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
4059 xbuf_len += args[ap->argno].stringified_length;
4061 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4063 /* Add 4 for two newline-space markers to prevent
4064 token concatenation. */
4065 assertSet (args); /*@i534 shouldn't need this */
4066 xbuf_len += args[ap->argno].raw_length + 4;
4070 /* We have an ordinary (expanded) occurrence of the arg.
4071 So compute its expansion, if we have not already. */
4073 assertSet (args); /*@i534 shouldn't need this */
4075 if (args[ap->argno].expand_length < 0)
4077 args[ap->argno].expanded = cpplib_getWritten (pfile);
4078 cpp_expand_to_buffer (pfile,
4079 ARG_BASE + args[ap->argno].raw,
4080 size_fromInt (args[ap->argno].raw_length));
4082 args[ap->argno].expand_length
4083 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4086 /* Add 4 for two newline-space markers to prevent
4087 token concatenation. */
4088 xbuf_len += args[ap->argno].expand_length + 4;
4090 if (args[ap->argno].use_count < 10)
4091 args[ap->argno].use_count++;
4094 xbuf = (char *) dmalloc (xbuf_len + 1);
4098 ** Generate in XBUF the complete expansion
4099 ** with arguments substituted in.
4100 ** TOTLEN is the total size generated so far.
4101 ** OFFSET is the index in the definition
4102 ** of where we are copying from.
4108 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4109 last_ap = ap, ap = ap->next)
4111 register struct argdata *arg = &args[ap->argno];
4112 size_t count_before = totlen;
4114 /* Add chars to XBUF. */
4115 for (i = 0; i < ap->nchars; i++, offset++)
4117 xbuf[totlen++] = exp[offset];
4120 /* If followed by an empty rest arg with concatenation,
4121 delete the last run of nonwhite chars. */
4122 if (rest_zero && totlen > count_before
4123 && ((ap->rest_args && ap->raw_before)
4124 || (last_ap != NULL && last_ap->rest_args
4125 && last_ap->raw_after)))
4127 /* Delete final whitespace. */
4128 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4133 /* Delete the nonwhites before them. */
4134 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4140 if (ap->stringify != 0)
4143 memcpy (xbuf + totlen,
4144 ARG_BASE + arg->stringified,
4145 size_fromInt (arg->stringified_length));
4146 totlen += arg->stringified_length;
4148 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4155 p1 = ARG_BASE + arg->raw;
4156 l1 = p1 + arg->raw_length;
4160 while (p1 != l1 && is_space[(int) *p1])
4165 while (p1 != l1 && is_idchar[(int) *p1])
4167 xbuf[totlen++] = *p1++;
4170 /* Delete any no-reexpansion marker that follows
4171 an identifier at the beginning of the argument
4172 if the argument is concatenated with what precedes it. */
4173 if (p1[0] == '@' && p1[1] == '-')
4178 /* Arg is concatenated after: delete trailing whitespace,
4179 whitespace markers, and no-reexpansion markers. */
4182 if (is_space[(int) l1[-1]]) l1--;
4183 else if (l1[-1] == '-')
4186 /* If a `-' is preceded by an odd number of newlines then it
4187 and the last newline are a no-reexpansion marker. */
4188 while (p2 != p1 && p2[-1] == '\n')
4193 if (((l1 - 1 - p2) & 1) != 0)
4199 /*@innerbreak@*/ break;
4204 /*@innerbreak@*/ break;
4209 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4217 expanded = ARG_BASE + arg->expanded;
4219 if (!ap->raw_before && totlen > 0
4220 && (arg->expand_length != 0)
4221 && !cppReader_isTraditional(pfile)
4222 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4224 xbuf[totlen++] = '@';
4225 xbuf[totlen++] = ' ';
4228 memcpy (xbuf + totlen, expanded,
4229 size_fromInt (arg->expand_length));
4230 totlen += arg->expand_length;
4232 if (!ap->raw_after && totlen > 0
4233 && offset < size_toInt (defn->length)
4234 && !cppReader_isTraditional(pfile)
4235 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4237 xbuf[totlen++] = '@';
4238 xbuf[totlen++] = ' ';
4241 /* If a macro argument with newlines is used multiple times,
4242 then only expand the newlines once. This avoids creating
4243 output lines which don't correspond to any input line,
4244 which confuses gdb and gcov. */
4245 if (arg->use_count > 1 && arg->newlines > 0)
4247 /* Don't bother doing change_newlines for subsequent
4251 = change_newlines (expanded, arg->expand_length);
4255 if (totlen > xbuf_len)
4259 /* if there is anything left of the definition
4260 after handling the arg list, copy that in too. */
4262 for (i = offset; i < size_toInt (defn->length); i++)
4264 /* if we've reached the end of the macro */
4267 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4268 && last_ap->raw_after))
4269 xbuf[totlen++] = exp[i];
4272 xbuf[totlen] = '\0';
4276 pfile->output_escapes--;
4278 /* Now put the expansion on the input stack
4279 so our caller will commence reading from it. */
4280 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4281 cppReader_getBuffer (pfile)->has_escapes = 1;
4283 /* Pop the space we've used in the token_buffer for argument expansion. */
4284 cppReader_setWritten (pfile, old_written);
4286 /* Recursive macro use sometimes works traditionally.
4287 #define foo(x,y) bar (x (y,0), y)
4290 if (!cppReader_isTraditional (pfile))
4291 hp->type = T_DISABLED;
4297 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4298 /*@dependent@*/ hashNode hp)
4300 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4307 mbuf->cleanup = cppReader_macroCleanup;
4309 llassert (mbuf->hnode == NULL);
4312 /* The first chars of the expansion should be a "@ " added by
4313 collect_expansion. This is to prevent accidental token-pasting
4314 between the text preceding the macro invocation, and the macro
4317 We would like to avoid adding unneeded spaces (for the sake of
4318 tools that use cpp, such as imake). In some common cases we can
4319 tell that it is safe to omit the space.
4321 The character before the macro invocation cannot have been an
4322 idchar (or else it would have been pasted with the idchars of
4323 the macro name). Therefore, if the first non-space character
4324 of the expansion is an idchar, we do not need the extra space
4325 to prevent token pasting.
4327 Also, we don't need the extra space if the first char is '(',
4328 or some other (less common) characters. */
4330 if (xbuf[0] == '@' && xbuf[1] == ' '
4331 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4332 || xbuf[2] == '\"'))
4334 llassert (mbuf->cur != NULL);
4340 /* Like cpplib_getToken, except that it does not read past end-of-line.
4341 Also, horizontal space is skipped, and macros are popped. */
4343 static enum cpp_token
4344 get_directive_token (cppReader *pfile)
4348 size_t old_written = cpplib_getWritten (pfile);
4349 enum cpp_token token;
4350 cppSkipHspace (pfile);
4351 if (cppReader_peekC (pfile) == '\n')
4356 token = cpplib_getToken (pfile);
4361 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4366 cppReader_setWritten (pfile, old_written);
4367 /*@switchbreak@*/ break;
4375 /* Handle #include and #import.
4376 This function expects to see "fname" or <fname> on the input.
4378 The input is normally in part of the output_buffer following
4379 cpplib_getWritten, and will get overwritten by output_line_command.
4380 I.e. in input file specification has been popped by cppReader_handleDirective.
4384 do_include (cppReader *pfile, struct directive *keyword,
4385 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4387 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4389 char *fbeg, *fend; /* Beginning and end of fname */
4390 enum cpp_token token;
4392 /* Chain of dirs to search */
4393 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4394 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4395 struct file_name_list *searchptr = NULL;
4396 size_t old_written = cpplib_getWritten (pfile);
4400 int f; /* file number */
4401 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4402 f= -1; /* JF we iz paranoid! */
4404 pfile->parsing_include_directive++;
4405 token = get_directive_token (pfile);
4406 pfile->parsing_include_directive--;
4408 if (token == CPP_STRING)
4410 /* FIXME - check no trailing garbage */
4411 fbeg = pfile->token_buffer + old_written + 1;
4412 fend = cpplib_getPWritten (pfile) - 1;
4413 if (fbeg[-1] == '<')
4416 /* If -I-, start with the first -I dir after the -I-. */
4417 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4418 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4420 /* If -I- was specified, don't search current dir, only spec'd ones. */
4421 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4423 cppBuffer *fp = CPPBUFFER (pfile);
4424 /* We have "filename". Figure out directory this source
4425 file is coming from and put it on the front of the list. */
4427 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4432 llassert (fp != NULL);
4436 if (cstring_isDefined (fp->nominal_fname))
4438 nam = cstring_toCharsSafe (fp->nominal_fname);
4440 /* Found a named file. Figure out dir of the file,
4441 and put it in front of the search list. */
4442 dsp[0].next = search_start;
4446 ep = strrchr (nam, CONNECTCHAR);
4448 ep = strrchr (nam, ']');
4449 if (ep == NULL) ep = strrchr (nam, '>');
4450 if (ep == NULL) ep = strrchr (nam, ':');
4451 if (ep != NULL) ep++;
4461 /*@-onlytrans@*/ /* This looks like a memory leak... */
4462 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4466 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
4467 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
4471 dsp[0].fname = cstring_undefined; /* Current directory */
4474 dsp[0].got_name_map = 0;
4485 else if (token == CPP_NAME)
4488 * Support '#include xyz' like VAX-C to allow for easy use of all the
4489 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4490 * code from case '<' is repeated here) and generates a warning.
4492 cppReader_warning (pfile,
4493 "VAX-C-style include specification found, use '#include <filename.h>' !");
4495 /* If -I-, start with the first -I dir after the -I-. */
4496 if (CPPOPTIONS (pfile)->first_bracket_include)
4497 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4498 fbeg = pfile->token_buffer + old_written;
4499 fend = cpplib_getPWritten (pfile);
4504 cppReader_error (pfile,
4505 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4508 cppReader_setWritten (pfile, old_written);
4509 cppReader_skipRestOfLine (pfile);
4515 token = get_directive_token (pfile);
4516 if (token != CPP_VSPACE)
4518 cppReader_errorLit (pfile,
4519 cstring_makeLiteralTemp ("Junk at end of #include"));
4521 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4523 token = get_directive_token (pfile);
4528 ** For #include_next, skip in the search path
4529 ** past the dir in which the containing file was found.
4534 cppBuffer *fp = CPPBUFFER (pfile);
4536 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4538 llassert (fp != NULL);
4540 if (fp->fname != NULL)
4542 /* fp->dir is null if the containing file was specified with
4543 an absolute file name. In that case, don't skip anything. */
4544 if (fp->dir == SELF_DIR_DUMMY)
4546 search_start = CPPOPTIONS (pfile)->include;
4548 else if (fp->dir != NULL)
4550 search_start = fp->dir->next;
4562 cppReader_setWritten (pfile, old_written);
4566 DPRINTF (("fbeg: %s", fbeg));
4570 cppReader_error (pfile,
4571 message ("Empty file name in #%s", keyword->name));
4576 ** Allocate this permanently, because it gets stored in the definitions
4580 fname = cstring_undefined;
4582 /* + 2 above for slash and terminating null. */
4583 /* + 2 added for '.h' on VMS (to support '#include filename') */
4585 /* If specified file name is absolute, just open it. */
4587 if (osd_isConnectChar (*fbeg)
4588 # if defined (WIN32) || defined (OS2)
4589 || (*(fbeg + 1) == ':')
4593 fname = cstring_copyLength (fbeg, flen);
4595 if (redundant_include_p (pfile, fname))
4597 cstring_free (fname);
4601 f = open_include_file (pfile, fname, NULL);
4603 if (f == IMPORT_FOUND)
4605 return 0; /* Already included this file */
4610 /* Search directory path, trying to open the file.
4611 Copy each filename tried into FNAME. */
4613 for (searchptr = search_start; searchptr != NULL;
4614 searchptr = searchptr->next)
4616 if (!cstring_isEmpty (searchptr->fname))
4618 /* The empty string in a search path is ignored.
4619 This makes it possible to turn off entirely
4620 a standard piece of the list. */
4621 if (cstring_isEmpty (searchptr->fname))
4624 fname = cstring_copy (searchptr->fname);
4625 fname = cstring_appendChar (fname, CONNECTCHAR);
4626 DPRINTF (("Here: %s", fname));
4633 fname = cstring_concatLength (fname, fbeg, flen);
4635 DPRINTF (("fname: %s", fname));
4637 /* Win32 directory fix from Kay Buschner. */
4638 #if defined (WIN32) || defined (OS2)
4639 /* Fix all unixdir slashes to win dir slashes */
4640 if (searchptr->fname && (searchptr->fname[0] != 0))
4642 cstring_replaceAll (fname, '/', '\\');
4647 /* Change this 1/2 Unix 1/2 VMS file specification into a
4648 full VMS file specification */
4649 if (searchptr->fname && (searchptr->fname[0] != 0)) {
4650 /* Fix up the filename */
4651 hack_vms_include_specification (fname);
4653 /* This is a normal VMS filespec, so use it unchanged. */
4654 strncpy (fname, fbeg, flen);
4656 /* if it's '#include filename', add the missing .h */
4657 if (strchr (fname,'.') == NULL) {
4658 strcat (fname, ".h");
4662 /* ??? There are currently 3 separate mechanisms for avoiding processing
4663 of redundant include files: #import, #pragma once, and
4664 redundant_include_p. It would be nice if they were unified. */
4666 if (redundant_include_p (pfile, fname))
4668 cstring_free (fname);
4672 DPRINTF (("Trying: %s", fname));
4674 f = open_include_file (pfile, fname, searchptr);
4676 if (f == IMPORT_FOUND)
4678 return 0; /* Already included this file */
4681 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4683 cppReader_warning (pfile,
4684 message ("Header file %s exists, but is not readable", fname));
4697 /* A file that was not found. */
4698 fname = cstring_copyLength (fbeg, flen);
4700 if (search_start != NULL)
4702 cppReader_error (pfile,
4703 message ("Cannot find include file %s on search path: %x",
4705 searchPath_unparse (search_start)));
4709 cppReader_error (pfile,
4710 message ("No include path in which to find %s", fname));
4715 ** Check to see if this include file is a once-only include file.
4719 struct file_name_list *ptr;
4721 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4723 if (cstring_equal (ptr->fname, fname))
4725 /* This file was included before. */
4732 /* This is the first time for this file. */
4733 /* Add it to list of files included. */
4735 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4736 ptr->control_macro = NULL;
4737 ptr->c_system_include_path = NULL;
4738 ptr->next = pfile->all_include_files;
4740 ptr->got_name_map = NULL;
4742 DPRINTF (("Including file: %s", fname));
4743 pfile->all_include_files = ptr;
4744 assertSet (pfile->all_include_files);
4747 if (angle_brackets != 0)
4749 pfile->system_include_depth++;
4752 /* Actually process the file */
4753 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4755 cstring_free (fname);
4759 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4760 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4762 output_line_command (pfile, 0, enter_file);
4763 pfile->only_seen_white = 2;
4768 pfile->system_include_depth--;
4771 } /*@=branchstate@*/
4776 /* Return nonzero if there is no need to include file NAME
4777 because it has already been included and it contains a conditional
4778 to make a repeated include do nothing. */
4781 redundant_include_p (cppReader *pfile, cstring name)
4783 struct file_name_list *l = pfile->all_include_files;
4785 for (; l != NULL; l = l->next)
4787 if (cstring_equal (name, l->fname)
4788 && (l->control_macro != NULL)
4789 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4798 /* Return nonzero if the given FILENAME is an absolute pathname which
4799 designates a file within one of the known "system" include file
4800 directories. We assume here that if the given FILENAME looks like
4801 it is the name of a file which resides either directly in a "system"
4802 include file directory, or within any subdirectory thereof, then the
4803 given file must be a "system" include file. This function tells us
4804 if we should suppress pedantic errors/warnings for the given FILENAME.
4806 The value is 2 if the file is a C-language system header file
4807 for which C++ should (on most systems) assume `extern "C"'. */
4810 is_system_include (cppReader *pfile, cstring filename)
4812 struct file_name_list *searchptr;
4814 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4816 searchptr = searchptr->next)
4818 if (!cstring_isEmpty (searchptr->fname)) {
4819 cstring sys_dir = searchptr->fname;
4820 int length = cstring_length (sys_dir);
4822 if (cstring_equalLen (sys_dir, filename, length)
4823 && osd_isConnectChar (cstring_getChar (filename, length)))
4825 if (searchptr->c_system_include_path)
4836 /* Convert a character string literal into a nul-terminated string.
4837 The input string is [IN ... LIMIT).
4838 The result is placed in RESULT. RESULT can be the same as IN.
4839 The value returned in the end of the string written to RESULT,
4840 or NULL on error. */
4842 static /*@null@*/ char *
4843 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4844 char *in, char *limit, int handle_escapes)
4864 /*@switchbreak@*/ break;
4868 char *bpc = (char *) in;
4869 int i = (char) cppReader_parseEscape (pfile, &bpc);
4872 *result++ = (char) c;
4873 /*@switchbreak@*/ break;
4887 * interpret #line command. Remembers previously seen fnames
4888 * in its very own hash table.
4891 /*@constant int FNAME_HASHSIZE@*/
4892 #define FNAME_HASHSIZE 37
4895 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
4897 cppBuffer *ip = cppReader_getBuffer (pfile);
4899 size_t old_written = cpplib_getWritten (pfile);
4900 enum file_change_code file_change = same_file;
4901 enum cpp_token token;
4903 token = get_directive_token (pfile);
4905 if (token != CPP_NUMBER
4906 || !isdigit(pfile->token_buffer[old_written]))
4908 cppReader_errorLit (pfile,
4909 cstring_makeLiteralTemp ("invalid format `#line' command"));
4911 goto bad_line_directive;
4914 /* The Newline at the end of this line remains to be processed.
4915 To put the next line at the specified line number,
4916 we must store a line number now that is one less. */
4917 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
4918 cppReader_setWritten (pfile, old_written);
4920 /* NEW_LINENO is one less than the actual line number here. */
4921 if (cppReader_isPedantic (pfile) && new_lineno < 0)
4922 cppReader_pedwarnLit (pfile,
4923 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
4925 token = get_directive_token (pfile);
4927 if (token == CPP_STRING) {
4928 char *fname = pfile->token_buffer + old_written;
4930 static hashNode fname_table[FNAME_HASHSIZE];
4932 hashNode *hash_bucket;
4937 /* Turn the file name, which is a character string literal,
4938 into a null-terminated string. Do this in place. */
4939 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
4940 if (end_name == NULL)
4942 cppReader_errorLit (pfile,
4943 cstring_makeLiteralTemp ("invalid format `#line' command"));
4944 goto bad_line_directive;
4947 fname_length = end_name - fname;
4948 num_start = cpplib_getWritten (pfile);
4950 token = get_directive_token (pfile);
4951 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
4952 p = pfile->token_buffer + num_start;
4953 if (cppReader_isPedantic (pfile))
4954 cppReader_pedwarnLit (pfile,
4955 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
4957 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
4959 cppReader_errorLit (pfile,
4960 cstring_makeLiteralTemp ("invalid format `#line' command"));
4961 goto bad_line_directive;
4964 file_change = enter_file;
4966 file_change = leave_file;
4968 ip->system_header_p = 1;
4969 else /* if (*p == 4) */
4970 ip->system_header_p = 2;
4972 cppReader_setWritten (pfile, num_start);
4973 token = get_directive_token (pfile);
4974 p = pfile->token_buffer + num_start;
4975 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
4976 ip->system_header_p = *p == 3 ? 1 : 2;
4977 token = get_directive_token (pfile);
4979 if (token != CPP_VSPACE) {
4980 cppReader_errorLit (pfile,
4981 cstring_makeLiteralTemp ("invalid format `#line' command"));
4983 goto bad_line_directive;
4988 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
4989 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
4991 if (hp->length == fname_length &&
4992 strncmp (hp->value.cpval, fname, size_fromInt (fname_length)) == 0) {
4993 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4999 /* Didn't find it; cons up a new one. */
5000 hp = (hashNode) dmalloc (sizeof (*hp));
5003 hp->bucket_hdr = NULL;
5005 hp->name = cstring_undefined;
5006 hp->next = *hash_bucket;
5010 hp->length = fname_length;
5011 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
5012 memcpy (hp->value.cpval, fname, size_fromInt (fname_length));
5013 hp->value.cpval[fname_length] = '\0';
5014 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
5017 else if (token != CPP_VSPACE && token != CPP_EOF)
5019 cppReader_errorLit (pfile,
5020 cstring_makeLiteralTemp ("invalid format `#line' command"));
5021 goto bad_line_directive;
5028 ip->lineno = new_lineno;
5030 cppReader_skipRestOfLine (pfile);
5031 cppReader_setWritten (pfile, old_written);
5032 output_line_command (pfile, 0, file_change);
5037 * remove the definition of a symbol from the symbol table.
5038 * according to un*x /lib/cpp, it is not an error to undef
5039 * something that has no definitions, so it isn't one here either.
5043 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
5048 char *orig_buf = buf;
5050 SKIP_WHITE_SPACE (buf);
5052 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
5054 while ((hp = cpphash_lookup (buf, sym_length, -1)) != NULL)
5056 /* If we are generating additional info for debugging (with -g) we
5057 need to pass through all effective #undef commands. */
5058 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
5060 pass_thru_directive (orig_buf, limit, pfile, keyword);
5063 if (hp->type != T_MACRO)
5065 cppReader_warning (pfile,
5066 message ("Undefining preprocessor builtin: %s",
5070 cppReader_deleteMacro (hp);
5073 if (cppReader_isPedantic (pfile)) {
5075 SKIP_WHITE_SPACE (buf);
5078 cppReader_pedwarnLit (pfile,
5079 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
5088 * Report an error detected by the program we are processing.
5089 * Use the text of the line in the error message.
5090 * (We use error because it prints the filename & line#.)
5094 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5095 char *buf, char *limit)
5097 int length = limit - buf;
5098 cstring copy = cstring_copyLength (buf, length);
5099 cstring adv = cstring_advanceWhiteSpace (copy);
5101 cppReader_error (pfile, message ("#error %s", adv));
5102 cstring_free (copy);
5107 * Report a warning detected by the program we are processing.
5108 * Use the text of the line in the warning message, then continue.
5109 * (We use error because it prints the filename & line#.)
5113 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5114 char *buf, char *limit)
5116 int length = limit - buf;
5117 cstring copy = cstring_copyLength (buf, length);
5118 cstring adv = cstring_advanceWhiteSpace (copy);
5119 cppReader_warning (pfile, message ("#warning %s", adv));
5120 cstring_free (copy);
5125 /* #ident has already been copied to the output file, so just ignore it. */
5128 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5129 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5131 /* Allow #ident in system headers, since that's not user's fault. */
5132 if (cppReader_isPedantic (pfile) && !cppReader_getBuffer (pfile)->system_header_p)
5133 cppReader_pedwarnLit (pfile,
5134 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5136 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5141 /* #pragma and its argument line have already been copied to the output file.
5142 Just check for some recognized pragmas that need validation here. */
5145 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5146 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5148 while (*buf == ' ' || *buf == '\t')
5153 if (!strncmp (buf, "implementation", 14)) {
5154 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5155 been included yet. */
5156 struct file_name_list *ptr;
5157 char *p = buf + 14, *fname, *inc_fname;
5159 SKIP_WHITE_SPACE (p);
5160 if (*p == '\n' || *p != '\"')
5164 p = (char *) strchr (fname, '\"');
5165 fname_len = p != NULL ? p - fname : mstring_length (fname);
5167 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5169 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5170 inc_fname = (inc_fname != NULL)
5171 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5173 if ((inc_fname != NULL)
5174 && (strncmp (inc_fname, fname, size_fromInt (fname_len)) == 0))
5176 cpp_setLocation (pfile);
5178 ppllerror (message ("`#pragma implementation' for `%s' appears "
5179 "after file is included",
5180 cstring_fromChars (fname)));
5189 * handle #if command by
5190 * 1) inserting special `defined' keyword into the hash table
5191 * that gets turned into 0 or 1 by special_symbol (thus,
5192 * if the luser has a symbol called `defined' already, it won't
5193 * work inside the #if command)
5194 * 2) rescan the input into a temporary output buffer
5195 * 3) pass the output buffer to the yacc parser and collect a value
5196 * 4) clean up the mess left from steps 1 and 2.
5197 * 5) call conditional_skip to skip til the next #endif (etc.),
5198 * or not, depending on the value from step 3.
5202 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5203 char *buf, char *limit)
5205 HOST_WIDE_INT value;
5206 DPRINTF (("Do if: %s", buf));
5207 value = eval_if_expression (pfile, buf, limit - buf);
5208 conditional_skip (pfile, value == 0, T_IF, NULL);
5213 * handle a #elif directive by not changing if_stack either.
5214 * see the comment above do_else.
5217 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5218 char *buf, char *limit)
5220 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5222 cppReader_errorLit (pfile,
5223 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5228 llassert (pfile->if_stack != NULL);
5230 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5232 cppReader_errorLit (pfile,
5233 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5235 if (pfile->if_stack->fname != NULL
5236 && cppReader_getBuffer (pfile)->fname != NULL
5237 && !cstring_equal (pfile->if_stack->fname,
5238 cppReader_getBuffer (pfile)->nominal_fname))
5239 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5240 fprintf (stderr, ")\n");
5242 pfile->if_stack->type = T_ELIF;
5245 if (pfile->if_stack->if_succeeded)
5247 skip_if_group (pfile, 0);
5251 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5253 skip_if_group (pfile, 0);
5256 ++pfile->if_stack->if_succeeded; /* continue processing input */
5257 output_line_command (pfile, 1, same_file);
5265 * evaluate a #if expression in BUF, of length LENGTH,
5266 * then parse the result as a C expression and return the value as an int.
5269 static HOST_WIDE_INT
5270 eval_if_expression (cppReader *pfile,
5271 /*@unused@*/ char *buf,
5272 /*@unused@*/ int length)
5274 hashNode save_defined;
5275 HOST_WIDE_INT value;
5276 size_t old_written = cpplib_getWritten (pfile);
5278 DPRINTF (("Saving defined..."));
5279 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5280 pfile->pcp_inside_if = 1;
5282 value = cppReader_parseExpression (pfile);
5283 pfile->pcp_inside_if = 0;
5285 /* Clean up special symbol */
5286 DPRINTF (("Removing defined..."));
5287 cppReader_deleteMacro (save_defined);
5288 cppReader_setWritten (pfile, old_written); /* Pop */
5294 * routine to handle ifdef/ifndef. Try to look up the symbol,
5295 * then do or don't skip to the #endif/#else/#elif depending
5296 * on what directive is actually being processed.
5300 do_xifdef (cppReader *pfile, struct directive *keyword,
5301 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5304 cppBuffer *ip = cppReader_getBuffer (pfile);
5307 enum cpp_token token;
5308 int start_of_file = 0;
5309 char *control_macro = 0;
5310 size_t old_written = cpplib_getWritten (pfile);
5312 DPRINTF (("do xifdef: %d",
5313 keyword->type == T_IFNDEF));
5315 /* Detect a #ifndef at start of file (not counting comments). */
5316 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5318 start_of_file = pfile->only_seen_white == 2;
5321 pfile->no_macro_expand++;
5322 token = get_directive_token (pfile);
5323 pfile->no_macro_expand--;
5325 ident = pfile->token_buffer + old_written;
5326 DPRINTF (("Ident: %s", ident));
5328 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5329 cppReader_setWritten (pfile, old_written); /* Pop */
5331 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5333 skip = (keyword->type == T_IFDEF);
5334 if (! cppReader_isTraditional (pfile))
5336 cppReader_pedwarn (pfile,
5337 message ("`#%s' with no argument", keyword->name));
5340 else if (token == CPP_NAME)
5342 hashNode hp = cpphash_lookup (ident, ident_length, -1);
5344 DPRINTF (("Lookup: %s %d", ident, ident_length));
5346 skip = (keyword->type == T_IFDEF)
5347 ? (hp == NULL) : (hp != NULL);
5349 DPRINTF (("hp null: %d / %d / %d",
5351 (keyword->type == T_IFNDEF),
5354 if (start_of_file && !skip)
5356 DPRINTF (("Not skipping!"));
5357 control_macro = (char *) dmalloc (size_fromInt (ident_length + 1));
5358 memcpy (control_macro, ident, size_fromInt (ident_length + 1));
5363 skip = (keyword->type == T_IFDEF);
5364 if (! cppReader_isTraditional (pfile))
5366 cppReader_error (pfile,
5367 message ("`#%s' with invalid argument", keyword->name));
5371 if (!cppReader_isTraditional (pfile))
5374 cppSkipHspace (pfile);
5375 c = cppReader_peekC (pfile);
5376 if (c != EOF && c != '\n')
5378 cppReader_pedwarn (pfile,
5379 message ("garbage at end of `#%s' argument", keyword->name));
5383 cppReader_skipRestOfLine (pfile);
5385 DPRINTF (("Conditional skip: %d", skip));
5386 conditional_skip (pfile, skip, T_IF, control_macro);
5390 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5391 If this is a #ifndef starting at the beginning of a file,
5392 CONTROL_MACRO is the macro name tested by the #ifndef.
5393 Otherwise, CONTROL_MACRO is 0. */
5396 conditional_skip (cppReader *pfile, int skip,
5397 enum node_type type,
5398 /*@dependent@*/ char *control_macro)
5400 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5402 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
5403 temp->next = pfile->if_stack;
5404 temp->control_macro = control_macro;
5406 temp->if_succeeded = 0;
5408 pfile->if_stack = temp;
5409 pfile->if_stack->type = type;
5413 skip_if_group (pfile, 0);
5418 ++pfile->if_stack->if_succeeded;
5419 output_line_command (pfile, 1, same_file);
5424 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5425 * leaves input ptr at the sharp sign found.
5426 * If ANY is nonzero, return at next directive of any sort.
5430 skip_if_group (cppReader *pfile, int any)
5433 struct directive *kt;
5434 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5435 register int ident_length;
5437 struct parse_marker line_start_mark;
5439 parseSetMark (&line_start_mark, pfile);
5441 if (CPPOPTIONS (pfile)->output_conditionals) {
5442 static char failed[] = "#failed\n";
5443 cppReader_puts (pfile, failed, sizeof(failed)-1);
5445 output_line_command (pfile, 1, same_file);
5449 if (CPPOPTIONS (pfile)->output_conditionals)
5451 cppBuffer *pbuf = cppReader_getBuffer (pfile);
5454 llassert (pbuf->buf != NULL);
5456 start_line = pbuf->buf + line_start_mark.position;
5457 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5460 parseMoveMark (&line_start_mark, pfile);
5462 if (!cppReader_isTraditional (pfile))
5464 cppSkipHspace (pfile);
5467 c = cppReader_getC (pfile);
5470 size_t old_written = cpplib_getWritten (pfile);
5471 cppSkipHspace (pfile);
5473 parse_name (pfile, cppReader_getC (pfile));
5474 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5475 ident = pfile->token_buffer + old_written;
5476 pfile->limit = ident;
5478 for (kt = directive_table; kt->length >= 0; kt++)
5480 cppIfStackFrame *temp;
5481 if (ident_length == kt->length
5482 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5484 /* If we are asked to return on next directive, do so now. */
5495 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5496 temp->next = pfile->if_stack;
5497 temp->fname = cppReader_getBuffer (pfile)->nominal_fname;
5498 temp->type = kt->type;
5500 temp->if_succeeded = 0;
5501 temp->control_macro = NULL;
5503 pfile->if_stack = temp;
5504 /*@switchbreak@*/ break;
5507 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5508 validate_else (pfile,
5509 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5512 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5514 cppReader_error (pfile,
5515 message ("Preprocessor command #%s is not within a conditional", kt->name));
5516 /*@switchbreak@*/ break;
5518 else if (pfile->if_stack == save_if_stack)
5520 goto done; /* found what we came for */
5527 if (kt->type != T_ENDIF)
5529 llassert (pfile->if_stack != NULL);
5531 if (pfile->if_stack->type == T_ELSE)
5533 cppReader_errorLit (pfile,
5534 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5537 pfile->if_stack->type = kt->type;
5538 /*@switchbreak@*/ break;
5541 temp = pfile->if_stack;
5542 llassert (temp != NULL);
5543 pfile->if_stack = temp->next;
5545 /*@switchbreak@*/ break;
5548 #if defined (OS2) && defined (__IBMC__)
5549 /* Dummy code to eleminate optimization problems with icc */
5558 /* Don't let erroneous code go by. */
5560 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5561 && cppReader_isPedantic (pfile))
5563 cppReader_pedwarnLit (pfile,
5564 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5568 c = cppReader_getC (pfile);
5570 /* We're in the middle of a line. Skip the rest of it. */
5578 case '/': /* possible comment */
5579 c = skip_comment (pfile, NULL);
5582 /*@switchbreak@*/ break;
5585 cppReader_forward (pfile, -1);
5586 old = cpplib_getWritten (pfile);
5587 (void) cpplib_getToken (pfile);
5588 cppReader_setWritten (pfile, old);
5589 /*@switchbreak@*/ break;
5591 /* Char after backslash loses its special meaning. */
5592 if (cppReader_peekC (pfile) == '\n')
5594 cppReader_forward (pfile, 1);
5597 /*@switchbreak@*/ break;
5601 c = cppReader_getC (pfile);
5604 if (CPPOPTIONS (pfile)->output_conditionals) {
5605 static char end_failed[] = "#endfailed\n";
5606 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5609 pfile->only_seen_white = 1;
5611 parseGotoMark (&line_start_mark, pfile);
5612 parseClearMark (&line_start_mark);
5616 * handle a #else directive. Do this by just continuing processing
5617 * without changing if_stack ; this is so that the error message
5618 * for missing #endif's etc. will point to the original #if. It
5619 * is possible that something different would be better.
5623 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5624 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5626 if (cppReader_isPedantic (pfile))
5628 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5631 cppReader_skipRestOfLine (pfile);
5633 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack) {
5634 cppReader_errorLit (pfile,
5635 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5638 /* #ifndef can't have its special treatment for containing the whole file
5639 if it has a #else clause. */
5641 llassert (pfile->if_stack != NULL);
5643 pfile->if_stack->control_macro = 0;
5645 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5647 cpp_setLocation (pfile);
5648 genppllerrorhint (FLG_PREPROC,
5649 message ("Pre-processor directive #else after #else"),
5650 message ("%q: Location of match",
5651 fileloc_unparseRaw (pfile->if_stack->fname,
5652 pfile->if_stack->lineno)));
5655 pfile->if_stack->type = T_ELSE;
5658 if (pfile->if_stack->if_succeeded)
5659 skip_if_group (pfile, 0);
5661 ++pfile->if_stack->if_succeeded; /* continue processing input */
5662 output_line_command (pfile, 1, same_file);
5669 * unstack after #endif command
5673 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5674 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5676 if (cppReader_isPedantic (pfile))
5678 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5681 cppReader_skipRestOfLine (pfile);
5683 if (pfile->if_stack == cppReader_getBuffer (pfile)->if_stack)
5685 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5689 cppIfStackFrame *temp = pfile->if_stack;
5691 llassert (temp != NULL);
5693 pfile->if_stack = temp->next;
5694 if (temp->control_macro != 0)
5696 /* This #endif matched a #ifndef at the start of the file.
5697 See if it is at the end of the file. */
5698 struct parse_marker start_mark;
5701 parseSetMark (&start_mark, pfile);
5705 cppSkipHspace (pfile);
5706 c = cppReader_getC (pfile);
5712 parseGotoMark (&start_mark, pfile);
5713 parseClearMark (&start_mark);
5717 /* If we get here, this #endif ends a #ifndef
5718 that contains all of the file (aside from whitespace).
5719 Arrange not to include the file again
5720 if the macro that was tested is defined.
5722 Do not do this for the top-level file in a -include or any
5723 file in a -imacros. */
5724 struct file_name_list *ifile = pfile->all_include_files;
5726 for ( ; ifile != NULL; ifile = ifile->next)
5728 if (cstring_equal (ifile->fname, cppReader_getBuffer (pfile)->fname))
5730 ifile->control_macro = temp->control_macro;
5738 output_line_command (pfile, 1, same_file);
5743 /* When an #else or #endif is found while skipping failed conditional,
5744 if -pedantic was specified, this is called to warn about text after
5745 the command name. P points to the first char after the command name. */
5748 validate_else (cppReader *pfile, cstring directive)
5751 cppSkipHspace (pfile);
5752 c = cppReader_peekC (pfile);
5753 if (c != EOF && c != '\n')
5755 cppReader_pedwarn (pfile,
5756 message ("text following `%s' violates ANSI standard", directive));
5761 ** Get the next token, and add it to the text in pfile->token_buffer.
5762 ** Return the kind of token we got.
5766 cpplib_getToken (cppReader *pfile)
5768 return cpplib_getTokenAux (pfile, FALSE);
5772 cpplib_getTokenForceExpand (cppReader *pfile)
5774 return cpplib_getTokenAux (pfile, TRUE);
5778 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5781 size_t old_written = 0;
5782 int start_line, start_column;
5783 enum cpp_token token;
5784 struct cppOptions *opts = CPPOPTIONS (pfile);
5785 cppReader_getBuffer (pfile)->prev = cppReader_getBuffer (pfile)->cur;
5788 c = cppReader_getC (pfile);
5789 DPRINTF (("Get next token: %c", c));
5794 if (cppReader_getBuffer (pfile)->seen_eof)
5796 cppBuffer *buf = cppReader_popBuffer (pfile);
5798 if (buf != cppReader_nullBuffer (pfile))
5809 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5810 cppReader_getBuffer (pfile)->seen_eof = 1;
5812 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname)
5813 && next_buf != cppReader_nullBuffer (pfile))
5815 /* We're about to return from an #include file.
5816 Emit #line information now (as part of the CPP_POP) result.
5817 But the #line refers to the file we will pop to. */
5818 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5819 CPPBUFFER (pfile) = next_buf;
5820 pfile->input_stack_listing_current = 0;
5821 output_line_command (pfile, 0, leave_file);
5822 CPPBUFFER (pfile) = cur_buffer;
5830 struct parse_marker start_mark;
5835 if (cppReader_peekC (pfile) == '=')
5840 if (opts->put_out_comments)
5842 parseSetMark (&start_mark, pfile);
5846 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5847 &start_line, &start_column);
5848 c = skip_comment (pfile, &newlines);
5850 if (opts->put_out_comments && (c == '/' || c == EOF))
5852 assertSet (&start_mark);
5853 parseClearMark (&start_mark);
5860 cppReader_errorWithLine (pfile, start_line, start_column,
5861 cstring_makeLiteral ("Unterminated comment"));
5864 c = '/'; /* Initial letter of comment. */
5866 /* Comments are equivalent to spaces.
5867 For -traditional, a comment is equivalent to nothing. */
5869 if (opts->put_out_comments)
5873 assertSet (&start_mark);
5874 res = cpp_handleComment (pfile, &start_mark);
5875 pfile->lineno += newlines;
5878 else if (cppReader_isTraditional (pfile))
5884 cpplib_reserve(pfile, 1);
5885 cppReader_putCharQ (pfile, ' ');
5890 if (!pfile->only_seen_white)
5895 if (cppReader_handleDirective (pfile))
5897 return CPP_DIRECTIVE;
5900 pfile->only_seen_white = 0;
5905 /* A single quoted string is treated like a double -- some
5906 programs (e.g., troff) are perverse this way */
5907 cppBuffer_lineAndColumn (cppReader_fileBuffer (pfile),
5908 &start_line, &start_column);
5909 old_written = cpplib_getWritten (pfile);
5911 cppReader_putChar (pfile, c);
5914 int cc = cppReader_getC (pfile);
5917 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
5919 /* try harder: this string crosses a macro expansion
5920 boundary. This can happen naturally if -traditional.
5921 Otherwise, only -D can make a macro with an unmatched
5924 = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
5925 (*cppReader_getBuffer (pfile)->cleanup)
5926 (cppReader_getBuffer (pfile), pfile);
5927 CPPBUFFER (pfile) = next_buf;
5930 if (!cppReader_isTraditional (pfile))
5932 cpp_setLocation (pfile);
5934 setLine (long_toInt (start_line));
5935 setColumn (long_toInt (start_column));
5937 if (pfile->multiline_string_line != long_toInt (start_line)
5938 && pfile->multiline_string_line != 0)
5942 message ("Unterminated string or character constant"),
5943 message ("%q: Possible real start of unterminated constant",
5945 (fileloc_filename (g_currentloc),
5946 pfile->multiline_string_line)));
5947 pfile->multiline_string_line = 0;
5953 message ("Unterminated string or character constant"));
5956 /*@loopbreak@*/ break;
5958 cppReader_putChar (pfile, cc);
5962 /* Traditionally, end of line ends a string constant with
5963 no error. So exit the loop and record the new line. */
5964 if (cppReader_isTraditional (pfile))
5970 if (cppReader_isPedantic (pfile)
5971 && pfile->multiline_string_line == 0)
5973 cppReader_pedwarnWithLine
5974 (pfile, long_toInt (start_line),
5975 long_toInt (start_column),
5976 cstring_makeLiteral ("String constant runs past end of line"));
5978 if (pfile->multiline_string_line == 0)
5980 pfile->multiline_string_line = start_line;
5983 /*@switchbreak@*/ break;
5986 cc = cppReader_getC (pfile);
5989 /* Backslash newline is replaced by nothing at all. */
5990 cppReader_adjustWritten (pfile, -1);
5995 /* ANSI stupidly requires that in \\ the second \
5996 is *not* prevented from combining with a newline. */
5999 cppReader_putChar (pfile, cc);
6001 /*@switchbreak@*/ break;
6007 /*@switchbreak@*/ break;
6011 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
6012 cpplib_getPWritten (pfile));
6013 pfile->only_seen_white = 0;
6014 return c == '\'' ? CPP_CHAR : CPP_STRING;
6017 if (!opts->dollars_in_ident)
6022 if (opts->cplusplus && cppReader_peekC (pfile) == ':')
6030 c2 = cppReader_peekC (pfile);
6031 if (c2 == c || c2 == '=')
6041 if (cppReader_peekC (pfile) == '=')
6047 c2 = cppReader_peekC (pfile);
6048 if (c2 == '-' && opts->chill)
6050 /* Chill style comment */
6051 if (opts->put_out_comments)
6053 parseSetMark (&start_mark, pfile);
6056 cppReader_forward (pfile, 1); /* Skip second '-'. */
6060 c = cppReader_getC (pfile);
6062 /*@loopbreak@*/ break;
6065 /* Don't consider final '\n' to be part of comment. */
6066 cppReader_forward (pfile, -1);
6067 /*@loopbreak@*/ break;
6071 goto return_comment;
6073 if (c2 == '-' || c2 == '=' || c2 == '>')
6078 if (pfile->parsing_include_directive)
6082 cppReader_putChar (pfile, c);
6084 /*@loopbreak@*/ break;
6085 c = cppReader_getC (pfile);
6087 if (c == '\n' || c == EOF)
6089 cppReader_errorLit (pfile,
6090 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
6091 /*@loopbreak@*/ break;
6099 c2 = cppReader_peekC (pfile);
6104 cppReader_forward (pfile, 1);
6105 cpplib_reserve (pfile, 4);
6106 cppReader_putChar (pfile, c);
6107 cppReader_putChar (pfile, c2);
6109 c3 = cppReader_peekC (pfile);
6111 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6112 cppReader_nullTerminateQ (pfile);
6113 pfile->only_seen_white = 0;
6117 if (cppReader_getBuffer (pfile)->has_escapes)
6119 c = cppReader_getC (pfile);
6122 if (pfile->output_escapes)
6123 cppReader_puts (pfile, "@-", 2);
6124 parse_name (pfile, cppReader_getC (pfile));
6127 else if (is_space [c])
6129 cpplib_reserve (pfile, 2);
6130 if (pfile->output_escapes)
6131 cppReader_putCharQ (pfile, '@');
6132 cppReader_putCharQ (pfile, c);
6140 if (pfile->output_escapes)
6142 cppReader_puts (pfile, "@@", 2);
6148 c2 = cppReader_peekC (pfile);
6151 cpplib_reserve(pfile, 2);
6152 cppReader_putCharQ (pfile, '.');
6153 c = cppReader_getC (pfile);
6157 /* FIXME - misses the case "..\\\n." */
6158 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6160 cpplib_reserve(pfile, 4);
6161 cppReader_putCharQ (pfile, '.');
6162 cppReader_putCharQ (pfile, '.');
6163 cppReader_putCharQ (pfile, '.');
6164 cppReader_forward (pfile, 2);
6165 cppReader_nullTerminateQ (pfile);
6166 pfile->only_seen_white = 0;
6172 pfile->only_seen_white = 0;
6174 cpplib_reserve(pfile, 3);
6175 cppReader_putCharQ (pfile, c);
6176 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6177 cppReader_nullTerminateQ (pfile);
6182 c2 = cppReader_peekC (pfile);
6183 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6185 cppReader_putChar (pfile, c);
6186 c = cppReader_getC (pfile);
6191 case '0': case '1': case '2': case '3': case '4':
6192 case '5': case '6': case '7': case '8': case '9':
6197 cpplib_reserve (pfile, 2);
6198 cppReader_putCharQ (pfile, c);
6200 c = cppReader_peekC (pfile);
6202 /*@loopbreak@*/ break;
6203 if (!is_idchar[c] && c != '.'
6204 && ((c2 != 'e' && c2 != 'E'
6205 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6206 || (c != '+' && c != '-')))
6207 /*@loopbreak@*/ break;
6208 cppReader_forward (pfile, 1);
6212 cppReader_nullTerminateQ (pfile);
6213 pfile->only_seen_white = 0;
6216 case 'b': case 'c': case 'd': case 'h': case 'o':
6217 case 'B': case 'C': case 'D': case 'H': case 'O':
6218 if (opts->chill && cppReader_peekC (pfile) == '\'')
6220 pfile->only_seen_white = 0;
6221 cpplib_reserve (pfile, 2);
6222 cppReader_putCharQ (pfile, c);
6223 cppReader_putCharQ (pfile, '\'');
6224 cppReader_forward (pfile, 1);
6227 c = cppReader_getC (pfile);
6229 goto chill_number_eof;
6232 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6234 cppReader_forward (pfile, 2);
6237 /*@loopbreak@*/ break;
6239 cppReader_putChar (pfile, c);
6243 cpplib_reserve (pfile, 2);
6244 cppReader_putCharQ (pfile, c);
6245 cppReader_nullTerminateQ (pfile);
6250 cppReader_forward (pfile, -1);
6252 cppReader_nullTerminate (pfile);
6259 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
6260 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
6261 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
6262 case 'x': case 'y': case 'z':
6263 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
6264 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
6265 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6271 size_t before_name_written = cpplib_getWritten (pfile);
6273 parse_name (pfile, c);
6274 pfile->only_seen_white = 0;
6276 if (pfile->no_macro_expand)
6278 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6282 ident = pfile->token_buffer + before_name_written;
6283 DPRINTF (("Ident: %s", ident));
6285 ident_len = (cpplib_getPWritten (pfile)) - ident;
6287 hp = cpphash_lookupExpand (ident, ident_len, -1, forceExpand);
6291 DPRINTF (("No expand: %s %d", ident, ident_len));
6295 if (hp->type == T_DISABLED)
6297 DPRINTF (("Disabled!"));
6299 if (pfile->output_escapes)
6300 { /* Return "@-IDENT", followed by '\0'. */
6302 cpplib_reserve (pfile, 3);
6303 ident = pfile->token_buffer + before_name_written;
6304 cppReader_adjustWritten (pfile, 2);
6306 for (i = ident_len; i >= 0; i--)
6308 ident[i+2] = ident[i];
6318 ** If macro wants an arglist, verify that a '(' follows.
6319 ** first skip all whitespace, copying it to the output
6320 ** after the macro name. Then, if there is no '(',
6321 ** decide this is not a macro call and leave things that way.
6324 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6326 struct parse_marker macro_mark;
6329 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6331 cppBuffer *next_buf;
6332 cppSkipHspace (pfile);
6333 if (cppReader_peekC (pfile) != EOF)
6335 /*@loopbreak@*/ break;
6338 next_buf = cppBuffer_prevBuffer (cppReader_getBuffer (pfile));
6339 (*cppReader_getBuffer (pfile)->cleanup) (cppReader_getBuffer (pfile), pfile);
6340 CPPBUFFER (pfile) = next_buf;
6343 parseSetMark (¯o_mark, pfile);
6347 cppSkipHspace (pfile);
6348 c = cppReader_peekC (pfile);
6349 is_macro_call = c == '(';
6351 /*@loopbreak@*/ break;
6352 cppReader_forward (pfile, 1);
6357 parseGotoMark (¯o_mark, pfile);
6360 parseClearMark (¯o_mark);
6367 /* This is now known to be a macro call. */
6369 /* it might not actually be a macro. */
6370 if (hp->type != T_MACRO)
6375 cppReader_setWritten (pfile, before_name_written);
6376 special_symbol (hp, pfile);
6377 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6378 xbuf = (char *) dmalloc (xbuf_len + 1);
6379 cppReader_setWritten (pfile, before_name_written);
6380 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6381 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6386 ** Expand the macro, reading arguments as needed,
6387 ** and push the expansion on the input stack.
6390 cpplib_macroExpand (pfile, hp);
6391 cppReader_setWritten (pfile, before_name_written);
6394 /* An extra "@ " is added to the end of a macro expansion
6395 to prevent accidental token pasting. We prefer to avoid
6396 unneeded extra spaces (for the sake of cpp-using tools like
6397 imake). Here we remove the space if it is safe to do so. */
6399 llassert (pfile->buffer->rlimit != NULL);
6401 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6402 && pfile->buffer->rlimit[-2] == '@'
6403 && pfile->buffer->rlimit[-1] == ' ')
6405 int c1 = pfile->buffer->rlimit[-3];
6406 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6408 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6409 pfile->buffer->rlimit -= 2;
6415 case ' ': case '\t': case '\v': case '\r':
6418 cppReader_putChar (pfile, c);
6419 c = cppReader_peekC (pfile);
6420 if (c == EOF || !is_hor_space[c])
6421 /*@loopbreak@*/ break;
6422 cppReader_forward (pfile, 1);
6427 c2 = cppReader_peekC (pfile);
6434 cppReader_putChar (pfile, c);
6435 if (pfile->only_seen_white == 0)
6436 pfile->only_seen_white = 1;
6438 output_line_command (pfile, 1, same_file);
6441 case '(': token = CPP_LPAREN; goto char1;
6442 case ')': token = CPP_RPAREN; goto char1;
6443 case '{': token = CPP_LBRACE; goto char1;
6444 case '}': token = CPP_RBRACE; goto char1;
6445 case ',': token = CPP_COMMA; goto char1;
6446 case ';': token = CPP_SEMICOLON; goto char1;
6452 pfile->only_seen_white = 0;
6453 cppReader_putChar (pfile, c);
6462 /* Parse an identifier starting with C. */
6465 parse_name (cppReader *pfile, int c)
6471 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6473 cppReader_forward (pfile, 2);
6477 cppReader_forward (pfile, -1);
6481 if (c == '$' && cppReader_isPedantic (pfile))
6483 cppReader_pedwarnLit (pfile,
6484 cstring_makeLiteralTemp ("`$' in identifier"));
6487 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6488 cppReader_putCharQ (pfile, c);
6489 c = cppReader_getC (pfile);
6495 cppReader_nullTerminateQ (pfile);
6498 /* The file_name_map structure holds a mapping of file names for a
6499 particular directory. This mapping is read from the file named
6500 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6501 map filenames on a file system with severe filename restrictions,
6502 such as DOS. The format of the file name map file is just a series
6503 of lines with two tokens on each line. The first token is the name
6504 to map, and the second token is the actual name to use. */
6506 struct file_name_map
6508 struct file_name_map *map_next;
6513 /*@constant observer char *FILE_NAME_MAP_FILE*/
6514 #define FILE_NAME_MAP_FILE "header.gcc"
6516 /* Read a space delimited string of unlimited length from a stdio
6519 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6525 set = alloc = dmalloc (len + 1);
6530 while ((ch = getc (f)) != EOF && ! is_space[ch])
6532 if (set - alloc == size_toInt (len))
6535 alloc = drealloc (alloc, len + 1);
6536 set = alloc + len / 2;
6537 /*@-branchstate@*/ }
6540 } /*@=branchstate@*/
6543 check (ungetc (ch, f) != EOF);
6545 return cstring_fromChars (alloc);
6548 /* This structure holds a linked list of file name maps, one per directory. */
6550 struct file_name_map_list
6552 struct file_name_map_list *map_list_next;
6553 cstring map_list_name;
6554 struct file_name_map *map_list_map;
6557 /* Read the file name map file for DIRNAME. */
6559 static struct file_name_map *
6560 read_name_map (cppReader *pfile, cstring dirname)
6562 struct file_name_map_list *map_list_ptr;
6566 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6567 map_list_ptr != NULL;
6568 map_list_ptr = map_list_ptr->map_list_next)
6570 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6572 return map_list_ptr->map_list_map;
6576 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6577 map_list_ptr->map_list_name = cstring_copy (dirname);
6578 map_list_ptr->map_list_map = NULL;
6580 name = cstring_copy (dirname);
6582 if (cstring_length (dirname) > 0)
6584 name = cstring_appendChar (name, CONNECTCHAR);
6587 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6589 f = fileTable_openFile (context_fileTable (), name, "r");
6590 cstring_free (name);
6594 map_list_ptr->map_list_map = NULL;
6600 while ((ch = getc (f)) != EOF)
6603 struct file_name_map *ptr;
6610 from = read_filename_string (ch, f);
6611 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6616 to = read_filename_string (ch, f);
6618 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6619 ptr->map_from = from;
6621 /* Make the real filename absolute. */
6622 if (cstring_length (to) > 1
6623 && osd_isConnectChar (cstring_firstChar (to)))
6629 ptr->map_to = cstring_copy (dirname);
6630 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6631 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6634 ptr->map_next = map_list_ptr->map_list_map;
6635 map_list_ptr->map_list_map = ptr;
6637 while ((ch = getc (f)) != '\n')
6641 /*@innerbreak@*/ break;
6646 assertSet (map_list_ptr->map_list_map);
6647 check (fileTable_closeFile (context_fileTable (),f) == 0);
6650 map_list_ptr->map_list_next = pfile->opts->map_list;
6651 pfile->opts->map_list = map_list_ptr;
6653 return map_list_ptr->map_list_map;
6656 /* Try to open include file FILENAME. SEARCHPTR is the directory
6657 being tried from the include file search path. This function maps
6658 filenames on file systems based on information read by
6662 open_include_file (cppReader *pfile,
6664 struct file_name_list *searchptr)
6666 char *filename = cstring_toCharsSafe (fname);
6667 struct file_name_map *map;
6671 cstring_markOwned (fname);
6673 cpp_setLocation (pfile);
6675 if (context_getFlag (FLG_NEVERINCLUDE))
6677 if (isHeaderFile (fname))
6679 return SKIP_INCLUDE;
6683 if ((searchptr != NULL) && ! searchptr->got_name_map)
6685 searchptr->name_map = read_name_map (pfile,
6686 !cstring_isEmpty (searchptr->fname)
6687 ? searchptr->fname :
6688 cstring_makeLiteralTemp ("."));
6689 searchptr->got_name_map = 1;
6692 /* First check the mapping for the directory we are using. */
6694 if ((searchptr != NULL)
6695 && (searchptr->name_map != NULL))
6699 if (!cstring_isEmpty (searchptr->fname))
6701 from += cstring_length (searchptr->fname) + 1;
6704 for (map = searchptr->name_map;
6706 map = map->map_next)
6708 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6711 ** Found a match. Check if the file should be skipped
6714 if (cpp_skipIncludeFile (map->map_to))
6716 return SKIP_INCLUDE;
6720 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6727 ** Try to find a mapping file for the particular directory we are
6728 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6729 ** in /usr/include/header.gcc and look up types.h in
6730 ** /usr/include/sys/header.gcc.
6733 p = strrchr (filename, CONNECTCHAR);
6740 if ((searchptr != NULL)
6741 && (cstring_isDefined (searchptr->fname))
6742 && (cstring_length (searchptr->fname) == p - filename)
6743 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6745 size_fromInt (p - filename)))
6747 /* filename is in SEARCHPTR, which we've already checked. */
6749 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6751 return SKIP_INCLUDE;
6755 return cpp_openIncludeFile (filename);
6761 dir = mstring_copy (".");
6766 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6767 memcpy (dir, filename, size_fromInt (p - filename));
6768 dir[p - filename] = '\0';
6772 for (map = read_name_map (pfile, cstring_fromChars (dir));
6774 map = map->map_next)
6776 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6780 if (cpp_skipIncludeFile (map->map_to))
6782 return SKIP_INCLUDE;
6786 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6793 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6795 return SKIP_INCLUDE;
6799 return cpp_openIncludeFile (filename);
6803 /* Process the contents of include file FNAME, already open on descriptor F,
6805 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6806 "system" include directories (as decided by the `is_system_include'
6808 DIRPTR is the link in the dir path through which this file was found,
6809 or 0 if the file name was absolute or via the current directory.
6810 Return 1 on success, 0 on failure.
6812 The caller is responsible for the cppReader_pushBuffer. */
6815 finclude (cppReader *pfile, int f,
6817 bool system_header_p,
6818 /*@dependent@*/ struct file_name_list *dirptr)
6824 cppBuffer *fp; /* For input stack frame */
6826 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
6828 cppReader_perrorWithName (pfile, fname);
6829 check (close (f) == 0);
6830 (void) cppReader_popBuffer (pfile);
6836 fp = cppReader_getBuffer (pfile);
6838 /*@-temptrans@*/ /* fname shouldn't really be temp */
6839 fp->nominal_fname = fp->fname = fname;
6843 fp->system_header_p = system_header_p;
6846 fp->cleanup = cppReader_fileCleanup;
6848 if (S_ISREG (st_mode))
6851 fp->buf = (char *) dmalloc (st_size + 2);
6852 fp->alimit = fp->buf + st_size + 2;
6855 /* Read the file contents, knowing that st_size is an upper bound
6856 on the number of bytes we can read. */
6857 length = safe_read (f, fp->buf, size_toInt (st_size));
6858 fp->rlimit = fp->buf + length;
6859 if (length < 0) goto nope;
6861 else if (S_ISDIR (st_mode))
6863 cppReader_error (pfile,
6864 message ("Directory specified in #include: %s", fname));
6865 check (close (f) == 0);
6871 ** Cannot count its file size before reading.
6872 ** First read the entire file into heap and
6873 ** copy them into buffer on stack.
6876 size_t bsize = 2000;
6881 fp->buf = (char *) dmalloc (bsize + 2);
6884 i = safe_read (f, fp->buf + st_size, size_toInt (bsize - st_size));
6887 goto nope; /* error! */
6890 if (st_size != bsize)
6892 break; /* End of file */
6896 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
6900 length = size_toInt (st_size);
6903 if ((length > 0 && fp->buf[length - 1] != '\n')
6904 /* Backslash-newline at end is not good enough. */
6905 || (length > 1 && fp->buf[length - 2] == '\\')) {
6906 fp->buf[length++] = '\n';
6909 fp->buf[length] = '\0';
6910 fp->rlimit = fp->buf + length;
6912 /* Close descriptor now, so nesting does not use lots of descriptors. */
6913 check (close (f) == 0);
6915 /* Must do this before calling trigraph_pcp, so that the correct file name
6916 will be printed in warning messages. */
6918 pfile->input_stack_listing_current = 0;
6923 cppReader_perrorWithName (pfile, fname);
6924 check (close (f) == 0);
6930 cpplib_init (cppReader *pfile)
6932 memset ((char *) pfile, 0, sizeof (*pfile));
6934 pfile->get_token = cpplib_getToken;
6935 pfile->token_buffer_size = 200;
6936 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
6937 pfile->all_include_files = NULL;
6941 cppReader_setWritten (pfile, 0);
6943 pfile->system_include_depth = 0;
6944 pfile->max_include_len = 0;
6945 pfile->timebuf = NULL;
6946 pfile->only_seen_white = 1;
6948 pfile->buffer = cppReader_nullBuffer (pfile);
6952 cppReader_finish (/*@unused@*/ cppReader *pfile)
6957 /* Free resources used by PFILE.
6958 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
6961 cppCleanup (cppReader *pfile)
6963 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
6965 (void) cppReader_popBuffer (pfile);
6968 if (pfile->token_buffer != NULL)
6970 sfree (pfile->token_buffer);
6971 pfile->token_buffer = NULL;
6974 while (pfile->if_stack != NULL)
6976 cppIfStackFrame *temp = pfile->if_stack;
6977 pfile->if_stack = temp->next;
6981 while (pfile->all_include_files != NULL)
6983 struct file_name_list *temp = pfile->all_include_files;
6984 pfile->all_include_files = temp->next;
6985 /*@-dependenttrans@*/
6986 cstring_free (temp->fname);
6987 /*@=dependenttrans@*/
6991 cppReader_hashCleanup ();
6995 ** Get the file-mode and data size of the file open on FD
6996 ** and store them in *MODE_POINTER and *SIZE_POINTER.
7000 file_size_and_mode (int fd, mode_t *mode_pointer, size_t *size_pointer)
7004 if (fstat (fd, &sbuf) < 0) {
7010 if (mode_pointer != NULL)
7012 *mode_pointer = sbuf.st_mode;
7015 if (size_pointer != NULL)
7017 *size_pointer = (size_t) sbuf.st_size;
7023 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7024 retrying if necessary. Return a negative value if an error occurs,
7025 otherwise return the actual number of bytes read,
7026 which must be LEN unless end-of-file was reached. */
7028 static int safe_read (int desc, char *ptr, int len)
7034 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
7035 /*@-compdef@*/ /* ptr is an out parameter */
7036 int nchars = _read (desc, ptr, (unsigned) left);
7039 ssize_t nchars = read (desc, ptr, size_fromInt (left));
7048 return (int) nchars;
7062 /* Initialize PMARK to remember the current position of PFILE. */
7065 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
7067 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7069 pmark->next = pbuf->marks;
7071 pbuf->marks = pmark;
7075 pmark->position = pbuf->cur - pbuf->buf;
7078 /* Cleanup PMARK - we no longer need it. */
7080 void parseClearMark (struct parse_marker *pmark)
7082 struct parse_marker **pp = &pmark->buf->marks;
7084 for (; ; pp = &(*pp)->next)
7086 llassert (*pp != NULL);
7087 if (*pp == pmark) break;
7093 /* Backup the current position of PFILE to that saved in PMARK. */
7096 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
7098 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7100 if (pbuf != pmark->buf)
7102 cpp_setLocation (pfile);
7103 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
7106 llassert (pbuf->buf != NULL);
7107 pbuf->cur = pbuf->buf + pmark->position;
7110 /* Reset PMARK to point to the current position of PFILE. (Same
7111 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
7114 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
7116 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7118 if (pbuf != pmark->buf)
7120 cpp_setLocation (pfile);
7121 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
7124 pmark->position = pbuf->cur - pbuf->buf;
7127 void cpplib_initializeReader (cppReader *pfile) /* Must be done after library is loaded. */
7129 struct cppOptions *opts = CPPOPTIONS (pfile);
7132 /* The code looks at the defaults through this pointer, rather than through
7133 the constant structure above. This pointer gets changed if an environment
7134 variable specifies other defaults. */
7136 struct default_include *include_defaults = include_defaults_array;
7138 /* Add dirs from INCLUDEPATH_VAR after dirs from -I. */
7139 /* There seems to be confusion about what CPATH should do,
7140 so for the moment it is not documented. */
7141 /* Some people say that CPATH should replace the standard include dirs,
7142 but that seems pointless: it comes before them, so it overrides them
7145 xp = osd_getEnvironmentVariable (INCLUDEPATH_VAR);
7147 if (cstring_isDefined (xp) && !opts->no_standard_includes)
7149 path_include (pfile, cstring_toCharsSafe (xp));
7152 /* Now that dollars_in_ident is known, initialize is_idchar. */
7153 initialize_char_syntax (opts);
7155 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
7156 and option processing. */
7158 initialize_builtins (pfile);
7160 /* Do standard #defines and assertions
7161 that identify system and machine type. */
7163 if (!opts->inhibit_predefs) {
7164 char *p = (char *) dmalloc (strlen (predefs) + 1);
7165 strcpy (p, predefs);
7171 while (*p == ' ' || *p == '\t')
7176 /* Handle -D options. */
7177 if (p[0] == '-' && p[1] == 'D')
7181 while (*p && *p != ' ' && *p != '\t')
7191 if (opts->debug_output)
7193 output_line_command (pfile, 0, same_file);
7196 cppReader_define (pfile, q);
7198 while (*p == ' ' || *p == '\t')
7212 opts->done_initializing = 1;
7214 { /* Read the appropriate environment variable and if it exists
7215 replace include_defaults with the listed path. */
7219 int win32_buf_size = 0; /* memory we need to allocate */
7222 if (opts->cplusplus)
7224 epath = getenv ("CPLUS_INCLUDE_PATH");
7228 epath = getenv ("C_INCLUDE_PATH");
7232 ** If the environment var for this language is set,
7233 ** add to the default list of include directories.
7236 if (epath != NULL) {
7237 char *nstore = (char *) dmalloc (strlen (epath) + 2);
7239 char *startp, *endp;
7242 /* if we have a posix path list, convert to win32 path list */
7243 if (cygwin32_posix_path_list_p (epath))
7245 win32_buf_size = cygwin32_posix_to_win32_path_list_buf_size (epath);
7246 win32epath = (char *) dmalloc /*@i4@*/ (win32_buf_size);
7247 cygwin32_posix_to_win32_path_list (epath, win32epath);
7251 for (num_dirs = 1, startp = epath; *startp; startp++)
7253 if (*startp == PATH_SEPARATOR)
7259 = (struct default_include *) dmalloc ((num_dirs
7260 * sizeof (struct default_include))
7261 + sizeof (include_defaults_array));
7264 startp = endp = epath;
7267 /* Handle cases like c:/usr/lib:d:/gcc/lib */
7268 if ((*endp == PATH_SEPARATOR) || *endp == 0)
7270 strncpy (nstore, startp, size_fromInt (endp - startp));
7273 strcpy (nstore, ".");
7277 nstore[endp-startp] = '\0';
7280 include_defaults[num_dirs].fname = cstring_fromCharsNew (nstore);
7281 include_defaults[num_dirs].cplusplus = opts->cplusplus;
7282 include_defaults[num_dirs].cxx_aware = 1;
7289 endp = startp = endp + 1;
7296 /* Put the usual defaults back in at the end. */
7297 memcpy ((char *) &include_defaults[num_dirs],
7298 (char *) include_defaults_array,
7299 sizeof (include_defaults_array));
7302 /*@-branchstate@*/ } /*@=branchstate@*/
7305 cppReader_appendIncludeChain (pfile, opts->before_system,
7306 opts->last_before_system);
7307 opts->first_system_include = opts->before_system;
7309 /* Unless -fnostdinc,
7310 tack on the standard include file dirs to the specified list */
7311 if (!opts->no_standard_includes) {
7312 struct default_include *p = include_defaults;
7313 char *specd_prefix = opts->include_prefix;
7314 char *default_prefix = mstring_copy (GCC_INCLUDE_DIR);
7315 int default_len = 0;
7317 /* Remove the `include' from /usr/local/lib/gcc.../include. */
7318 if (default_prefix != NULL) {
7319 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
7320 default_len = strlen (default_prefix) - 7;
7321 default_prefix[default_len] = 0;
7325 /* Search "translated" versions of GNU directories.
7326 These have /usr/local/lib/gcc... replaced by specd_prefix. */
7327 if (specd_prefix != 0 && default_len != 0)
7328 for (p = include_defaults; p->fname != NULL; p++) {
7329 /* Some standard dirs are only for C++. */
7331 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
7332 /* Does this dir start with the prefix? */
7333 if (!strncmp (cstring_toCharsSafe (p->fname), default_prefix,
7334 size_fromInt (default_len)))
7336 /* Yes; change prefix and add to search list. */
7337 struct file_name_list *nlist
7338 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7339 size_t this_len = strlen (specd_prefix) + cstring_length (p->fname) - default_len;
7340 char *str = (char *) dmalloc (this_len + 1);
7341 strcpy (str, specd_prefix);
7342 strcat (str, cstring_toCharsSafe (p->fname) + default_len);
7345 nlist->fname = cstring_fromChars (str);
7346 nlist->control_macro = 0;
7347 nlist->c_system_include_path = !p->cxx_aware;
7348 nlist->got_name_map = 0;
7350 cppReader_addIncludeChain (pfile, nlist);
7351 if (opts->first_system_include == 0)
7353 opts->first_system_include = nlist;
7359 /* Search ordinary names for GNU include directories. */
7361 for (p = include_defaults; p->fname != NULL; p++)
7363 /* Some standard dirs are only for C++. */
7365 || (opts->cplusplus && !opts->no_standard_cplusplus_includes))
7367 struct file_name_list *nlist
7368 = (struct file_name_list *) dmalloc (sizeof (*nlist));
7369 nlist->control_macro = 0;
7370 nlist->c_system_include_path = !p->cxx_aware;
7371 nlist->fname = p->fname;
7372 nlist->got_name_map = 0;
7375 cppReader_addIncludeChain (pfile, nlist);
7377 if (opts->first_system_include == 0)
7379 opts->first_system_include = nlist;
7383 sfree (default_prefix);
7386 /* Tack the after_include chain at the end of the include chain. */
7387 cppReader_appendIncludeChain (pfile, opts->after_include,
7388 opts->last_after_include);
7390 if (opts->first_system_include == 0)
7392 opts->first_system_include = opts->after_include;
7395 /* With -v, print the list of dirs to search. */
7396 if (opts->verbose) {
7397 struct file_name_list *p;
7398 fprintf (stderr, "#include \"...\" search starts here:\n");
7400 for (p = opts->include; p != NULL; p = p->next) {
7401 if (p == opts->first_bracket_include)
7402 fprintf (stderr, "#include <...> search starts here:\n");
7404 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7406 fprintf (stderr, "End of search list.\n");
7410 int cppReader_startProcess (cppReader *pfile, cstring fname)
7414 struct cppOptions *opts = CPPOPTIONS (pfile);
7416 fp = cppReader_pushBuffer (pfile, NULL, 0);
7423 if (opts->in_fname == NULL)
7425 opts->in_fname = cstring_makeLiteralTemp ("");
7428 fp->fname = opts->in_fname;
7429 fp->nominal_fname = fp->fname;
7432 /* Copy the entire contents of the main input file into
7433 the stacked input buffer previously allocated for it. */
7435 if (cstring_isEmpty (fname))
7437 fname = cstring_makeLiteralTemp ("");
7440 else if ((f = open (cstring_toCharsSafe (fname), O_RDONLY, 0666)) < 0)
7442 cppReader_error (pfile,
7443 message ("Error opening %s for reading: %s",
7444 fname, lldecodeerror (errno)));
7453 if (finclude (pfile, f, fname, 0, NULL))
7455 output_line_command (pfile, 0, same_file);
7461 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7463 return pfile->buffer;
7466 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7468 llassert (pfile->buffer != NULL);
7469 return pfile->buffer;
7472 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7474 llassert (buf->buf != NULL);
7475 return (buf->buf + buf->line_base);
7478 int cpplib_bufPeek (cppBuffer *buf)
7480 if (buf->cur == NULL || buf->rlimit == NULL) {
7484 if (buf->cur < buf->rlimit) {
7491 bool cppBuffer_isMacro (cppBuffer *buf)
7495 return (buf->cleanup == cppReader_macroCleanup);
7502 ** Returns true if the macro should be checked, false
7503 ** if it should be expanded normally.
7506 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7507 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7508 static bool expectiter = FALSE; /* preceeded by @iter@ */
7509 static bool expectenditer = FALSE; /* second after @iter@ */
7510 static bool expectfunction = FALSE; /* preceeded by @function@ */
7511 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7512 static bool expectmacro = FALSE; /* preceeded by notfunction or notparseable */
7514 static void cpp_setLocation (cppReader *pfile)
7519 if (pfile->buffer != NULL)
7521 if (cstring_isDefined (cppReader_getBuffer (pfile)->nominal_fname))
7523 cstring fname = cppReader_getBuffer (pfile)->nominal_fname;
7525 DPRINTF (("Looking up: %s", fname));
7527 if (fileTable_exists (context_fileTable (), fname))
7529 fid = fileTable_lookup (context_fileTable (), fname);
7533 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7535 fid = fileTable_lookup (context_fileTable (),
7536 cppReader_getBuffer (pfile)->fname);
7541 fid = fileTable_lookup (context_fileTable (),
7542 cppReader_getBuffer (pfile)->fname);
7545 line = cppReader_getBuffer (pfile)->lineno;
7546 fileloc_free (g_currentloc);
7548 if (fileId_isValid (fid))
7550 g_currentloc = fileloc_create (fid, line, 1);
7554 g_currentloc = fileloc_createBuiltin ();
7559 fileloc_free (g_currentloc);
7560 g_currentloc = fileloc_createBuiltin ();
7564 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7566 bool checkmacro = FALSE;
7567 bool hasParams = FALSE;
7568 bool noexpand = FALSE;
7572 cpp_setLocation (pfile);
7574 DPRINTF (("Should check macro? %s", p));
7576 if (expectiter || expectconstant || expectenditer)
7581 expectenditer = TRUE;
7586 expectconstant = FALSE;
7587 expectenditer = FALSE;
7590 if (notfunction || notparseable)
7592 notfunction = FALSE;
7593 notparseable = FALSE;
7602 llassert (*p == '#');
7605 while (*p == ' ' || *p == '\t')
7610 llassert (*p == 'd'); /* define starts */
7614 while (*p == ' ' || *p == '\t')
7619 sname = cstring_fromChars (p);
7620 DPRINTF (("Check macro: %s", sname));
7622 while (((c = *p) != ' ')
7623 && c != '\0' && c != '('
7624 && c != '\t' && c != '\\' && c != '\n'
7630 hasParams = (c == '(');
7635 notparseable = FALSE;
7637 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7639 DPRINTF (("Clear notfunction"));
7640 notfunction = FALSE;
7655 if (usymtab_existsReal (sname))
7657 uentry ue = usymtab_lookup (sname);
7659 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7661 if (fileloc_isPreproc (uentry_whereLast (ue)))
7667 if (uentry_isSpecified (ue))
7669 checkmacro = context_getFlag (FLG_SPECMACROS);
7675 checkmacro = context_getFlag (FLG_LIBMACROS)
7676 || context_getFlag (FLG_FCNMACROS);
7684 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7686 if (fileloc_isSystemFile (g_currentloc)
7687 && context_getFlag (FLG_SYSTEMDIREXPAND))
7689 ; /* don't check this macro */
7690 DPRINTF (("Don't check 1"));
7698 DPRINTF (("Has params..."));
7700 if (context_getFlag (FLG_FCNMACROS))
7702 if (usymtab_exists (sname))
7705 ** only get here is macro is redefined
7706 ** error reported elsewhere
7709 DPRINTF (("It exists!"));
7714 ** We make it a forward function, since it might be declared elsewhere.
7715 ** After all headers have been processed, we should check the forward
7719 fileloc loc = fileloc_makePreproc (g_currentloc);
7721 /* the line is off-by-one, since the newline was already read */
7726 expectfunction = FALSE;
7729 le = uentry_makeForwardFunction (sname,
7730 typeId_invalid, loc);
7736 /* Do not define here! */
7738 (void) usymtab_addEntry (le);
7742 DPRINTF (("Check: TRUE"));
7746 DPRINTF (("Flag FCN_MACROS not set!"));
7751 DPRINTF (("No params"));
7753 if (context_getFlag (FLG_CONSTMACROS))
7755 bool nocontent = FALSE;
7768 ** Check if there is nothing after the define.
7771 while ((*rest) != '\0' && isspace (*rest))
7778 nocontent = TRUE; /* empty macro, don't check */
7783 if (usymtab_exists (sname))
7789 fileloc loc = fileloc_makePreproc (g_currentloc);
7790 DPRINTF (("Make constant: %s", sname));
7791 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
7792 (void) usymtab_addEntry (le);
7795 checkmacro = !nocontent;
7800 if (checkmacro && usymtab_existsType (sname))
7802 DPRINTF (("Making false..."));
7804 ppllerror (message ("Specified type implemented as macro: %s", sname));
7814 if (usymtab_exists (sname))
7816 uentry ue = usymtab_lookupExpose (sname);
7817 fileloc tloc = fileloc_makePreproc (g_currentloc);
7819 uentry_setDefined (ue, tloc);
7820 fileloc_free (tloc);
7821 uentry_setUsed (ue, fileloc_undefined);
7825 fileloc tloc = fileloc_makePreproc (g_currentloc);
7826 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7827 DPRINTF (("Make expanded macro: %s", sname));
7828 DPRINTF (("Not in symbol table: %s", sname));
7830 (void) usymtab_addGlobalEntry (ue);
7831 fileloc_free (tloc);
7836 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7840 static enum cpp_token
7841 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7843 cppBuffer *pbuf = cppReader_getBuffer (pfile);
7846 bool eliminateComment = FALSE;
7848 llassert (pbuf->buf != NULL);
7850 start = pbuf->buf + smark->position;
7852 llassert (pbuf->cur != NULL);
7853 len = pbuf->cur - start;
7856 && start[1] == context_getCommentMarkerChar ())
7860 char *scomment = start + 2;
7861 char savec = start[len];
7863 start[0] = BEFORE_COMMENT_MARKER[0];
7864 start[1] = BEFORE_COMMENT_MARKER[1];
7866 llassert (start[len - 2] == '*');
7867 start[len - 2] = AFTER_COMMENT_MARKER[0];
7869 llassert (start[len - 1] == '/');
7870 start[len - 1] = AFTER_COMMENT_MARKER[1];
7872 cpplib_reserve(pfile, size_fromInt (1 + len));
7873 cppReader_putCharQ (pfile, c);
7875 cpp_setLocation (pfile);
7879 if (mstring_containsString (scomment, "/*"))
7881 (void) cppoptgenerror
7883 message ("Comment starts inside syntactic comment: %s",
7884 cstring_fromChars (scomment)),
7890 if (mstring_equalPrefix (scomment, "ignore"))
7892 if (!context_getFlag (FLG_NOCOMMENTS))
7894 context_enterSuppressRegion ();
7897 else if (mstring_equalPrefix (scomment, "end"))
7899 if (!context_getFlag (FLG_NOCOMMENTS))
7901 context_exitSuppressRegion ();
7904 else if (mstring_equalPrefix (scomment, "notparseable"))
7906 notparseable = TRUE;
7908 eliminateComment = TRUE;
7910 else if (mstring_equalPrefix (scomment, "notfunction"))
7914 eliminateComment = TRUE;
7916 else if (mstring_equalPrefix (scomment, "iter"))
7920 else if (mstring_equalPrefix (scomment, "function"))
7922 expectfunction = TRUE;
7924 else if (mstring_equalPrefix (scomment, "constant"))
7926 expectconstant = TRUE;
7930 char sChar = *scomment;
7936 char *rest = scomment + 1;
7938 if (mstring_equalPrefix (rest, "commentchar"))
7940 eliminateComment = TRUE;
7944 ppllerror (cstring_makeLiteral
7945 ("Cannot restore commentchar"));
7949 char *next = scomment + 12; /* strlen commentchar = 12 */
7951 if (*next != ' ' && *next != '\t' && *next != '\n')
7955 ("Syntactic commentchar comment is not followed by a "
7956 "whitespace character: %c",
7961 char cchar = *(next + 1);
7966 (cstring_makeLiteral
7967 ("Cannot set commentchar to NUL"));
7971 context_setCommentMarkerChar (cchar);
7972 /* setComment = TRUE; */
7977 else if (mstring_equalPrefix (scomment, "nestcomment"))
7979 /* fix from Mike Miller <MikeM@xata.com> */
7980 context_fileSetFlag (FLG_NESTCOMMENT,
7981 ynm_fromCodeChar (sChar));
7983 else if (mstring_equalPrefix (rest, "namechecks"))
7985 context_fileSetFlag (FLG_NAMECHECKS,
7986 ynm_fromCodeChar (sChar));
7988 else if (mstring_equalPrefix (rest, "macroredef"))
7990 context_fileSetFlag (FLG_MACROREDEF,
7991 ynm_fromCodeChar (sChar));
7993 else if (mstring_equalPrefix (rest, "usevarargs"))
7995 context_fileSetFlag (FLG_USEVARARGS,
7996 ynm_fromCodeChar (sChar));
7998 else if (mstring_equalPrefix (rest, "nextlinemacros"))
8000 context_fileSetFlag (FLG_MACRONEXTLINE,
8001 ynm_fromCodeChar (sChar));
8003 else if (mstring_equalPrefix (rest, "allmacros")
8004 || mstring_equalPrefix (rest, "fcnmacros")
8005 || mstring_equalPrefix (rest, "constmacros"))
8009 if (mstring_equalPrefix (rest, "allmacros"))
8013 else if (mstring_equalPrefix (rest, "fcnmacros"))
8019 llassert (mstring_equalPrefix (rest, "constmacros"));
8020 fl = FLG_CONSTMACROS;
8024 context_fileSetFlag (fl, ynm_fromCodeChar (sChar));
8025 notfunction = FALSE;
8038 if (eliminateComment)
8043 /* Replaces comment char's in start with spaces */
8045 for (i = 2; i < len - 2; i++)
8047 if (start[i] == BEFORE_COMMENT_MARKER[0]
8048 || start[i] == BEFORE_COMMENT_MARKER[1]
8049 || start[i] == context_getCommentMarkerChar ())
8055 cppReader_putStrN (pfile, start, size_fromInt (len));
8056 parseClearMark (smark);
8066 ** Output the comment as all spaces so line/column
8067 ** in output file is still correct.
8071 cstring lintcomment = cstring_undefined;
8073 if (context_getFlag (FLG_LINTCOMMENTS))
8075 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
8077 lintcomment = cstring_makeLiteralTemp ("l_notreach");
8079 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
8081 lintcomment = cstring_makeLiteralTemp ("l_printfli");
8083 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
8085 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
8087 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
8089 lintcomment = cstring_makeLiteralTemp ("l_argsus");
8091 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
8093 lintcomment = cstring_makeLiteralTemp ("l_fallth");
8097 lintcomment = cstring_undefined;
8102 lintcomment = cstring_undefined;
8105 if (cstring_isDefined (lintcomment))
8107 c = BEFORE_COMMENT_MARKER[0];
8108 start[0] = BEFORE_COMMENT_MARKER[1];
8110 llassert (cstring_length (lintcomment) == len - 3);
8112 for (i = 1; i < len - 2; i++)
8114 start[i] = cstring_getChar (lintcomment, i);
8117 start[len - 2] = AFTER_COMMENT_MARKER[0];
8118 start[len - 1] = AFTER_COMMENT_MARKER[1];
8122 /* Replaces char's in start with spaces */
8123 for (i = 0; i < len; i++)
8127 && start[i + 1] == '*') {
8128 (void) cppoptgenerror (FLG_NESTCOMMENT,
8129 message ("Comment starts inside comment"),
8133 if (start[i] != '\n')
8140 cpplib_reserve (pfile, size_fromInt (1 + len));
8141 cppReader_putCharQ (pfile, c);
8142 cppReader_putStrN (pfile, start, size_fromInt (len));
8143 parseClearMark (smark);
8149 static int cpp_openIncludeFile (char *filename)
8151 int res = open (filename, O_RDONLY, 0666);
8153 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
8156 if (!fileTable_exists (context_fileTable (),
8157 cstring_fromChars (filename)))
8159 if (fileloc_isXHFile (g_currentloc))
8162 ** Files includes by XH files are also XH files
8165 (void) fileTable_addXHFile (context_fileTable (),
8166 cstring_fromChars (filename));
8170 (void) fileTable_addHeaderFile (context_fileTable (),
8171 cstring_fromChars (filename));
8176 DPRINTF (("File already exists: %s", filename));
8183 static bool cpp_skipIncludeFile (cstring fname)
8185 if (context_isSystemDir (fname))
8187 DPRINTF (("System dir: %s", fname));
8189 if (lcllib_isSkipHeader (fname))
8191 DPRINTF (("Skip include TRUE: %s", fname));
8195 if (context_getFlag (FLG_SKIPSYSHEADERS))
8197 DPRINTF (("Skip include TRUE: %s", fname));
8202 if (context_getFlag (FLG_SINGLEINCLUDE))
8204 fname = removePreDirs (fname);
8206 # if defined (WIN32) || defined (OS2)
8207 cstring_replaceAll (fname, '\\', '/');
8210 if (fileTable_exists (context_fileTable (), fname))
8212 DPRINTF (("Skip include TRUE: %s", fname));
8217 DPRINTF (("Skip include FALSE: %s", fname));
8221 static int cpp_peekN (cppReader *pfile, int n)
8223 cppBuffer *buf = cppReader_getBuffer (pfile);
8225 llassert (buf->cur != NULL);
8227 return (buf->rlimit - buf->cur >= (n)
8232 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
8237 void cppBuffer_forward (cppBuffer *buf, int n)
8239 llassert (buf->cur != NULL);