aboutsummaryrefslogtreecommitdiff
path: root/gcc/cppmain.c
blob: 3304c154948ef7ded7cc8e49b3e606ba10f7a4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* CPP main program, using CPP Library.
   Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
   Written by Per Bothner, 1994-95.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "intl.h"

/* Encapsulates state used to convert the stream of tokens coming from
   cpp_get_token back into a text file.  */
struct printer
{
  FILE *outf;			/* stream to write to.  */
  const char *last_fname;	/* previous file name.  */
  const char *syshdr_flags;	/* system header flags, if any.  */
  unsigned int lineno;		/* line currently being written.  */
  unsigned char printed;	/* nonzero if something output at lineno.  */
  unsigned char no_line_dirs;	/* nonzero to output no line directives.  */
};

int main		PARAMS ((int, char **));

/* General output routines.  */
static void scan_buffer	PARAMS ((cpp_reader *));
static int printer_init PARAMS ((cpp_reader *));
static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));

static void print_line PARAMS ((const char *));
static void maybe_print_line PARAMS ((unsigned int));
static void move_printer PARAMS ((cpp_reader *, unsigned int, const char *));

/* Callback routines for the parser.   Most of these are active only
   in specific modes.  */
static void cb_define	PARAMS ((cpp_reader *, cpp_hashnode *));
static void cb_undef	PARAMS ((cpp_reader *, cpp_hashnode *));
static void cb_include	PARAMS ((cpp_reader *, const unsigned char *,
				 const cpp_token *));
static void cb_ident	  PARAMS ((cpp_reader *, const cpp_string *));
static void cb_enter_file PARAMS ((cpp_reader *));
static void cb_leave_file PARAMS ((cpp_reader *));
static void cb_rename_file PARAMS ((cpp_reader *));
static void cb_def_pragma PARAMS ((cpp_reader *));
static void do_pragma_implementation PARAMS ((cpp_reader *));

const char *progname;
static cpp_reader parse_in;
static struct printer print;

int
main (argc, argv)
     int argc;
     char **argv;
{
  char *p;
  cpp_reader *pfile = &parse_in;
  int argi = 1;  /* Next argument to handle.  */

  p = argv[0] + strlen (argv[0]);
  while (p != argv[0] && ! IS_DIR_SEPARATOR (p[-1])) --p;
  progname = p;

  xmalloc_set_program_name (progname);

#ifdef HAVE_LC_MESSAGES
  setlocale (LC_MESSAGES, "");
#endif
  (void) bindtextdomain (PACKAGE, localedir);
  (void) textdomain (PACKAGE);

  cpp_init ();
  cpp_reader_init (pfile);
  
  argi += cpp_handle_options (pfile, argc - argi , argv + argi);
  if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
    cpp_fatal (pfile, "Invalid option %s", argv[argi]);
  if (CPP_FATAL_ERRORS (pfile))
    return (FATAL_EXIT_CODE);

  /* Open the output now.  We must do so even if no_output is on,
     because there may be other output than from the actual
     preprocessing (e.g. from -dM).  */
  if (printer_init (pfile))
    return (FATAL_EXIT_CODE);

  /* Set callbacks.  */
  if (! CPP_OPTION (pfile, no_output))
    {
      pfile->cb.ident      = cb_ident;
      pfile->cb.def_pragma = cb_def_pragma;
      if (! CPP_OPTION (pfile, no_line_commands))
	{
	  pfile->cb.enter_file = cb_enter_file;
	  pfile->cb.leave_file = cb_leave_file;
	  pfile->cb.rename_file = cb_rename_file;
	}
    }

  if (CPP_OPTION (pfile, dump_includes))
    pfile->cb.include  = cb_include;

  if (CPP_OPTION (pfile, debug_output)
      || CPP_OPTION (pfile, dump_macros) == dump_names
      || CPP_OPTION (pfile, dump_macros) == dump_definitions)
    {
      pfile->cb.define = cb_define;
      pfile->cb.undef  = cb_undef;
      pfile->cb.poison = cb_def_pragma;
    }

  /* Register one #pragma which needs special handling.  */
  cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
  cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);

  if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
    return (FATAL_EXIT_CODE);

  if (CPP_BUFFER (pfile))
    {
      if (CPP_OPTION (pfile, no_output))
	cpp_scan_buffer_nooutput (pfile);
      else
	scan_buffer (pfile);
    }

  /* -dM command line option.  */
  if (CPP_OPTION (pfile, dump_macros) == dump_only)
    cpp_forall_identifiers (pfile, dump_macro, NULL);

  cpp_finish (pfile);
  cpp_cleanup (pfile);

  /* Flush any pending output.  */
  if (print.printed)
    putc ('\n', print.outf);
  if (ferror (print.outf) || fclose (print.outf))
    cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));

  if (parse_in.errors)
    return (FATAL_EXIT_CODE);
  return (SUCCESS_EXIT_CODE);
}

