1 /* CPP Library - non-diagnostic output.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
6 Broken out to separate file, Sep 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 static void output_line_command
PARAMS ((cpp_reader
*, cpp_printer
*,
30 static void output_token
PARAMS ((cpp_reader
*, FILE *, const cpp_token
*,
31 const cpp_token
*, int));
32 static void dump_macro_args
PARAMS ((FILE *, const cpp_toklist
*));
33 static void dump_param_spelling
PARAMS ((FILE *, const cpp_toklist
*,
36 /* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output. Used
37 for handling -imacros, -dM, -M and -MM. */
39 cpp_scan_buffer_nooutput (pfile
)
42 cpp_buffer
*stop
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
43 const cpp_token
*token
;
45 /* In no-output mode, we can ignore everything but directives. */
48 token
= _cpp_get_token (pfile
);
50 if (token
->type
== CPP_EOF
)
52 cpp_pop_buffer (pfile
);
53 if (CPP_BUFFER (pfile
) == stop
)
57 if (token
->type
== CPP_HASH
&& token
->flags
& BOL
58 && pfile
->token_list
.directive
)
60 _cpp_process_directive (pfile
, token
);
64 _cpp_skip_rest_of_line (pfile
);
68 /* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT. */
70 cpp_scan_buffer (pfile
, print
)
74 cpp_buffer
*stop
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
75 const cpp_token
*token
, *prev
= 0;
79 token
= _cpp_get_token (pfile
);
80 if (token
->type
== CPP_EOF
)
82 cpp_pop_buffer (pfile
);
84 if (CPP_BUFFER (pfile
) == stop
)
91 if (token
->flags
& BOL
)
93 output_line_command (pfile
, print
, token
->line
);
96 if (token
->type
== CPP_HASH
&& pfile
->token_list
.directive
)
98 _cpp_process_directive (pfile
, token
);
103 if (token
->type
!= CPP_PLACEMARKER
)
105 output_token (pfile
, print
->outf
, token
, prev
, 1);
106 pfile
->need_newline
= 1;
113 /* Notify the compiler proper that the current line number has jumped,
114 or the current file name has changed. */
116 output_line_command (pfile
, print
, line
)
121 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
126 /* End the previous line of text. */
127 if (pfile
->need_newline
)
129 putc ('\n', print
->outf
);
132 pfile
->need_newline
= 0;
134 if (CPP_OPTION (pfile
, no_line_commands
))
137 /* If the current file has not changed, we can output a few newlines
138 instead if we want to increase the line number by a small amount.
139 We cannot do this if print->lineno is zero, because that means we
140 haven't output any line commands yet. (The very first line
141 command output is a `same_file' command.)
143 'nominal_fname' values are unique, so they can be compared by
144 comparing pointers. */
145 if (ip
->nominal_fname
== print
->last_fname
&& print
->lineno
> 0
146 && line
>= print
->lineno
&& line
< print
->lineno
+ 8)
148 while (line
> print
->lineno
)
150 putc ('\n', print
->outf
);
156 fprintf (print
->outf
, "# %u \"%s\"%s\n", line
, ip
->nominal_fname
,
157 cpp_syshdr_flags (pfile
, ip
));
159 print
->last_fname
= ip
->nominal_fname
;
160 print
->lineno
= line
;
163 /* Output all the tokens of LIST, starting at TOKEN, to FP. */
165 cpp_output_list (pfile
, fp
, list
, token
)
168 const cpp_toklist
*list
;
169 const cpp_token
*token
;
171 const cpp_token
*limit
= list
->tokens
+ list
->tokens_used
;
172 const cpp_token
*prev
= 0;
175 while (token
< limit
)
177 /* XXX Find some way we can write macro args from inside
178 output_token/spell_token. */
179 if (token
->type
== CPP_MACRO_ARG
)
181 if (white
&& token
->flags
& PREV_WHITE
)
183 if (token
->flags
& STRINGIFY_ARG
)
185 dump_param_spelling (fp
, list
, token
->val
.aux
);
188 output_token (pfile
, fp
, token
, prev
, white
);
189 if (token
->flags
& PASTE_LEFT
)
197 /* Write the spelling of a token TOKEN, with any appropriate
198 whitespace before it, to FP. PREV is the previous token, which
199 is used to determine if we need to shove in an extra space in order
200 to avoid accidental token paste. If WHITE is 0, do not insert any
201 leading whitespace. */
203 output_token (pfile
, fp
, token
, prev
, white
)
206 const cpp_token
*token
, *prev
;
213 if (token
->col
&& (token
->flags
& BOL
))
215 /* Supply enough whitespace to put this token in its original
216 column. Don't bother trying to reconstruct tabs; we can't
217 get it right in general, and nothing ought to care. (Yes,
218 some things do care; the fault lies with them.) */
219 unsigned int spaces
= token
->col
- 1;
224 else if (token
->flags
& PREV_WHITE
)
227 /* Check for and prevent accidental token pasting.
228 In addition to the cases handled by _cpp_can_paste, consider
230 a + ++b - if there is not a space between the + and ++, it
231 will be misparsed as a++ + b. But + ## ++ doesn't produce
234 && (_cpp_can_paste (pfile
, prev
, token
, &dummy
) != CPP_EOF
235 || (prev
->type
== CPP_PLUS
&& token
->type
== CPP_PLUS_PLUS
)
236 || (prev
->type
== CPP_MINUS
&& token
->type
== CPP_MINUS_MINUS
)))
240 switch (TOKEN_SPELL (token
))
244 const unsigned char *spelling
;
246 if (token
->flags
& DIGRAPH
)
247 spelling
= _cpp_digraph_spellings
[token
->type
- CPP_FIRST_DIGRAPH
];
248 else if (token
->flags
& NAMED_OP
)
251 spelling
= TOKEN_NAME (token
);
253 ufputs (spelling
, fp
);
259 ufputs (token
->val
.node
->name
, fp
);
264 int left
, right
, tag
;
267 case CPP_STRING
: left
= '"'; right
= '"'; tag
= '\0'; break;
268 case CPP_WSTRING
: left
= '"'; right
= '"'; tag
= 'L'; break;
269 case CPP_OSTRING
: left
= '"'; right
= '"'; tag
= '@'; break;
270 case CPP_CHAR
: left
= '\''; right
= '\''; tag
= '\0'; break;
271 case CPP_WCHAR
: left
= '\''; right
= '\''; tag
= 'L'; break;
272 case CPP_HEADER_NAME
: left
= '<'; right
= '>'; tag
= '\0'; break;
273 default: left
= '\0'; right
= '\0'; tag
= '\0'; break;
275 if (tag
) putc (tag
, fp
);
276 if (left
) putc (left
, fp
);
277 fwrite (token
->val
.str
.text
, 1, token
->val
.str
.len
, fp
);
278 if (right
) putc (right
, fp
);
283 putc (token
->val
.aux
, fp
);
287 /* Placemarker or EOF - no output. (Macro args are handled
293 /* Dump the original user's spelling of argument index ARG_NO to the
294 macro whose expansion is LIST. */
296 dump_param_spelling (fp
, list
, arg_no
)
298 const cpp_toklist
*list
;
301 const U_CHAR
*param
= list
->namebuf
;
304 param
+= ustrlen (param
) + 1;
308 /* Dump the definition of macro MACRO on FP. The format is suitable
309 to be read back in again. Caller is expected to generate the
310 "#define NAME" bit. */
313 cpp_dump_definition (pfile
, fp
, hp
)
316 const cpp_hashnode
*hp
;
318 const cpp_toklist
*list
= hp
->value
.expansion
;
320 if (hp
->type
!= T_MACRO
)
322 cpp_ice (pfile
, "invalid hash type %d in dump_definition", hp
->type
);
326 if (list
->paramc
>= 0)
327 dump_macro_args (fp
, list
);
330 cpp_output_list (pfile
, fp
, list
, list
->tokens
);
334 dump_macro_args (fp
, list
)
336 const cpp_toklist
*list
;
339 const U_CHAR
*param
= list
->namebuf
;
342 for (i
= 0; i
++ < list
->paramc
;)
346 len
= ustrlen (param
);
347 if (!(list
->flags
& VAR_ARGS
) || ustrcmp (param
, U
"__VA_ARGS__"))
349 if (i
< list
->paramc
)
351 else if (list
->flags
& VAR_ARGS
)
359 /* Like fprintf, but writes to a printer object. You should be sure
360 always to generate a complete line when you use this function. */
362 cpp_printf
VPARAMS ((cpp_reader
*pfile
, cpp_printer
*print
,
363 const char *fmt
, ...))
366 #ifndef ANSI_PROTOTYPES
374 #ifndef ANSI_PROTOTYPES
375 pfile
= va_arg (ap
, cpp_reader
*);
376 print
= va_arg (ap
, cpp_printer
*);
377 fmt
= va_arg (ap
, const char *);
380 /* End the previous line of text. */
381 if (pfile
->need_newline
)
383 putc ('\n', print
->outf
);
386 pfile
->need_newline
= 0;
388 vfprintf (print
->outf
, fmt
, ap
);