* dbxout.c (dbxout_source_line): Remove extra tab.
[gcc.git] / gcc / cppoutput.c
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
7
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
11 later version.
12
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.
17
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. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "intl.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27
28 static void output_line_command PARAMS ((cpp_reader *, cpp_printer *,
29 unsigned int));
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 *,
34 unsigned int));
35
36 /* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output. Used
37 for handling -imacros, -dM, -M and -MM. */
38 void
39 cpp_scan_buffer_nooutput (pfile)
40 cpp_reader *pfile;
41 {
42 cpp_buffer *stop = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
43 const cpp_token *token;
44
45 /* In no-output mode, we can ignore everything but directives. */
46 for (;;)
47 {
48 token = _cpp_get_token (pfile);
49
50 if (token->type == CPP_EOF)
51 {
52 cpp_pop_buffer (pfile);
53 if (CPP_BUFFER (pfile) == stop)
54 break;
55 }
56
57 if (token->type == CPP_HASH && token->flags & BOL
58 && pfile->token_list.directive)
59 {
60 _cpp_process_directive (pfile, token);
61 continue;
62 }
63
64 _cpp_skip_rest_of_line (pfile);
65 }
66 }
67
68 /* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT. */
69 void
70 cpp_scan_buffer (pfile, print)
71 cpp_reader *pfile;
72 cpp_printer *print;
73 {
74 cpp_buffer *stop = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
75 const cpp_token *token, *prev = 0;
76
77 for (;;)
78 {
79 token = _cpp_get_token (pfile);
80 if (token->type == CPP_EOF)
81 {
82 cpp_pop_buffer (pfile);
83
84 if (CPP_BUFFER (pfile) == stop)
85 return;
86
87 prev = 0;
88 continue;
89 }
90
91 if (token->flags & BOL)
92 {
93 output_line_command (pfile, print, token->line);
94 prev = 0;
95
96 if (token->type == CPP_HASH && pfile->token_list.directive)
97 {
98 _cpp_process_directive (pfile, token);
99 continue;
100 }
101 }
102
103 if (token->type != CPP_PLACEMARKER)
104 {
105 output_token (pfile, print->outf, token, prev, 1);
106 pfile->need_newline = 1;
107 }
108
109 prev = token;
110 }
111 }
112
113 /* Notify the compiler proper that the current line number has jumped,
114 or the current file name has changed. */
115 static void
116 output_line_command (pfile, print, line)
117 cpp_reader *pfile;
118 cpp_printer *print;
119 unsigned int line;
120 {
121 cpp_buffer *ip = CPP_BUFFER (pfile);
122
123 if (line == 0)
124 return;
125
126 /* End the previous line of text. */
127 if (pfile->need_newline)
128 {
129 putc ('\n', print->outf);
130 print->lineno++;
131 }
132 pfile->need_newline = 0;
133
134 if (CPP_OPTION (pfile, no_line_commands))
135 return;
136
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.)
142
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)
147 {
148 while (line > print->lineno)
149 {
150 putc ('\n', print->outf);
151 print->lineno++;
152 }
153 return;
154 }
155
156 fprintf (print->outf, "# %u \"%s\"%s\n", line, ip->nominal_fname,
157 cpp_syshdr_flags (pfile, ip));
158
159 print->last_fname = ip->nominal_fname;
160 print->lineno = line;
161 }
162
163 /* Output all the tokens of LIST, starting at TOKEN, to FP. */
164 void
165 cpp_output_list (pfile, fp, list, token)
166 cpp_reader *pfile;
167 FILE *fp;
168 const cpp_toklist *list;
169 const cpp_token *token;
170 {
171 const cpp_token *limit = list->tokens + list->tokens_used;
172 const cpp_token *prev = 0;
173 int white = 0;
174
175 while (token < limit)
176 {
177 /* XXX Find some way we can write macro args from inside
178 output_token/spell_token. */
179 if (token->type == CPP_MACRO_ARG)
180 {
181 if (white && token->flags & PREV_WHITE)
182 putc (' ', fp);
183 if (token->flags & STRINGIFY_ARG)
184 putc ('#', fp);
185 dump_param_spelling (fp, list, token->val.aux);
186 }
187 else
188 output_token (pfile, fp, token, prev, white);
189 if (token->flags & PASTE_LEFT)
190 fputs (" ##", fp);
191 prev = token;
192 token++;
193 white = 1;
194 }
195 }
196
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. */
202 static void
203 output_token (pfile, fp, token, prev, white)
204 cpp_reader *pfile;
205 FILE *fp;
206 const cpp_token *token, *prev;
207 int white;
208 {
209 if (white)
210 {
211 int dummy;
212
213 if (token->col && (token->flags & BOL))
214 {
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;
220
221 while (spaces--)
222 putc (' ', fp);
223 }
224 else if (token->flags & PREV_WHITE)
225 putc (' ', fp);
226 else
227 /* Check for and prevent accidental token pasting.
228 In addition to the cases handled by _cpp_can_paste, consider
229
230 a + ++b - if there is not a space between the + and ++, it
231 will be misparsed as a++ + b. But + ## ++ doesn't produce
232 a valid token. */
233 if (prev
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)))
237 putc (' ', fp);
238 }
239
240 switch (TOKEN_SPELL (token))
241 {
242 case SPELL_OPERATOR:
243 {
244 const unsigned char *spelling;
245
246 if (token->flags & DIGRAPH)
247 spelling = _cpp_digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
248 else if (token->flags & NAMED_OP)
249 goto spell_ident;
250 else
251 spelling = TOKEN_NAME (token);
252
253 ufputs (spelling, fp);
254 }
255 break;
256
257 case SPELL_IDENT:
258 spell_ident:
259 ufputs (token->val.node->name, fp);
260 break;
261
262 case SPELL_STRING:
263 {
264 int left, right, tag;
265 switch (token->type)
266 {
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;
274 }
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);
279 }
280 break;
281
282 case SPELL_CHAR:
283 putc (token->val.aux, fp);
284 break;
285
286 case SPELL_NONE:
287 /* Placemarker or EOF - no output. (Macro args are handled
288 elsewhere. */
289 break;
290 }
291 }
292
293 /* Dump the original user's spelling of argument index ARG_NO to the
294 macro whose expansion is LIST. */
295 static void
296 dump_param_spelling (fp, list, arg_no)
297 FILE *fp;
298 const cpp_toklist *list;
299 unsigned int arg_no;
300 {
301 const U_CHAR *param = list->namebuf;
302
303 while (arg_no--)
304 param += ustrlen (param) + 1;
305 ufputs (param, fp);
306 }
307
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. */
311
312 void
313 cpp_dump_definition (pfile, fp, hp)
314 cpp_reader *pfile;
315 FILE *fp;
316 const cpp_hashnode *hp;
317 {
318 const cpp_toklist *list = hp->value.expansion;
319
320 if (hp->type != T_MACRO)
321 {
322 cpp_ice (pfile, "invalid hash type %d in dump_definition", hp->type);
323 return;
324 }
325
326 if (list->paramc >= 0)
327 dump_macro_args (fp, list);
328
329 putc (' ', fp);
330 cpp_output_list (pfile, fp, list, list->tokens);
331 }
332
333 static void
334 dump_macro_args (fp, list)
335 FILE *fp;
336 const cpp_toklist *list;
337 {
338 int i;
339 const U_CHAR *param = list->namebuf;
340
341 putc ('(', fp);
342 for (i = 0; i++ < list->paramc;)
343 {
344 unsigned int len;
345
346 len = ustrlen (param);
347 if (!(list->flags & VAR_ARGS) || ustrcmp (param, U"__VA_ARGS__"))
348 ufputs (param, fp);
349 if (i < list->paramc)
350 fputs (", ", fp);
351 else if (list->flags & VAR_ARGS)
352 fputs ("...", fp);
353
354 param += len + 1;
355 }
356 putc (')', fp);
357 }
358
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. */
361 void
362 cpp_printf VPARAMS ((cpp_reader *pfile, cpp_printer *print,
363 const char *fmt, ...))
364 {
365 va_list ap;
366 #ifndef ANSI_PROTOTYPES
367 cpp_reader *pfile;
368 cpp_printer *print;
369 const char *fmt;
370 #endif
371
372 VA_START (ap, fmt);
373
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 *);
378 #endif
379
380 /* End the previous line of text. */
381 if (pfile->need_newline)
382 {
383 putc ('\n', print->outf);
384 print->lineno++;
385 }
386 pfile->need_newline = 0;
387
388 vfprintf (print->outf, fmt, ap);
389 va_end (ap);
390 }