Various minor fixes for compiler/linter (other then splint itself) warnings.
[splint-patched.git] / src / cpplib.c
blob6b442a75417681cb8f8b89e2a046699e6799d3e0
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
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.
10 **
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.
15 **
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
25 ** cpplib.c
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
36 later version.
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! */
51 # include <time.h>
52 # include <errno.h>
54 # include "splintMacros.nf"
55 # include "basic.h"
57 # include "lcllib.h"
58 # include "cppconf.h"
59 # include "cpplib.h"
60 # include "cpperror.h"
61 # include "cpphash.h"
62 # include "cppexp.h"
63 # include "osd.h"
66 ** This is really kludgey code...
69 /*@+boolint@*/
70 /*@+charint@*/
72 /* Warnings for using sprintf - suppress them all for now... */
73 /*@-bufferoverflowhigh@*/
74 /*@-bounds@*/
76 /*@constant int IMPORT_FOUND@*/
77 # define IMPORT_FOUND -2
79 /*@constant int SKIP_INCLUDE@*/
80 # define SKIP_INCLUDE IMPORT_FOUND
82 /*@constant unused int IMPORT_NOT_FOUND@*/
83 # define IMPORT_NOT_FOUND -1
85 #ifndef STDC_VALUE
86 /*@constant unused int STDC_VALUE@*/
87 #define STDC_VALUE 1
88 #endif
90 static void parse_name (cppReader *, int);
92 static int cpp_openIncludeFile (char *p_filename)
93 /*@modifies fileSystem @*/ ;
95 static void cpp_setLocation (cppReader *p_pfile)
96 /*@modifies g_currentloc@*/ ;
98 static enum cpp_token cpp_handleComment (cppReader *p_pfile,
99 struct parse_marker *p_smark)
100 /*@modifies p_pfile, p_smark@*/;
102 static bool cpp_shouldCheckMacro (cppReader *p_pfile, char *p_p) /*@modifies p_p@*/ ;
104 static size_t cppReader_checkMacroName (cppReader *p_pfile, char *p_symname, cstring p_usage);
105 #if 0
106 static size_t cppReader_checkMacroNameLoc (fileloc p_loc, char *p_symname, cstring p_usage) ;
107 #endif
109 static bool cpp_skipIncludeFile (cstring p_fname) /*@*/ ;
111 /* Symbols to predefine. */
113 static /*@observer@*/ const char *predefs = CPP_PREDEFINES;
115 /* We let tm.h override the types used here, to handle trivial differences
116 such as the choice of unsigned int or long unsigned int for size_t.
117 When machines start needing nontrivial differences in the size type,
118 it would be best to do something here to figure out automatically
119 from other information what type to use. */
121 /* The string value for __SIZE_TYPE__. */
123 #ifndef SIZE_TYPE
124 /*@constant observer char *SIZE_TYPE@*/
125 #define SIZE_TYPE "long unsigned int"
126 #endif
128 /* The string value for __PTRDIFF_TYPE__. */
130 #ifndef PTRDIFF_TYPE
131 /*@constant observer char *PTRDIFF_TYPE@*/
132 #define PTRDIFF_TYPE "long int"
133 #endif
135 /* The string value for __WCHAR_TYPE__. */
137 #ifndef WCHAR_TYPE
138 /*@constant observer char *WCHAR_TYPE@*/
139 #define WCHAR_TYPE "int"
140 #endif
142 /* The string value for __USER_LABEL_PREFIX__ */
144 #ifndef USER_LABEL_PREFIX
145 /*@constant observer char *USER_LABEL_PREFIX@*/
146 #define USER_LABEL_PREFIX ""
147 #endif
149 /* The string value for __REGISTER_PREFIX__ */
151 #ifndef REGISTER_PREFIX
152 /*@constant observer char *REGISTER_PREFIX@*/
153 #define REGISTER_PREFIX ""
154 #endif
156 /* table to tell if char can be part of a C identifier. */
157 static bool is_idchar[256];
158 /* table to tell if char can be first char of a c identifier. */
159 static bool is_idstart[256];
160 /* table to tell if c is horizontal space. */
161 static bool is_hor_space[256];
162 /* table to tell if c is horizontal or vertical space. */
163 static bool is_space[256];
165 static /*@exposed@*/ /*@null@*/ cppBuffer *
166 cppReader_getBuffer (/*@special@*/ cppReader *p_pfile)
167 /*@uses p_pfile->buffer@*/
168 /*@modifies nothing@*/ ;
170 /*@notfunction@*/
171 # define SKIP_WHITE_SPACE(p) do { /*@access cstring@*/ while (is_hor_space[(int) *(p)]) { (p)++; } } /*@noaccess cstring@*/ while (0)
173 #ifdef DEADCODE
174 /*@notfunction@*/
175 # define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*(p)]) { (p)++; } } while (0)
176 #endif
178 static int cpp_peekN (cppReader *p_pfile, int p_n) /*@*/ ;
180 static void cppReader_growBuffer (cppReader *, size_t);
182 /* Make sure PFILE->token_buffer has space for at least N more characters. */
184 /*@function static void cpplib_reserve (sef cppReader *, sef/ size_t); @*/
185 #define cpplib_reserve(PFILE, N) \
186 do { \
187 if ((cpplib_getWritten (PFILE) + (N) > (PFILE)->token_buffer_size)) \
188 cppReader_growBuffer (PFILE, (N)); \
189 } while (0)
191 /*@function static int cppBuffer_get (sef cppBuffer *p_b) modifies *p_b ; @*/
192 # define cppBuffer_get(BUFFER) \
193 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
195 /*@function static int cppBuffer_reachedEOF (sef cppBuffer *p_b) modifies nothing; @*/
196 # define cppBuffer_reachedEOF(b) \
197 ((b)->cur < (b)->rlimit ? FALSE : TRUE)
199 /* Append string STR (of length N) to PFILE's output buffer. Assume there is enough space. */
201 /*@ function static void cppReader_putStrN (sef cppReader *p_file, @unique@ char *p_str,
202 sef size_t p_n) modifies *p_file; @*/
203 #define cppReader_putStrN(PFILE, STR, N) \
204 do { memcpy ((PFILE)->limit, STR, (N)); (PFILE)->limit += (N); } while (0)
206 /* Append string STR (of length N) to PFILE's output buffer. Make space. */
208 /*@function static void cppReader_puts (sef cppReader *p_file, char *p_str, sef size_t p_n)
209 modifies *p_file; @*/
210 # define cppReader_puts(PFILE, STR, N) \
211 do { cpplib_reserve(PFILE, N); cppReader_putStrN (PFILE, STR,N); } while (0)
213 /* Append character CH to PFILE's output buffer. Assume sufficient space. */
215 /*@function static void cppReader_putCharQ (cppReader *p_file, char p_ch)
216 modifies *p_file; @*/
217 # define cppReader_putCharQ(PFILE, CH) (*(PFILE)->limit++ = (CH))
219 static void cppReader_putCharQ (cppReader *p_file, char p_ch)
221 fprintf (stderr, "put char: %c\n", p_ch);
222 (*(p_file)->limit++ = (p_ch));
226 /* Append character CH to PFILE's output buffer. Make space if need be. */
228 /*@function static void cppReader_putChar (sef cppReader *p_file, char p_ch)
229 modifies *p_file; @*/
230 #define cppReader_putChar(PFILE, CH) \
231 do { cpplib_reserve (PFILE, (size_t) 1); cppReader_putCharQ (PFILE, CH); } while (0)
233 /* Make sure PFILE->limit is followed by '\0'. */
234 /*@function static void cppReader_nullTerminateQ (cppReader *p_file)
235 modifies *p_file; @*/
237 #define cppReader_nullTerminateQ(PFILE) (*(PFILE)->limit = 0)
239 /*@function static void cppReader_nullTerminate (sef cppReader *p_file)
240 modifies *p_file; @*/
241 # define cppReader_nullTerminate(PFILE) \
242 do { cpplib_reserve (PFILE, (size_t) 1); *(PFILE)->limit = 0; } while (0)
244 /*@function static void cppReader_adjustWritten (cppReader *p_file, size_t)
245 modifies *p_file; @*/
246 #define cppReader_adjustWritten(PFILE,DELTA) ((PFILE)->limit += (DELTA))
248 /*@function static bool cppReader_isC89 (cppReader *) modifies nothing; @*/
249 #define cppReader_isC89(PFILE) (CPPOPTIONS(PFILE)->c89)
251 static void cppBuffer_forward (cppBuffer *p_buf, int p_n) /*@modifies *p_buf@*/ ;
253 /*@function static void cppReader_forward (cppReader *p_pfile, int) modifies *p_pfile; @*/
254 # define cppReader_forward(pfile, N) \
255 (cppBuffer_forward (cppReader_getBufferSafe (pfile), (N)))
257 /*@function static int cppReader_getC (cppReader *p_pfile) modifies *p_pfile; @*/
258 # define cppReader_getC(pfile) (cppBuffer_get (cppReader_getBufferSafe (pfile)))
260 /*@function static int cppReader_reachedEOF (sef cppReader *p_pfile) modifies *p_pfile; @*/
261 # define cppReader_reachedEOF(pfile) (cppBuffer_reachedEOF (cppReader_getBufferSafe (pfile)))
263 /*@function static int cppReader_peekC (cppReader *) modifies nothing;@*/
264 # define cppReader_peekC(pfile) (cpplib_bufPeek (cppReader_getBufferSafe (pfile)))
266 /* Move all backslash-newline pairs out of embarrassing places.
267 Exchange all such pairs following BP
268 with any potentially-embarrassing characters that follow them.
269 Potentially-embarrassing characters are / and *
270 (because a backslash-newline inside a comment delimiter
271 would cause it not to be recognized). */
273 /*@notfunction@*/
274 # define NEWLINE_FIX \
275 do { while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n') { cppReader_forward (pfile, 2); } } while(FALSE)
277 /* Same, but assume we've already read the potential '\\' into C. */
278 /*@notfunction@*/
279 # define NEWLINE_FIX1(C) do { \
280 while ((C) == '\\' && cppReader_peekC (pfile) == '\n') { cppReader_forward (pfile, 1); (C) = cppReader_getC (pfile); }\
281 } while(0)
283 static void parseSetMark (/*@out@*/ struct parse_marker *,
284 cppReader *);
285 static void parseClearMark (struct parse_marker *);
286 static void parseGotoMark (struct parse_marker *, cppReader *);
287 static void parseMoveMark (struct parse_marker *, cppReader *);
289 /* If we have a huge buffer, may need to cache more recent counts */
290 static /*@exposed@*/ char *cppLineBase (/*@sef@*/ cppBuffer *);
292 static /*@exposed@*/ /*@null@*/ cppBuffer *
293 cppReader_pushBuffer (cppReader *p_pfile,
294 /*@owned@*/ /*@null@*/ char *, size_t)
295 /*@modifies p_pfile@*/ ;
297 static void cppReader_appendIncludeChain (cppReader *p_pfile,
298 /*@keep@*/ struct file_name_list *p_first,
299 /*@dependent@*/ struct file_name_list *p_last,
300 bool p_system);
302 static void cppReader_macroCleanup (cppBuffer *p_pbuf, cppReader *p_pfile);
303 static enum cpp_token cppReader_nullUnderflow (/*@unused@*/ cppReader *p_pfile);
305 static void cppReader_nullCleanup (/*@unused@*/ cppBuffer *p_pbuf,
306 /*@unused@*/ cppReader *p_pfile);
308 static void cppReader_fileCleanup (cppBuffer *p_pbuf,
309 /*@unused@*/ cppReader *p_pfile);
311 static int cppReader_handleDirective (cppReader *p_pfile);
313 static void cppReader_scanBuffer (cppReader *p_pfile);
316 ** cppBuffer_isMacro is true if the buffer contains macro expansion.
317 ** (Note that it is false while we're expanding marco *arguments*.)
320 static bool cppBuffer_isMacro (/*@null@*/ cppBuffer *) /*@*/ ;
322 static void initialize_builtins (cppReader *p_pfile)
323 /*@modifies p_pfile@*/ ;
325 static void initialize_char_syntax (struct cppOptions *p_opts) ;
327 static int /*@alt void@*/ finclude (cppReader *p_pfile, int p_f,
328 cstring p_fname,
329 bool p_system_header_p,
330 /*@dependent@*/ /*@null@*/ struct file_name_list *p_dirptr);
332 static void validate_else (cppReader *p_pfile, cstring p_directive);
334 static void conditional_skip (cppReader *p_pfile, int p_skip,
335 enum node_type p_type,
336 /*@dependent@*/ /*@null@*/ char *p_control_macro);
338 static HOST_WIDE_INT eval_if_expression (cppReader *p_pfile,
339 char *p_buf,
340 int p_length);
342 static void skip_if_group (cppReader *p_pfile, bool p_any);
344 static bool comp_def_part (bool p_first, char *p_beg1, int p_len1,
345 char *p_beg2, int p_len2, bool p_last);
347 static bool redundant_include_p (cppReader *p_pfile, /*@null@*/ cstring p_name);
348 static bool is_system_include (cppReader *p_pfile, cstring p_filename);
350 static /*@observer@*/ /*@null@*/ struct file_name_map *
351 read_name_map (cppReader *p_pfile, cstring p_dirname);
353 static cstring read_filename_string (int p_ch, /*:open:*/ FILE *p_f);
355 static int open_include_file (cppReader *p_pfile,
356 /*@owned@*/ cstring p_fname,
357 /*@null@*/ struct file_name_list *p_searchptr);
359 static void push_macro_expansion (cppReader *,
360 /*@owned@*/ char *, size_t,
361 /*@dependent@*/ hashNode);
363 /* Last arg to output_line_command. */
364 enum file_change_code {
365 same_file, enter_file, leave_file
368 /* `struct directive' defines one #-directive, including how to handle it. */
370 struct directive {
371 int length; /* Length of name */
372 /*@null@*/ int (*func)(); /* Function to handle directive */
373 /*@observer@*/ cstring name; /* Name of directive */
374 enum node_type type; /* Code which describes which directive. */
375 bool command_reads_line; /* One if rest of line is read by func. */
376 bool traditional_comments; /* True: keep comments if -traditional. */
377 bool pass_thru; /* Copy preprocessed directive to output file.*/
380 /* These functions are declared to return int instead of void since they
381 are going to be placed in a table and some old compilers have trouble with
382 pointers to functions returning void. */
384 static int do_define (cppReader *, /*@null@*/ struct directive *,
385 /*@exposed@*/ char *, char *);
386 static int do_defineAux (cppReader *, /*@null@*/ struct directive *,
387 /*@exposed@*/ char *, char *, bool);
389 static int do_line (cppReader *, /*@null@*/ struct directive *);
390 static int do_include (cppReader *, struct directive *, char *, char *);
391 static int do_undef (cppReader *, struct directive *, char *, char *);
392 static int do_error (cppReader *, struct directive *, char *, char *);
393 static int do_pragma (cppReader *, struct directive *, char *, char *);
394 static int do_ident (cppReader *, struct directive *, char *, char *);
395 static int do_if (cppReader *, struct directive *, char *, char *);
396 static int do_xifdef (cppReader *, struct directive *, char *, char *);
397 static int do_else (cppReader *, struct directive *, char *, char *);
398 static int do_elif (cppReader *, struct directive *, char *, char *);
399 static int do_endif (cppReader *, struct directive *, char *, char *);
400 static int do_warning (cppReader *, struct directive *, char *, char *);
402 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
403 via the same directory as the file that #included it. */
405 /*@constant observer struct file_name_list *SELF_DIR_DUMMY@*/
406 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
408 /* #include "file" looks in source file dir, then stack. */
409 /* #include <file> just looks in the stack. */
410 /* -I directories are added to the end, then the defaults are added. */
412 /* Here is the actual list of #-directives, most-often-used first.
413 The initialize_builtins function assumes #define is the very first. */
415 /*@access cstring@*/
417 static struct directive directive_table[] = {
418 { 6, do_define, "define", T_DEFINE, FALSE, TRUE, FALSE },
419 { 5, do_xifdef, "ifdef", T_IFDEF, TRUE, FALSE, FALSE },
420 { 6, do_xifdef, "ifndef", T_IFNDEF, TRUE, FALSE, FALSE },
421 { 7, do_include, "include", T_INCLUDE, TRUE, FALSE, FALSE },
422 #if 0
423 { 12, do_include, "include_next", T_INCLUDE_NEXT, TRUE, FALSE, FALSE },
424 #endif
425 { 5, do_endif, "endif", T_ENDIF, TRUE, FALSE, FALSE },
426 { 4, do_else, "else", T_ELSE, TRUE, FALSE, FALSE },
427 { 2, do_if, "if", T_IF, TRUE, FALSE, FALSE },
428 { 4, do_elif, "elif", T_ELIF, TRUE, FALSE, FALSE },
429 { 5, do_undef, "undef", T_UNDEF, FALSE, FALSE, FALSE },
430 { 5, do_error, "error", T_ERROR, FALSE, FALSE, FALSE },
431 { 7, do_warning, "warning", T_WARNING, FALSE, FALSE, FALSE },
432 { 6, do_pragma, "pragma", T_PRAGMA, FALSE, FALSE, TRUE},
433 { 4, do_line, "line", T_LINE, TRUE, FALSE, FALSE },
434 { 5, do_ident, "ident", T_IDENT, TRUE, FALSE, TRUE },
435 /* { 8, do_unassert, "unassert", T_UNASSERT, TRUE, FALSE, FALSE }, */
436 { -1, NULL, "", T_UNUSED, FALSE, FALSE, FALSE },
438 /*@noaccess cstring@*/
440 static cstring searchPath_unparse (struct file_name_list *search_start)
442 cstring res = cstring_newEmpty ();
443 struct file_name_list *searchptr;
445 for (searchptr = search_start; searchptr != NULL;
446 searchptr = searchptr->next)
448 if (!cstring_isEmpty (searchptr->fname))
450 res = cstring_concatFree1 (res, searchptr->fname);
451 if (searchptr->next != NULL) {
452 res = cstring_appendChar (res, PATH_SEPARATOR);
457 return res;
460 /*@+charint@*/
461 static void
462 initialize_char_syntax (struct cppOptions *opts)
464 char i;
467 * Set up is_idchar and is_idstart tables. These should be
468 * faster than saying (is_alpha (c) || c == '_'), etc.
469 * Set up these things before calling any routines that
470 * refer to them.
473 for (i = 'a'; i <= 'z'; i++) {
474 is_idchar[i - 'a' + 'A'] = TRUE;
475 is_idchar[(int) i] = TRUE;
476 is_idstart[i - 'a' + 'A'] = TRUE;
477 is_idstart[(int) i] = TRUE;
480 for (i = '0'; i <= '9'; i++)
482 is_idchar[(int) i] = TRUE;
485 is_idchar['_'] = TRUE;
486 is_idstart['_'] = TRUE;
487 is_idchar['$'] = opts->dollars_in_ident;
488 is_idstart['$'] = opts->dollars_in_ident;
490 /* horizontal space table */
491 is_hor_space[' '] = TRUE;
492 is_hor_space['\t'] = TRUE;
493 is_hor_space['\v'] = TRUE;
494 is_hor_space['\f'] = TRUE;
495 is_hor_space['\r'] = TRUE;
497 is_space[' '] = TRUE;
498 is_space['\t'] = TRUE;
499 is_space['\v'] = TRUE;
500 is_space['\f'] = TRUE;
501 is_space['\n'] = TRUE;
502 is_space['\r'] = TRUE;
505 bool isIdentifierChar (char c)
507 return is_idchar[(int) c];
510 /* Place into P_PFILE a quoted string representing the string SRC.
511 Caller must reserve enough space in pfile->token_buffer. */
513 static void
514 quote_string (cppReader *pfile, char *src)
516 char c;
518 cppReader_putCharQ (pfile, '\"');
519 for (;;)
521 switch ((c = *src++))
523 default:
524 if (isprint (c))
525 cppReader_putCharQ (pfile, c);
526 else
528 sprintf (cpplib_getPWritten (pfile), "\\%03o",
529 (unsigned int) c);
530 cppReader_adjustWritten (pfile, (size_t) 4);
532 /*@switchbreak@*/ break;
534 case '\"':
535 case '\\':
536 cppReader_putCharQ (pfile, '\\');
537 cppReader_putCharQ (pfile, c);
538 /*@switchbreak@*/ break;
540 case '\0':
541 cppReader_putCharQ (pfile, '\"');
542 cppReader_nullTerminateQ (pfile);
543 return;
548 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
550 static void
551 cppReader_growBuffer (cppReader *pfile, size_t n)
553 size_t old_written = cpplib_getWritten (pfile);
554 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
555 pfile->token_buffer = (char *)
556 drealloc (pfile->token_buffer, pfile->token_buffer_size);
557 cppReader_setWritten (pfile, old_written);
561 * process a given definition string, for initialization
562 * If STR is just an identifier, define it with value 1.
563 * If STR has anything after the identifier, then it should
564 * be identifier=definition.
567 static void
568 cppReader_defineReal (cppReader *pfile, char *str)
570 char *buf = NULL;
571 char *p = str;
573 DPRINTF (("Cpp reader define: %s", str));
575 if (!is_idstart[(int) *p])
577 DPRINTF (("ERROR 1"));
578 cppReader_error (pfile,
579 message ("Malformed option `-D%s'",
580 cstring_fromChars (str)));
582 return;
585 p++;
587 DPRINTF (("Here 2"));
589 while (is_idchar[(int) *p])
591 p++;
594 if (*p == '(') {
595 p++;
596 while (*p != ')' && *p != '\0') {
597 p++;
600 if (*p == ')') {
601 p++;
602 } else {
603 cppReader_error
604 (pfile,
605 message ("Malformed option: -D%s (no closing parenthesis)",
606 cstring_fromChars (str)));
610 DPRINTF (("Here 2"));
612 if (*p == '\0')
614 buf = (char *) dmalloc (size_fromInt (p - str + 4));
615 strcpy ((char *) buf, str);
616 strcat ((char *) buf, " 1");
618 else if (*p != '=')
620 DPRINTF (("ERROR 2"));
621 cppReader_error (pfile,
622 message ("Malformed option: -D%s (expected '=', found '%c')",
623 cstring_fromChars (str),
624 *p));
625 return;
627 else
629 char *q;
630 /* Copy the entire option so we can modify it. */
631 DPRINTF (("Copying..."));
632 buf = (char *) dmalloc (2 * strlen (str) + 1);
633 strncpy (buf, str, size_fromInt (p - str));
635 /* Change the = to a space. */
636 buf[p - str] = ' ';
637 /* Scan for any backslash-newline and remove it. */
638 p++;
639 q = &buf[p - str];
641 while (*p != '\0')
643 if (*p == '\\' && p[1] == '\n')
644 p += 2;
645 else
646 *q++ = *p++;
649 DPRINTF (("Here we are..."));
650 *q = '\0';
653 llassert (buf != NULL);
654 DPRINTF (("Do define: %s / %ld", buf, size_toLong (strlen (buf))));
655 (void) do_define (pfile, NULL, buf, buf + strlen (buf));
656 sfree (buf);
659 static void
660 cppReader_define (cppReader *pfile, char *str)
662 cppBuffer *tbuf = CPPBUFFER (pfile);
664 CPPBUFFER (pfile) = NULL;
665 cppReader_defineReal (pfile, str);
666 CPPBUFFER (pfile) = tbuf;
669 /* Append a chain of `struct file_name_list's
670 to the end of the main include chain.
671 FIRST is the beginning of the chain to append, and LAST is the end. */
673 static void
674 cppReader_appendIncludeChain (cppReader *pfile,
675 struct file_name_list *first,
676 struct file_name_list *last,
677 bool system)
679 struct cppOptions *opts = CPPOPTIONS (pfile);
681 if (first == NULL || last == NULL)
683 return;
686 if (opts->include == NULL)
688 opts->include = first;
690 else
692 llassert (opts->last_include->next == NULL);
693 opts->last_include->next = first;
696 if (opts->first_bracket_include == NULL)
698 opts->first_bracket_include = first;
701 if (system)
703 /*@-usereleased@*/ /* Spurious warnings for opts->first_system_include */
704 if (opts->first_system_include == NULL)
706 opts->first_system_include = first;
708 /*@=usereleased@*/
711 llassert (last->next == NULL);
712 opts->last_include = last;
715 static void
716 cppReader_appendDirIncludeChain (cppReader *pfile, char* fname, bool system)
718 struct file_name_list *dirtmp;
720 DPRINTF (("Adding include dir: %s", fname));
722 dirtmp = (struct file_name_list *) dmalloc (sizeof (*dirtmp));
723 llassert (dirtmp != NULL);
725 /* create a chain of only one element */
726 dirtmp->fname = mstring_copy (fname);
727 dirtmp->next = NULL;
728 dirtmp->control_macro = NULL;
729 dirtmp->name_map = NULL;
730 dirtmp->got_name_map = FALSE;
732 cppReader_appendIncludeChain (pfile, dirtmp, dirtmp, system);
735 static void
736 pathList_to_fileNameList (const char *pathList,
737 struct file_name_list **pfirst, struct file_name_list **plast)
739 struct file_name_list *first = NULL, *last = NULL;
741 llassert (pfirst != NULL);
742 llassert (plast != NULL);
744 if (pathList != NULL && *pathList != '\0')
746 const char *p, *q;
748 #ifdef __CYGWIN32__
749 char *win32temp;
751 /* if we have a posix path list, convert to win32 path list */
752 win32temp = (char *) dmalloc /*@i4@*/
753 (cygwin32_posix_to_win32_path_list_buf_size (pathList));
754 cygwin32_posix_to_win32_path_list (pathList, win32temp);
755 p = win32temp;
756 #else
757 p = pathList;
758 #endif
762 q = p;
764 /* Find the end of this name. */
765 while (*q != '\0' && *q != PATH_SEPARATOR)
767 q++;
771 char *name;
772 struct file_name_list *tmp;
774 if (q == p)
776 /* An empty name in the path stands for the current directory. */
777 name = (char *) dmalloc ((size_t) 2);
778 name[0] = '.';
779 name[1] = '\0';
781 else
783 /* Otherwise use the directory that is named. */
784 size_t len = size_fromInt (q-p);
785 name = (char *) dmalloc (len + 1);
786 memcpy (name, p, len);
787 name[len] = '\0';
790 tmp = (struct file_name_list *) dmalloc (sizeof (*tmp));
791 llassert (tmp != NULL);
793 tmp->next = NULL; /* New one goes on the end */
794 tmp->fname = cstring_fromChars (name);
795 tmp->control_macro = NULL;
796 tmp->name_map = NULL;
797 tmp->got_name_map = FALSE;
799 if (last == NULL) /* initialize first & last */
801 llassert (first == NULL);
802 last = first = tmp;
804 else
806 llassert (first != NULL);
808 last->next = tmp;
809 last = tmp;
813 p = q+1; /* Advance past this name. */
815 while (*q != '\0');
817 #ifdef __CYGWIN32__
818 sfree (win32temp);
819 #endif
822 *pfirst = first;
823 *plast = last;
826 static void
827 cppReader_appendPathListIncludeChain (cppReader *pfile,
828 const char *pathList, bool system)
830 struct file_name_list *first, *last;
831 pathList_to_fileNameList (pathList, &first, &last);
832 cppReader_appendIncludeChain (pfile, first, last, system);
835 static void
836 cppOptions_init (/*@out@*/ cppOptions *opts)
838 memset ((char *) opts, 0, sizeof *opts);
839 assertSet (opts);
841 opts->in_fname = NULL;
842 opts->out_fname = NULL;
844 /* Initialize is_idchar to allow $. */
845 opts->dollars_in_ident = TRUE;
847 opts->no_line_commands = FALSE;
848 opts->no_trigraphs = TRUE;
849 opts->put_out_comments = TRUE;
850 opts->dump_macros = DUMP_DEFINITIONS; /* DUMP_NONE; */
851 opts->no_output = FALSE;
853 opts->cplusplus_comments = TRUE;
854 opts->verbose = FALSE;
855 opts->lang_asm = FALSE;
856 opts->pedantic_errors = FALSE;
857 opts->warn_comments = FALSE;
858 opts->warnings_are_errors = FALSE;
860 /* Added 2003-07-10: */
861 opts->traditional = FALSE;
862 opts->c89 = TRUE;
865 enum cpp_token
866 cppReader_nullUnderflow (/*@unused@*/ cppReader *pfile)
868 return CPP_EOF;
871 void
872 cppReader_nullCleanup (/*@unused@*/ cppBuffer *pbuf,
873 /*@unused@*/ cppReader *pfile)
878 void
879 cppReader_macroCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
881 hashNode macro = pbuf->hnode;
883 if (macro->type == T_DISABLED)
885 macro->type = T_MACRO;
888 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
890 sfree (pbuf->buf);
891 pbuf->buf = NULL;
895 void
896 cppReader_fileCleanup (cppBuffer *pbuf, /*@unused@*/ cppReader *pfile)
898 if (pbuf->buf != NULL)
900 sfree (pbuf->buf);
901 pbuf->buf = NULL;
905 /* Assuming we have read '/'.
906 If this is the start of a comment (followed by '*' or '/'),
907 skip to the end of the comment, and return ' '.
908 Return EOF if we reached the end of file before the end of the comment.
909 If not the start of a comment, return '/'. */
911 static int
912 skip_comment (cppReader *pfile, /*@null@*/ long *linep)
914 int c = 0;
916 llassert (pfile->buffer != NULL);
917 llassert (pfile->buffer->cur != NULL);
919 while (cppReader_peekC (pfile) == '\\' && cpp_peekN (pfile, 1) == '\n')
921 if (linep != NULL)
923 (*linep)++;
926 cppReader_forward (pfile, 2);
929 if (cppReader_peekC (pfile) == '*')
931 cppReader_forward (pfile, 1);
933 for (;;)
935 int prev_c = c;
936 c = cppReader_getC (pfile);
938 if (c == EOF)
940 return EOF;
943 while (c == (int) '\\' && cppReader_peekC (pfile) == (int) '\n')
945 if (linep != NULL )
947 (*linep)++;
950 cppReader_forward (pfile, 1), c = cppReader_getC (pfile);
953 if (prev_c == (int) '*' && c == (int) '/')
955 return (int) ' ';
958 if (c == (int) '\n' && (linep != NULL))
960 (*linep)++;
964 else if (cppReader_peekC (pfile) == '/'
965 && CPPOPTIONS (pfile)->cplusplus_comments)
968 (void) cppoptgenerror
969 (FLG_SLASHSLASHCOMMENT,
970 message ("C++ style // comment"
972 pfile);
974 cppReader_forward (pfile, 1);
976 for (;;)
978 c = cppReader_getC (pfile);
980 if (c == EOF)
982 /* Allow hash comment to be terminated by EOF. */
983 return (int) ' ';
986 while (c == (int) '\\' && cppReader_peekC (pfile) == '\n')
988 cppReader_forward (pfile, 1);
989 c = cppReader_getC (pfile);
991 if (linep != NULL)
993 (*linep)++;
997 if (c == (int) '\n')
999 /* Don't consider final '\n' to be part of comment. */
1000 cppReader_forward (pfile, -1);
1001 return (int) ' ';
1005 else
1007 return (int) '/';
1011 /* Skip whitespace \-newline and comments. Does not macro-expand. */
1012 int /*@alt void@*/
1013 cppSkipHspace (cppReader *pfile)
1015 int nspaces = 0;
1017 while (TRUE)
1019 int c;
1021 llassert (pfile->buffer != NULL);
1023 c = cppReader_peekC (pfile);
1025 if (c == EOF)
1027 return 0; /* FIXME */
1030 if (is_hor_space[c])
1032 if ((c == '\f' || c == '\v') && cppReader_isPedantic (pfile))
1033 cppReader_pedwarn (pfile,
1034 message ("%s in preprocessing directive",
1035 c == '\f'
1036 ? cstring_makeLiteralTemp ("formfeed")
1037 : cstring_makeLiteralTemp ("vertical tab")));
1039 nspaces++;
1040 cppReader_forward (pfile, 1);
1042 else if (c == '/')
1044 cppReader_forward (pfile, 1);
1045 c = skip_comment (pfile, NULL);
1047 if (c == '/')
1049 cppReader_forward (pfile, -1);
1052 if (c == EOF || c == '/')
1054 return nspaces;
1057 else if (c == '\\' && cpp_peekN (pfile, 1) == '\n')
1059 cppReader_forward (pfile, 2);
1061 else if (c == '@' && CPPBUFFER (pfile)->has_escapes
1062 && is_hor_space [cpp_peekN (pfile, 1)])
1064 cppReader_forward (pfile, 2);
1066 else
1068 return nspaces;
1073 /* Read the rest of the current line.
1074 The line is appended to PFILE's output buffer. */
1076 static void
1077 copy_rest_of_line (cppReader *pfile)
1079 struct cppOptions *opts = CPPOPTIONS (pfile);
1081 for (;;)
1083 int c;
1084 int nextc;
1086 llassert (pfile->buffer != NULL);
1088 c = cppReader_getC (pfile);
1090 switch (c)
1092 case EOF:
1093 goto end_directive;
1094 case '\\':
1096 ** Patch from Brian St. Pierre for handling MS-DOS files.
1099 DPRINTF (("Reading directive: %d", (int) c));
1101 if (cppReader_peekC (pfile) == '\n'
1102 || cppReader_peekC (pfile) == '\r')
1104 DPRINTF (("Reading directive..."));
1105 if (cppReader_peekC (pfile) == '\r')
1107 DPRINTF (("Reading directive..."));
1108 cppReader_forward (pfile, 1);
1111 DPRINTF (("Reading directive..."));
1112 cppReader_forward (pfile, 1);
1113 continue;
1116 DPRINTF (("Falling..."));
1117 /*@fallthrough@*/ case '\'': case '\"':
1118 goto scan_directive_token;
1120 case '/':
1121 nextc = cppReader_peekC (pfile);
1124 ** was (opts->cplusplus_comments && nextc == '*')
1125 ** yoikes!
1128 if (nextc == '*'
1129 || (opts->cplusplus_comments && nextc == '/'))
1131 goto scan_directive_token;
1133 /*@switchbreak@*/ break;
1134 case '\f':
1135 case '\v':
1136 if (cppReader_isPedantic (pfile))
1137 cppReader_pedwarn (pfile,
1138 message ("%s in preprocessing directive",
1139 c == '\f'
1140 ? cstring_makeLiteralTemp ("formfeed")
1141 : cstring_makeLiteralTemp ("vertical tab")));
1142 /*@switchbreak@*/ break;
1144 case '\n':
1145 cppReader_forward (pfile, -1);
1146 goto end_directive;
1147 scan_directive_token:
1148 cppReader_forward (pfile, -1);
1149 (void) cpplib_getToken (pfile);
1150 continue;
1152 cppReader_putChar (pfile, c);
1154 end_directive: ;
1155 cppReader_nullTerminate (pfile);
1158 void
1159 cppReader_skipRestOfLine (cppReader *pfile)
1161 size_t old = cpplib_getWritten (pfile);
1162 copy_rest_of_line (pfile);
1163 cppReader_setWritten (pfile, old);
1166 /* Handle a possible # directive.
1167 '#' has already been read. */
1169 static int
1170 cppReader_handleDirective (cppReader *pfile)
1172 int c;
1173 struct directive *kt = NULL;
1174 int ident_length;
1175 size_t after_ident = 0;
1176 char *ident = NULL;
1177 char *line_end = NULL;
1178 size_t old_written = cpplib_getWritten (pfile);
1179 int nspaces = cppSkipHspace (pfile);
1181 c = cppReader_peekC (pfile);
1183 if (c >= '0' && c <= '9')
1185 /* Handle # followed by a line number. */
1186 if (cppReader_isPedantic (pfile))
1188 cppReader_pedwarnLit
1189 (pfile,
1190 cstring_makeLiteralTemp ("`#' followed by integer"));
1193 (void) do_line (pfile, NULL);
1194 goto done_a_directive;
1198 /* Now find the directive name. */
1200 cppReader_putChar (pfile, '#');
1202 parse_name (pfile, cppReader_getC (pfile));
1204 llassert (pfile->token_buffer != NULL);
1205 ident = pfile->token_buffer + old_written + 1;
1207 ident_length = cpplib_getPWritten (pfile) - ident;
1209 if (ident_length == 0 && cppReader_peekC (pfile) == '\n')
1211 /* A line of just `#' becomes blank. */
1212 return 1;
1215 for (kt = directive_table; ; kt++)
1217 if (kt->length <= 0)
1219 return 0; /* goto not_a_directive; */
1222 if (kt->length == ident_length
1223 && (cstring_equalPrefix (kt->name, cstring_fromChars (ident))))
1225 break;
1229 if (kt->command_reads_line)
1231 after_ident = 0;
1233 else
1235 /* True means do not delete comments within the directive.
1236 #define needs this when -traditional. */
1237 bool comments = TRUE; /*cppReader_isTraditional (pfile) && kt->traditional_comments; */
1238 int save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
1239 CPPOPTIONS (pfile)->put_out_comments = comments;
1240 after_ident = cpplib_getWritten (pfile);
1241 copy_rest_of_line (pfile);
1242 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
1246 /* For #pragma and #define, we may want to pass through the directive.
1247 Other directives may create output, but we don't want the directive
1248 itself out, so we pop it now. For example #include may write a #line
1249 command (see comment in do_include), and conditionals may emit
1250 #failed ... #endfailed stuff. But note that popping the buffer
1251 means the parameters to kt->func may point after pfile->limit
1252 so these parameters are invalid as soon as something gets appended
1253 to the token_buffer. */
1255 line_end = cpplib_getPWritten (pfile);
1258 if (!kt->pass_thru && kt->type != T_DEFINE)
1260 cppReader_setWritten (pfile, old_written);
1263 llassert (pfile->token_buffer != NULL);
1265 /* was kt->pass_thru || */
1267 if (kt->type == T_DEFINE
1268 && cpp_shouldCheckMacro (pfile, pfile->token_buffer + old_written))
1270 char *p = pfile->token_buffer + old_written;
1273 ** Still need to record value for preprocessing, so
1274 ** #ifdef's, etc. using the value behave correctly.
1277 (void) do_defineAux (pfile, kt,
1278 pfile->token_buffer + after_ident,
1279 line_end,
1280 TRUE);
1282 if (*p == '#')
1284 *p = ' ';
1287 SKIP_WHITE_SPACE (p);
1289 llassert (*p == 'd');
1290 *p++ = LLMRCODE[0];
1292 llassert (*p == 'e');
1293 *p++ = LLMRCODE[1];
1295 llassert (*p == 'f');
1296 *p++ = LLMRCODE[2];
1298 llassert (*p == 'i');
1299 *p++ = LLMRCODE[3];
1301 llassert (*p == 'n');
1302 *p++ = LLMRCODE[4];
1304 llassert (*p == 'e');
1307 ** This is way-bogus. We use the last char to record the number of
1308 ** spaces. Its too hard to get them back into the input stream.
1311 if (nspaces > 9) nspaces = 9;
1313 *p++ = '0' + nspaces;
1315 return 0; /* not_a_directive */
1317 else if (kt->pass_thru)
1319 /* Just leave the entire #define in the output stack. */
1320 return 0; /* not_a_directive */
1323 else if (kt->type == T_DEFINE
1324 && CPPOPTIONS (pfile)->dump_macros == DUMP_NAMES)
1326 char *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1327 SKIP_WHITE_SPACE (p);
1329 while (is_idchar[(int) *p])
1331 p++;
1334 pfile->limit = p;
1335 cppReader_putChar (pfile, '\n');
1337 else if (kt->type == T_DEFINE)
1339 cppReader_setWritten (pfile, old_written);
1341 else
1346 done_a_directive:
1347 if (kt == NULL) {
1348 return 1;
1349 } else {
1350 llassert (kt->func != NULL);
1351 (void) (kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1352 return 1;
1356 /* Pass a directive through to the output file.
1357 BUF points to the contents of the directive, as a contiguous string.
1358 LIMIT points to the first character past the end of the directive.
1359 KEYWORD is the keyword-table entry for the directive. */
1361 static void
1362 pass_thru_directive (char *buf, char *limit,
1363 cppReader *pfile,
1364 struct directive *keyword)
1366 int keyword_length = keyword->length;
1368 cpplib_reserve (pfile,
1369 size_fromInt (2 + keyword_length + (limit - buf)));
1370 cppReader_putCharQ (pfile, '#');
1371 /*@-observertrans@*/
1372 cppReader_putStrN (pfile, cstring_toCharsSafe (keyword->name),
1373 size_fromInt (keyword_length));
1374 /*:=observertrans@*/
1376 if (limit != buf && buf[0] != ' ')
1378 /* Was a bug, since reserve only used 1 + ... */
1379 cppReader_putCharQ (pfile, ' ');
1382 cppReader_putStrN (pfile, buf, size_fromInt (limit - buf));
1385 /* Read a replacement list for a macro with parameters.
1386 Build the DEFINITION structure.
1387 Reads characters of text starting at BUF until END.
1388 ARGLIST specifies the formal parameters to look for
1389 in the text of the definition; NARGS is the number of args
1390 in that list, or -1 for a macro name that wants no argument list.
1391 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1392 and NAMELEN is its length in characters.
1394 Note that comments, backslash-newlines, and leading white space
1395 have already been deleted from the argument. */
1397 static DEFINITION *
1398 collect_expansion (cppReader *pfile, char *buf, char *limit,
1399 int nargs, /*@null@*/ struct arglist *arglist)
1401 DEFINITION *defn;
1402 char *p, *lastp, *exp_p;
1403 struct reflist *endpat = NULL;
1404 /* Pointer to first nonspace after last ## seen. */
1405 char *concat = 0;
1406 /* Pointer to first nonspace after last single-# seen. */
1407 char *stringify = 0;
1408 size_t maxsize;
1409 char expected_delimiter = '\0';
1412 /* Scan thru the replacement list, ignoring comments and quoted
1413 strings, picking up on the macro calls. It does a linear search
1414 thru the arg list on every potential symbol. Profiling might say
1415 that something smarter should happen. */
1417 if (limit < buf)
1418 abort ();
1420 /* Find the beginning of the trailing whitespace. */
1421 p = buf;
1423 while (p < limit && is_space[(int) limit[-1]])
1425 limit--;
1428 /* Allocate space for the text in the macro definition.
1429 Leading and trailing whitespace chars need 2 bytes each.
1430 Each other input char may or may not need 1 byte,
1431 so this is an upper bound. The extra 5 are for invented
1432 leading and trailing newline-marker and final null. */
1433 maxsize = (sizeof (*defn) + (limit - p) + 5);
1435 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1436 while (p < limit)
1438 if (*p++ == '@')
1440 maxsize++;
1444 defn = (DEFINITION *) dmalloc (maxsize);
1445 defn->noExpand = FALSE;
1446 defn->file = NULL;
1447 defn->pattern = NULL;
1448 defn->nargs = nargs;
1449 defn->predefined = NULL;
1451 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1452 *defn->expansion = '\0'; /* convince splint it is initialized */
1454 defn->line = 0;
1455 defn->rest_args = NULL;
1456 defn->args.argnames = NULL;
1458 lastp = exp_p;
1460 p = buf;
1462 /* Add one initial space escape-marker to prevent accidental
1463 token-pasting (often removed by cpplib_macroExpand). */
1464 *exp_p++ = '@';
1465 *exp_p++ = ' ';
1467 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1468 cppReader_errorLit (pfile,
1469 cstring_makeLiteralTemp ("`##' at start of macro definition"));
1470 p += 2;
1473 /* Process the main body of the definition. */
1474 while (p < limit) {
1475 int skipped_arg = 0;
1476 register char c = *p++;
1478 *exp_p++ = c;
1480 if (!cppReader_isTraditional (pfile)) {
1481 switch (c) {
1482 case '\'':
1483 case '\"':
1484 if (expected_delimiter != '\0')
1486 if (c == expected_delimiter)
1487 expected_delimiter = '\0';
1489 else
1491 expected_delimiter = c;
1493 /*@switchbreak@*/ break;
1495 case '\\':
1496 if (p < limit && (expected_delimiter != '\0'))
1498 /* In a string, backslash goes through
1499 and makes next char ordinary. */
1500 *exp_p++ = *p++;
1502 /*@switchbreak@*/ break;
1504 case '@':
1505 /* An '@' in a string or character constant stands for itself,
1506 and does not need to be escaped. */
1507 if (expected_delimiter == '\0')
1509 *exp_p++ = c;
1512 /*@switchbreak@*/ break;
1514 case '#':
1515 /* # is ordinary inside a string. */
1516 if (expected_delimiter != '\0')
1518 /*@switchbreak@*/ break;
1521 if (p < limit && *p == '#') {
1522 /* ##: concatenate preceding and following tokens. */
1523 /* Take out the first #, discard preceding whitespace. */
1524 exp_p--;
1526 /*@-usedef@*/
1527 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1529 --exp_p;
1531 /*@=usedef@*/
1533 /* Skip the second #. */
1534 p++;
1535 /* Discard following whitespace. */
1536 SKIP_WHITE_SPACE (p);
1537 concat = p;
1538 if (p == limit)
1540 cppReader_errorLit (pfile,
1541 cstring_makeLiteralTemp ("`##' at end of macro definition"));
1543 } else if (nargs >= 0) {
1544 /* Single #: stringify following argument ref.
1545 Don't leave the # in the expansion. */
1546 exp_p--;
1547 SKIP_WHITE_SPACE (p);
1548 if (p == limit || ! is_idstart[(int) *p]
1549 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1550 cppReader_errorLit (pfile,
1551 cstring_makeLiteralTemp ("`#' operator is not followed by a macro argument name"));
1552 else
1553 stringify = p;
1554 } else {
1555 ; /* BADBRANCH; */
1558 /*@switchbreak@*/ break;
1560 } else {
1561 /* In -traditional mode, recognize arguments inside strings and
1562 and character constants, and ignore special properties of #.
1563 Arguments inside strings are considered "stringified", but no
1564 extra quote marks are supplied. */
1565 switch (c) {
1566 case '\'':
1567 case '\"':
1568 if (expected_delimiter != '\0') {
1569 if (c == expected_delimiter)
1570 expected_delimiter = '\0';
1571 } else
1572 expected_delimiter = c;
1573 /*@switchbreak@*/ break;
1575 case '\\':
1576 /* Backslash quotes delimiters and itself, but not macro args. */
1577 if (expected_delimiter != '\0' && p < limit
1578 && (*p == expected_delimiter || *p == '\\')) {
1579 *exp_p++ = *p++;
1580 continue;
1582 /*@switchbreak@*/ break;
1584 case '/':
1585 if (expected_delimiter != '\0') /* No comments inside strings. */
1586 /*@switchbreak@*/ break;
1587 if (*p == '*') {
1588 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1589 this must be -traditional. So replace the comment with
1590 nothing at all. */
1591 exp_p--;
1592 p += 1;
1593 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1595 p++;
1598 /*@switchbreak@*/ break;
1602 /* Handle the start of a symbol. */
1603 if (is_idchar[(int) c] && nargs > 0) {
1604 char *id_beg = p - 1;
1605 size_t id_len;
1607 --exp_p;
1608 while (p != limit && is_idchar[(int) *p])
1610 p++;
1613 id_len = size_fromInt (p - id_beg);
1615 if (is_idstart[(int) c]
1616 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1617 register struct arglist *arg;
1619 for (arg = arglist; arg != NULL; arg = arg->next) {
1620 struct reflist *tpat;
1622 if (arg->name[0] == c
1623 && arg->length == id_len
1624 && strncmp (arg->name, id_beg, id_len) == 0) {
1625 char *p1;
1627 if (expected_delimiter && CPPOPTIONS (pfile)->warn_stringify) {
1628 if (cppReader_isTraditional (pfile)) {
1629 cppReader_warning (pfile,
1630 message ("macro argument `%x' is stringified.",
1631 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1632 } else {
1633 cppReader_warning (pfile,
1634 message ("macro arg `%x' would be stringified with -traditional.",
1635 cstring_prefix (cstring_fromChars (arg->name), id_len)));
1638 /* If ANSI, don't actually substitute inside a string. */
1639 if (!cppReader_isTraditional (pfile) && expected_delimiter)
1640 /*@innerbreak@*/ break;
1641 /* make a pat node for this arg and append it to the end of
1642 the pat list */
1643 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1644 tpat->next = NULL;
1645 tpat->raw_before = (concat == id_beg);
1646 tpat->raw_after = 0;
1647 tpat->rest_args = arg->rest_args;
1648 tpat->stringify = (cppReader_isTraditional (pfile)
1649 ? expected_delimiter != '\0'
1650 : stringify == id_beg);
1652 if (endpat == NULL)
1654 defn->pattern = tpat;
1656 else
1658 endpat->next = tpat;
1659 /*@-branchstate@*/
1660 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
1662 endpat = tpat;
1664 tpat->argno = arg->argno;
1665 tpat->nchars = exp_p - lastp;
1667 p1 = p;
1669 SKIP_WHITE_SPACE (p1);
1671 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1673 tpat->raw_after = 1;
1676 lastp = exp_p; /* place to start copying from next time */
1677 skipped_arg = 1;
1679 /*@innerbreak@*/ break;
1684 /* If this was not a macro arg, copy it into the expansion. */
1685 if (skipped_arg == 0) {
1686 register char *lim1 = p;
1687 p = id_beg;
1689 while (p != lim1)
1691 *exp_p++ = *p++;
1694 if (stringify == id_beg)
1695 cppReader_errorLit (pfile,
1696 cstring_makeLiteralTemp ("`#' operator should be followed by a macro argument name"));
1701 if (!cppReader_isTraditional (pfile) && expected_delimiter == '\0')
1703 /* If ANSI, put in a "@ " marker to prevent token pasting.
1704 But not if "inside a string" (which in ANSI mode
1705 happens only for -D option). */
1706 *exp_p++ = '@';
1707 *exp_p++ = ' ';
1710 *exp_p = '\0';
1712 defn->length = size_fromInt (exp_p - defn->expansion);
1714 /* Crash now if we overrun the allocated size. */
1715 if (defn->length + 1 > maxsize)
1717 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
1720 return defn; /* Spurious warning here */
1723 # if 0
1724 /* FIXME */
1726 ** evans 2001-12-31
1727 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
1730 static DEFINITION *
1731 collect_expansionLoc (fileloc loc, char *buf, char *limit,
1732 int nargs, /*@null@*/ struct arglist *arglist)
1734 DEFINITION *defn;
1735 char *p, *lastp, *exp_p;
1736 struct reflist *endpat = NULL;
1737 /* Pointer to first nonspace after last ## seen. */
1738 char *concat = 0;
1739 /* Pointer to first nonspace after last single-# seen. */
1740 char *stringify = 0;
1741 size_t maxsize;
1742 char expected_delimiter = '\0';
1745 /* Scan thru the replacement list, ignoring comments and quoted
1746 strings, picking up on the macro calls. It does a linear search
1747 thru the arg list on every potential symbol. Profiling might say
1748 that something smarter should happen. */
1750 if (limit < buf)
1752 llfatalbug (message ("%q: Limit is less than initial buffer pointer",
1753 fileloc_unparse (loc)));
1756 /* Find the beginning of the trailing whitespace. */
1757 p = buf;
1759 while (p < limit && is_space[(int) limit[-1]])
1761 limit--;
1764 /* Allocate space for the text in the macro definition.
1765 Leading and trailing whitespace chars need 2 bytes each.
1766 Each other input char may or may not need 1 byte,
1767 so this is an upper bound. The extra 5 are for invented
1768 leading and trailing newline-marker and final null. */
1769 maxsize = (sizeof (*defn) + (limit - p) + 5);
1771 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1772 while (p < limit)
1774 if (*p++ == '@')
1776 maxsize++;
1780 defn = (DEFINITION *) dmalloc (maxsize);
1781 defn->noExpand = FALSE;
1782 defn->file = NULL;
1783 defn->pattern = NULL;
1784 defn->nargs = nargs;
1785 defn->predefined = NULL;
1786 exp_p = defn->expansion = (char *) defn + sizeof (*defn);
1788 defn->line = 0;
1789 defn->rest_args = NULL;
1790 defn->args.argnames = NULL;
1792 lastp = exp_p;
1794 p = buf;
1796 /* Add one initial space escape-marker to prevent accidental
1797 token-pasting (often removed by cpplib_macroExpand). */
1798 *exp_p++ = '@';
1799 *exp_p++ = ' ';
1801 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1802 voptgenerror (FLG_PREPROC,
1803 cstring_makeLiteral ("Paste marker ## at start of macro definition"),
1804 loc);
1805 p += 2;
1808 /* Process the main body of the definition. */
1809 while (p < limit) {
1810 int skipped_arg = 0;
1811 register char c = *p++;
1813 *exp_p++ = c;
1815 if (TRUE) { /* !cppReader_isTraditional (pfile)) { */
1816 switch (c) {
1817 case '\'':
1818 case '\"':
1819 if (expected_delimiter != '\0')
1821 if (c == expected_delimiter)
1822 expected_delimiter = '\0';
1824 else
1826 expected_delimiter = c;
1828 /*@switchbreak@*/ break;
1830 case '\\':
1831 if (p < limit && (expected_delimiter != '\0'))
1833 /* In a string, backslash goes through
1834 and makes next char ordinary. */
1835 *exp_p++ = *p++;
1837 /*@switchbreak@*/ break;
1839 case '@':
1840 /* An '@' in a string or character constant stands for itself,
1841 and does not need to be escaped. */
1842 if (expected_delimiter == '\0')
1844 *exp_p++ = c;
1847 /*@switchbreak@*/ break;
1849 case '#':
1850 /* # is ordinary inside a string. */
1851 if (expected_delimiter != '\0')
1853 /*@switchbreak@*/ break;
1856 if (p < limit && *p == '#') {
1857 /* ##: concatenate preceding and following tokens. */
1858 /* Take out the first #, discard preceding whitespace. */
1859 exp_p--;
1861 /*@-usedef@*/
1862 while (exp_p > lastp && is_hor_space[(int) exp_p[-1]])
1864 --exp_p;
1866 /*@=usedef@*/
1868 /* Skip the second #. */
1869 p++;
1870 /* Discard following whitespace. */
1871 SKIP_WHITE_SPACE (p);
1872 concat = p;
1873 if (p == limit)
1875 voptgenerror (FLG_PREPROC,
1876 cstring_makeLiteral ("`##' at end of macro definition"),
1877 loc);
1879 } else if (nargs >= 0) {
1880 /* Single #: stringify following argument ref.
1881 Don't leave the # in the expansion. */
1882 exp_p--;
1883 SKIP_WHITE_SPACE (p);
1884 if (p == limit || ! is_idstart[(int) *p]
1885 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '\"')))
1887 voptgenerror
1888 (FLG_PREPROC,
1889 cstring_makeLiteral ("`#' operator is not followed by a macro argument name"),
1890 loc);
1892 else
1893 stringify = p;
1894 } else {
1895 ; /* BADBRANCH; */
1898 /*@switchbreak@*/ break;
1900 } else {
1901 /* In -traditional mode, recognize arguments inside strings and
1902 and character constants, and ignore special properties of #.
1903 Arguments inside strings are considered "stringified", but no
1904 extra quote marks are supplied. */
1905 switch (c) {
1906 case '\'':
1907 case '\"':
1908 if (expected_delimiter != '\0') {
1909 if (c == expected_delimiter)
1910 expected_delimiter = '\0';
1911 } else
1912 expected_delimiter = c;
1913 /*@switchbreak@*/ break;
1915 case '\\':
1916 /* Backslash quotes delimiters and itself, but not macro args. */
1917 if (expected_delimiter != '\0' && p < limit
1918 && (*p == expected_delimiter || *p == '\\')) {
1919 *exp_p++ = *p++;
1920 continue;
1922 /*@switchbreak@*/ break;
1924 case '/':
1925 if (expected_delimiter != '\0') /* No comments inside strings. */
1926 /*@switchbreak@*/ break;
1927 if (*p == '*') {
1928 /* If we find a comment that wasn't removed by cppReader_handleDirective,
1929 this must be -traditional. So replace the comment with
1930 nothing at all. */
1931 exp_p--;
1932 p += 1;
1933 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1935 p++;
1938 /*@switchbreak@*/ break;
1942 /* Handle the start of a symbol. */
1943 if (is_idchar[(int) c] && nargs > 0) {
1944 char *id_beg = p - 1;
1945 size_t id_len;
1947 --exp_p;
1948 while (p != limit && is_idchar[(int) *p])
1950 p++;
1953 id_len = size_fromInt (p - id_beg);
1955 if (is_idstart[(int) c]
1956 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '\"'))) {
1957 register struct arglist *arg;
1959 for (arg = arglist; arg != NULL; arg = arg->next) {
1960 struct reflist *tpat;
1962 if (arg->name[0] == c
1963 && arg->length == id_len
1964 && strncmp (arg->name, id_beg, id_len) == 0) {
1965 char *p1;
1967 if (expected_delimiter) { /* && CPPOPTIONS (pfile)->warn_stringify) { */
1968 if (FALSE) { /* cppReader_isTraditional (pfile)) { */
1969 voptgenerror (FLG_PREPROC,
1970 message ("macro argument `%x' is stringified.",
1971 cstring_prefix (cstring_fromChars (arg->name), id_len)),
1972 loc);
1974 } else {
1975 voptgenerror (FLG_PREPROC,
1976 message ("Macro arg `%x' would be stringified with -traditional.",
1977 cstring_prefix (cstring_fromChars (arg->name), id_len)),
1978 loc);
1982 /* If ANSI, don't actually substitute inside a string. */
1983 if (TRUE /* !cppReader_isTraditional (pfile) */ && expected_delimiter)
1984 /*@innerbreak@*/ break;
1985 /* make a pat node for this arg and append it to the end of
1986 the pat list */
1987 tpat = (struct reflist *) dmalloc (sizeof (*tpat));
1988 tpat->next = NULL;
1989 tpat->raw_before = (concat == id_beg);
1990 tpat->raw_after = 0;
1991 tpat->rest_args = arg->rest_args;
1992 tpat->stringify = (FALSE /* cppReader_isTraditional (pfile) */
1993 ? expected_delimiter != '\0'
1994 : stringify == id_beg);
1996 if (endpat == NULL)
1998 defn->pattern = tpat;
2000 else
2002 endpat->next = tpat;
2003 /*@-branchstate@*/
2004 } /*@=branchstate@*/ /* evs 2000 was =branchstate */
2006 endpat = tpat;
2008 tpat->argno = arg->argno;
2009 tpat->nchars = exp_p - lastp;
2011 p1 = p;
2013 SKIP_WHITE_SPACE (p1);
2015 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2017 tpat->raw_after = 1;
2020 lastp = exp_p; /* place to start copying from next time */
2021 skipped_arg = 1;
2023 /*@innerbreak@*/ break;
2028 /* If this was not a macro arg, copy it into the expansion. */
2029 if (skipped_arg == 0) {
2030 register char *lim1 = p;
2031 p = id_beg;
2033 while (p != lim1)
2035 *exp_p++ = *p++;
2038 if (stringify == id_beg)
2040 voptgenerror
2041 (FLG_PREPROC,
2042 cstring_makeLiteral ("# operator should be followed by a macro argument name"),
2043 loc);
2049 if (/*!cppReader_isTraditional (pfile) && */ expected_delimiter == '\0')
2051 /* If ANSI, put in a "@ " marker to prevent token pasting.
2052 But not if "inside a string" (which in ANSI mode
2053 happens only for -D option). */
2054 *exp_p++ = '@';
2055 *exp_p++ = ' ';
2058 *exp_p = '\0';
2060 defn->length = size_fromInt (exp_p - defn->expansion);
2062 /* Crash now if we overrun the allocated size. */
2063 if (defn->length + 1 > maxsize)
2065 llfatalbug (cstring_makeLiteral ("Maximum definition size exceeded."));
2068 /*@-compdef@*/ /* defn->expansion defined? */
2069 return defn;
2070 /*@=compdef@*/
2072 # endif
2075 * special extension string that can be added to the last macro argument to
2076 * allow it to absorb the "rest" of the arguments when expanded. Ex:
2077 * #define wow(a, b...) process (b, a, b)
2078 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
2079 * { wow (one, two); } -> { process (two, one, two); }
2080 * if this "rest_arg" is used with the concat token '##' and if it is not
2081 * supplied then the token attached to with ## will not be outputted. Ex:
2082 * #define wow (a, b...) process (b ## , a, ## b)
2083 * { wow (1, 2); } -> { process (2, 1, 2); }
2084 * { wow (one); } -> { process (one); {
2087 /*@-readonlytrans@*/
2088 static char rest_extension[] = "...";
2089 /*:=readonlytrans@*/
2091 /*@notfunction@*/
2092 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
2094 /* Create a DEFINITION node from a #define directive. Arguments are
2095 as for do_define. */
2098 static /*@null@*/ macroDef
2099 create_definition (/*@exposed@*/ char *buf, char *limit,
2100 cppReader *pfile, bool predefinition,
2101 bool noExpand)
2103 char *bp; /* temp ptr into input buffer */
2104 char *symname; /* remember where symbol name starts */
2105 size_t sym_length; /* and how long it is */
2106 int rest_args = 0; /* really int! */
2107 int line;
2108 int col;
2109 cstring file = (CPPBUFFER (pfile) != NULL)
2110 ? CPPBUFFER (pfile)->nominal_fname : cstring_makeLiteralTemp ("");
2111 DEFINITION *defn;
2112 int arglengths = 0; /* Accumulate lengths of arg names
2113 plus number of args. */
2114 macroDef mdef;
2115 char save = *limit;
2116 *limit = '\0';
2117 DPRINTF (("Create definition: %s", buf));
2118 *limit = save;
2120 cppBuffer_getLineAndColumn (CPPBUFFER (pfile), &line, &col);
2122 bp = buf;
2124 while (is_hor_space[(int) *bp])
2126 bp++;
2129 symname = bp; /* remember where it starts */
2131 sym_length = cppReader_checkMacroName (pfile, bp, cstring_makeLiteralTemp ("macro"));
2133 bp += sym_length;
2135 /* Lossage will occur if identifiers or control keywords are broken
2136 across lines using backslash. This is not the right place to take
2137 care of that. */
2139 if (*bp == '(') {
2140 struct arglist *arg_ptrs = NULL;
2141 int argno = 0;
2143 bp++; /* skip '(' */
2144 SKIP_WHITE_SPACE (bp);
2146 /* Loop over macro argument names. */
2147 while (*bp != ')')
2149 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2150 temp->name = bp;
2151 temp->next = arg_ptrs;
2152 temp->argno = argno++;
2153 temp->rest_args = 0;
2155 arg_ptrs = temp;
2157 if (rest_args != 0)
2159 cppReader_pedwarn (pfile,
2160 message ("another parameter follows `%s'",
2161 cstring_fromChars (rest_extension)));
2164 if (!is_idstart[(int) *bp])
2166 cppReader_pedwarnLit (pfile,
2167 cstring_makeLiteralTemp ("Invalid character in macro parameter name"));
2170 /* Find the end of the arg name. */
2171 while (is_idchar[(int) *bp])
2173 bp++;
2174 /* do we have a "special" rest-args extension here? */
2175 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2176 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2178 rest_args = 1;
2179 temp->rest_args = 1;
2180 /*@innerbreak@*/ break;
2184 temp->length = size_fromInt (bp - temp->name);
2186 if (rest_args != 0)
2188 bp += REST_EXTENSION_LENGTH;
2191 arglengths += temp->length + 2;
2192 SKIP_WHITE_SPACE (bp);
2194 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2195 cppReader_errorLit (pfile,
2196 cstring_makeLiteralTemp ("Parameter list for #define is not parseable"));
2197 goto nope;
2200 if (*bp == ',') {
2201 bp++;
2202 SKIP_WHITE_SPACE (bp);
2204 if (bp >= limit) {
2205 cppReader_errorLit (pfile,
2206 cstring_makeLiteralTemp ("unterminated parameter list in `#define'"));
2207 goto nope;
2210 struct arglist *otemp;
2212 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2214 if (temp->length == otemp->length &&
2215 strncmp (temp->name, otemp->name, temp->length) == 0) {
2216 cstring name = cstring_copyLength (temp->name, temp->length);
2217 cppReader_error (pfile,
2218 message ("duplicate argument name `%x' in `#define'", name));
2219 goto nope;
2225 ++bp; /* skip paren */
2226 SKIP_WHITE_SPACE (bp);
2227 /* now everything from bp before limit is the definition. */
2228 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
2229 defn->rest_args = rest_args;
2231 /* Now set defn->args.argnames to the result of concatenating
2232 the argument names in reverse order
2233 with comma-space between them. */
2234 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2237 struct arglist *temp;
2238 int i = 0;
2239 for (temp = arg_ptrs; temp != NULL; temp = temp->next)
2241 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2242 i += temp->length;
2243 if (temp->next != 0)
2245 defn->args.argnames[i++] = ',';
2246 defn->args.argnames[i++] = ' ';
2250 defn->args.argnames[i] = '\0';
2253 sfree (arg_ptrs);
2254 } else {
2255 /* Simple expansion or empty definition. */
2257 if (bp < limit)
2259 if (is_hor_space[(int) *bp]) {
2260 bp++;
2261 SKIP_WHITE_SPACE (bp);
2262 } else {
2263 switch (*bp) {
2264 case '!': case '\"': case '#': case '%': case '&': case '\'':
2265 case ')': case '*': case '+': case ',': case '-': case '.':
2266 case '/': case ':': case ';': case '<': case '=': case '>':
2267 case '?': case '[': case '\\': case ']': case '^': case '{':
2268 case '|': case '}': case '~':
2269 cppReader_warning (pfile,
2270 message ("Missing white space after #define %x",
2271 cstring_prefix (cstring_fromChars (symname),
2272 sym_length)));
2273 break;
2275 default:
2276 cppReader_pedwarn (pfile,
2277 message ("Missing white space after #define %x",
2278 cstring_prefix (cstring_fromChars (symname),
2279 sym_length)));
2280 break;
2284 /* now everything from bp before limit is the definition. */
2285 defn = collect_expansion (pfile, bp, limit, -1, NULL);
2286 defn->args.argnames = mstring_createEmpty ();
2289 defn->noExpand = noExpand;
2290 DPRINTF (("No expand: %d", noExpand));
2292 defn->line = line;
2294 /* not: llassert (cstring_isUndefined (defn->file)); */
2295 defn->file = file;
2297 /* OP is null if this is a predefinition */
2298 defn->predefined = predefinition;
2299 mdef.defn = defn;
2300 mdef.symnam = symname;
2301 mdef.symlen = sym_length;
2303 return mdef;
2305 nope:
2306 mdef.defn = NULL;
2307 mdef.symnam = NULL;
2308 return mdef;
2311 # if 0
2312 /*@null@*/ macroDef
2313 cpplib_createDefinition (cstring def,
2314 fileloc loc,
2315 bool predefinition,
2316 bool noExpand)
2318 char *buf = cstring_toCharsSafe (def);
2319 char *limit = buf + cstring_length (def);
2320 char *bp; /* temp ptr into input buffer */
2321 char *symname; /* remember where symbol name starts */
2322 size_t sym_length; /* and how long it is */
2323 int rest_args = 0; /* really int! */
2324 int line = fileloc_lineno (loc);
2325 cstring file = fileloc_filename (loc);
2326 DEFINITION *defn;
2327 int arglengths = 0; /* Accumulate lengths of arg names
2328 plus number of args. */
2329 macroDef mdef;
2331 bp = buf;
2333 DPRINTF (("Creating definition: %s", buf));
2335 while (is_hor_space[(int) *bp])
2337 bp++;
2340 symname = bp; /* remember where it starts */
2342 sym_length = cppReader_checkMacroNameLoc (loc, symname, cstring_makeLiteralTemp ("macro"));
2344 DPRINTF (("length: %d", sym_length));
2346 bp += sym_length;
2348 DPRINTF (("Here: %s", bp));
2350 /* Lossage will occur if identifiers or control keywords are broken
2351 across lines using backslash. This is not the right place to take
2352 care of that. */
2354 if (*bp == '(') {
2355 struct arglist *arg_ptrs = NULL;
2356 int argno = 0;
2358 bp++; /* skip '(' */
2359 SKIP_WHITE_SPACE (bp);
2361 /* Loop over macro argument names. */
2362 while (*bp != ')')
2364 struct arglist *temp = (struct arglist *) dmalloc (sizeof (*temp));
2365 temp->name = bp;
2366 temp->next = arg_ptrs;
2367 temp->argno = argno++;
2368 temp->rest_args = 0;
2370 arg_ptrs = temp;
2372 if (rest_args != 0)
2374 voptgenerror (FLG_PREPROC,
2375 message ("Another parameter follows %s",
2376 cstring_fromChars (rest_extension)),
2377 loc);
2380 if (!is_idstart[(int) *bp])
2382 voptgenerror (FLG_PREPROC,
2383 message ("Invalid character in macro parameter name: %c", *bp),
2384 loc);
2387 /* Find the end of the arg name. */
2388 while (is_idchar[(int) *bp])
2390 bp++;
2391 /* do we have a "special" rest-args extension here? */
2392 if (limit - bp > size_toInt (REST_EXTENSION_LENGTH)
2393 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
2395 rest_args = 1;
2396 temp->rest_args = 1;
2397 /*@innerbreak@*/ break;
2401 temp->length = size_fromInt (bp - temp->name);
2403 if (rest_args != 0)
2405 bp += REST_EXTENSION_LENGTH;
2408 arglengths += temp->length + 2;
2409 SKIP_WHITE_SPACE (bp);
2411 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2412 voptgenerror (FLG_PREPROC,
2413 cstring_makeLiteral ("Parameter list for #define is not parseable"),
2414 loc);
2415 goto nope;
2418 if (*bp == ',') {
2419 bp++;
2420 SKIP_WHITE_SPACE (bp);
2422 if (bp >= limit) {
2423 voptgenerror (FLG_PREPROC,
2424 cstring_makeLiteral ("Unterminated parameter list in #define'"),
2425 loc);
2426 goto nope;
2429 struct arglist *otemp;
2431 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
2433 if (temp->length == otemp->length &&
2434 strncmp (temp->name, otemp->name, temp->length) == 0) {
2435 cstring name = cstring_copyLength (temp->name, temp->length);
2437 voptgenerror (FLG_PREPROC,
2438 message ("Duplicate argument name in #define: %s", name),
2439 loc);
2440 goto nope;
2446 ++bp; /* skip paren */
2447 SKIP_WHITE_SPACE (bp);
2448 /* now everything from bp before limit is the definition. */
2449 defn = collect_expansionLoc (loc, bp, limit, argno, arg_ptrs);
2450 defn->rest_args = rest_args;
2452 /* Now set defn->args.argnames to the result of concatenating
2453 the argument names in reverse order
2454 with comma-space between them. */
2455 defn->args.argnames = (char *) dmalloc (size_fromInt (arglengths + 1));
2458 struct arglist *temp;
2459 int i = 0;
2460 for (temp = arg_ptrs; temp != NULL; temp = temp->next) {
2461 memcpy (&defn->args.argnames[i], temp->name, temp->length);
2462 i += temp->length;
2463 if (temp->next != 0) {
2464 defn->args.argnames[i++] = ',';
2465 defn->args.argnames[i++] = ' ';
2469 defn->args.argnames[i] = '\0';
2472 sfree (arg_ptrs);
2473 } else {
2474 /* Simple expansion or empty definition. */
2476 if (bp < limit)
2478 if (is_hor_space[(int) *bp]) {
2479 bp++;
2480 SKIP_WHITE_SPACE (bp);
2481 } else {
2482 switch (*bp) {
2483 case '!': case '\"': case '#': case '%': case '&': case '\'':
2484 case ')': case '*': case '+': case ',': case '-': case '.':
2485 case '/': case ':': case ';': case '<': case '=': case '>':
2486 case '?': case '[': case '\\': case ']': case '^': case '{':
2487 case '|': case '}': case '~':
2488 voptgenerror (FLG_PREPROC,
2489 message ("Missing white space after #define %x",
2490 cstring_prefix (cstring_fromChars (symname),
2491 sym_length)),
2492 loc);
2493 break;
2495 default:
2496 voptgenerror (FLG_PREPROC,
2497 message ("Missing white space after #define %x",
2498 cstring_prefix (cstring_fromChars (symname),
2499 sym_length)),
2500 loc);
2501 break;
2506 /* now everything from bp before limit is the definition. */
2507 llassert (limit > bp);
2508 defn = collect_expansionLoc (loc, bp, limit, -1, NULL);
2509 defn->args.argnames = mstring_createEmpty ();
2512 defn->noExpand = noExpand;
2513 DPRINTF (("No expand: %d", noExpand));
2515 defn->line = line;
2517 /* not: llassert (cstring_isUndefined (defn->file)); */
2518 defn->file = file;
2520 /* OP is null if this is a predefinition */
2521 defn->predefined = predefinition;
2523 mdef.defn = defn;
2524 mdef.symnam = symname;
2525 mdef.symlen = sym_length;
2527 return mdef;
2529 nope:
2530 mdef.defn = NULL;
2531 mdef.symnam = NULL;
2532 return mdef;
2534 # endif
2536 /* Check a purported macro name SYMNAME, and yield its length.
2537 USAGE is the kind of name this is intended for. */
2539 static size_t cppReader_checkMacroName (cppReader *pfile, char *symname, cstring usage)
2541 char *p;
2542 size_t sym_length;
2544 for (p = symname; is_idchar[(int) *p]; p++)
2549 sym_length = size_fromInt (p - symname);
2551 if (sym_length == 0
2552 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2554 cppReader_error (pfile, message ("invalid %s name", usage));
2556 else if (!is_idstart[(int) *symname])
2558 char *msg = (char *) dmalloc (sym_length + 1);
2559 memcpy (msg, symname, sym_length);
2560 msg[sym_length] = '\0';
2561 cppReader_error (pfile, message ("invalid %s name `%s'", usage,
2562 cstring_fromChars (msg)));
2563 sfree (msg);
2565 else
2567 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2569 cppReader_error (pfile, message ("invalid %s name `defined'", usage));
2573 return sym_length;
2576 # if 0
2577 /* FIXME */
2579 ** evans 2001-12-31
2580 ** Gasp...cut-and-pasted from above to deal with pfile (should replace throughout with this...)
2583 size_t cppReader_checkMacroNameLoc (fileloc loc, char *symname, cstring usage)
2585 char *p;
2586 size_t sym_length;
2588 for (p = symname; is_idchar[(int) *p]; p++)
2593 sym_length = size_fromInt (p - symname);
2595 if (sym_length == 0
2596 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '\"')))
2598 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2599 cstring_fromChars (symname)), loc);
2601 else if (!is_idstart[(int) *symname])
2603 char *msg = (char *) dmalloc (sym_length + 1);
2604 memcpy (msg, symname, sym_length);
2605 msg[sym_length] = '\0';
2606 voptgenerror (FLG_PREPROC, message ("Invalid %s name: %s", usage,
2607 cstring_fromChars (msg)),
2608 loc);
2609 sfree (msg);
2611 else
2613 if ((strncmp (symname, "defined", 7) == 0) && sym_length == 7)
2615 voptgenerror (FLG_PREPROC, message ("Invalid %s name: defined", usage), loc);
2619 return sym_length;
2621 # endif
2623 /* Return zero if two DEFINITIONs are isomorphic. */
2625 static bool
2626 compare_defs (DEFINITION *d1, DEFINITION *d2)
2628 register struct reflist *a1, *a2;
2629 register char *p1 = d1->expansion;
2630 register char *p2 = d2->expansion;
2631 bool first = TRUE;
2633 if (d1->nargs != d2->nargs)
2635 return TRUE;
2638 llassert (d1->args.argnames != NULL);
2639 llassert (d2->args.argnames != NULL);
2641 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames) != 0)
2643 return TRUE;
2646 for (a1 = d1->pattern, a2 = d2->pattern;
2647 (a1 != NULL) && (a2 != NULL);
2648 a1 = a1->next, a2 = a2->next) {
2649 if (!((a1->nchars == a2->nchars
2650 && (strncmp (p1, p2, size_fromInt (a1->nchars)) == 0))
2651 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2652 || a1->argno != a2->argno
2653 || a1->stringify != a2->stringify
2654 || a1->raw_before != a2->raw_before
2655 || a1->raw_after != a2->raw_after)
2656 return TRUE;
2657 first = 0;
2658 p1 += a1->nchars;
2659 p2 += a2->nchars;
2661 if (a1 != a2)
2662 return TRUE;
2664 if (comp_def_part (first, p1, size_toInt (d1->length - (p1 - d1->expansion)),
2665 p2, size_toInt (d2->length - (p2 - d2->expansion)), 1))
2666 return TRUE;
2668 return FALSE;
2672 ** Return TRUE if two parts of two macro definitions are effectively different.
2673 ** One of the parts starts at BEG1 and has LEN1 chars;
2674 ** the other has LEN2 chars at BEG2.
2675 ** Any sequence of whitespace matches any other sequence of whitespace.
2676 ** FIRST means these parts are the first of a macro definition;
2677 ** so ignore leading whitespace entirely.
2678 ** LAST means these parts are the last of a macro definition;
2679 ** so ignore trailing whitespace entirely.
2682 static bool
2683 comp_def_part (bool first, char *beg1, int len1, char *beg2, int len2, bool last)
2685 char *end1 = beg1 + len1;
2686 char *end2 = beg2 + len2;
2688 if (first) {
2689 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2690 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2692 if (last) {
2693 while (beg1 != end1 && is_space[(int) end1[-1]]) { end1--; }
2694 while (beg2 != end2 && is_space[(int) end2[-1]]) { end2--; }
2696 while (beg1 != end1 && beg2 != end2) {
2697 if (is_space[(int) *beg1] && is_space[(int) *beg2]) {
2698 while (beg1 != end1 && is_space[(int) *beg1]) { beg1++; }
2699 while (beg2 != end2 && is_space[(int) *beg2]) { beg2++; }
2700 } else if (*beg1 == *beg2) {
2701 beg1++; beg2++;
2702 } else break;
2704 return (beg1 != end1) || (beg2 != end2);
2708 ** Process a #define command.
2709 ** BUF points to the contents of the #define command, as a contiguous string.
2710 ** LIMIT points to the first character past the end of the definition.
2711 ** KEYWORD is the keyword-table entry for #define,
2712 ** or NULL for a "predefined" macro.
2715 static int
2716 do_defineAux (cppReader *pfile, struct directive *keyword,
2717 /*@exposed@*/ char *buf, char *limit, bool noExpand)
2719 int hashcode;
2720 macroDef mdef;
2721 hashNode hp;
2723 mdef = create_definition (buf, limit, pfile, keyword == NULL, noExpand);
2725 if (mdef.defn == 0)
2726 goto nope;
2728 hashcode = cpphash_hashCode (mdef.symnam, mdef.symlen, CPP_HASHSIZE);
2730 if ((hp = cpphash_lookup (mdef.symnam, size_toInt (mdef.symlen), hashcode)) != NULL)
2732 bool ok = FALSE;
2734 /* Redefining a precompiled key is ok. */
2735 if (hp->type == T_PCSTRING)
2736 ok = TRUE;
2737 /* Redefining a macro is ok if the definitions are the same. */
2738 else if (hp->type == T_MACRO)
2739 ok = !compare_defs (mdef.defn, hp->value.defn);
2740 /* Redefining a constant is ok with -D. */
2741 else if (hp->type == T_CONST)
2742 ok = !pfile->done_initializing;
2743 else {
2744 ok = FALSE; /* Redefining anything else is bad. */
2747 /* Print the warning if it's not ok. */
2748 if (!ok)
2751 ** If we are passing through #define and #undef directives, do
2752 ** that for this re-definition now.
2755 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2757 /* llassert (keyword != NULL); */
2758 pass_thru_directive (buf, limit, pfile, keyword);
2761 cpp_setLocation (pfile);
2763 if (hp->type == T_MACRO)
2765 if (hp->value.defn->noExpand)
2767 ; /* error will be reported checking macros */
2769 else
2771 genppllerrorhint
2772 (FLG_MACROREDEF,
2773 message ("Macro %q already defined",
2774 cstring_copyLength (mdef.symnam, mdef.symlen)),
2775 message ("%q: Previous definition of %q",
2776 fileloc_unparseRaw (hp->value.defn->file,
2777 (int) hp->value.defn->line),
2778 cstring_copyLength (mdef.symnam, mdef.symlen)));
2781 else
2783 genppllerror (FLG_MACROREDEF,
2784 message ("Macro %q already defined",
2785 cstring_copyLength (mdef.symnam,
2786 mdef.symlen)));
2790 /* Replace the old definition. */
2791 hp->type = T_MACRO;
2792 hp->value.defn = mdef.defn;
2794 else
2797 ** If we are passing through #define and #undef directives, do
2798 ** that for this new definition now.
2801 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
2803 pass_thru_directive (buf, limit, pfile, keyword);
2806 DPRINTF (("Define macro: %s / %d",
2807 mdef.symnam, mdef.defn->noExpand));
2809 cpphash_installMacro (mdef.symnam, mdef.symlen, mdef.defn, hashcode);
2810 /*@-branchstate@*/
2811 } /*@=branchstate@*/
2813 return 0;
2815 nope:
2816 return 1;
2819 static int
2820 do_define (cppReader *pfile, struct directive *keyword,
2821 /*@exposed@*/ char *buf, char *limit)
2823 DPRINTF (("Regular do define"));
2824 return do_defineAux (pfile, keyword, buf, limit, FALSE);
2828 ** This structure represents one parsed argument in a macro call.
2829 ** `raw' points to the argument text as written (`raw_length' is its length).
2830 ** `expanded' points to the argument's macro-expansion
2831 ** (its length is `expand_length').
2832 ** `stringified_length' is the length the argument would have
2833 ** if stringified.
2834 ** `use_count' is the number of times this macro arg is substituted
2835 ** into the macro. If the actual use count exceeds 10,
2836 ** the value stored is 10.
2839 /* raw and expanded are relative to ARG_BASE */
2840 /*@notfunction@*/
2841 #define ARG_BASE ((pfile)->token_buffer)
2843 struct argdata {
2844 /* Strings relative to pfile->token_buffer */
2845 long raw;
2846 size_t expanded;
2847 size_t stringified;
2848 int raw_length;
2849 int expand_length;
2850 int stringified_length;
2851 bool newlines;
2852 int use_count;
2856 ** Allocate a new cppBuffer for PFILE, and push it on the input buffer stack.
2857 ** If BUFFER != NULL, then use the LENGTH characters in BUFFER
2858 ** as the new input buffer.
2859 ** Return the new buffer, or NULL on failure.
2862 /*@null@*/ /*@exposed@*/ cppBuffer *
2863 cppReader_pushBuffer (cppReader *pfile, char *buffer, size_t length)
2865 cppBuffer *buf = cppReader_getBufferSafe (pfile);
2867 if (buf == pfile->buffer_stack)
2869 cppReader_fatalError
2870 (pfile,
2871 message ("%s: macro or `#include' recursion too deep",
2872 (buf->fname != NULL)
2873 ? buf->fname
2874 : cstring_makeLiteral ("<no name>")));
2875 sfreeEventually (buffer);
2876 return NULL;
2879 llassert (buf != NULL);
2881 buf--;
2882 memset ((char *) buf, 0, sizeof (*buf));
2883 DPRINTF (("Pushing buffer: %s", cstring_copyLength (buffer, length)));
2884 CPPBUFFER (pfile) = buf;
2886 buf->if_stack = pfile->if_stack;
2887 buf->cleanup = cppReader_nullCleanup;
2888 buf->underflow = cppReader_nullUnderflow;
2889 buf->buf = buffer;
2890 buf->cur = buf->buf;
2892 if (buffer != NULL)
2894 buf->alimit = buf->rlimit = buffer + length;
2896 else
2898 buf->alimit = buf->rlimit = NULL;
2901 return buf;
2904 cppBuffer *
2905 cppReader_popBuffer (cppReader *pfile)
2907 cppBuffer *buf = CPPBUFFER (pfile);
2909 llassert (buf != NULL);
2911 (void) (*buf->cleanup) (buf, pfile);
2912 return ++CPPBUFFER (pfile);
2916 ** Scan until CPPBUFFER (PFILE) is exhausted into PFILE->token_buffer.
2917 ** Pop the buffer when done.
2920 void
2921 cppReader_scanBuffer (cppReader *pfile)
2923 cppBuffer *buffer = CPPBUFFER (pfile);
2924 for (;;)
2926 enum cpp_token token;
2928 token = cpplib_getToken (pfile);
2930 if (token == CPP_EOF) /* Should not happen ... */
2932 break;
2935 if (token == CPP_POP && CPPBUFFER (pfile) == buffer)
2937 (void) cppReader_popBuffer (pfile);
2938 break;
2944 * Rescan a string (which may have escape marks) into pfile's buffer.
2945 * Place the result in pfile->token_buffer.
2947 * The input is copied before it is scanned, so it is safe to pass
2948 * it something from the token_buffer that will get overwritten
2949 * (because it follows cpplib_getWritten). This is used by do_include.
2952 static void
2953 cpp_expand_to_buffer (cppReader *pfile, char *buf, size_t length)
2955 register cppBuffer *ip;
2956 char *limit = buf + length;
2957 char *buf1, *p1, *p2;
2959 DPRINTF (("Expand to buffer: %s", cstring_copyLength (buf, length)));
2961 /* Set up the input on the input stack. */
2963 buf1 = (char *) dmalloc (length + 1);
2965 p1 = buf;
2966 p2 = buf1;
2968 while (p1 != limit)
2970 *p2++ = *p1++;
2973 buf1[length] = '\0';
2975 ip = cppReader_pushBuffer (pfile, buf1, length);
2977 if (ip == NULL)
2978 return;
2980 ip->has_escapes = TRUE;
2982 /* Scan the input, create the output. */
2983 cppReader_scanBuffer (pfile);
2985 cppReader_nullTerminate (pfile);
2988 static void
2989 adjust_position (char *buf, char *limit, int *linep, int *colp)
2991 while (buf < limit)
2993 char ch = *buf++;
2994 if (ch == '\n')
2995 (*linep)++, (*colp) = 1;
2996 else
2997 (*colp)++;
3001 /* Move line_base forward, updating lineno and colno. */
3003 static void
3004 update_position (cppBuffer *pbuf)
3006 char *old_pos;
3007 char *new_pos = pbuf->cur;
3008 register struct parse_marker *mark;
3010 llassert (pbuf->buf != NULL);
3011 old_pos = pbuf->buf + pbuf->line_base;
3013 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
3015 if (pbuf->buf + mark->position < new_pos)
3016 new_pos = pbuf->buf + mark->position;
3018 pbuf->line_base += new_pos - old_pos;
3020 llassert (old_pos != NULL);
3021 llassert (new_pos != NULL);
3023 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
3026 void
3027 cppBuffer_getLineAndColumn (/*@null@*/ cppBuffer *pbuf, /*@out@*/ int *linep,
3028 /*@null@*/ /*@out@*/ int *colp)
3030 int dummy;
3032 if (colp == NULL)
3034 colp = &dummy;
3035 /*@-branchstate@*/
3036 } /*@=branchstate@*/
3038 if (pbuf != NULL)
3040 *linep = pbuf->lineno;
3041 *colp = pbuf->colno;
3043 llassert (pbuf->buf != NULL);
3044 llassert (pbuf->cur != NULL);
3046 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
3048 else
3050 *linep = 0;
3051 *colp = 0;
3055 /* Return the cppBuffer that corresponds to a file (not a macro). */
3057 /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_fileBuffer (cppReader *pfile)
3059 cppBuffer *ip = cppReader_getBuffer (pfile);
3061 for ( ;
3062 ip != NULL && ip != cppReader_nullBuffer (pfile);
3063 ip = cppBuffer_prevBuffer (ip))
3065 if (ip->fname != NULL)
3067 return ip;
3071 return NULL;
3074 static long
3075 count_newlines (char *buf, char *limit)
3077 register long count = 0;
3079 while (buf < limit)
3081 char ch = *buf++;
3082 if (ch == '\n')
3083 count++;
3085 return count;
3089 * write out a #line command, for instance, after an #include file.
3090 * If CONDITIONAL is true, we can omit the #line if it would
3091 * appear to be a no-op, and we can output a few newlines instead
3092 * if we want to increase the line number by a small amount.
3093 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3096 static void
3097 output_line_command (cppReader *pfile, bool conditional,
3098 enum file_change_code file_change)
3100 int line, col;
3101 cppBuffer *ip = CPPBUFFER (pfile);
3102 cppBuffer *buf;
3104 llassert (ip != NULL);
3106 if (ip->fname == NULL)
3107 return;
3109 update_position (ip);
3111 if (CPPOPTIONS (pfile)->no_line_commands
3112 || CPPOPTIONS (pfile)->no_output)
3113 return;
3115 buf = CPPBUFFER (pfile);
3117 llassert (buf != NULL);
3119 line = buf->lineno;
3120 col = buf->colno;
3122 llassert (ip->cur != NULL);
3124 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3126 if (CPPOPTIONS (pfile)->no_line_commands)
3127 return;
3129 if (conditional) {
3130 if (line == pfile->lineno)
3131 return;
3133 /* If the inherited line number is a little too small,
3134 output some newlines instead of a #line command. */
3136 if (line > pfile->lineno && line < pfile->lineno + 8)
3138 cpplib_reserve (pfile, 20);
3139 while (line > pfile->lineno)
3141 cppReader_putCharQ (pfile, '\n');
3142 pfile->lineno++;
3145 return;
3149 cpplib_reserve (pfile, 4 * cstring_length (ip->nominal_fname) + 50);
3152 #ifdef OUTPUT_LINE_COMMANDS
3153 static char sharp_line[] = "#line ";
3154 #else
3155 static char sharp_line[] = "# ";
3156 #endif
3157 cppReader_putStrN (pfile, sharp_line, sizeof(sharp_line)-1);
3160 sprintf (cpplib_getPWritten (pfile), "%d ", line);
3161 cppReader_adjustWritten (pfile, strlen (cpplib_getPWritten (pfile)));
3163 quote_string (pfile, cstring_toCharsSafe (ip->nominal_fname));
3165 if (file_change != same_file) {
3166 cppReader_putCharQ (pfile, ' ');
3167 cppReader_putCharQ (pfile, file_change == enter_file ? '1' : '2');
3169 /* Tell cc1 if following text comes from a system header file. */
3170 if (ip->system_header_p) {
3171 cppReader_putCharQ (pfile, ' ');
3172 cppReader_putCharQ (pfile, '3');
3174 cppReader_putCharQ (pfile, '\n');
3175 pfile->lineno = line;
3180 * Parse a macro argument and append the info on PFILE's token_buffer.
3181 * REST_ARGS means to absorb the rest of the args.
3182 * Return nonzero to indicate a syntax error.
3185 static enum cpp_token
3186 macarg (cppReader *pfile, int rest_args)
3188 int paren = 0;
3189 enum cpp_token token;
3190 char save_put_out_comments = CPPOPTIONS (pfile)->put_out_comments;
3191 bool oldexpand = pfile->no_macro_expand;
3192 CPPOPTIONS (pfile)->put_out_comments = 1;
3194 /* Try to parse as much of the argument as exists at this
3195 input stack level. */
3197 pfile->no_macro_expand = TRUE;
3199 for (;;)
3201 token = cpplib_getToken (pfile);
3203 switch (token)
3205 case CPP_EOF:
3206 goto done;
3207 case CPP_POP:
3208 /* If we've hit end of file, it's an error (reported by caller).
3209 Ditto if it's the end of cpp_expand_to_buffer text.
3210 If we've hit end of macro, just continue. */
3211 if (!cppBuffer_isMacro (CPPBUFFER (pfile)))
3212 goto done;
3213 /*@switchbreak@*/ break;
3214 case CPP_LPAREN:
3215 paren++;
3216 /*@switchbreak@*/ break;
3217 case CPP_RPAREN:
3218 if (--paren < 0)
3219 goto found;
3220 /*@switchbreak@*/ break;
3221 case CPP_COMMA:
3222 /* if we've returned to lowest level and
3223 we aren't absorbing all args */
3224 if (paren == 0 && rest_args == 0)
3225 goto found;
3226 /*@switchbreak@*/ break;
3227 found:
3228 /* Remove ',' or ')' from argument buffer. */
3229 cppReader_adjustWritten (pfile, -1);
3230 goto done;
3231 default:
3236 done:
3237 CPPOPTIONS (pfile)->put_out_comments = save_put_out_comments;
3238 pfile->no_macro_expand = oldexpand;
3240 return token;
3244 /* Turn newlines to spaces in the string of length LENGTH at START,
3245 except inside of string constants.
3246 The string is copied into itself with its beginning staying fixed. */
3248 static int
3249 change_newlines (char *start, int length)
3251 register char *ibp;
3252 register char *obp;
3253 register char *limit;
3254 char c;
3256 ibp = start;
3257 limit = start + length;
3258 obp = start;
3260 while (ibp < limit) {
3261 *obp++ = c = *ibp++;
3262 switch (c) {
3264 case '\'':
3265 case '\"':
3266 /* Notice and skip strings, so that we don't delete newlines in them. */
3268 char quotec = c;
3269 while (ibp < limit) {
3270 *obp++ = c = *ibp++;
3271 if (c == quotec)
3272 /*@innerbreak@*/ break;
3273 if (c == '\n' && quotec == '\'')
3274 /*@innerbreak@*/ break;
3277 /*@switchbreak@*/ break;
3281 return obp - start;
3284 static /*@observer@*/ struct tm *
3285 timestamp (/*@returned@*/ cppReader *pfile)
3287 if (pfile->timebuf == NULL)
3289 time_t t = time ((time_t *) 0);
3290 pfile->timebuf = localtime (&t);
3293 llassert (pfile->timebuf != NULL);
3295 return pfile->timebuf;
3298 static /*@observer@*/ const char* monthnames[] = {
3299 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3300 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3304 * expand things like __FILE__. Place the expansion into the output
3305 * buffer *without* rescanning.
3308 static void
3309 special_symbol (hashNode hp, cppReader *pfile)
3311 cstring buf = cstring_undefined;
3312 size_t len;
3313 int true_indepth;
3314 cppBuffer *ip;
3316 int paren = 0; /* For special `defined' keyword */
3318 for (ip = cppReader_getBuffer (pfile); ip != NULL; ip = cppBuffer_prevBuffer (ip))
3320 if (ip == cppReader_nullBuffer (pfile))
3322 cppReader_errorLit (pfile,
3323 cstring_makeLiteralTemp ("cccp error: not in any file?!"));
3324 return; /* the show must go on */
3327 if (ip != NULL && ip->fname != NULL)
3329 break;
3333 switch (hp->type)
3335 case T_FILE:
3336 case T_BASE_FILE:
3338 char *string;
3339 if (hp->type == T_BASE_FILE)
3341 while (cppBuffer_prevBuffer (ip) != cppReader_nullBuffer (pfile))
3343 ip = cppBuffer_prevBuffer (ip);
3347 llassert (ip != NULL);
3348 string = cstring_toCharsSafe (ip->nominal_fname);
3350 if (string == NULL)
3352 string = "";
3355 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3356 quote_string (pfile, string);
3357 return;
3359 case T_FUNC: /* added in ISO C99 */
3361 /* We don't know the actual name of the function, but it doesn't matter */
3362 char *string = "** function-name **";
3363 cpplib_reserve (pfile, 3 + 4 * strlen (string));
3364 quote_string (pfile, string);
3365 return;
3368 case T_INCLUDE_LEVEL:
3369 true_indepth = 0;
3370 ip = cppReader_getBuffer (pfile);
3372 for (; ip != cppReader_nullBuffer (pfile) && ip != NULL;
3373 ip = cppBuffer_prevBuffer (ip))
3375 if (ip != NULL && ip->fname != NULL)
3377 true_indepth++;
3381 buf = message ("%d", true_indepth - 1);
3382 break;
3384 case T_VERSION:
3385 buf = cstring_makeLiteral ("\"--- cpp version---\"");
3386 break;
3388 #ifndef NO_BUILTIN_SIZE_TYPE
3389 case T_SIZE_TYPE:
3390 buf = cstring_makeLiteral (SIZE_TYPE);
3391 break;
3392 #endif
3394 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3395 case T_PTRDIFF_TYPE:
3396 buf = cstring_makeLiteral (PTRDIFF_TYPE);
3397 break;
3398 #endif
3400 case T_WCHAR_TYPE:
3401 buf = cstring_makeLiteral (WCHAR_TYPE);
3402 break;
3404 case T_USER_LABEL_PREFIX_TYPE:
3405 buf = cstring_makeLiteral (USER_LABEL_PREFIX);
3406 break;
3408 case T_REGISTER_PREFIX_TYPE:
3409 buf = cstring_makeLiteral (REGISTER_PREFIX);
3410 break;
3412 case T_CONST:
3413 buf = message ("%d", hp->value.ival);
3414 break;
3416 case T_SPECLINE:
3418 if (ip != NULL)
3420 int line = ip->lineno;
3421 int col = ip->colno;
3423 llassert (ip->cur != NULL);
3424 adjust_position (cppLineBase (ip), ip->cur, &line, &col);
3426 buf = message ("%d", (int) line);
3428 else
3430 BADBRANCH;
3433 break;
3435 case T_DATE:
3436 case T_TIME:
3438 #if 1
3439 struct tm *timebuf;
3440 char *sbuf = (char *) dmalloc (20);
3441 timebuf = timestamp (pfile);
3442 if (hp->type == T_DATE)
3444 sprintf (sbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
3445 timebuf->tm_mday, timebuf->tm_year + 1900);
3447 else
3449 sprintf (sbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
3450 timebuf->tm_sec);
3453 buf = cstring_fromCharsNew (sbuf);
3454 sfree (sbuf);
3455 #endif
3456 break;
3459 case T_SPEC_DEFINED:
3460 buf = cstring_makeLiteral (" 0 "); /* Assume symbol is not defined */
3461 ip = cppReader_getBuffer (pfile);
3462 llassert (ip != NULL);
3463 llassert (ip->cur != NULL);
3464 SKIP_WHITE_SPACE (ip->cur);
3466 if (*ip->cur == '(')
3468 paren++;
3469 ip->cur++; /* Skip over the paren */
3470 SKIP_WHITE_SPACE (ip->cur);
3473 if (!is_idstart[(int) *ip->cur])
3474 goto oops;
3475 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '\"'))
3476 goto oops;
3478 if ((hp = cpphash_lookup (ip->cur, -1, -1)) != 0)
3480 cstring_free (buf);
3481 buf = cstring_makeLiteral (" 1 ");
3484 while (is_idchar[(int) *ip->cur])
3486 ++ip->cur;
3489 SKIP_WHITE_SPACE (ip->cur);
3491 if (paren != 0)
3493 if (*ip->cur != ')')
3494 goto oops;
3495 ++ip->cur;
3497 break;
3499 oops:
3501 cppReader_errorLit (pfile,
3502 cstring_makeLiteralTemp ("`defined' without an identifier"));
3503 break;
3505 default:
3506 cpp_setLocation (pfile);
3507 llfatalerror (message ("Pre-processing error: invalid special hash type"));
3510 len = cstring_length (buf);
3512 cpplib_reserve (pfile, len + 1);
3513 cppReader_putStrN (pfile, cstring_toCharsSafe (buf), len);
3514 cppReader_nullTerminateQ (pfile);
3516 cstring_free (buf);
3517 return;
3520 /* Write out a #define command for the special named MACRO_NAME
3521 to PFILE's token_buffer. */
3523 static void
3524 dump_special_to_buffer (cppReader *pfile, char *macro_name)
3526 static char define_directive[] = "#define ";
3527 size_t macro_name_length = strlen (macro_name);
3528 output_line_command (pfile, 0, same_file);
3529 cpplib_reserve (pfile, sizeof(define_directive) + macro_name_length);
3530 cppReader_putStrN (pfile, define_directive, sizeof(define_directive)-1);
3531 cppReader_putStrN (pfile, macro_name, macro_name_length);
3532 cppReader_putCharQ (pfile, ' ');
3533 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
3534 cppReader_putChar (pfile, '\n');
3537 /* Initialize the built-in macros. */
3539 static void
3540 cpplib_installBuiltin (/*@observer@*/ char *name, ctype ctyp,
3541 int len, enum node_type type,
3542 int ivalue, /*@null@*/ /*@only@*/ char *value,
3543 int hash)
3545 cstring sname = cstring_fromCharsNew (name);
3547 llassert (usymtab_inGlobalScope ());
3550 ** Be careful here: this is done before the ctype table has
3551 ** been initialized.
3554 if (!usymtab_exists (sname))
3556 uentry ue = uentry_makeConstant (sname, ctyp, fileloc_createBuiltin ());
3558 if (ctype_equal (ctyp, ctype_string))
3560 qualList ql = qualList_new ();
3561 ql = qualList_add (ql, qual_createObserver ());
3562 uentry_reflectQualifiers (ue, ql);
3563 qualList_free (ql);
3566 usymtab_addGlobalEntry (ue);
3568 else
3573 (void) cpphash_install (name, len, type, ivalue, value, hash);
3574 cstring_free (sname);
3577 static void
3578 cpplib_installBuiltinType (/*@observer@*/ char *name, ctype ctyp,
3579 int len, enum node_type type,
3580 int ivalue,
3581 /*@only@*/ /*@null@*/ char *value, int hash)
3583 cstring sname = cstring_fromChars (name);
3585 llassert (usymtab_inGlobalScope ());
3587 if (!usymtab_existsTypeEither (sname))
3589 uentry ue = uentry_makeDatatype (sname, ctyp,
3590 NO, qual_createConcrete (),
3591 fileloc_createBuiltin ());
3592 llassert (!usymtab_existsEither (sname));
3593 usymtab_addGlobalEntry (ue);
3596 (void) cpphash_install (name, len, type, ivalue, value, hash);
3599 static void
3600 initialize_builtins (cppReader *pfile)
3602 cpplib_installBuiltin ("__LINE__", ctype_int, -1, T_SPECLINE, 0, NULL, -1);
3603 cpplib_installBuiltin ("__DATE__", ctype_string, -1, T_DATE, 0, NULL, -1);
3604 cpplib_installBuiltin ("__FILE__", ctype_string, -1, T_FILE, 0, NULL, -1);
3605 cpplib_installBuiltin ("__BASE_FILE__", ctype_string, -1, T_BASE_FILE, 0, NULL, -1);
3606 cpplib_installBuiltin ("__func__", ctype_string, -1, T_FUNC, 0, NULL, -1);
3607 cpplib_installBuiltin ("__INCLUDE_LEVEL__", ctype_int, -1, T_INCLUDE_LEVEL, 0, NULL, -1);
3608 cpplib_installBuiltin ("__VERSION__", ctype_string, -1, T_VERSION, 0, NULL, -1);
3609 #ifndef NO_BUILTIN_SIZE_TYPE
3610 cpplib_installBuiltinType ("__SIZE_TYPE__", ctype_anyintegral, -1, T_SIZE_TYPE, 0, NULL, -1);
3611 #endif
3612 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3613 cpplib_installBuiltinType ("__PTRDIFF_TYPE__", ctype_anyintegral, -1, T_PTRDIFF_TYPE, 0, NULL, -1);
3614 #endif
3615 cpplib_installBuiltinType ("__WCHAR_TYPE__", ctype_anyintegral, -1, T_WCHAR_TYPE, 0, NULL, -1);
3616 cpplib_installBuiltin ("__USER_LABEL_PREFIX__", ctype_string, -1, T_USER_LABEL_PREFIX_TYPE, 0, NULL, -1);
3617 cpplib_installBuiltin ("__REGISTER_PREFIX__", ctype_string, -1, T_REGISTER_PREFIX_TYPE, 0, NULL, -1);
3618 cpplib_installBuiltin ("__TIME__", ctype_string, -1, T_TIME, 0, NULL, -1);
3620 if (!cppReader_isTraditional (pfile))
3622 cpplib_installBuiltin ("__STDC__", ctype_int, -1, T_CONST, STDC_VALUE, NULL, -1);
3626 ** This is supplied using a -D by the compiler driver
3627 ** so that it is present only when truly compiling with GNU C.
3630 /* cpplib_install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
3632 cpplib_installBuiltin ("S_SPLINT_S", ctype_int, -1, T_CONST, 2, NULL, -1);
3633 cpplib_installBuiltin ("__LCLINT__", ctype_int, -1, T_CONST, 2, NULL, -1);
3635 if (CPPOPTIONS (pfile)->debug_output)
3637 dump_special_to_buffer (pfile, "__BASE_FILE__");
3638 dump_special_to_buffer (pfile, "__VERSION__");
3639 #ifndef NO_BUILTIN_SIZE_TYPE
3640 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
3641 #endif
3642 #ifndef NO_BUILTIN_PTRDIFF_TYPE
3643 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
3644 #endif
3645 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
3646 dump_special_to_buffer (pfile, "__DATE__");
3647 dump_special_to_buffer (pfile, "__TIME__");
3648 if (!cppReader_isTraditional (pfile))
3649 dump_special_to_buffer (pfile, "__STDC__");
3654 /* Return 1 iff a token ending in C1 followed directly by a token C2
3655 could cause mis-tokenization. */
3657 static bool
3658 unsafe_chars (char c1, char c2)
3660 switch (c1)
3662 case '+': case '-':
3663 if (c2 == c1 || c2 == '=')
3664 return 1;
3665 goto letter;
3666 case '.':
3667 case '0': case '1': case '2': case '3': case '4':
3668 case '5': case '6': case '7': case '8': case '9':
3669 case 'e': case 'E': case 'p': case 'P':
3670 if (c2 == '-' || c2 == '+')
3671 return 1; /* could extend a pre-processing number */
3672 goto letter;
3673 case 'L':
3674 if (c2 == '\'' || c2 == '\"')
3675 return 1; /* Could turn into L"xxx" or L'xxx'. */
3676 goto letter;
3677 letter:
3678 case '_':
3679 case 'a': case 'b': case 'c': case 'd': case 'f':
3680 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3681 case 'm': case 'n': case 'o': case 'q': case 'r':
3682 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3683 case 'y': case 'z':
3684 case 'A': case 'B': case 'C': case 'D': case 'F':
3685 case 'G': case 'H': case 'I': case 'J': case 'K':
3686 case 'M': case 'N': case 'O': case 'Q': case 'R':
3687 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3688 case 'Y': case 'Z':
3689 /* We're in the middle of either a name or a pre-processing number. */
3690 return (is_idchar[(int) c2] || c2 == '.');
3691 case '<': case '>': case '!': case '%': case '#': case ':':
3692 case '^': case '&': case '|': case '*': case '/': case '=':
3693 return (c2 == c1 || c2 == '=');
3695 return 0;
3698 /* Expand a macro call.
3699 HP points to the symbol that is the macro being called.
3700 Put the result of expansion onto the input stack
3701 so that subsequent input by our caller will use it.
3703 If macro wants arguments, caller has already verified that
3704 an argument list follows; arguments come from the input stack. */
3706 static void
3707 cpplib_macroExpand (cppReader *pfile, /*@dependent@*/ hashNode hp)
3709 int nargs;
3710 DEFINITION *defn = hp->value.defn;
3711 char *xbuf;
3712 char *oxbuf = NULL;
3713 int start_line;
3714 int start_column;
3715 int end_line;
3716 int end_column;
3717 size_t xbuf_len;
3718 size_t old_written = cpplib_getWritten (pfile);
3719 int rest_args;
3720 int rest_zero = 0;
3721 int i;
3722 struct argdata *args = NULL;
3724 pfile->output_escapes++;
3725 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &start_line, &start_column);
3726 DPRINTF (("Expand macro: %d:%d", start_line, start_column));
3728 nargs = defn->nargs;
3730 if (nargs >= 0)
3732 enum cpp_token token = CPP_EOF;
3734 args = (struct argdata *) dmalloc ((nargs + 1) * sizeof (*args));
3736 for (i = 0; i < nargs; i++)
3738 args[i].expanded = 0;
3739 args[i].raw = 0;
3740 args[i].raw_length = 0;
3741 args[i].expand_length = args[i].stringified_length = -1;
3742 args[i].use_count = 0;
3746 ** Parse all the macro args that are supplied. I counts them.
3747 ** The first NARGS args are stored in ARGS.
3748 ** The rest are discarded. If rest_args is set then we assume
3749 ** macarg absorbed the rest of the args.
3752 i = 0;
3753 rest_args = 0;
3755 cppReader_forward (pfile, 1); /* Discard the open-parenthesis before the first arg. */
3758 if (rest_args != 0)
3760 continue;
3763 if (i < nargs || (nargs == 0 && i == 0))
3765 /* if we are working on last arg which absorbs rest of args... */
3766 if (i == nargs - 1 && defn->rest_args)
3768 rest_args = 1;
3771 args[i].raw = size_toLong (cpplib_getWritten (pfile));
3772 token = macarg (pfile, rest_args);
3773 args[i].raw_length = size_toInt (cpplib_getWritten (pfile) - args[i].raw);
3774 args[i].newlines = FALSE; /* FIXME */
3776 else
3778 token = macarg (pfile, 0);
3781 if (token == CPP_EOF || token == CPP_POP)
3783 cppReader_errorWithLine (pfile, start_line, start_column,
3784 cstring_fromCharsNew ("unterminated macro call"));
3785 sfree (args);
3786 return;
3788 i++;
3789 } while (token == CPP_COMMA);
3791 /* If we got one arg but it was just whitespace, call that 0 args. */
3792 if (i == 1)
3794 char *bp;
3795 char *lim;
3797 assertSet (args);
3799 bp = ARG_BASE + args[0].raw;
3800 lim = bp + args[0].raw_length;
3802 /* cpp.texi says for foo ( ) we provide one argument.
3803 However, if foo wants just 0 arguments, treat this as 0. */
3805 if (nargs == 0)
3807 while (bp != lim && is_space[(int) *bp])
3809 bp++;
3813 if (bp == lim)
3814 i = 0;
3817 /* Don't output an error message if we have already output one for
3818 a parse error above. */
3819 rest_zero = 0;
3821 if (nargs == 0 && i > 0)
3823 cppReader_error (pfile,
3824 message ("arguments given to macro `%s'", hp->name));
3826 else if (i < nargs)
3828 /* traditional C allows foo() if foo wants one argument. */
3829 if (nargs == 1 && i == 0 && cppReader_isTraditional (pfile))
3833 /* the rest args token is allowed to absorb 0 tokens */
3834 else if (i == nargs - 1 && defn->rest_args)
3835 rest_zero = 1;
3836 else if (i == 0)
3837 cppReader_error (pfile,
3838 message ("macro `%s' used without args", hp->name));
3839 else if (i == 1)
3840 cppReader_error (pfile,
3841 message ("macro `%s' used with just one arg", hp->name));
3842 else
3844 cppReader_error (pfile,
3845 message ("macro `%s' used with only %d args",
3846 hp->name, i));
3849 else if (i > nargs)
3851 cppReader_error (pfile,
3852 message ("macro `%s' used with too many (%d) args", hp->name, i));
3854 else
3861 ** If the agrument list was multiple lines, need to insert new lines to keep line
3862 ** numbers accurate.
3865 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile), &end_line, &end_column);
3866 DPRINTF (("Expand macro: %d:%d", end_line, end_column));
3868 /* If macro wants zero args, we parsed the arglist for checking only.
3869 Read directly from the macro definition. */
3871 if (nargs <= 0)
3873 xbuf = defn->expansion;
3874 xbuf_len = defn->length;
3876 else
3878 char *exp = defn->expansion;
3879 int offset; /* offset in expansion,
3880 copied a piece at a time */
3881 size_t totlen; /* total amount of exp buffer filled so far */
3883 register struct reflist *ap, *last_ap;
3885 assertSet (args); /* args is defined since the nargs > 0 path was taken */
3887 /* Macro really takes args. Compute the expansion of this call. */
3889 /* Compute length in characters of the macro's expansion.
3890 Also count number of times each arg is used. */
3891 xbuf_len = defn->length;
3893 llassert (args != NULL);
3895 for (ap = defn->pattern; ap != NULL; ap = ap->next)
3897 if (ap->stringify)
3899 struct argdata *arg = &args[ap->argno];
3901 /* Stringify it it hasn't already been */
3902 assertSet (arg);
3904 if (arg->stringified_length < 0)
3906 int arglen = arg->raw_length;
3907 bool escaped = FALSE;
3908 char in_string = '\0';
3909 char c;
3911 /* Initially need_space is -1. Otherwise, 1 means the
3912 previous character was a space, but we suppressed it;
3913 0 means the previous character was a non-space. */
3914 int need_space = -1;
3916 i = 0;
3917 arg->stringified = cpplib_getWritten (pfile);
3918 if (!cppReader_isTraditional (pfile))
3919 cppReader_putChar (pfile, '\"'); /* insert beginning quote */
3920 for (; i < arglen; i++)
3922 c = (ARG_BASE + arg->raw)[i];
3924 if (in_string == '\0')
3926 /* Internal sequences of whitespace are replaced by
3927 one space except within an string or char token.*/
3928 if (is_space[(int) c])
3930 if (cpplib_getWritten (pfile) > arg->stringified
3931 && (cpplib_getPWritten (pfile))[-1] == '@')
3933 /* "@ " escape markers are removed */
3934 cppReader_adjustWritten (pfile, -1);
3935 /*@innercontinue@*/ continue;
3937 if (need_space == 0)
3938 need_space = 1;
3939 /*@innercontinue@*/ continue;
3941 else if (need_space > 0)
3942 cppReader_putChar (pfile, ' ');
3943 else
3948 need_space = 0;
3951 if (escaped)
3952 escaped = 0;
3953 else
3955 if (c == '\\')
3956 escaped = 1;
3958 if (in_string != '\0')
3960 if (c == in_string)
3961 in_string = '\0';
3963 else if (c == '\"' || c == '\'')
3965 in_string = c;
3967 else
3973 /* Escape these chars */
3974 if (c == '\"' || (in_string != '\0' && c == '\\'))
3975 cppReader_putChar (pfile, '\\');
3976 if (isprint (c))
3977 cppReader_putChar (pfile, c);
3978 else
3980 cpplib_reserve (pfile, 4);
3981 sprintf (cpplib_getPWritten (pfile), "\\%03o",
3982 (unsigned int) c);
3983 cppReader_adjustWritten (pfile, 4);
3986 if (!cppReader_isTraditional (pfile))
3987 cppReader_putChar (pfile, '\"'); /* insert ending quote */
3988 arg->stringified_length
3989 = size_toInt (cpplib_getWritten (pfile) - arg->stringified);
3992 xbuf_len += args[ap->argno].stringified_length;
3994 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
3996 /* Add 4 for two newline-space markers to prevent token concatenation. */
3997 assertSet (args); /* Splint shouldn't need this */
3998 xbuf_len += args[ap->argno].raw_length + 4;
4000 else
4002 /* We have an ordinary (expanded) occurrence of the arg.
4003 So compute its expansion, if we have not already. */
4005 assertSet (args); /* shouldn't need this */
4007 if (args[ap->argno].expand_length < 0)
4009 args[ap->argno].expanded = cpplib_getWritten (pfile);
4010 cpp_expand_to_buffer (pfile,
4011 ARG_BASE + args[ap->argno].raw,
4012 size_fromInt (args[ap->argno].raw_length));
4014 args[ap->argno].expand_length
4015 = size_toInt (cpplib_getWritten (pfile) - args[ap->argno].expanded);
4018 /* Add 4 for two newline-space markers to prevent
4019 token concatenation. */
4020 xbuf_len += args[ap->argno].expand_length + 4;
4022 if (args[ap->argno].use_count < 10)
4023 args[ap->argno].use_count++;
4026 xbuf = (char *) dmalloc (xbuf_len + 1);
4027 oxbuf = xbuf;
4030 ** Generate in XBUF the complete expansion
4031 ** with arguments substituted in.
4032 ** TOTLEN is the total size generated so far.
4033 ** OFFSET is the index in the definition
4034 ** of where we are copying from.
4037 offset = 0;
4038 totlen = 0;
4040 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
4041 last_ap = ap, ap = ap->next)
4043 register struct argdata *arg = &args[ap->argno];
4044 size_t count_before = totlen;
4046 /* Add chars to XBUF. */
4047 for (i = 0; i < ap->nchars; i++, offset++)
4049 xbuf[totlen++] = exp[offset];
4052 /* If followed by an empty rest arg with concatenation,
4053 delete the last run of nonwhite chars. */
4054 if (rest_zero && totlen > count_before
4055 && ((ap->rest_args && ap->raw_before)
4056 || (last_ap != NULL && last_ap->rest_args
4057 && last_ap->raw_after)))
4059 /* Delete final whitespace. */
4060 while (totlen > count_before && is_space[(int) xbuf[totlen - 1]])
4062 totlen--;
4065 /* Delete the nonwhites before them. */
4066 while (totlen > count_before && ! is_space[(int) xbuf[totlen - 1]])
4068 totlen--;
4072 if (ap->stringify != 0)
4074 assertSet(arg);
4075 memcpy (xbuf + totlen,
4076 ARG_BASE + arg->stringified,
4077 size_fromInt (arg->stringified_length));
4078 totlen += arg->stringified_length;
4080 else if (ap->raw_before || ap->raw_after || cppReader_isTraditional (pfile))
4082 char *p1;
4083 char *l1;
4085 assertSet (arg);
4087 p1 = ARG_BASE + arg->raw;
4088 l1 = p1 + arg->raw_length;
4090 if (ap->raw_before)
4092 while (p1 != l1 && is_space[(int) *p1])
4094 p1++;
4097 while (p1 != l1 && is_idchar[(int) *p1])
4099 xbuf[totlen++] = *p1++;
4102 /* Delete any no-reexpansion marker that follows
4103 an identifier at the beginning of the argument
4104 if the argument is concatenated with what precedes it. */
4105 if (p1[0] == '@' && p1[1] == '-')
4106 p1 += 2;
4108 if (ap->raw_after)
4110 /* Arg is concatenated after: delete trailing whitespace,
4111 whitespace markers, and no-reexpansion markers. */
4112 while (p1 != l1)
4114 if (is_space[(int) l1[-1]]) l1--;
4115 else if (l1[-1] == '-')
4117 char *p2 = l1 - 1;
4118 /* If a `-' is preceded by an odd number of newlines then it
4119 and the last newline are a no-reexpansion marker. */
4120 while (p2 != p1 && p2[-1] == '\n')
4122 p2--;
4125 if (((l1 - 1 - p2) & 1) != 0)
4127 l1 -= 2;
4129 else
4131 /*@innerbreak@*/ break;
4134 else
4136 /*@innerbreak@*/ break;
4141 memcpy (xbuf + totlen, p1, size_fromInt (l1 - p1));
4142 totlen += l1 - p1;
4144 else
4146 char *expanded;
4148 assertSet (arg);
4149 expanded = ARG_BASE + arg->expanded;
4151 if (!ap->raw_before && totlen > 0
4152 && (arg->expand_length != 0)
4153 && !cppReader_isTraditional(pfile)
4154 && unsafe_chars (xbuf[totlen-1], expanded[0]))
4156 xbuf[totlen++] = '@';
4157 xbuf[totlen++] = ' ';
4160 memcpy (xbuf + totlen, expanded,
4161 size_fromInt (arg->expand_length));
4162 totlen += arg->expand_length;
4164 if (!ap->raw_after && totlen > 0
4165 && offset < size_toInt (defn->length)
4166 && !cppReader_isTraditional(pfile)
4167 && unsafe_chars (xbuf[totlen-1], exp[offset]))
4169 xbuf[totlen++] = '@';
4170 xbuf[totlen++] = ' ';
4173 /* If a macro argument with newlines is used multiple times,
4174 then only expand the newlines once. This avoids creating
4175 output lines which don't correspond to any input line,
4176 which confuses gdb and gcov. */
4177 if (arg->use_count > 1 && arg->newlines > 0)
4179 /* Don't bother doing change_newlines for subsequent
4180 uses of arg. */
4181 arg->use_count = 1;
4182 arg->expand_length
4183 = change_newlines (expanded, arg->expand_length);
4187 if (totlen > xbuf_len)
4188 abort ();
4191 /* if there is anything left of the definition
4192 after handling the arg list, copy that in too. */
4194 for (i = offset; i < size_toInt (defn->length); i++)
4196 /* if we've reached the end of the macro */
4197 if (exp[i] == ')')
4198 rest_zero = 0;
4199 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
4200 && last_ap->raw_after))
4201 xbuf[totlen++] = exp[i];
4204 xbuf[totlen] = '\0';
4205 xbuf_len = totlen;
4208 pfile->output_escapes--;
4210 /* Now put the expansion on the input stack
4211 so our caller will commence reading from it. */
4212 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4214 if (end_line != start_line)
4216 /* xbuf must have enough newlines */
4217 int newlines = end_line - start_line;
4218 int foundnewlines = 0;
4219 char *xbufptr = xbuf;
4221 while ((xbufptr = strchr (xbufptr, '\n')) != NULL && foundnewlines <= newlines)
4223 foundnewlines++;
4224 xbufptr++;
4226 if (*xbufptr == '\0')
4228 break;
4232 if (foundnewlines < newlines)
4234 cstring newbuf = cstring_copyLength (xbuf, xbuf_len);
4236 while (foundnewlines < newlines)
4238 newbuf = cstring_appendChar (newbuf, '\n');
4239 foundnewlines++;
4242 sfree (oxbuf);
4243 xbuf = cstring_toCharsSafe (newbuf);
4244 xbuf_len = cstring_length (newbuf);
4245 /*@-branchstate@*/
4246 } /*@=branchstate@*/
4249 DPRINTF (("Pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4251 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4252 DPRINTF (("After pushing expansion: %s", cstring_copyLength (xbuf, xbuf_len)));
4253 cppReader_getBufferSafe (pfile)->has_escapes = 1;
4255 /* Pop the space we've used in the token_buffer for argument expansion. */
4256 cppReader_setWritten (pfile, old_written);
4257 DPRINTF (("Done set written"));
4259 /* Recursive macro use sometimes works traditionally.
4260 #define foo(x,y) bar (x (y,0), y)
4261 foo (foo, baz) */
4263 if (!cppReader_isTraditional (pfile))
4264 hp->type = T_DISABLED;
4266 sfree (args);
4269 static void
4270 push_macro_expansion (cppReader *pfile, char *xbuf, size_t xbuf_len,
4271 /*@dependent@*/ hashNode hp)
4273 cppBuffer *mbuf = cppReader_pushBuffer (pfile, xbuf, xbuf_len);
4275 if (mbuf == NULL)
4277 return;
4280 mbuf->cleanup = cppReader_macroCleanup;
4282 llassert (mbuf->hnode == NULL);
4283 mbuf->hnode = hp;
4285 /* The first chars of the expansion should be a "@ " added by
4286 collect_expansion. This is to prevent accidental token-pasting
4287 between the text preceding the macro invocation, and the macro
4288 expansion text.
4290 We would like to avoid adding unneeded spaces (for the sake of
4291 tools that use cpp, such as imake). In some common cases we can
4292 tell that it is safe to omit the space.
4294 The character before the macro invocation cannot have been an
4295 idchar (or else it would have been pasted with the idchars of
4296 the macro name). Therefore, if the first non-space character
4297 of the expansion is an idchar, we do not need the extra space
4298 to prevent token pasting.
4300 Also, we don't need the extra space if the first char is '(',
4301 or some other (less common) characters. */
4303 if (xbuf[0] == '@' && xbuf[1] == ' '
4304 && (is_idchar[(int) xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
4305 || xbuf[2] == '\"'))
4307 llassert (mbuf->cur != NULL);
4308 DPRINTF (("Eating: %c", xbuf[2]));
4309 mbuf->cur += 2;
4315 /* Like cpplib_getToken, except that it does not read past end-of-line.
4316 Also, horizontal space is skipped, and macros are popped. */
4318 static enum cpp_token
4319 get_directive_token (cppReader *pfile)
4321 for (;;)
4323 size_t old_written = cpplib_getWritten (pfile);
4324 enum cpp_token token;
4325 cppSkipHspace (pfile);
4326 if (cppReader_peekC (pfile) == '\n')
4328 return CPP_VSPACE;
4331 token = cpplib_getToken (pfile);
4333 switch (token)
4335 case CPP_POP:
4336 if (!cppBuffer_isMacro (cppReader_getBuffer (pfile)))
4337 return token;
4338 /*@fallthrough@*/
4339 case CPP_HSPACE:
4340 case CPP_COMMENT:
4341 cppReader_setWritten (pfile, old_written);
4342 /*@switchbreak@*/ break;
4343 default:
4344 return token;
4349 /* Handle #include and #import.
4350 This function expects to see "fname" or <fname> on the input.
4352 The input is normally in part of the output_buffer following
4353 cpplib_getWritten, and will get overwritten by output_line_command.
4354 I.e. in input file specification has been popped by cppReader_handleDirective.
4355 This is safe. */
4357 static int
4358 do_include (cppReader *pfile, struct directive *keyword,
4359 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
4361 bool skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4362 cstring fname;
4363 char *fbeg, *fend; /* Beginning and end of fname */
4364 enum cpp_token token;
4366 /* Chain of dirs to search */
4367 struct file_name_list *search_start = CPPOPTIONS (pfile)->include;
4368 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
4369 struct file_name_list *searchptr = NULL;
4370 size_t old_written = cpplib_getWritten (pfile);
4371 size_t flen;
4373 int f; /* file number */
4374 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4375 f= -1; /* JF we iz paranoid! */
4377 pfile->parsing_include_directive = TRUE;
4378 token = get_directive_token (pfile);
4379 pfile->parsing_include_directive = FALSE;
4381 if (token == CPP_STRING)
4383 /* FIXME - check no trailing garbage */
4384 fbeg = pfile->token_buffer + old_written + 1;
4385 fend = cpplib_getPWritten (pfile) - 1;
4386 if (fbeg[-1] == '<')
4388 angle_brackets = 1;
4389 /* If -I-, start with the first -I dir after the -I-. */
4390 if (CPPOPTIONS (pfile)->first_bracket_include != NULL)
4391 search_start = CPPOPTIONS (pfile)->first_bracket_include;
4393 /* If -I- was specified, don't search current dir, only spec'd ones. */
4394 else if (!CPPOPTIONS (pfile)->ignore_srcdir)
4396 cppBuffer *fp = CPPBUFFER (pfile);
4397 /* We have "filename". Figure out directory this source
4398 file is coming from and put it on the front of the list. */
4400 for ( ; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4402 int n;
4403 char *ep,*nam;
4405 llassert (fp != NULL);
4407 nam = NULL;
4409 if (cstring_isDefined (fp->nominal_fname))
4411 nam = cstring_toCharsSafe (fp->nominal_fname);
4413 /* Found a named file. Figure out dir of the file,
4414 and put it in front of the search list. */
4415 dsp[0].next = search_start;
4416 search_start = dsp;
4418 ep = strrchr (nam, CONNECTCHAR);
4419 if (ep != NULL)
4421 char save;
4423 n = ep - nam;
4424 save = nam[n];
4425 nam[n] = '\0';
4427 /*@-onlytrans@*/ /* This looks like a memory leak... */
4428 dsp[0].fname = cstring_fromCharsNew (nam); /* evs 2000-07-20: was fromChars */
4429 /*@=onlytrans@*/
4430 nam[n] = save;
4432 else
4434 dsp[0].fname = cstring_undefined; /* Current directory */
4437 dsp[0].got_name_map = FALSE;
4438 break;
4442 else
4447 else
4449 cppReader_error (pfile,
4450 message ("Preprocessor command #%s expects \"FILENAME\" or <FILENAME>",
4451 keyword->name));
4453 cppReader_setWritten (pfile, old_written);
4454 cppReader_skipRestOfLine (pfile);
4455 return 0;
4458 *fend = 0;
4460 token = get_directive_token (pfile);
4461 if (token != CPP_VSPACE)
4463 cppReader_errorLit (pfile,
4464 cstring_makeLiteralTemp ("Junk at end of #include"));
4466 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
4468 token = get_directive_token (pfile);
4473 ** For #include_next, skip in the search path
4474 ** past the dir in which the containing file was found.
4477 if (skip_dirs)
4479 cppBuffer *fp = CPPBUFFER (pfile);
4481 for (; fp != cppReader_nullBuffer (pfile); fp = cppBuffer_prevBuffer (fp))
4483 llassert (fp != NULL);
4485 if (fp->fname != NULL)
4487 /* fp->dir is null if the containing file was specified with
4488 an absolute file name. In that case, don't skip anything. */
4489 if (fp->dir == SELF_DIR_DUMMY)
4491 search_start = CPPOPTIONS (pfile)->include;
4493 else if (fp->dir != NULL)
4495 search_start = fp->dir->next;
4497 else
4502 break;
4507 cppReader_setWritten (pfile, old_written);
4509 flen = size_fromInt (fend - fbeg);
4511 DPRINTF (("fbeg: %s", fbeg));
4513 if (flen == 0)
4515 cppReader_error (pfile,
4516 message ("Empty file name in #%s", keyword->name));
4517 return 0;
4521 ** Allocate this permanently, because it gets stored in the definitions
4522 ** of macros.
4525 fname = cstring_undefined;
4527 /* If specified file name is absolute, just open it. */
4529 if (osd_pathIsAbsolute (fbeg))
4531 fname = cstring_copyLength (fbeg, flen);
4533 if (redundant_include_p (pfile, fname))
4535 cstring_free (fname);
4536 return 0;
4539 f = open_include_file (pfile, fname, NULL);
4541 if (f == IMPORT_FOUND)
4543 return 0; /* Already included this file */
4546 else
4548 /* Search directory path, trying to open the file.
4549 Copy each filename tried into FNAME. */
4551 for (searchptr = search_start; searchptr != NULL;
4552 searchptr = searchptr->next)
4554 #if 0
4555 /* The empty string in a search path is ignored.
4556 This makes it possible to turn off entirely
4557 a standard piece of the list. */
4558 if (cstring_isEmpty (searchptr->fname))
4559 continue;
4560 #endif
4561 if (!cstring_isEmpty (searchptr->fname))
4563 fname = cstring_copy (searchptr->fname);
4564 fname = cstring_appendChar (fname, CONNECTCHAR);
4565 osd_pathMakeNative (fname);
4566 DPRINTF (("Here: %s", fname));
4568 else
4573 fname = cstring_concatLength (fname, fbeg, flen);
4575 DPRINTF (("fname: %s", fname));
4577 /* ??? There are currently 3 separate mechanisms for avoiding processing
4578 of redundant include files: #import, #pragma once, and
4579 redundant_include_p. It would be nice if they were unified. */
4581 if (redundant_include_p (pfile, fname))
4583 cstring_free (fname);
4584 return 0;
4587 DPRINTF (("Trying: %s", fname));
4589 f = open_include_file (pfile, fname, searchptr);
4591 if (f == IMPORT_FOUND)
4593 return 0; /* Already included this file */
4595 #ifdef EACCES
4596 else if (f == IMPORT_NOT_FOUND && errno == EACCES)
4598 cppReader_warning (pfile,
4599 message ("Header file %s exists, but is not readable", fname));
4601 #endif
4603 if (f >= 0)
4605 break;
4610 if (f < 0)
4612 /* A file that was not found. */
4613 fname = cstring_copyLength (fbeg, flen);
4615 if (search_start != NULL)
4617 cppReader_error (pfile,
4618 message ("Cannot find include file %s on search path: %x",
4619 fname,
4620 searchPath_unparse (search_start)));
4622 else
4624 cppReader_error (pfile,
4625 message ("No include path in which to find %s", fname));
4628 else {
4630 ** Check to see if this include file is a once-only include file.
4631 ** If so, give up.
4634 struct file_name_list *ptr;
4636 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
4638 if (cstring_equal (ptr->fname, fname))
4640 /* This file was included before. */
4641 break;
4645 if (ptr == NULL)
4647 /* This is the first time for this file. */
4648 /* Add it to list of files included. */
4650 ptr = (struct file_name_list *) dmalloc (sizeof (*ptr));
4651 ptr->control_macro = NULL;
4652 ptr->next = pfile->all_include_files;
4653 ptr->fname = fname;
4654 ptr->name_map = NULL;
4655 ptr->got_name_map = FALSE;
4657 DPRINTF (("Including file: %s", fname));
4658 pfile->all_include_files = ptr;
4659 assertSet (pfile->all_include_files);
4662 if (angle_brackets != 0)
4664 pfile->system_include_depth++;
4667 /* Actually process the file */
4668 if (cppReader_pushBuffer (pfile, NULL, 0) == NULL)
4670 cstring_free (fname);
4671 return 0;
4674 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
4675 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
4677 output_line_command (pfile, 0, enter_file);
4678 pfile->only_seen_white = 2;
4681 if (angle_brackets)
4683 pfile->system_include_depth--;
4685 /*@-branchstate@*/
4686 } /*@=branchstate@*/
4688 return 0;
4691 /* Return true if there is no need to include file NAME
4692 because it has already been included and it contains a conditional
4693 to make a repeated include do nothing. */
4695 static bool
4696 redundant_include_p (cppReader *pfile, cstring name)
4698 struct file_name_list *l = pfile->all_include_files;
4700 for (; l != NULL; l = l->next)
4702 if (cstring_equal (name, l->fname)
4703 && (l->control_macro != NULL)
4704 && (cpphash_lookup (l->control_macro, -1, -1) != NULL))
4706 return TRUE;
4710 return FALSE;
4713 /* Return true if the given FILENAME is an absolute pathname which
4714 designates a file within one of the known "system" include file
4715 directories. We assume here that if the given FILENAME looks like
4716 it is the name of a file which resides either directly in a "system"
4717 include file directory, or within any subdirectory thereof, then the
4718 given file must be a "system" include file. This function tells us
4719 if we should suppress pedantic errors/warnings for the given FILENAME. */
4721 static bool
4722 is_system_include (cppReader *pfile, cstring filename)
4724 struct file_name_list *searchptr;
4726 for (searchptr = CPPOPTIONS (pfile)->first_system_include;
4727 searchptr != NULL;
4728 searchptr = searchptr->next)
4730 if (!cstring_isEmpty (searchptr->fname))
4732 cstring sys_dir = searchptr->fname;
4733 size_t length = cstring_length (sys_dir);
4735 if (cstring_equalLen (sys_dir, filename, length)
4736 && osd_isConnectChar (cstring_getChar (filename, length)))
4738 return TRUE;
4743 return FALSE;
4746 /* Convert a character string literal into a nul-terminated string.
4747 The input string is [IN ... LIMIT).
4748 The result is placed in RESULT. RESULT can be the same as IN.
4749 The value returned in the end of the string written to RESULT,
4750 or NULL on error. */
4752 static /*@null@*/ char *
4753 convert_string (cppReader *pfile, /*@returned@*/ char *result,
4754 char *in, char *limit, int handle_escapes)
4756 char c;
4757 c = *in++;
4759 if (c != '\"')
4761 return NULL;
4764 while (in < limit)
4766 c = *in++;
4768 switch (c)
4770 case '\0':
4771 return NULL;
4772 case '\"':
4773 limit = in;
4774 /*@switchbreak@*/ break;
4775 case '\\':
4776 if (handle_escapes)
4778 char *bpc = (char *) in;
4779 int i = (char) cppReader_parseEscape (pfile, &bpc);
4780 in = (char *) bpc;
4781 if (i >= 0)
4782 *result++ = (char) c;
4783 /*@switchbreak@*/ break;
4786 /*@fallthrough@*/
4787 default:
4788 *result++ = c;
4792 *result = 0;
4793 return result;
4797 * interpret #line command. Remembers previously seen fnames
4798 * in its very own hash table.
4801 /*@constant int FNAME_HASHSIZE@*/
4802 #define FNAME_HASHSIZE 37
4804 static int
4805 do_line (cppReader *pfile, /*@unused@*/ struct directive *keyword)
4807 cppBuffer *ip = cppReader_getBuffer (pfile);
4808 int new_lineno;
4809 size_t old_written = cpplib_getWritten (pfile);
4810 enum file_change_code file_change = same_file;
4811 enum cpp_token token;
4813 llassert (ip != NULL);
4814 token = get_directive_token (pfile);
4816 if (token != CPP_NUMBER
4817 || !isdigit(pfile->token_buffer[old_written]))
4819 cppReader_errorLit (pfile,
4820 cstring_makeLiteralTemp ("invalid format `#line' command"));
4822 goto bad_line_directive;
4825 /* The Newline at the end of this line remains to be processed.
4826 To put the next line at the specified line number,
4827 we must store a line number now that is one less. */
4828 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
4829 cppReader_setWritten (pfile, old_written);
4831 /* NEW_LINENO is one less than the actual line number here. */
4832 if (cppReader_isPedantic (pfile) && new_lineno < 0)
4833 cppReader_pedwarnLit (pfile,
4834 cstring_makeLiteralTemp ("line number out of range in `#line' command"));
4836 token = get_directive_token (pfile);
4838 if (token == CPP_STRING) {
4839 char *fname = pfile->token_buffer + old_written;
4840 char *end_name;
4841 static hashNode fname_table[FNAME_HASHSIZE];
4842 hashNode hp;
4843 hashNode *hash_bucket;
4844 char *p;
4845 size_t num_start;
4846 size_t fname_length;
4848 /* Turn the file name, which is a character string literal,
4849 into a null-terminated string. Do this in place. */
4850 end_name = convert_string (pfile, fname, fname, cpplib_getPWritten (pfile), 1);
4851 if (end_name == NULL)
4853 cppReader_errorLit (pfile,
4854 cstring_makeLiteralTemp ("invalid format `#line' command"));
4855 goto bad_line_directive;
4858 fname_length = size_fromInt (end_name - fname);
4859 num_start = cpplib_getWritten (pfile);
4861 token = get_directive_token (pfile);
4862 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
4863 p = pfile->token_buffer + num_start;
4864 if (cppReader_isPedantic (pfile))
4865 cppReader_pedwarnLit (pfile,
4866 cstring_makeLiteralTemp ("garbage at end of `#line' command"));
4868 if (token != CPP_NUMBER || *p < '0' || *p > '3' || p[1] != '\0')
4870 cppReader_errorLit (pfile,
4871 cstring_makeLiteralTemp ("invalid format `#line' command"));
4872 goto bad_line_directive;
4874 if (*p == '1')
4875 file_change = enter_file;
4876 else if (*p == 2)
4877 file_change = leave_file;
4878 else /* if (*p == 3) */
4879 ip->system_header_p = TRUE;
4881 cppReader_setWritten (pfile, num_start);
4882 token = get_directive_token (pfile);
4883 p = pfile->token_buffer + num_start;
4884 if (token == CPP_NUMBER && p[1] == '\0' && *p == '3') {
4885 ip->system_header_p = TRUE;
4886 token = get_directive_token (pfile);
4888 if (token != CPP_VSPACE) {
4889 cppReader_errorLit (pfile,
4890 cstring_makeLiteralTemp ("invalid format `#line' command"));
4892 goto bad_line_directive;
4896 hash_bucket =
4897 &fname_table[cpphash_hashCode (fname, fname_length, FNAME_HASHSIZE)];
4899 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
4901 if (hp->length == fname_length)
4903 llassert (hp->value.cpval != NULL);
4905 if (strncmp (hp->value.cpval, fname, fname_length) == 0)
4907 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4908 break;
4913 if (hp == 0) {
4914 /* Didn't find it; cons up a new one. */
4915 hp = (hashNode) dmalloc (sizeof (*hp));
4917 hp->prev = NULL;
4918 hp->bucket_hdr = NULL;
4919 hp->type = T_NONE;
4920 hp->name = cstring_undefined;
4921 hp->next = *hash_bucket;
4923 *hash_bucket = hp;
4925 hp->length = fname_length;
4926 hp->value.cpval = dmalloc (sizeof (*hp->value.cpval) * (fname_length + 1));
4927 memcpy (hp->value.cpval, fname, fname_length);
4928 hp->value.cpval[fname_length] = '\0';
4929 ip->nominal_fname = cstring_fromChars (hp->value.cpval);
4932 else if (token != CPP_VSPACE && token != CPP_EOF)
4934 cppReader_errorLit (pfile,
4935 cstring_makeLiteralTemp ("invalid format `#line' command"));
4936 goto bad_line_directive;
4938 else
4943 ip->lineno = new_lineno;
4944 bad_line_directive:
4945 cppReader_skipRestOfLine (pfile);
4946 cppReader_setWritten (pfile, old_written);
4947 output_line_command (pfile, 0, file_change);
4948 return 0;
4952 * remove the definition of a symbol from the symbol table.
4953 * according to un*x /lib/cpp, it is not an error to undef
4954 * something that has no definitions, so it isn't one here either.
4957 static int
4958 do_undef (cppReader *pfile, struct directive *keyword, char *buf, char *limit)
4961 size_t sym_length;
4962 hashNode hp;
4963 char *orig_buf = buf;
4965 SKIP_WHITE_SPACE (buf);
4967 sym_length = cppReader_checkMacroName (pfile, buf, cstring_makeLiteralTemp ("macro"));
4969 while ((hp = cpphash_lookup (buf, size_toInt (sym_length), -1)) != NULL)
4971 /* If we are generating additional info for debugging (with -g) we
4972 need to pass through all effective #undef commands. */
4973 if (CPPOPTIONS (pfile)->debug_output && (keyword != NULL))
4975 pass_thru_directive (orig_buf, limit, pfile, keyword);
4978 if (hp->type != T_MACRO)
4980 cppReader_warning (pfile,
4981 message ("Undefining preprocessor builtin: %s",
4982 hp->name));
4985 cppReader_deleteMacro (hp);
4988 if (cppReader_isPedantic (pfile)) {
4989 buf += sym_length;
4990 SKIP_WHITE_SPACE (buf);
4991 if (buf != limit)
4993 cppReader_pedwarnLit (pfile,
4994 cstring_makeLiteralTemp ("garbage after `#undef' directive"));
4998 return 0;
5003 * Report an error detected by the program we are processing.
5004 * Use the text of the line in the error message.
5005 * (We use error because it prints the filename & line#.)
5008 static int
5009 do_error (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5010 char *buf, char *limit)
5012 size_t length = size_fromInt (limit - buf);
5013 cstring copy = cstring_copyLength (buf, length);
5014 cstring adv = cstring_advanceWhiteSpace (copy);
5016 cppReader_error (pfile, message ("#error %s", adv));
5017 cstring_free (copy);
5018 return 0;
5022 * Report a warning detected by the program we are processing.
5023 * Use the text of the line in the warning message, then continue.
5024 * (We use error because it prints the filename & line#.)
5027 static int
5028 do_warning (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5029 char *buf, char *limit)
5031 size_t length = size_fromInt (limit - buf);
5032 cstring copy = cstring_copyLength (buf, length);
5033 cstring adv = cstring_advanceWhiteSpace (copy);
5034 cppReader_warning (pfile, message ("#warning %s", adv));
5035 cstring_free (copy);
5036 return 0;
5040 /* #ident has already been copied to the output file, so just ignore it. */
5042 static int
5043 do_ident (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5044 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5046 /* Allow #ident in system headers, since that's not user's fault. */
5047 if (cppReader_isPedantic (pfile)
5048 && !cppReader_getBufferSafe (pfile)->system_header_p)
5049 cppReader_pedwarnLit (pfile,
5050 cstring_makeLiteralTemp ("ANSI C does not allow `#ident'"));
5052 /* Leave rest of line to be read by later calls to cpplib_getToken. */
5054 return 0;
5057 /* #pragma and its argument line have already been copied to the output file.
5058 Just check for some recognized pragmas that need validation here. */
5060 static int
5061 do_pragma (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5062 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5064 while (*buf == ' ' || *buf == '\t')
5066 buf++;
5069 if (!strncmp (buf, "implementation", 14)) {
5070 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5071 been included yet. */
5072 struct file_name_list *ptr;
5073 char *p = buf + 14, *fname, *inc_fname;
5074 size_t fname_len;
5075 SKIP_WHITE_SPACE (p);
5076 if (*p == '\n' || *p != '\"')
5077 return 0;
5079 fname = p + 1;
5080 p = (char *) strchr (fname, '\"');
5081 fname_len = p != NULL ? size_fromInt (p - fname) : mstring_length (fname);
5083 for (ptr = pfile->all_include_files; ptr != NULL; ptr = ptr->next)
5085 inc_fname = (char *) strrchr (cstring_toCharsSafe (ptr->fname), CONNECTCHAR);
5086 inc_fname = (inc_fname != NULL)
5087 ? inc_fname + 1 : cstring_toCharsSafe (ptr->fname);
5089 if ((inc_fname != NULL)
5090 && (strncmp (inc_fname, fname, fname_len) == 0))
5092 cpp_setLocation (pfile);
5094 ppllerror (message ("`#pragma implementation' for `%s' appears "
5095 "after file is included",
5096 cstring_fromChars (fname)));
5101 return 0;
5105 * handle #if command by
5106 * 1) inserting special `defined' keyword into the hash table
5107 * that gets turned into 0 or 1 by special_symbol (thus,
5108 * if the luser has a symbol called `defined' already, it won't
5109 * work inside the #if command)
5110 * 2) rescan the input into a temporary output buffer
5111 * 3) pass the output buffer to the yacc parser and collect a value
5112 * 4) clean up the mess left from steps 1 and 2.
5113 * 5) call conditional_skip to skip til the next #endif (etc.),
5114 * or not, depending on the value from step 3.
5117 static int
5118 do_if (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5119 char *buf, char *limit)
5121 HOST_WIDE_INT value;
5122 DPRINTF (("Do if: %s", buf));
5123 value = eval_if_expression (pfile, buf, limit - buf);
5124 conditional_skip (pfile, value == 0, T_IF, NULL);
5125 return 0;
5129 * handle a #elif directive by not changing if_stack either.
5130 * see the comment above do_else.
5133 static int do_elif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5134 char *buf, char *limit)
5136 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5138 cppReader_errorLit (pfile,
5139 cstring_makeLiteralTemp ("Preprocessor command #elif is not within a conditional"));
5140 return 0;
5142 else
5144 llassert (pfile->if_stack != NULL);
5146 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5148 cppReader_errorLit (pfile,
5149 cstring_makeLiteralTemp ("`#elif' after `#else'"));
5151 if (pfile->if_stack->fname != NULL
5152 && cppReader_getBufferSafe (pfile)->fname != NULL
5153 && !cstring_equal (pfile->if_stack->fname,
5154 cppReader_getBufferSafe (pfile)->nominal_fname))
5155 fprintf (stderr, ", file %s", cstring_toCharsSafe (pfile->if_stack->fname));
5156 fprintf (stderr, ")\n");
5158 pfile->if_stack->type = T_ELIF;
5161 if (pfile->if_stack->if_succeeded)
5163 skip_if_group (pfile, FALSE);
5165 else
5167 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
5168 if (value == 0)
5169 skip_if_group (pfile, FALSE);
5170 else
5172 ++pfile->if_stack->if_succeeded; /* continue processing input */
5173 output_line_command (pfile, 1, same_file);
5177 return 0;
5181 * evaluate a #if expression in BUF, of length LENGTH,
5182 * then parse the result as a C expression and return the value as an int.
5185 static HOST_WIDE_INT
5186 eval_if_expression (cppReader *pfile,
5187 /*@unused@*/ char *buf,
5188 /*@unused@*/ int length)
5190 hashNode save_defined;
5191 HOST_WIDE_INT value;
5192 size_t old_written = cpplib_getWritten (pfile);
5194 DPRINTF (("Saving defined..."));
5195 save_defined = cpphash_install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
5196 pfile->pcp_inside_if = TRUE;
5198 value = cppReader_parseExpression (pfile);
5199 pfile->pcp_inside_if = FALSE;
5201 /* Clean up special symbol */
5202 DPRINTF (("Removing defined..."));
5203 cppReader_deleteMacro (save_defined);
5204 cppReader_setWritten (pfile, old_written); /* Pop */
5206 return value;
5210 * routine to handle ifdef/ifndef. Try to look up the symbol,
5211 * then do or don't skip to the #endif/#else/#elif depending
5212 * on what directive is actually being processed.
5215 static int
5216 do_xifdef (cppReader *pfile, struct directive *keyword,
5217 /*@unused@*/ char *unused1, /*@unused@*/ char *unused2)
5219 int skip;
5220 cppBuffer *ip = cppReader_getBufferSafe (pfile);
5221 char *ident;
5222 size_t ident_length;
5223 enum cpp_token token;
5224 int start_of_file = 0;
5225 char *control_macro = 0;
5226 size_t old_written = cpplib_getWritten (pfile);
5228 DPRINTF (("do xifdef: %d",
5229 keyword->type == T_IFNDEF));
5231 /* Detect a #ifndef at start of file (not counting comments). */
5232 if (cstring_isDefined (ip->fname) && keyword->type == T_IFNDEF)
5234 start_of_file = pfile->only_seen_white == 2;
5237 pfile->no_macro_expand++;
5238 token = get_directive_token (pfile);
5239 pfile->no_macro_expand--;
5241 ident = pfile->token_buffer + old_written;
5242 DPRINTF (("Ident: %s", ident));
5244 ident_length = cpplib_getWritten (pfile) - old_written;
5245 cppReader_setWritten (pfile, old_written); /* Pop */
5247 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
5249 skip = (keyword->type == T_IFDEF);
5250 if (! cppReader_isTraditional (pfile))
5252 cppReader_pedwarn (pfile,
5253 message ("`#%s' with no argument", keyword->name));
5256 else if (token == CPP_NAME)
5258 hashNode hp = cpphash_lookup (ident, size_toInt (ident_length), -1);
5260 skip = (keyword->type == T_IFDEF) ? (hp == NULL) : (hp != NULL);
5262 DPRINTF (("hp null: %d / %d / %d", hp == NULL, keyword->type == T_IFNDEF, skip));
5264 if (start_of_file && !skip)
5266 DPRINTF (("Not skipping!"));
5267 control_macro = (char *) dmalloc (ident_length + 1);
5268 memcpy (control_macro, ident, ident_length + 1);
5271 else
5273 skip = (keyword->type == T_IFDEF);
5274 if (! cppReader_isTraditional (pfile))
5276 cppReader_error (pfile,
5277 message ("`#%s' with invalid argument", keyword->name));
5281 if (!cppReader_isTraditional (pfile))
5283 int c;
5284 cppSkipHspace (pfile);
5285 c = cppReader_peekC (pfile);
5286 if (c != EOF && c != '\n')
5288 cppReader_pedwarn (pfile,
5289 message ("garbage at end of `#%s' argument", keyword->name));
5293 cppReader_skipRestOfLine (pfile);
5295 DPRINTF (("Conditional skip: %d", skip));
5296 conditional_skip (pfile, skip, T_IF, control_macro);
5297 return 0;
5300 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
5301 If this is a #ifndef starting at the beginning of a file,
5302 CONTROL_MACRO is the macro name tested by the #ifndef.
5303 Otherwise, CONTROL_MACRO is 0. */
5305 static void
5306 conditional_skip (cppReader *pfile, int skip,
5307 enum node_type type,
5308 /*@dependent@*/ char *control_macro)
5310 cppIfStackFrame *temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5312 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5313 temp->next = pfile->if_stack;
5314 temp->control_macro = control_macro;
5315 temp->lineno = 0;
5316 temp->if_succeeded = 0;
5318 pfile->if_stack = temp;
5319 pfile->if_stack->type = type;
5321 if (skip != 0)
5323 skip_if_group (pfile, FALSE);
5324 return;
5326 else
5328 ++pfile->if_stack->if_succeeded;
5329 output_line_command (pfile, 1, same_file);
5334 * skip to #endif, #else, or #elif. adjust line numbers, etc.
5335 * leaves input ptr at the sharp sign found.
5336 * If ANY is true, return at next directive of any sort.
5339 static void
5340 skip_if_group (cppReader *pfile, bool any)
5342 int c;
5343 struct directive *kt;
5344 cppIfStackFrame *save_if_stack = pfile->if_stack; /* don't pop past here */
5345 register int ident_length;
5346 char *ident;
5347 struct parse_marker line_start_mark;
5349 parseSetMark (&line_start_mark, pfile);
5351 if (CPPOPTIONS (pfile)->output_conditionals) {
5352 static char failed[] = "#failed\n";
5353 cppReader_puts (pfile, failed, sizeof(failed)-1);
5354 pfile->lineno++;
5355 output_line_command (pfile, 1, same_file);
5358 beg_of_line:
5359 if (CPPOPTIONS (pfile)->output_conditionals)
5361 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
5362 char *start_line;
5364 llassert (pbuf->buf != NULL);
5366 start_line = pbuf->buf + line_start_mark.position;
5367 cppReader_puts (pfile, start_line, size_fromInt (pbuf->cur - start_line));
5370 parseMoveMark (&line_start_mark, pfile);
5372 if (!cppReader_isTraditional (pfile))
5374 cppSkipHspace (pfile);
5377 c = cppReader_getC (pfile);
5378 if (c == '#')
5380 size_t old_written = cpplib_getWritten (pfile);
5381 cppSkipHspace (pfile);
5383 parse_name (pfile, cppReader_getC (pfile));
5384 ident_length = size_toInt (cpplib_getWritten (pfile) - old_written);
5385 ident = pfile->token_buffer + old_written;
5386 pfile->limit = ident;
5388 for (kt = directive_table; kt->length >= 0; kt++)
5390 cppIfStackFrame *temp;
5391 if (ident_length == kt->length
5392 && cstring_equalPrefix (kt->name, cstring_fromChars (ident)))
5394 /* If we are asked to return on next directive, do so now. */
5395 if (any)
5397 goto done;
5400 switch (kt->type)
5402 case T_IF:
5403 case T_IFDEF:
5404 case T_IFNDEF:
5405 temp = (cppIfStackFrame *) dmalloc (sizeof (*temp));
5406 temp->next = pfile->if_stack;
5407 temp->fname = cppReader_getBufferSafe (pfile)->nominal_fname;
5408 temp->type = kt->type;
5409 temp->lineno = 0;
5410 temp->if_succeeded = 0;
5411 temp->control_macro = NULL;
5413 pfile->if_stack = temp;
5414 /*@switchbreak@*/ break;
5415 case T_ELSE:
5416 case T_ENDIF:
5417 if (cppReader_isPedantic (pfile) && pfile->if_stack != save_if_stack)
5418 validate_else (pfile,
5419 cstring_makeLiteralTemp (kt->type == T_ELSE ? "#else" : "#endif"));
5420 /*@fallthrough@*/
5421 case T_ELIF:
5422 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5424 cppReader_error (pfile,
5425 message ("Preprocessor command #%s is not within a conditional", kt->name));
5426 /*@switchbreak@*/ break;
5428 else if (pfile->if_stack == save_if_stack)
5430 goto done; /* found what we came for */
5432 else
5437 if (kt->type != T_ENDIF)
5439 llassert (pfile->if_stack != NULL);
5441 if (pfile->if_stack->type == T_ELSE)
5443 cppReader_errorLit (pfile,
5444 cstring_makeLiteralTemp ("`#else' or `#elif' after `#else'"));
5447 pfile->if_stack->type = kt->type;
5448 /*@switchbreak@*/ break;
5451 temp = pfile->if_stack;
5452 llassert (temp != NULL);
5453 pfile->if_stack = temp->next;
5454 sfree (temp);
5455 /*@switchbreak@*/ break;
5456 default: ;
5457 /*@-branchstate@*/
5459 /*@=branchstate@*/
5460 break;
5463 /* Don't let erroneous code go by. */
5465 if (kt->length < 0 && !CPPOPTIONS (pfile)->lang_asm
5466 && cppReader_isPedantic (pfile))
5468 cppReader_pedwarnLit (pfile,
5469 cstring_makeLiteralTemp ("Invalid preprocessor directive name"));
5473 c = cppReader_getC (pfile);
5475 /* We're in the middle of a line. Skip the rest of it. */
5476 for (;;) {
5477 size_t old;
5479 switch (c)
5481 case EOF:
5482 goto done;
5483 case '/': /* possible comment */
5484 c = skip_comment (pfile, NULL);
5485 if (c == EOF)
5486 goto done;
5487 /*@switchbreak@*/ break;
5488 case '\"':
5489 case '\'':
5490 cppReader_forward (pfile, -1);
5491 old = cpplib_getWritten (pfile);
5492 (void) cpplib_getToken (pfile);
5493 cppReader_setWritten (pfile, old);
5494 /*@switchbreak@*/ break;
5495 case '\\':
5496 /* Char after backslash loses its special meaning. */
5497 if (cppReader_peekC (pfile) == '\n')
5499 cppReader_forward (pfile, 1);
5502 /*@switchbreak@*/ break;
5503 case '\n':
5504 goto beg_of_line;
5506 c = cppReader_getC (pfile);
5508 done:
5509 if (CPPOPTIONS (pfile)->output_conditionals) {
5510 static char end_failed[] = "#endfailed\n";
5511 cppReader_puts (pfile, end_failed, sizeof(end_failed)-1);
5512 pfile->lineno++;
5514 pfile->only_seen_white = 1;
5516 parseGotoMark (&line_start_mark, pfile);
5517 parseClearMark (&line_start_mark);
5521 * handle a #else directive. Do this by just continuing processing
5522 * without changing if_stack ; this is so that the error message
5523 * for missing #endif's etc. will point to the original #if. It
5524 * is possible that something different would be better.
5527 static int
5528 do_else (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5529 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5531 if (cppReader_isPedantic (pfile))
5533 validate_else (pfile, cstring_makeLiteralTemp ("#else"));
5536 cppReader_skipRestOfLine (pfile);
5538 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack) {
5539 cppReader_errorLit (pfile,
5540 cstring_makeLiteralTemp ("Preprocessor command #else is not within a conditional"));
5541 return 0;
5542 } else {
5543 /* #ifndef can't have its special treatment for containing the whole file
5544 if it has a #else clause. */
5546 llassert (pfile->if_stack != NULL);
5548 pfile->if_stack->control_macro = 0;
5550 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
5552 cpp_setLocation (pfile);
5553 genppllerrorhint (FLG_PREPROC,
5554 message ("Pre-processor directive #else after #else"),
5555 message ("%q: Location of match",
5556 fileloc_unparseRaw (pfile->if_stack->fname,
5557 pfile->if_stack->lineno)));
5560 pfile->if_stack->type = T_ELSE;
5563 if (pfile->if_stack->if_succeeded)
5564 skip_if_group (pfile, FALSE);
5565 else {
5566 ++pfile->if_stack->if_succeeded; /* continue processing input */
5567 output_line_command (pfile, 1, same_file);
5570 return 0;
5574 * unstack after #endif command
5577 static int
5578 do_endif (cppReader *pfile, /*@unused@*/ struct directive *keyword,
5579 /*@unused@*/ char *buf, /*@unused@*/ char *limit)
5581 if (cppReader_isPedantic (pfile))
5583 validate_else (pfile, cstring_makeLiteralTemp ("#endif"));
5586 cppReader_skipRestOfLine (pfile);
5588 if (pfile->if_stack == cppReader_getBufferSafe (pfile)->if_stack)
5590 cppReader_errorLit (pfile, cstring_makeLiteralTemp ("Unbalanced #endif"));
5592 else
5594 cppIfStackFrame *temp = pfile->if_stack;
5596 llassert (temp != NULL);
5598 pfile->if_stack = temp->next;
5599 if (temp->control_macro != 0)
5601 /* This #endif matched a #ifndef at the start of the file.
5602 See if it is at the end of the file. */
5603 struct parse_marker start_mark;
5604 int c;
5606 parseSetMark (&start_mark, pfile);
5608 for (;;)
5610 cppSkipHspace (pfile);
5611 c = cppReader_getC (pfile);
5613 if (c != '\n')
5614 break;
5617 parseGotoMark (&start_mark, pfile);
5618 parseClearMark (&start_mark);
5620 if (c == EOF)
5622 /* If we get here, this #endif ends a #ifndef
5623 that contains all of the file (aside from whitespace).
5624 Arrange not to include the file again
5625 if the macro that was tested is defined.
5627 Do not do this for the top-level file in a -include or any
5628 file in a -imacros. */
5629 struct file_name_list *ifile = pfile->all_include_files;
5631 for ( ; ifile != NULL; ifile = ifile->next)
5633 if (cstring_equal (ifile->fname, cppReader_getBufferSafe (pfile)->fname))
5635 ifile->control_macro = temp->control_macro;
5636 break;
5642 sfree (temp);
5643 output_line_command (pfile, 1, same_file);
5645 return 0;
5648 /* When an #else or #endif is found while skipping failed conditional,
5649 if -pedantic was specified, this is called to warn about text after
5650 the command name. P points to the first char after the command name. */
5652 static void
5653 validate_else (cppReader *pfile, cstring directive)
5655 int c;
5656 cppSkipHspace (pfile);
5657 c = cppReader_peekC (pfile);
5658 if (c != EOF && c != '\n')
5660 cppReader_pedwarn (pfile,
5661 message ("text following `%s' violates ANSI standard", directive));
5666 ** Get the next token, and add it to the text in pfile->token_buffer.
5667 ** Return the kind of token we got.
5670 static enum cpp_token
5671 cpplib_getTokenAux (cppReader *pfile, bool forceExpand)
5673 int c, c2, c3;
5674 size_t old_written = 0;
5675 int start_line, start_column;
5676 enum cpp_token token;
5677 struct cppOptions *opts = CPPOPTIONS (pfile);
5678 cppReader_getBufferSafe (pfile)->prev = cppReader_getBufferSafe (pfile)->cur;
5680 get_next:
5681 c = cppReader_getC (pfile);
5682 DPRINTF (("Get next token: %c", c));
5684 if (c == EOF)
5686 handle_eof:
5687 if (cppReader_getBufferSafe (pfile)->seen_eof)
5689 cppBuffer *buf = cppReader_popBuffer (pfile);
5691 if (buf != cppReader_nullBuffer (pfile))
5693 goto get_next;
5695 else
5697 return CPP_EOF;
5700 else
5702 cppBuffer *next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
5703 cppReader_getBufferSafe (pfile)->seen_eof = TRUE;
5705 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname)
5706 && next_buf != cppReader_nullBuffer (pfile))
5708 /* We're about to return from an #include file.
5709 Emit #line information now (as part of the CPP_POP) result.
5710 But the #line refers to the file we will pop to. */
5711 cppBuffer *cur_buffer = CPPBUFFER (pfile);
5712 CPPBUFFER (pfile) = next_buf;
5713 pfile->input_stack_listing_current = FALSE;
5714 output_line_command (pfile, 0, leave_file);
5715 CPPBUFFER (pfile) = cur_buffer;
5717 return CPP_POP;
5720 else
5722 long newlines;
5723 struct parse_marker start_mark;
5725 switch (c)
5727 case '/':
5728 if (cppReader_peekC (pfile) == '=')
5730 goto op2;
5733 if (opts->put_out_comments)
5735 parseSetMark (&start_mark, pfile);
5738 newlines = 0;
5739 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
5740 &start_line, &start_column);
5741 c = skip_comment (pfile, &newlines);
5742 DPRINTF (("c = %c", c));
5743 if (opts->put_out_comments && (c == '/' || c == EOF))
5745 assertSet (&start_mark);
5746 parseClearMark (&start_mark);
5749 if (c == '/')
5750 goto randomchar;
5751 if (c == EOF)
5753 cppReader_errorWithLine (pfile, start_line, start_column,
5754 cstring_makeLiteral ("Unterminated comment"));
5755 goto handle_eof;
5757 c = '/'; /* Initial letter of comment. */
5758 /* Comments are equivalent to spaces.
5759 For -traditional, a comment is equivalent to nothing. */
5761 if (opts->put_out_comments)
5763 enum cpp_token res;
5765 assertSet (&start_mark);
5766 res = cpp_handleComment (pfile, &start_mark);
5767 pfile->lineno += newlines;
5768 return res;
5770 else if (cppReader_isTraditional (pfile))
5772 return CPP_COMMENT;
5774 else
5776 cpplib_reserve(pfile, 1);
5777 cppReader_putCharQ (pfile, ' ');
5778 return CPP_HSPACE;
5781 case '#':
5782 if (!pfile->only_seen_white)
5784 goto randomchar;
5787 if (cppReader_handleDirective (pfile))
5789 return CPP_DIRECTIVE;
5792 pfile->only_seen_white = 0;
5793 return CPP_OTHER;
5795 case '\"':
5796 case '\'':
5797 /* A single quoted string is treated like a double -- some
5798 programs (e.g., troff) are perverse this way */
5799 cppBuffer_getLineAndColumn (cppReader_fileBuffer (pfile),
5800 &start_line, &start_column);
5801 old_written = cpplib_getWritten (pfile);
5802 string:
5803 DPRINTF (("Reading string: %c", c));
5804 cppReader_putChar (pfile, c);
5805 while (TRUE)
5807 /* evans-2003-06-07
5808 ** Because of ISO8859-1 characters in string literals, we need a special test here.
5811 if (cppReader_reachedEOF (pfile))
5814 DPRINTF (("Matches EOF!"));
5815 if (cppBuffer_isMacro (CPPBUFFER (pfile)))
5817 /* try harder: this string crosses a macro expansion
5818 boundary. This can happen naturally if -traditional.
5819 Otherwise, only -D can make a macro with an unmatched
5820 quote. */
5821 cppBuffer *next_buf
5822 = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
5823 (*cppReader_getBufferSafe (pfile)->cleanup)
5824 (cppReader_getBufferSafe (pfile), pfile);
5825 CPPBUFFER (pfile) = next_buf;
5826 continue;
5829 if (!cppReader_isTraditional (pfile))
5831 cpp_setLocation (pfile);
5833 setLine (long_toInt (start_line));
5834 setColumn (long_toInt (start_column));
5836 if (pfile->multiline_string_line != long_toInt (start_line)
5837 && pfile->multiline_string_line != 0)
5839 genppllerrorhint
5840 (FLG_PREPROC,
5841 message ("Unterminated string or character constant"),
5842 message ("%q: Possible real start of unterminated constant",
5843 fileloc_unparseRaw
5844 (fileloc_filename (g_currentloc),
5845 pfile->multiline_string_line)));
5846 pfile->multiline_string_line = 0;
5848 else
5850 genppllerror
5851 (FLG_PREPROC,
5852 message ("Unterminated string or character constant"));
5855 /*@loopbreak@*/ break;
5857 else
5859 int cc = cppReader_getC (pfile);
5860 DPRINTF (("cc: %c [%d] [%d]", cc, cc, EOF));
5861 DPRINTF (("putting char: %c", cc));
5862 cppReader_putChar (pfile, cc);
5863 switch (cc)
5865 case '\n':
5866 /* Traditionally, end of line ends a string constant with
5867 no error. So exit the loop and record the new line. */
5868 if (cppReader_isTraditional (pfile))
5869 goto while2end;
5870 if (c == '\'')
5872 goto while2end;
5874 if (cppReader_isPedantic (pfile)
5875 && pfile->multiline_string_line == 0)
5877 cppReader_pedwarnWithLine
5878 (pfile, long_toInt (start_line),
5879 long_toInt (start_column),
5880 cstring_makeLiteral ("String constant runs past end of line"));
5882 if (pfile->multiline_string_line == 0)
5884 pfile->multiline_string_line = start_line;
5887 /*@switchbreak@*/ break;
5889 case '\\':
5890 cc = cppReader_getC (pfile);
5891 if (cc == '\n')
5893 /* Backslash newline is replaced by nothing at all. */
5894 pfile->lineno++; /* 2003-11-03: AMiller suggested adding this, but
5895 its not clear why it is needed. */
5896 cppReader_adjustWritten (pfile, -1);
5897 pfile->lineno++;
5899 else
5901 /* ANSI stupidly requires that in \\ the second \
5902 is *not* prevented from combining with a newline. */
5903 NEWLINE_FIX1(cc);
5904 if (cc != EOF)
5905 cppReader_putChar (pfile, cc);
5907 /*@switchbreak@*/ break;
5909 case '\"':
5910 case '\'':
5911 if (cc == c)
5912 goto while2end;
5913 /*@switchbreak@*/ break;
5917 while2end:
5918 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
5919 cpplib_getPWritten (pfile));
5920 pfile->only_seen_white = 0;
5921 return c == '\'' ? CPP_CHAR : CPP_STRING;
5923 case '$':
5924 if (!opts->dollars_in_ident)
5925 goto randomchar;
5926 goto letter;
5928 case ':':
5929 goto randomchar;
5931 case '&':
5932 case '+':
5933 case '|':
5934 NEWLINE_FIX;
5935 c2 = cppReader_peekC (pfile);
5936 if (c2 == c || c2 == '=')
5937 goto op2;
5938 goto randomchar;
5940 case '*':
5941 case '!':
5942 case '%':
5943 case '=':
5944 case '^':
5945 NEWLINE_FIX;
5946 if (cppReader_peekC (pfile) == '=')
5947 goto op2;
5948 goto randomchar;
5950 case '-':
5951 NEWLINE_FIX;
5952 c2 = cppReader_peekC (pfile);
5953 if (c2 == '-' || c2 == '=' || c2 == '>')
5954 goto op2;
5955 goto randomchar;
5957 case '<':
5958 if (pfile->parsing_include_directive)
5960 for (;;)
5962 cppReader_putChar (pfile, c);
5963 if (c == '>')
5964 /*@loopbreak@*/ break;
5965 c = cppReader_getC (pfile);
5966 NEWLINE_FIX1 (c);
5967 if (c == '\n' || c == EOF)
5969 cppReader_errorLit (pfile,
5970 cstring_makeLiteralTemp ("Missing '>' in \"#include <FILENAME>\""));
5971 /*@loopbreak@*/ break;
5974 return CPP_STRING;
5976 /*@fallthrough@*/
5977 case '>':
5978 NEWLINE_FIX;
5979 c2 = cppReader_peekC (pfile);
5980 if (c2 == '=')
5981 goto op2;
5982 if (c2 != c)
5983 goto randomchar;
5984 cppReader_forward (pfile, 1);
5985 cpplib_reserve (pfile, 4);
5986 cppReader_putChar (pfile, c);
5987 cppReader_putChar (pfile, c2);
5988 NEWLINE_FIX;
5989 c3 = cppReader_peekC (pfile);
5990 if (c3 == '=')
5991 cppReader_putCharQ (pfile, cppReader_getC (pfile));
5992 cppReader_nullTerminateQ (pfile);
5993 pfile->only_seen_white = 0;
5994 return CPP_OTHER;
5996 case '@':
5997 DPRINTF (("Macro @!"));
5998 if (cppReader_getBufferSafe (pfile)->has_escapes)
6000 c = cppReader_getC (pfile);
6001 DPRINTF (("got c: %c", c));
6002 if (c == '-')
6004 if (pfile->output_escapes)
6005 cppReader_puts (pfile, "@-", 2);
6006 parse_name (pfile, cppReader_getC (pfile));
6007 return CPP_NAME;
6009 else if (is_space [c])
6011 cpplib_reserve (pfile, 2);
6012 if (pfile->output_escapes)
6013 cppReader_putCharQ (pfile, '@');
6014 cppReader_putCharQ (pfile, c);
6015 return CPP_HSPACE;
6017 else
6022 if (pfile->output_escapes)
6024 cppReader_puts (pfile, "@@", 2);
6025 return CPP_OTHER;
6027 goto randomchar;
6028 case '.':
6029 NEWLINE_FIX;
6030 c2 = cppReader_peekC (pfile);
6031 if (isdigit(c2))
6033 cpplib_reserve(pfile, 2);
6034 cppReader_putCharQ (pfile, '.');
6035 c = cppReader_getC (pfile);
6036 goto number;
6039 /* FIXME - misses the case "..\\\n." */
6040 if (c2 == '.' && cpp_peekN (pfile, 1) == '.')
6042 cpplib_reserve(pfile, 4);
6043 cppReader_putCharQ (pfile, '.');
6044 cppReader_putCharQ (pfile, '.');
6045 cppReader_putCharQ (pfile, '.');
6046 cppReader_forward (pfile, 2);
6047 cppReader_nullTerminateQ (pfile);
6048 pfile->only_seen_white = 0;
6049 return CPP_3DOTS;
6051 goto randomchar;
6052 op2:
6053 token = CPP_OTHER;
6054 pfile->only_seen_white = 0;
6055 op2any: /* jumped to for \ continuations */
6056 cpplib_reserve(pfile, 3);
6057 cppReader_putCharQ (pfile, c);
6059 /* evans 2003-08-24: This is a hack to fix line output for \
6060 continuations. Someday I really should get a decent pre-processor!
6063 if (c == '\\') {
6064 (void) cppReader_getC (pfile); /* skip the newline to avoid extra lines */
6065 } else {
6066 cppReader_putCharQ (pfile, cppReader_getC (pfile));
6069 cppReader_nullTerminateQ (pfile);
6070 return token;
6072 case 'L':
6073 NEWLINE_FIX;
6074 c2 = cppReader_peekC (pfile);
6075 if ((c2 == '\'' || c2 == '\"') && !cppReader_isTraditional (pfile))
6077 cppReader_putChar (pfile, c);
6078 c = cppReader_getC (pfile);
6079 goto string;
6081 goto letter;
6083 case '0': case '1': case '2': case '3': case '4':
6084 case '5': case '6': case '7': case '8': case '9':
6085 number:
6086 c2 = '.';
6087 for (;;)
6089 cpplib_reserve (pfile, 2);
6090 cppReader_putCharQ (pfile, c);
6091 NEWLINE_FIX;
6092 c = cppReader_peekC (pfile);
6093 if (c == EOF)
6094 /*@loopbreak@*/ break;
6095 if (!is_idchar[c] && c != '.'
6096 && ((c2 != 'e' && c2 != 'E'
6097 && ((c2 != 'p' && c2 != 'P') || cppReader_isC89 (pfile)))
6098 || (c != '+' && c != '-')))
6099 /*@loopbreak@*/ break;
6100 cppReader_forward (pfile, 1);
6101 c2= c;
6104 cppReader_nullTerminateQ (pfile);
6105 pfile->only_seen_white = 0;
6106 return CPP_NUMBER;
6108 case '_':
6109 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
6110 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
6111 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
6112 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
6113 case 'y': case 'z':
6114 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
6115 case 'G': case 'H': case 'I': case 'J': case 'K':
6116 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
6117 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
6118 case 'Y': case 'Z':
6119 letter:
6121 hashNode hp;
6122 char *ident;
6123 size_t before_name_written = cpplib_getWritten (pfile);
6124 size_t ident_len;
6125 parse_name (pfile, c);
6126 pfile->only_seen_white = 0;
6128 if (pfile->no_macro_expand)
6130 DPRINTF (("Not expanding: %s", pfile->token_buffer));
6131 return CPP_NAME;
6134 ident = pfile->token_buffer + before_name_written;
6135 DPRINTF (("Ident: %s", ident));
6137 ident_len = size_fromInt ((cpplib_getPWritten (pfile)) - ident);
6139 hp = cpphash_lookupExpand (ident, size_toInt (ident_len), -1, forceExpand);
6141 if (hp == NULL)
6143 DPRINTF (("No expand: %s %d", ident, ident_len));
6144 return CPP_NAME;
6147 if (hp->type == T_DISABLED)
6149 DPRINTF (("Disabled!"));
6151 if (pfile->output_escapes)
6152 { /* Return "@-IDENT", followed by '\0'. */
6153 int i;
6154 cpplib_reserve (pfile, 3);
6155 ident = pfile->token_buffer + before_name_written;
6156 cppReader_adjustWritten (pfile, 2);
6158 for (i = size_toInt (ident_len); i >= 0; i--)
6160 ident[i+2] = ident[i];
6163 ident[0] = '@';
6164 ident[1] = '-';
6166 return CPP_NAME;
6170 ** If macro wants an arglist, verify that a '(' follows.
6171 ** first skip all whitespace, copying it to the output
6172 ** after the macro name. Then, if there is no '(',
6173 ** decide this is not a macro call and leave things that way.
6176 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
6178 struct parse_marker macro_mark;
6179 int is_macro_call;
6181 DPRINTF (("Arglist macro!"));
6184 ** evans 2002-07-03: Moved this here (from below).
6185 ** This bug caused necessary whitespace to be lost
6186 ** when parsing parameterized macros without parameters.
6189 parseSetMark (&macro_mark, pfile);
6191 while (cppBuffer_isMacro (CPPBUFFER (pfile)))
6193 cppBuffer *next_buf;
6194 cppSkipHspace (pfile);
6195 if (cppReader_peekC (pfile) != EOF)
6197 DPRINTF (("Peeking!"));
6198 /*@loopbreak@*/ break;
6201 next_buf = cppBuffer_prevBuffer (cppReader_getBufferSafe (pfile));
6202 (*cppReader_getBufferSafe (pfile)->cleanup) (cppReader_getBufferSafe (pfile), pfile);
6203 CPPBUFFER (pfile) = next_buf;
6206 /* parseSetMark (&macro_mark, pfile); */
6208 for (;;)
6210 cppSkipHspace (pfile);
6211 c = cppReader_peekC (pfile);
6212 DPRINTF (("c: %c", c));
6213 is_macro_call = c == '(';
6214 if (c != '\n')
6215 /*@loopbreak@*/ break;
6216 cppReader_forward (pfile, 1);
6219 if (!is_macro_call)
6221 parseGotoMark (&macro_mark, pfile);
6224 parseClearMark (&macro_mark);
6226 if (!is_macro_call)
6228 DPRINTF (("not macro call!"));
6229 return CPP_NAME;
6233 /* This is now known to be a macro call. */
6235 /* it might not actually be a macro. */
6236 if (hp->type != T_MACRO)
6238 size_t xbuf_len;
6239 char *xbuf;
6241 cppReader_setWritten (pfile, before_name_written);
6242 special_symbol (hp, pfile);
6243 xbuf_len = cpplib_getWritten (pfile) - before_name_written;
6244 xbuf = (char *) dmalloc (xbuf_len + 1);
6245 cppReader_setWritten (pfile, before_name_written);
6246 memcpy (xbuf, cpplib_getPWritten (pfile), xbuf_len + 1);
6247 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
6249 else
6252 ** Expand the macro, reading arguments as needed,
6253 ** and push the expansion on the input stack.
6256 cpplib_macroExpand (pfile, hp);
6257 cppReader_setWritten (pfile, before_name_written);
6260 /* An extra "@ " is added to the end of a macro expansion
6261 to prevent accidental token pasting. We prefer to avoid
6262 unneeded extra spaces (for the sake of cpp-using tools like
6263 imake). Here we remove the space if it is safe to do so. */
6265 llassert (pfile->buffer->rlimit != NULL);
6267 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
6268 && pfile->buffer->rlimit[-2] == '@'
6269 && pfile->buffer->rlimit[-1] == ' ')
6271 int c1 = pfile->buffer->rlimit[-3];
6272 int cl2 = cpplib_bufPeek (cppBuffer_prevBuffer (CPPBUFFER (pfile)));
6274 if (cl2 == EOF || !unsafe_chars ((char) c1, (char) cl2))
6275 pfile->buffer->rlimit -= 2;
6278 goto get_next;
6281 case ' ': case '\t': case '\v': case '\r':
6282 for (;;)
6284 cppReader_putChar (pfile, c);
6285 c = cppReader_peekC (pfile);
6286 if (c == EOF || !is_hor_space[c])
6287 /*@loopbreak@*/ break;
6288 cppReader_forward (pfile, 1);
6290 return CPP_HSPACE;
6292 case '\\':
6293 c2 = cppReader_peekC (pfile);
6294 /* allow other stuff here if a flag is set? */
6295 DPRINTF (("Got continuation!"));
6296 if (c2 != '\n')
6297 goto randomchar;
6298 token = CPP_HSPACE;
6299 goto op2any;
6301 case '\n':
6302 cppReader_putChar (pfile, c);
6303 if (pfile->only_seen_white == 0)
6304 pfile->only_seen_white = 1;
6305 pfile->lineno++;
6306 output_line_command (pfile, 1, same_file);
6307 return CPP_VSPACE;
6309 case '(': token = CPP_LPAREN; goto char1;
6310 case ')': token = CPP_RPAREN; goto char1;
6311 case '{': token = CPP_LBRACE; goto char1;
6312 case '}': token = CPP_RBRACE; goto char1;
6313 case ',': token = CPP_COMMA; goto char1;
6314 case ';': token = CPP_SEMICOLON; goto char1;
6316 randomchar:
6317 default:
6318 token = CPP_OTHER;
6319 char1:
6320 pfile->only_seen_white = 0;
6321 cppReader_putChar (pfile, c);
6322 return token;
6326 BADBRANCH;
6327 /*@notreached@*/
6330 enum cpp_token
6331 cpplib_getToken (cppReader *pfile)
6333 return cpplib_getTokenAux (pfile, FALSE);
6336 enum cpp_token
6337 cpplib_getTokenForceExpand (cppReader *pfile)
6339 return cpplib_getTokenAux (pfile, TRUE);
6342 /* Parse an identifier starting with C. */
6344 void
6345 parse_name (cppReader *pfile, int c)
6347 for (;;)
6349 if (!is_idchar[c])
6351 if (c == '\\' && cppReader_peekC (pfile) == '\n')
6353 cppReader_forward (pfile, 2);
6354 continue;
6357 cppReader_forward (pfile, -1);
6358 break;
6361 if (c == '$' && cppReader_isPedantic (pfile))
6363 cppReader_pedwarnLit (pfile,
6364 cstring_makeLiteralTemp ("`$' in identifier"));
6367 cpplib_reserve(pfile, 2); /* One more for final NUL. */
6368 cppReader_putCharQ (pfile, c);
6369 c = cppReader_getC (pfile);
6371 if (c == EOF)
6372 break;
6375 cppReader_nullTerminateQ (pfile);
6378 /* The file_name_map structure holds a mapping of file names for a
6379 particular directory. This mapping is read from the file named
6380 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
6381 map filenames on a file system with severe filename restrictions,
6382 such as DOS. The format of the file name map file is just a series
6383 of lines with two tokens on each line. The first token is the name
6384 to map, and the second token is the actual name to use. */
6386 struct file_name_map
6388 struct file_name_map *map_next;
6389 cstring map_from;
6390 cstring map_to;
6393 /*@constant observer char *FILE_NAME_MAP_FILE*/
6394 #define FILE_NAME_MAP_FILE "header.gcc"
6396 /* Read a space delimited string of unlimited length from a stdio
6397 file. */
6399 static cstring read_filename_string (int ch, /*:open:*/ FILE *f)
6401 char *alloc, *set;
6402 size_t len;
6404 len = 20;
6405 set = alloc = dmalloc (len + 1);
6407 if (!is_space[ch])
6409 *set++ = ch;
6410 while ((ch = getc (f)) != EOF && ! is_space[ch])
6412 if (set - alloc == size_toInt (len))
6414 len *= 2;
6415 alloc = drealloc (alloc, len + 1);
6416 set = alloc + len / 2;
6417 /*@-branchstate@*/ }
6419 *set++ = ch;
6420 } /*@=branchstate@*/
6422 *set = '\0';
6423 check (ungetc (ch, f) != EOF);
6425 return cstring_fromChars (alloc);
6428 /* This structure holds a linked list of file name maps, one per directory. */
6430 struct file_name_map_list
6432 /*@only@*/ struct file_name_map_list *map_list_next;
6433 /*@only@*/ cstring map_list_name;
6434 /*@null@*/ struct file_name_map *map_list_map;
6437 /* Read the file name map file for DIRNAME. */
6439 static struct file_name_map *
6440 read_name_map (cppReader *pfile, cstring dirname)
6442 struct file_name_map_list *map_list_ptr;
6443 cstring name;
6444 FILE *f;
6446 for (map_list_ptr = CPPOPTIONS (pfile)->map_list;
6447 map_list_ptr != NULL;
6448 map_list_ptr = map_list_ptr->map_list_next)
6450 if (cstring_equal (map_list_ptr->map_list_name, dirname))
6452 return map_list_ptr->map_list_map;
6456 map_list_ptr = (struct file_name_map_list *) dmalloc (sizeof (*map_list_ptr));
6457 map_list_ptr->map_list_name = cstring_copy (dirname);
6458 map_list_ptr->map_list_map = NULL;
6460 name = cstring_copy (dirname);
6462 if (cstring_length (dirname) > 0)
6464 name = cstring_appendChar (name, CONNECTCHAR);
6467 name = cstring_concatFree1 (name, cstring_makeLiteralTemp (FILE_NAME_MAP_FILE));
6469 f = fileTable_openReadFile (context_fileTable (), name);
6470 cstring_free (name);
6472 if (f == NULL)
6474 map_list_ptr->map_list_map = NULL;
6476 else
6478 int ch;
6480 while ((ch = getc (f)) != EOF)
6482 cstring from, to;
6483 struct file_name_map *ptr;
6485 if (is_space[ch])
6487 continue;
6490 from = read_filename_string (ch, f);
6491 while ((ch = getc (f)) != EOF && is_hor_space[ch])
6496 to = read_filename_string (ch, f);
6498 ptr = (struct file_name_map *) dmalloc (sizeof (*ptr));
6499 ptr->map_from = from;
6501 /* Make the real filename absolute. */
6502 if (cstring_length (to) > 1
6503 && osd_isConnectChar (cstring_firstChar (to)))
6505 ptr->map_to = to;
6507 else
6509 ptr->map_to = cstring_copy (dirname);
6510 ptr->map_to = cstring_appendChar (ptr->map_to, CONNECTCHAR);
6511 ptr->map_to = cstring_concatFree (ptr->map_to, to);
6514 ptr->map_next = map_list_ptr->map_list_map;
6515 map_list_ptr->map_list_map = ptr;
6517 while ((ch = getc (f)) != '\n')
6519 if (ch == EOF)
6521 /*@innerbreak@*/ break;
6526 assertSet (map_list_ptr->map_list_map);
6527 check (fileTable_closeFile (context_fileTable (),f) == 0);
6530 map_list_ptr->map_list_next = pfile->opts->map_list;
6531 pfile->opts->map_list = map_list_ptr;
6533 return map_list_ptr->map_list_map;
6536 /* Try to open include file FILENAME. SEARCHPTR is the directory
6537 being tried from the include file search path. This function maps
6538 filenames on file systems based on information read by
6539 read_name_map. */
6541 static int
6542 open_include_file (cppReader *pfile,
6543 cstring fname,
6544 struct file_name_list *searchptr)
6546 char *filename = cstring_toCharsSafe (fname);
6547 struct file_name_map *map;
6548 char *from;
6549 char *p, *dir;
6551 cstring_markOwned (fname);
6553 cpp_setLocation (pfile);
6555 if (context_getFlag (FLG_NEVERINCLUDE))
6557 if (isHeaderFile (fname))
6559 return SKIP_INCLUDE;
6563 if ((searchptr != NULL) && ! searchptr->got_name_map)
6565 searchptr->name_map = read_name_map (pfile,
6566 !cstring_isEmpty (searchptr->fname)
6567 ? searchptr->fname :
6568 cstring_makeLiteralTemp ("."));
6569 searchptr->got_name_map = TRUE;
6572 /* First check the mapping for the directory we are using. */
6574 if ((searchptr != NULL)
6575 && (searchptr->name_map != NULL))
6577 from = filename;
6579 if (!cstring_isEmpty (searchptr->fname))
6581 from += cstring_length (searchptr->fname) + 1;
6584 for (map = searchptr->name_map;
6585 map != NULL;
6586 map = map->map_next)
6588 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6590 if (cpp_skipIncludeFile (map->map_to))
6592 return SKIP_INCLUDE;
6594 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6600 ** Try to find a mapping file for the particular directory we are
6601 ** looking in. Thus #include <sys/types.h> will look up sys/types.h
6602 ** in /usr/include/header.gcc and look up types.h in
6603 ** /usr/include/sys/header.gcc.
6606 p = strrchr (filename, CONNECTCHAR);
6607 if (p == NULL)
6609 p = filename;
6612 if ((searchptr != NULL)
6613 && (cstring_isDefined (searchptr->fname))
6614 && (size_toInt (cstring_length (searchptr->fname)) == p - filename)
6615 && !strncmp (cstring_toCharsSafe (searchptr->fname),
6616 filename,
6617 size_fromInt (p - filename)))
6619 /* filename is in SEARCHPTR, which we've already checked. */
6620 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6622 return SKIP_INCLUDE;
6624 return cpp_openIncludeFile (filename);
6627 if (p == filename)
6629 dir = mstring_copy (".");
6630 from = filename;
6632 else
6634 dir = (char *) dmalloc (size_fromInt (p - filename + 1));
6635 memcpy (dir, filename, size_fromInt (p - filename));
6636 dir[p - filename] = '\0';
6637 from = p + 1;
6640 for (map = read_name_map (pfile, cstring_fromChars (dir));
6641 map != NULL;
6642 map = map->map_next)
6644 if (cstring_equal (map->map_from, cstring_fromChars (from)))
6646 sfree (dir);
6647 if (cpp_skipIncludeFile (map->map_to))
6649 return SKIP_INCLUDE;
6651 return cpp_openIncludeFile (cstring_toCharsSafe (map->map_to));
6655 sfree (dir);
6657 if (cpp_skipIncludeFile (cstring_fromChars (filename)))
6659 return SKIP_INCLUDE;
6661 return cpp_openIncludeFile (filename);
6664 /* Process the contents of include file FNAME, already open on descriptor F,
6665 with output to OP.
6666 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
6667 "system" include directories (as decided by the `is_system_include'
6668 function above).
6669 DIRPTR is the link in the dir path through which this file was found,
6670 or 0 if the file name was absolute or via the current directory.
6671 Return 1 on success, 0 on failure.
6673 The caller is responsible for the cppReader_pushBuffer. */
6675 static int
6676 finclude (cppReader *pfile, int f,
6677 cstring fname,
6678 bool system_header_p,
6679 /*@dependent@*/ struct file_name_list *dirptr)
6681 filemode mode;
6682 size_t size;
6683 long i;
6684 int length = 0;
6685 cppBuffer *fp; /* For input stack frame */
6687 if (osd_file_mode_and_size (f, &mode, &size) < 0)
6689 cppReader_perrorWithName (pfile, fname);
6690 check (osd_close (f) == 0);
6691 (void) cppReader_popBuffer (pfile);
6692 /*@-mustfree@*/
6693 return 0;
6694 /*@=mustfree@*/
6697 fp = cppReader_getBufferSafe (pfile);
6699 /*@-temptrans@*/ /* fname shouldn't really be temp */
6700 fp->nominal_fname = fp->fname = fname;
6701 /*@=temptrans@*/
6703 fp->dir = dirptr;
6704 fp->system_header_p = system_header_p;
6705 fp->lineno = 1;
6706 fp->colno = 1;
6707 fp->cleanup = cppReader_fileCleanup;
6709 if (mode == OSD_MODEREGULAR)
6711 sfree (fp->buf);
6712 fp->buf = (char *) dmalloc (size + 2);
6713 fp->alimit = fp->buf + size + 2;
6714 fp->cur = fp->buf;
6716 /* Read the file contents, knowing that size is an upper bound
6717 on the number of bytes we can read. */
6718 length = osd_readSafe (f, fp->buf, size_toInt (size));
6719 fp->rlimit = fp->buf + length;
6720 if (length < 0) goto nope;
6722 else if (mode == OSD_MODEDIRECTORY)
6724 cppReader_error (pfile,
6725 message ("Directory specified where file is expected: %s", fname));
6726 check (osd_close (f) == 0);
6727 return 0;
6729 else
6732 ** Cannot count its file size before reading.
6733 ** First read the entire file into heap and
6734 ** copy them into buffer on stack.
6737 size_t bsize = 2000;
6739 size = 0;
6741 sfree (fp->buf);
6742 fp->buf = (char *) dmalloc (bsize + 2);
6744 for (;;) {
6745 i = osd_readSafe (f, fp->buf + size, size_toInt (bsize - size));
6747 if (i < 0)
6748 goto nope; /* error! */
6749 size += i;
6751 if (size != bsize)
6753 break; /* End of file */
6756 bsize *= 2;
6757 fp->buf = (char *) drealloc (fp->buf, bsize + 2);
6760 fp->cur = fp->buf;
6761 length = size_toInt (size);
6764 if ((length > 0 && fp->buf[length - 1] != '\n')
6765 /* Backslash-newline at end is not good enough. */
6766 || (length > 1 && fp->buf[length - 2] == '\\')) {
6767 fp->buf[length++] = '\n';
6770 fp->buf[length] = '\0';
6771 fp->rlimit = fp->buf + length;
6773 /* Close descriptor now, so nesting does not use lots of descriptors. */
6774 check (osd_close (f) == 0);
6776 /* Must do this before calling trigraph_pcp, so that the correct file name
6777 will be printed in warning messages. */
6779 pfile->input_stack_listing_current = FALSE;
6780 return 1;
6782 nope:
6784 cppReader_perrorWithName (pfile, fname);
6785 check (osd_close (f) == 0);
6786 sfree (fp->buf);
6787 return 1;
6790 static void
6791 cpplib_init (/*@out@*/ cppReader *pfile)
6793 memset ((char *) pfile, 0, sizeof (*pfile));
6795 pfile->get_token = cpplib_getToken;
6796 pfile->token_buffer_size = 200;
6797 pfile->token_buffer = (char *) dmalloc (pfile->token_buffer_size);
6798 pfile->all_include_files = NULL;
6800 assertSet (pfile);
6802 cppReader_setWritten (pfile, 0);
6804 pfile->system_include_depth = 0;
6805 pfile->timebuf = NULL;
6806 pfile->only_seen_white = 1;
6808 pfile->buffer = cppReader_nullBuffer (pfile);
6810 pfile->opts = (struct cppOptions *) dmalloc (sizeof (struct cppOptions));
6813 void
6814 cppReader_finish (/*@unused@*/ cppReader *pfile)
6819 /* Free resources used by PFILE.
6820 This is the cppReader 'finalizer' or 'destructor' (in C++ terminology). */
6822 void
6823 cppCleanup (/*@special@*/ cppReader *pfile)
6824 /*@uses pfile@*/
6825 /*@releases pfile@*/
6827 DPRINTF (("cppCleanup!"));
6829 while (CPPBUFFER (pfile) != cppReader_nullBuffer (pfile))
6831 (void) cppReader_popBuffer (pfile);
6834 if (pfile->token_buffer != NULL)
6836 sfree (pfile->token_buffer);
6837 pfile->token_buffer = NULL;
6840 while (pfile->if_stack != NULL)
6842 cppIfStackFrame *temp = pfile->if_stack;
6843 pfile->if_stack = temp->next;
6844 sfree (temp);
6847 while (pfile->all_include_files != NULL)
6849 struct file_name_list *temp = pfile->all_include_files;
6850 pfile->all_include_files = temp->next;
6851 /*@-dependenttrans@*/
6852 cstring_free (temp->fname);
6853 /*@=dependenttrans@*/
6854 sfree (temp);
6857 /* evans 2002-07-12 */
6858 while (pfile->opts->map_list != NULL)
6860 struct file_name_map_list *temp = pfile->opts->map_list;
6861 pfile->opts->map_list = pfile->opts->map_list->map_list_next;
6862 cstring_free (temp->map_list_name);
6863 sfree (temp);
6866 while (pfile->opts->include != NULL)
6868 struct file_name_list *temp = pfile->opts->include;
6869 pfile->opts->include = pfile->opts->include->next;
6870 /* cstring_free (temp->fname); */
6871 sfree (temp);
6874 sfree (pfile->opts);
6875 pfile->opts = NULL;
6876 cppReader_hashCleanup ();
6879 /* Initialize PMARK to remember the current position of PFILE. */
6881 void
6882 parseSetMark (struct parse_marker *pmark, cppReader *pfile)
6884 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
6886 pmark->next = pbuf->marks;
6887 /*@-temptrans@*/
6888 pbuf->marks = pmark;
6889 /*@=temptrans@*/
6891 pmark->buf = pbuf;
6892 pmark->position = pbuf->cur - pbuf->buf;
6893 DPRINTF (("set mark: %d / %s", pmark->position, pbuf->cur));
6896 /* Cleanup PMARK - we no longer need it. */
6898 void parseClearMark (struct parse_marker *pmark)
6900 struct parse_marker **pp = &pmark->buf->marks;
6902 for (; ; pp = &(*pp)->next)
6904 llassert (*pp != NULL);
6905 if (*pp == pmark) break;
6908 *pp = pmark->next;
6911 /* Backup the current position of PFILE to that saved in PMARK. */
6913 void
6914 parseGotoMark (struct parse_marker *pmark, cppReader *pfile)
6916 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
6918 if (pbuf != pmark->buf)
6920 cpp_setLocation (pfile);
6921 llfatalbug (cstring_makeLiteral ("Internal error parseGotoMark"));
6924 llassert (pbuf->buf != NULL);
6925 pbuf->cur = pbuf->buf + pmark->position;
6926 DPRINTF (("goto mark: %d / %s", pmark->position, pbuf->cur));
6929 /* Reset PMARK to point to the current position of PFILE. (Same
6930 as parseClearMark (PMARK), parseSetMark (PMARK, PFILE) but faster. */
6932 static void
6933 parseMoveMark (struct parse_marker *pmark, cppReader *pfile)
6935 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
6937 if (pbuf != pmark->buf)
6939 cpp_setLocation (pfile);
6940 llfatalerror (cstring_makeLiteral ("Internal error parseMoveMark"));
6943 pmark->position = pbuf->cur - pbuf->buf;
6944 DPRINTF (("move mark: %d", pmark->position));
6947 static void
6948 handleCmdFlag (cppReader *pfile, /* const */ char *arg)
6950 llassert (CPPOPTIONS (pfile) != NULL);
6951 llassert (arg[0] == 'D' || arg[0] == 'U' || arg[0] == 'I');
6953 if (arg[0] == 'D') {
6954 cppReader_define (pfile, arg+1);
6955 } else if (arg[0] == 'U') {
6956 size_t sym_length;
6957 hashNode hp;
6959 sym_length = cppReader_checkMacroName (pfile, arg+1,
6960 cstring_makeLiteralTemp ("macro"));
6962 while ((hp = cpphash_lookup (arg+1, size_toInt (sym_length), -1)) != NULL)
6964 cppReader_deleteMacro (hp);
6966 } else /* if (arg[0] == 'I') */ {
6967 if (mstring_equal (arg+1, "-")) { /* Special handle for "-I-". */
6968 CPPOPTIONS (pfile)->ignore_srcdir = TRUE;
6969 CPPOPTIONS (pfile)->first_bracket_include = NULL;
6970 } else {
6971 cppReader_appendDirIncludeChain (pfile, arg+1, FALSE);
6976 void cpplib_initializeReader (cppReader *pfile, cstringList *cmdArgs)
6978 struct cppOptions *opts;
6980 cpplib_init (pfile);
6982 opts = CPPOPTIONS (pfile);
6983 cppOptions_init (opts);
6985 /* Now that dollars_in_ident is known, initialize is_idchar. */
6986 initialize_char_syntax (opts);
6988 /* CppReader_Install __LINE__, etc. Must follow initialize_char_syntax
6989 and option processing. */
6990 initialize_builtins (pfile);
6992 /* Do standard #defines and assertions
6993 that identify system and machine type. */
6994 if (!opts->inhibit_predefs) {
6995 char *p, *r;
6996 r = p = (char *) dmalloc (strlen (predefs) + 1);
6997 strcpy (p, predefs);
6999 while (*p)
7001 char *q;
7003 while (*p == ' ' || *p == '\t')
7005 p++;
7008 /* Handle -D options. */
7009 if (p[0] == '-' && p[1] == 'D')
7011 q = &p[2];
7013 while (*p && *p != ' ' && *p != '\t')
7015 p++;
7018 if (*p != 0)
7020 *p++= 0;
7023 if (opts->debug_output)
7025 output_line_command (pfile, 0, same_file);
7028 cppReader_define (pfile, q);
7030 while (*p == ' ' || *p == '\t')
7032 p++;
7035 else
7037 abort ();
7041 sfree (r);
7044 /* Process user -D, -U and -I options, if any. */
7045 DPRINTF (("Pass through: %s", cstringSList_unparse (*cmdArgs)));
7046 cstringList_elements (*cmdArgs, thisarg)
7048 handleCmdFlag (pfile, cstring_toCharsSafe (thisarg));
7050 end_cstringList_elements;
7052 pfile->done_initializing = TRUE;
7054 cppReader_appendPathListIncludeChain(pfile,
7055 # if defined(WIN32) || defined(OS2)
7056 getenv ("INCLUDE"),
7057 # else
7058 getenv ("CPATH"),
7059 # endif
7060 FALSE);
7062 /* Unless -nostdinc,
7063 tack on the standard include file dirs to the specified list */
7064 if (!opts->no_standard_includes)
7066 cppReader_appendIncludeChain (pfile, opts->before_system,
7067 opts->last_before_system, TRUE);
7069 cppReader_appendPathListIncludeChain(pfile, getenv ("C_INCLUDE_PATH"), TRUE);
7071 cppReader_appendPathListIncludeChain(pfile, context_getString (FLG_SYSTEMDIRS), TRUE);
7073 /* Tack the after_include chain at the end of the include chain. */
7074 cppReader_appendIncludeChain (pfile, opts->after_include,
7075 opts->last_after_include, TRUE);
7078 /* With -v, print the list of dirs to search. */
7079 if (opts->verbose)
7081 struct file_name_list *p;
7082 fprintf (stderr, "#include \"...\" search starts here:\n");
7084 for (p = opts->include; p != NULL; p = p->next) {
7085 if (p == opts->first_bracket_include)
7086 fprintf (stderr, "#include <...> search starts here:\n");
7087 if (p == opts->first_system_include)
7088 fprintf (stderr, "system search starts here:\n");
7089 fprintf (stderr, " %s\n", cstring_toCharsSafe (p->fname));
7091 fprintf (stderr, "End of search list.\n");
7095 int cppReader_startProcess (cppReader *pfile, cstring fname)
7097 cppBuffer *fp;
7098 int f;
7099 struct cppOptions *opts = CPPOPTIONS (pfile);
7101 fp = cppReader_pushBuffer (pfile, NULL, 0);
7103 if (fp == NULL)
7105 return 0;
7108 if (opts->in_fname == NULL)
7110 opts->in_fname = cstring_makeLiteralTemp ("");
7113 fp->fname = opts->in_fname;
7114 fp->nominal_fname = fp->fname;
7115 fp->lineno = 0;
7117 /* Copy the entire contents of the main input file into
7118 the stacked input buffer previously allocated for it. */
7120 if (cstring_isEmpty (fname))
7122 fname = cstring_makeLiteralTemp ("");
7123 f = 0;
7125 else if ((f = osd_openForReading (cstring_toCharsSafe (fname))) < 0)
7127 cppReader_error (pfile,
7128 message ("Error opening %s for reading: %s",
7129 fname, lldecodeerror (errno)));
7131 return 0;
7133 else
7138 if (finclude (pfile, f, fname, FALSE, NULL))
7140 output_line_command (pfile, 0, same_file);
7143 return 1;
7146 static /*@exposed@*/ /*@null@*/ cppBuffer *cppReader_getBuffer (cppReader *pfile)
7148 return pfile->buffer;
7151 /*@exposed@*/ cppBuffer *cppReader_getBufferSafe (cppReader *pfile)
7153 llassert (pfile->buffer != NULL);
7154 return pfile->buffer;
7157 /*@exposed@*/ char *cppLineBase (cppBuffer *buf)
7159 llassert (buf->buf != NULL);
7160 return (buf->buf + buf->line_base);
7163 int cpplib_bufPeek (cppBuffer *buf)
7165 if (buf->cur == NULL || buf->rlimit == NULL) {
7166 return EOF;
7169 if (buf->cur < buf->rlimit) {
7170 return *(buf->cur);
7173 return EOF;
7176 bool cppBuffer_isMacro (cppBuffer *buf)
7178 if (buf != NULL)
7180 return (buf->cleanup == cppReader_macroCleanup);
7183 return FALSE;
7186 static bool notparseable = FALSE; /* preceeded by @notparseable@ */
7187 static bool notfunction = FALSE; /* preceeded by @notfunction@ */
7188 static bool expectiter = FALSE; /* preceeded by @iter@ */
7189 static bool expectenditer = FALSE; /* second after @iter@ */
7190 static bool expectfunction = FALSE; /* preceeded by @function@ */
7191 static bool expectconstant = FALSE; /* preceeded by @constant@ */
7193 static void cpp_setLocation (cppReader *pfile)
7195 fileId fid;
7196 int line;
7198 if (pfile->buffer != NULL)
7200 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->nominal_fname))
7202 cstring fname = cppReader_getBufferSafe (pfile)->nominal_fname;
7204 DPRINTF (("Looking up: %s", fname));
7206 if (fileTable_exists (context_fileTable (), fname))
7208 fid = fileTable_lookup (context_fileTable (), fname);
7210 else
7212 DPRINTF (("Trying %s", cppReader_getBuffer (pfile)->fname));
7214 fid = fileTable_lookup (context_fileTable (),
7215 cppReader_getBufferSafe (pfile)->fname);
7218 else
7220 fid = fileTable_lookup (context_fileTable (),
7221 cppReader_getBufferSafe (pfile)->fname);
7224 line = cppReader_getBufferSafe (pfile)->lineno;
7225 fileloc_free (g_currentloc);
7227 if (fileId_isValid (fid))
7229 g_currentloc = fileloc_create (fid, line, 1);
7231 else
7233 g_currentloc = fileloc_createBuiltin ();
7236 else
7238 fileloc_free (g_currentloc);
7239 g_currentloc = fileloc_createBuiltin ();
7244 ** Returns true if the macro should be checked, false
7245 ** if it should be expanded normally.
7248 static bool cpp_shouldCheckMacro (cppReader *pfile, char *p) /*@modifies p*/
7250 bool checkmacro = FALSE;
7251 bool hasParams = FALSE;
7252 bool noexpand = FALSE;
7253 cstring sname;
7254 char c;
7256 cpp_setLocation (pfile);
7258 DPRINTF (("Should check macro? %s", p));
7260 if (expectiter || expectconstant || expectenditer)
7262 if (expectiter)
7264 expectiter = FALSE;
7265 expectenditer = TRUE;
7267 else
7269 expectiter = FALSE;
7270 expectconstant = FALSE;
7271 expectenditer = FALSE;
7274 if (notfunction || notparseable)
7276 notfunction = FALSE;
7277 notparseable = FALSE;
7278 return FALSE;
7280 else
7282 return TRUE;
7286 llassert (*p == '#');
7287 p++;
7289 while (*p == ' ' || *p == '\t')
7291 p++;
7294 llassert (*p == 'd'); /* define starts */
7296 p += 6;
7298 while (*p == ' ' || *p == '\t')
7300 p++;
7303 sname = cstring_fromChars (p);
7304 DPRINTF (("Check macro: %s", sname));
7306 while (((c = *p) != ' ')
7307 && c != '\0' && c != '('
7308 && c != '\t' && c != '\\' && c != '\n'
7309 && !iscntrl (c))
7311 p++;
7314 hasParams = (c == '(');
7315 *p = '\0';
7317 if (notparseable)
7319 notparseable = FALSE;
7321 else if (notfunction || fileloc_isStandardLib (g_currentloc))
7323 DPRINTF (("Clear notfunction"));
7324 notfunction = FALSE;
7326 else
7328 if (noexpand)
7330 checkmacro = TRUE;
7332 if (!expectenditer)
7334 noexpand = FALSE;
7337 else
7339 if (usymtab_existsReal (sname))
7341 uentry ue = usymtab_lookup (sname);
7343 DPRINTF (("Lookup macro: %s", uentry_unparse (ue)));
7345 if (fileloc_isPreproc (uentry_whereLast (ue)))
7347 goto macroDne;
7349 else
7351 if (uentry_isSpecified (ue))
7353 checkmacro = context_getFlag (FLG_SPECMACROS);
7355 else
7357 if (hasParams)
7359 checkmacro = context_getFlag (FLG_LIBMACROS)
7360 || context_getFlag (FLG_FCNMACROS);
7365 else
7367 macroDne:
7368 DPRINTF (("Macro doesn't exist: %s", bool_unparse (checkmacro)));
7370 if (fileloc_isSystemFile (g_currentloc)
7371 && context_getFlag (FLG_SYSTEMDIREXPAND))
7373 ; /* don't check this macro */
7374 DPRINTF (("Don't check 1"));
7376 else
7378 uentry le;
7380 if (hasParams)
7382 DPRINTF (("Has params..."));
7384 if (context_getFlag (FLG_FCNMACROS))
7386 if (usymtab_exists (sname))
7389 ** only get here is macro is redefined
7390 ** error reported elsewhere
7393 DPRINTF (("It exists!"));
7395 else
7398 ** We make it a forward function, since it might be declared elsewhere.
7399 ** After all headers have been processed, we should check the forward
7400 ** functions.
7403 fileloc loc = fileloc_makePreproc (g_currentloc);
7405 /* the line is off-by-one, since the newline was already read */
7406 decLine ();
7408 if (expectfunction)
7410 expectfunction = FALSE;
7413 le = uentry_makeForwardFunction (sname,
7414 typeId_invalid, loc);
7416 fileloc_free (loc);
7418 incLine ();
7420 /* Do not define here! */
7422 (void) usymtab_addEntry (le);
7425 checkmacro = TRUE;
7426 DPRINTF (("Check: TRUE"));
7428 else
7430 DPRINTF (("Flag FCN_MACROS not set!"));
7433 else
7435 DPRINTF (("No params"));
7437 if (context_getFlag (FLG_CONSTMACROS))
7439 bool nocontent = FALSE;
7441 if (c == '\0')
7443 nocontent = TRUE;
7445 else
7447 if (isspace (c))
7449 char *rest = p + 1;
7452 ** Check if there is nothing after the define.
7455 while ((*rest) != '\0' && isspace (*rest))
7457 rest++;
7460 if (*rest == '\0')
7462 nocontent = TRUE; /* empty macro, don't check */
7467 if (usymtab_exists (sname))
7471 else
7473 fileloc loc = fileloc_makePreproc (g_currentloc);
7474 DPRINTF (("Make constant: %s", sname));
7475 le = uentry_makeMacroConstant (sname, ctype_unknown, loc);
7476 (void) usymtab_addEntry (le);
7479 checkmacro = !nocontent;
7484 if (checkmacro && usymtab_existsType (sname))
7486 DPRINTF (("Making false..."));
7487 decLine ();
7488 ppllerror (message ("Specified type implemented as macro: %s", sname));
7489 checkmacro = FALSE;
7490 incLine ();
7496 if (!checkmacro)
7498 if (usymtab_exists (sname))
7500 uentry ue = usymtab_lookupExpose (sname);
7501 fileloc tloc = fileloc_makePreproc (g_currentloc);
7503 uentry_setDefined (ue, tloc);
7504 fileloc_free (tloc);
7505 uentry_setUsed (ue, fileloc_undefined);
7507 else
7509 fileloc tloc = fileloc_makePreproc (g_currentloc);
7510 uentry ue = uentry_makeExpandedMacro (sname, tloc);
7511 DPRINTF (("Make expanded macro: %s", sname));
7512 DPRINTF (("Not in symbol table: %s", sname));
7514 (void) usymtab_addGlobalEntry (ue);
7515 fileloc_free (tloc);
7519 *p = c;
7520 DPRINTF (("Returning: %s", bool_unparse (checkmacro)));
7521 return checkmacro;
7524 static enum cpp_token
7525 cpp_handleComment (cppReader *pfile, struct parse_marker *smark)
7527 cppBuffer *pbuf = cppReader_getBufferSafe (pfile);
7528 char *start;
7529 int len;
7530 fileloc loc;
7531 bool eliminateComment = FALSE;
7533 llassert (pbuf->buf != NULL);
7535 start = pbuf->buf + smark->position;
7537 llassert (pbuf->cur != NULL);
7538 len = pbuf->cur - start;
7540 if (start[0] == '*'
7541 && start[1] == context_getCommentMarkerChar ())
7543 int i;
7544 char c = ' ';
7545 char *scomment = start + 2;
7546 char savec = start[len];
7548 cpp_setLocation (pfile);
7549 loc = fileloc_copy (g_currentloc);
7551 start[0] = BEFORE_COMMENT_MARKER[0];
7552 start[1] = BEFORE_COMMENT_MARKER[1];
7554 llassert (start[len - 2] == '*');
7555 start[len - 2] = AFTER_COMMENT_MARKER[0];
7557 llassert (start[len - 1] == '/');
7558 start[len - 1] = AFTER_COMMENT_MARKER[1];
7560 cpplib_reserve(pfile, size_fromInt (1 + len));
7561 cppReader_putCharQ (pfile, c);
7563 cpp_setLocation (pfile);
7565 start[len] = '\0';
7567 if (mstring_containsString (scomment, "/*"))
7569 (void) cppoptgenerror
7570 (FLG_NESTCOMMENT,
7571 message ("Comment starts inside syntactic comment: %s",
7572 cstring_fromChars (scomment)),
7573 pfile);
7576 start[len] = savec;
7578 if (mstring_equalPrefix (scomment, "ignore"))
7580 if (!context_getFlag (FLG_NOCOMMENTS))
7582 context_enterSuppressRegion (loc);
7585 else if (mstring_equalPrefix (scomment, "end"))
7587 if (!context_getFlag (FLG_NOCOMMENTS))
7589 context_exitSuppressRegion (loc);
7592 else if (mstring_equalPrefix (scomment, "notparseable"))
7594 notparseable = TRUE;
7595 eliminateComment = TRUE;
7597 else if (mstring_equalPrefix (scomment, "notfunction"))
7599 notfunction = TRUE;
7600 eliminateComment = TRUE;
7602 else if (mstring_equalPrefix (scomment, "iter"))
7604 expectiter = TRUE;
7606 else if (mstring_equalPrefix (scomment, "function"))
7608 expectfunction = TRUE;
7610 else if (mstring_equalPrefix (scomment, "constant"))
7612 expectconstant = TRUE;
7614 else
7616 char sChar = *scomment;
7618 if (sChar == '='
7619 || sChar == '-'
7620 || sChar == '+')
7622 char *rest = scomment + 1;
7624 if (mstring_equalPrefix (rest, "commentchar"))
7626 eliminateComment = TRUE;
7628 if (sChar == '=')
7630 ppllerror (cstring_makeLiteral
7631 ("Cannot restore commentchar"));
7633 else
7635 char *next = scomment + 12; /* strlen commentchar = 12 */
7637 if (*next != ' ' && *next != '\t' && *next != '\n')
7639 ppllerror
7640 (message
7641 ("Syntactic commentchar comment is not followed by a "
7642 "whitespace character: %c",
7643 *next));
7645 else
7647 char cchar = *(next + 1);
7649 if (cchar == '\0')
7651 ppllerror
7652 (cstring_makeLiteral
7653 ("Cannot set commentchar to NUL"));
7655 else
7657 context_setCommentMarkerChar (cchar);
7658 /* setComment = TRUE; */
7663 else if (mstring_equalPrefix (scomment, "nestcomment"))
7665 /* fix from Mike Miller <MikeM@xata.com> */
7666 context_fileSetFlag (FLG_NESTCOMMENT,
7667 ynm_fromCodeChar (sChar),
7668 loc);
7670 else if (mstring_equalPrefix (rest, "namechecks"))
7672 context_fileSetFlag (FLG_NAMECHECKS,
7673 ynm_fromCodeChar (sChar),
7674 loc);
7676 else if (mstring_equalPrefix (rest, "macroredef"))
7678 context_fileSetFlag (FLG_MACROREDEF,
7679 ynm_fromCodeChar (sChar),
7680 loc);
7682 else if (mstring_equalPrefix (rest, "usevarargs"))
7684 context_fileSetFlag (FLG_USEVARARGS,
7685 ynm_fromCodeChar (sChar),
7686 loc);
7688 else if (mstring_equalPrefix (rest, "nextlinemacros"))
7690 context_fileSetFlag (FLG_MACRONEXTLINE,
7691 ynm_fromCodeChar (sChar),
7692 loc);
7694 else if (mstring_equalPrefix (rest, "allmacros")
7695 || mstring_equalPrefix (rest, "fcnmacros")
7696 || mstring_equalPrefix (rest, "constmacros"))
7698 flagcode fl;
7700 if (mstring_equalPrefix (rest, "allmacros"))
7702 fl = FLG_ALLMACROS;
7704 else if (mstring_equalPrefix (rest, "fcnmacros"))
7706 fl = FLG_FCNMACROS;
7708 else
7710 llassert (mstring_equalPrefix (rest, "constmacros"));
7711 fl = FLG_CONSTMACROS;
7714 context_fileSetFlag (fl, ynm_fromCodeChar (sChar), loc);
7715 notfunction = FALSE;
7717 else
7722 else
7728 if (eliminateComment)
7730 goto removeComment;
7733 /* Replaces comment char's in start with spaces */
7735 for (i = 2; i < len - 2; i++)
7737 if (start[i] == BEFORE_COMMENT_MARKER[0]
7738 || start[i] == BEFORE_COMMENT_MARKER[1]
7739 || start[i] == context_getCommentMarkerChar ())
7741 start[i] = ' ';
7745 cppReader_putStrN (pfile, start, size_fromInt (len));
7746 parseClearMark (smark);
7747 return CPP_COMMENT;
7749 else
7751 removeComment:
7753 int i;
7756 ** Output the comment as all spaces so line/column
7757 ** in output file is still correct.
7760 char c = ' ';
7761 cstring lintcomment = cstring_undefined;
7763 if (context_getFlag (FLG_LINTCOMMENTS))
7765 if (mstring_equalPrefix (start, "*NOTREACHED*/"))
7767 lintcomment = cstring_makeLiteralTemp ("l_notreach");
7769 else if (mstring_equalPrefix (start, "*PRINTFLIKE*/"))
7771 lintcomment = cstring_makeLiteralTemp ("l_printfli");
7773 else if (mstring_equalPrefix (start, "*FALLTHROUGH*/"))
7775 lintcomment = cstring_makeLiteralTemp ("l_fallthrou");
7777 else if (mstring_equalPrefix (start, "*ARGSUSED*/"))
7779 lintcomment = cstring_makeLiteralTemp ("l_argsus");
7781 else if (mstring_equalPrefix (start, "*FALLTHRU*/"))
7783 lintcomment = cstring_makeLiteralTemp ("l_fallth");
7785 else
7787 lintcomment = cstring_undefined;
7790 else
7792 lintcomment = cstring_undefined;
7795 if (cstring_isDefined (lintcomment))
7797 c = BEFORE_COMMENT_MARKER[0];
7798 start[0] = BEFORE_COMMENT_MARKER[1];
7800 llassert (size_toLong (cstring_length (lintcomment)) == len - 3);
7802 for (i = 1; i < len - 2; i++)
7804 start[i] = cstring_getChar (lintcomment, size_fromInt (i));
7807 start[len - 2] = AFTER_COMMENT_MARKER[0];
7808 start[len - 1] = AFTER_COMMENT_MARKER[1];
7810 else
7812 /* Replaces char's in start with spaces */
7813 for (i = 0; i < len; i++)
7815 if (start[i] == '/'
7816 && i < len - 1
7817 && start[i + 1] == '*') {
7818 (void) cppoptgenerror
7819 (FLG_NESTCOMMENT,
7820 message ("Comment starts inside comment"),
7821 pfile);
7824 if (start[i] != '\n')
7826 start[i] = ' ';
7831 cpplib_reserve (pfile, size_fromInt (1 + len));
7832 cppReader_putCharQ (pfile, c);
7833 cppReader_putStrN (pfile, start, size_fromInt (len));
7834 parseClearMark (smark);
7835 return CPP_COMMENT;
7840 static int cpp_openIncludeFile (char *filename)
7842 int res = osd_openForReading (filename);
7844 /* evans 2001-08-23: was (res) - open returns -1 on error! reported by Robin Watts */
7845 if (res >= 0)
7847 if (!fileTable_exists (context_fileTable (),
7848 cstring_fromChars (filename)))
7850 fileType ftype = context_inXHFile () ? FILE_XH : FILE_HEADER;
7851 (void) fileTable_addFile (context_fileTable (), ftype,
7852 cstring_fromChars (filename));
7854 else
7856 DPRINTF (("File already exists: %s", filename));
7860 return res;
7863 static bool cpp_skipIncludeFile (cstring fname)
7865 if (context_isSystemDir (fname))
7867 DPRINTF (("System dir: %s", fname));
7869 if (lcllib_isSkipHeader (fname))
7871 DPRINTF (("Skip include TRUE: %s", fname));
7872 return TRUE;
7875 if (context_getFlag (FLG_SKIPSYSHEADERS))
7878 ** 2003-04-18: Patch from Randal Parsons
7882 ** Don't skip include file unless the file actually exists.
7883 ** It may be in a different directory.
7886 if (osd_fileIsReadable(fname))
7888 DPRINTF (("Skip include TRUE: %s", fname));
7889 return TRUE;
7891 else
7893 ; /* Keep looking... */
7898 if (context_getFlag (FLG_SINGLEINCLUDE))
7900 fname = osd_removePreDirs (fname);
7902 osd_pathMakePosix (fname);
7904 if (fileTable_exists (context_fileTable (), fname))
7906 DPRINTF (("Skip include TRUE: %s", fname));
7907 return TRUE;
7911 DPRINTF (("Skip include FALSE: %s", fname));
7912 return FALSE;
7915 static int cpp_peekN (cppReader *pfile, int n)
7917 cppBuffer *buf = cppReader_getBufferSafe (pfile);
7919 llassert (buf->cur != NULL);
7921 return (buf->rlimit - buf->cur >= (n)
7922 ? buf->cur[n]
7923 : EOF);
7926 cppBuffer *cppBuffer_prevBuffer (cppBuffer *buf)
7928 return buf + 1;
7931 void cppBuffer_forward (cppBuffer *buf, int n)
7933 llassert (buf->cur != NULL);
7934 buf->cur += n;
7937 /*@=bufferoverflowhigh@*/
7938 /*@=bounds@*/