1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994-95.
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, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "coretypes.h"
25 #include "../libcpp/internal.h"
27 #include "c-common.h" /* For flags. */
28 #include "c-pragma.h" /* For parse_in. */
30 /* Encapsulates state used to convert a stream of tokens into a text
34 FILE *outf
; /* Stream to write to. */
35 const cpp_token
*prev
; /* Previous token. */
36 const cpp_token
*source
; /* Source token for spacing. */
37 int src_line
; /* Line number currently being written. */
38 unsigned char printed
; /* Nonzero if something output at line. */
39 bool first_time
; /* pp_file_change hasn't been called yet. */
42 /* General output routines. */
43 static void scan_translation_unit (cpp_reader
*);
44 static void scan_translation_unit_trad (cpp_reader
*);
45 static void account_for_newlines (const unsigned char *, size_t);
46 static int dump_macro (cpp_reader
*, cpp_hashnode
*, void *);
48 static void print_line (source_location
, const char *);
49 static void maybe_print_line (source_location
);
51 /* Callback routines for the parser. Most of these are active only
53 static void cb_line_change (cpp_reader
*, const cpp_token
*, int);
54 static void cb_define (cpp_reader
*, fileline
, cpp_hashnode
*);
55 static void cb_undef (cpp_reader
*, fileline
, cpp_hashnode
*);
56 static void cb_include (cpp_reader
*, fileline
, const unsigned char *,
58 static void cb_ident (cpp_reader
*, fileline
, const cpp_string
*);
59 static void cb_def_pragma (cpp_reader
*, fileline
);
61 /* Preprocess and output. */
63 preprocess_file (cpp_reader
*pfile
)
65 /* A successful cpp_read_main_file guarantees that we can call
66 cpp_scan_nooutput or cpp_get_token next. */
69 /* Scan -included buffers, then the main file. */
70 while (pfile
->buffer
->prev
)
71 cpp_scan_nooutput (pfile
);
72 cpp_scan_nooutput (pfile
);
74 else if (cpp_get_options (pfile
)->traditional
)
75 scan_translation_unit_trad (pfile
);
77 scan_translation_unit (pfile
);
79 /* -dM command line option. Should this be elsewhere? */
80 if (flag_dump_macros
== 'M')
81 cpp_forall_identifiers (pfile
, dump_macro
, NULL
);
83 /* Flush any pending output. */
85 putc ('\n', print
.outf
);
88 /* Set up the callbacks as appropriate. */
90 init_pp_output (FILE *out_stream
)
92 cpp_callbacks
*cb
= cpp_get_callbacks (parse_in
);
96 cb
->line_change
= cb_line_change
;
97 /* Don't emit #pragma or #ident directives if we are processing
98 assembly language; the assembler may choke on them. */
99 if (cpp_get_options (parse_in
)->lang
!= CLK_ASM
)
101 cb
->ident
= cb_ident
;
102 cb
->def_pragma
= cb_def_pragma
;
106 if (flag_dump_includes
)
107 cb
->include
= cb_include
;
109 if (flag_dump_macros
== 'N' || flag_dump_macros
== 'D')
111 cb
->define
= cb_define
;
112 cb
->undef
= cb_undef
;
115 /* Initialize the print structure. Setting print.src_line to -1 here is
116 a trick to guarantee that the first token of the file will cause
117 a linemarker to be output by maybe_print_line. */
121 print
.outf
= out_stream
;
122 print
.first_time
= 1;
125 /* Writes out the preprocessed file, handling spacing and paste
128 scan_translation_unit (cpp_reader
*pfile
)
130 bool avoid_paste
= false;
135 const cpp_token
*token
= cpp_get_token (pfile
);
137 if (token
->type
== CPP_PADDING
)
140 if (print
.source
== NULL
141 || (!(print
.source
->flags
& PREV_WHITE
)
142 && token
->val
.source
== NULL
))
143 print
.source
= token
->val
.source
;
147 if (token
->type
== CPP_EOF
)
150 /* Subtle logic to output a space if and only if necessary. */
153 if (print
.source
== NULL
)
154 print
.source
= token
;
155 if (print
.source
->flags
& PREV_WHITE
157 && cpp_avoid_paste (pfile
, print
.prev
, token
))
158 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
159 putc (' ', print
.outf
);
161 else if (token
->flags
& PREV_WHITE
)
162 putc (' ', print
.outf
);
167 cpp_output_token (token
, print
.outf
);
169 if (token
->type
== CPP_COMMENT
)
170 account_for_newlines (token
->val
.str
.text
, token
->val
.str
.len
);
174 /* Adjust print.src_line for newlines embedded in output. */
176 account_for_newlines (const unsigned char *str
, size_t len
)
183 /* Writes out a traditionally preprocessed file. */
185 scan_translation_unit_trad (cpp_reader
*pfile
)
187 while (_cpp_read_logical_line_trad (pfile
))
189 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
190 maybe_print_line (pfile
->out
.first_line
);
191 fwrite (pfile
->out
.base
, 1, len
, print
.outf
);
193 if (!CPP_OPTION (pfile
, discard_comments
))
194 account_for_newlines (pfile
->out
.base
, len
);
198 /* If the token read on logical line LINE needs to be output on a
199 different line to the current one, output the required newlines or
200 a line marker, and return 1. Otherwise return 0. */
202 maybe_print_line (source_location src_loc
)
204 const struct line_map
*map
= linemap_lookup (&line_table
, src_loc
);
205 int src_line
= SOURCE_LINE (map
, src_loc
);
206 /* End the previous line of text. */
209 putc ('\n', print
.outf
);
214 if (src_line
>= print
.src_line
&& src_line
< print
.src_line
+ 8)
216 while (src_line
> print
.src_line
)
218 putc ('\n', print
.outf
);
223 print_line (src_loc
, "");
226 /* Output a line marker for logical line LINE. Special flags are "1"
227 or "2" indicating entering or leaving a file. */
229 print_line (source_location src_loc
, const char *special_flags
)
231 /* End any previous line of text. */
233 putc ('\n', print
.outf
);
236 if (!flag_no_line_commands
)
238 const struct line_map
*map
= linemap_lookup (&line_table
, src_loc
);
240 size_t to_file_len
= strlen (map
->to_file
);
241 unsigned char *to_file_quoted
= alloca (to_file_len
* 4 + 1);
244 print
.src_line
= SOURCE_LINE (map
, src_loc
);
246 /* cpp_quote_string does not nul-terminate, so we have to do it
248 p
= cpp_quote_string (to_file_quoted
,
249 (unsigned char *)map
->to_file
, to_file_len
);
251 fprintf (print
.outf
, "# %u \"%s\"%s", print
.src_line
,
252 to_file_quoted
, special_flags
);
255 fputs (" 3 4", print
.outf
);
256 else if (map
->sysp
== 1)
257 fputs (" 3", print
.outf
);
259 putc ('\n', print
.outf
);
263 /* Called when a line of output is started. TOKEN is the first token
264 of the line, and at end of file will be CPP_EOF. */
266 cb_line_change (cpp_reader
*pfile
, const cpp_token
*token
,
269 source_location src_loc
= token
->src_loc
;
271 if (token
->type
== CPP_EOF
|| parsing_args
)
274 maybe_print_line (src_loc
);
278 /* Supply enough spaces to put this token in its original column,
279 one space per column greater than 2, since scan_translation_unit
280 will provide a space if PREV_WHITE. Don't bother trying to
281 reconstruct tabs; we can't get it right in general, and nothing
282 ought to care. Some things do care; the fault lies with them. */
283 if (!CPP_OPTION (pfile
, traditional
))
285 const struct line_map
*map
= linemap_lookup (&line_table
, src_loc
);
286 int spaces
= SOURCE_COLUMN (map
, src_loc
) - 2;
289 while (-- spaces
>= 0)
290 putc (' ', print
.outf
);
295 cb_ident (cpp_reader
*pfile ATTRIBUTE_UNUSED
, fileline line
,
296 const cpp_string
*str
)
298 maybe_print_line (line
);
299 fprintf (print
.outf
, "#ident \"%s\"\n", str
->text
);
304 cb_define (cpp_reader
*pfile
, fileline line
, cpp_hashnode
*node
)
306 maybe_print_line (line
);
307 fputs ("#define ", print
.outf
);
309 /* 'D' is whole definition; 'N' is name only. */
310 if (flag_dump_macros
== 'D')
311 fputs ((const char *) cpp_macro_definition (pfile
, node
),
314 fputs ((const char *) NODE_NAME (node
), print
.outf
);
316 putc ('\n', print
.outf
);
321 cb_undef (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
324 maybe_print_line (line
);
325 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
330 cb_include (cpp_reader
*pfile ATTRIBUTE_UNUSED
, source_location line
,
331 const unsigned char *dir
, const char *header
, int angle_brackets
)
333 maybe_print_line (line
);
335 fprintf (print
.outf
, "#%s <%s>\n", dir
, header
);
337 fprintf (print
.outf
, "#%s \"%s\"\n", dir
, header
);
341 /* Callback called when -fworking-director and -E to emit working
342 directory in cpp output file. */
345 pp_dir_change (cpp_reader
*pfile ATTRIBUTE_UNUSED
, const char *dir
)
347 size_t to_file_len
= strlen (dir
);
348 unsigned char *to_file_quoted
= alloca (to_file_len
* 4 + 1);
351 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
352 p
= cpp_quote_string (to_file_quoted
, (unsigned char *) dir
, to_file_len
);
354 fprintf (print
.outf
, "# 1 \"%s//\"\n", to_file_quoted
);
357 /* The file name, line number or system header flags have changed, as
361 pp_file_change (const struct line_map
*map
)
363 const char *flags
= "";
365 if (flag_no_line_commands
|| flag_no_output
)
370 if (print
.first_time
)
372 /* Avoid printing foo.i when the main file is foo.c. */
373 if (!cpp_get_options (parse_in
)->preprocessed
)
374 print_line (map
->start_location
, flags
);
375 print
.first_time
= 0;
379 /* Bring current file to correct line when entering a new file. */
380 if (map
->reason
== LC_ENTER
)
382 const struct line_map
*from
= INCLUDED_FROM (&line_table
, map
);
383 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from
));
385 if (map
->reason
== LC_ENTER
)
387 else if (map
->reason
== LC_LEAVE
)
389 print_line (map
->start_location
, flags
);
394 /* Copy a #pragma directive to the preprocessed output. */
396 cb_def_pragma (cpp_reader
*pfile
, fileline line
)
398 maybe_print_line (line
);
399 fputs ("#pragma ", print
.outf
);
400 cpp_output_line (pfile
, print
.outf
);
404 /* Dump out the hash table. */
406 dump_macro (cpp_reader
*pfile
, cpp_hashnode
*node
, void *v ATTRIBUTE_UNUSED
)
408 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
410 fputs ("#define ", print
.outf
);
411 fputs ((const char *) cpp_macro_definition (pfile
, node
),
413 putc ('\n', print
.outf
);