/* Writes out the preprocessed file.  Alternates between two tokens,
   so that we can avoid accidental token pasting.  */
static void
scan_buffer (pfile)
     cpp_reader *pfile;
{
  unsigned int index, line;
  cpp_token tokens[2], *token;

  do
    {
      for (index = 0;; index = 1 - index)
	{
	  token = &tokens[index];
	  cpp_get_token (pfile, token);

	  if (token->type == CPP_EOF)
	    break;

	  line = cpp_get_line (pfile)->output_line;
	  if (print.lineno != line)
	    {
	      unsigned int col = cpp_get_line (pfile)->col;

	      /* Supply enough whitespace to put this token in its original
		 column.  Don't bother trying to reconstruct tabs; we can't
		 get it right in general, and nothing ought to care.  (Yes,
		 some things do care; the fault lies with them.)  */
	      maybe_print_line (line);
	      if (col > 1)
		{
		  if (token->flags & PREV_WHITE)
		    col--;
		  while (--col)
		    putc (' ', print.outf);
		}
	    }
	  else if (print.printed && ! (token->flags & PREV_WHITE)
		   && cpp_avoid_paste (pfile, &tokens[1 - index], token))
	    token->flags |= PREV_WHITE;

	  cpp_output_token (token, print.outf);
	  print.printed = 1;
	}
    }
  while (cpp_pop_buffer (pfile) != 0);
}

/* Initialize a cpp_printer structure.  As a side effect, open the
   output file.  */
static int
printer_init (pfile)
     cpp_reader *pfile;
{
  print.last_fname = 0;
  print.lineno = 0;
  print.printed = 0;
  print.no_line_dirs = CPP_OPTION (pfile, no_line_commands);

  if (CPP_OPTION (pfile, out_fname) == NULL)
    CPP_OPTION (pfile, out_fname) = "";
  
  if (CPP_OPTION (pfile, out_fname)[0] == '\0')
    print.outf = stdout;
  else
    {
      print.outf = fopen (CPP_OPTION (pfile, out_fname), "w");
      if (! print.outf)
	{
	  cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
	  return 1;
	}
    }
  return 0;
}

/* Newline-terminate any output line currently in progress.  If
   appropriate, write the current line number to the output, or pad
   with newlines so the output line matches the current line.  */
static void
maybe_print_line (line)
     unsigned int line;
{
  /* End the previous line of text (probably only needed until we get
     multi-line tokens fixed).  */
  if (print.printed)
    {
      putc ('\n', print.outf);
      print.lineno++;
      print.printed = 0;
    }

  if (print.no_line_dirs)
    return;

  if (line >= print.lineno && line < print.lineno + 8)
    {
      while (line > print.lineno)
	{
	  putc ('\n', print.outf);
	  print.lineno++;
	}
    }
  else
    {
      print.lineno = line;
      print_line ("");
    }
}

static void
print_line (special_flags)
  const char *special_flags;
{
  /* End any previous line of text.  */
  if (print.printed)
    putc ('\n', print.outf);
  print.printed = 0;

  fprintf (print.outf, "# %u \"%s\"%s%s\n",
	   print.lineno, print.last_fname, special_flags, print.syshdr_flags);
}

static void
move_printer (pfile, line, special_flags)
     cpp_reader *pfile;
     unsigned int line;
     const char *special_flags;
{
  print.lineno = line;
  print.last_fname = pfile->buffer->nominal_fname;
  print.syshdr_flags = cpp_syshdr_flags (pfile, pfile->buffer);
  print_line (special_flags);
}

/* Callbacks */

static void
cb_ident (pfile, str)
     cpp_reader *pfile ATTRIBUTE_UNUSED;
     const cpp_string * str;
{
  maybe_print_line (cpp_get_line (pfile)->output_line);
  fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
  print.lineno++;
}

static void
cb_define (pfile, node)
     cpp_reader *pfile;
     cpp_hashnode *node;
{
  if (pfile->done_initializing)
    {
      maybe_print_line (cpp_get_line (pfile)->output_line);
      fprintf (print.outf, "#define %s", node->name);

      /* -dD or -g3 command line options.  */
      if (CPP_OPTION (pfile, debug_output)
	  || CPP_OPTION (pfile, dump_macros) == dump_definitions)
	fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);

      putc ('\n', print.outf);
      print.lineno++;
    }
}

static void
cb_undef (pfile, node)
     cpp_reader *pfile;
     cpp_hashnode *node;
{
  if (pfile->done_initializing)
    {
      maybe_print_line (cpp_get_line (pfile)->output_line);
      fprintf (print.outf, "#undef %s\n", node->name);
      print.lineno++;
    }
}

static void
cb_include (pfile, dir, header)
     cpp_reader *pfile ATTRIBUTE_UNUSED;
     const unsigned char *dir;
     const cpp_token *header;
{
  maybe_print_line (cpp_get_line (pfile)->output_line);
  fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
  print.lineno++;
}

static void
cb_enter_file (pfile)
     cpp_reader *pfile;
{
  /* Bring current file to correct line (except main file).  FIXME: we
     may be using the same buffer via a # NUMBER "file" 1 directive.  */
  if (pfile->done_initializing && pfile->buffer->prev)
    maybe_print_line (pfile->buffer->prev->lineno);

  move_printer (pfile, 1, pfile->done_initializing ? " 1": "");
}

static void
cb_leave_file (pfile)
     cpp_reader *pfile;
{
  move_printer (pfile, pfile->buffer->lineno + 1, " 2");
}

static void
cb_rename_file (pfile)
     cpp_reader *pfile;
{
  move_printer (pfile, pfile->buffer->lineno + 1, "");
}

static void
cb_def_pragma (pfile)
     cpp_reader *pfile;
{
  maybe_print_line (cpp_get_line (pfile)->output_line);
  fputs ("#pragma ", print.outf);
  cpp_output_line (pfile, print.outf);
  print.lineno++;
}

static void
do_pragma_implementation (pfile)
     cpp_reader *pfile;
{
  /* Be quiet about `#pragma implementation' for a file only if it hasn't
     been included yet.  */
  cpp_token token;

  cpp_start_lookahead (pfile);
  cpp_get_token (pfile, &token);
  cpp_stop_lookahead (pfile, 0);

  /* If it's not a string, pass it through and let the front end complain.  */
  if (token.type == CPP_STRING)
    {
     /* Make a NUL-terminated copy of the string.  */
      char *filename = alloca (token.val.str.len + 1);
      memcpy (filename, token.val.str.text, token.val.str.len);
      filename[token.val.str.len] = '\0';
      if (cpp_included (pfile, filename))
	cpp_warning (pfile,
	     "#pragma GCC implementation for \"%s\" appears after file is included",
		     filename);
    }
  else if (token.type != CPP_EOF)
    {
      cpp_error (pfile, "malformed #pragma GCC implementation");
      return;
    }

  /* Output?  This is nasty, but we don't have [GCC] implementation in
     the buffer.  */
  if (pfile->cb.def_pragma)
    {
      maybe_print_line (cpp_get_line (pfile)->output_line);
      fputs ("#pragma GCC implementation ", print.outf);
      cpp_output_line (pfile, print.outf);
      print.lineno++;
    }
}

/* Dump out the hash table.  */
static int
dump_macro (pfile, node, v)
     cpp_reader *pfile;
     cpp_hashnode *node;
     void *v ATTRIBUTE_UNUSED;
{
  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
    {
      fprintf (print.outf, "#define %s", node->name);
      fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
      putc ('\n', print.outf);
      print.lineno++;
    }

  return 1;
}