Imported Upstream version 2.9.0
[debian/cc1111] / support / cpp / sdcpp.c
1 /*-------------------------------------------------------------------------
2   sdcppmain.c - sdcpp: SDCC preprocessor main file, using cpplib.
3
4   Written by Borut Razem, 2006.
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, or (at your option) any
9   later version.
10
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.
15
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.
19
20   In other words, you are welcome to use, share and improve this program.
21   You are forbidden to forbid anyone else to use, share and improve
22   what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
29 #include "version.h"
30 #include "mkdeps.h"
31 #include "opts.h"
32 #include "intl.h"
33
34 const char *progname;           /* Needs to be global.  */
35
36 /* From laghooks-def.h */
37 /* The whole thing.  The structure is defined in langhooks.h.  */
38 #define LANG_HOOKS_INITIALIZER { \
39   LANG_HOOKS_INIT_OPTIONS, \
40   LANG_HOOKS_HANDLE_OPTION, \
41   LANG_HOOKS_MISSING_ARGUMENT, \
42   LANG_HOOKS_POST_OPTIONS, \
43   LANG_HOOKS_INIT, \
44   LANG_HOOKS_FINISH, \
45 }
46
47 /* From c-lang.c */
48 #define LANG_HOOKS_INIT_OPTIONS sdcpp_init_options
49 #define LANG_HOOKS_HANDLE_OPTION sdcpp_common_handle_option
50 #define LANG_HOOKS_MISSING_ARGUMENT sdcpp_common_missing_argument
51 #define LANG_HOOKS_POST_OPTIONS sdcpp_common_post_options
52 #define LANG_HOOKS_INIT sdcpp_common_init
53 #define LANG_HOOKS_FINISH sdcpp_common_finish
54
55 static unsigned int sdcpp_init_options (unsigned int argc, const char **argv);
56
57 /* Each front end provides its own lang hook initializer.  */
58 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
59
60 /* Name of top-level original source file (what was input to cpp).
61    This comes from the #-command at the beginning of the actual input.
62    If there isn't any there, then this is the cc1 input file name.  */
63
64 const char *main_input_filename;
65
66 struct line_maps line_table;
67
68 /* Temporarily suppress certain warnings.
69    This is set while reading code from a system header file.  */
70
71 int in_system_header = 0;
72
73 /* Nonzero means change certain warnings into errors.
74    Usually these are warnings about failure to conform to some standard.  */
75
76 int flag_pedantic_errors = 0;
77
78 cpp_reader *parse_in;           /* Declared in c-pragma.h.  */
79
80 /* Nonzero means `char' should be signed.  */
81
82 int flag_signed_char;
83
84 /* Nonzero means don't output line number information.  */
85
86 char flag_no_line_commands;
87
88 /* Nonzero causes -E output not to be done, but directives such as
89    #define that have side effects are still obeyed.  */
90
91 char flag_no_output;
92
93 /* Nonzero means dump macros in some fashion.  */
94
95 char flag_dump_macros;
96
97 /* Nonzero means pass #include lines through to the output.  */
98
99 char flag_dump_includes;
100
101 /* 0 means we want the preprocessor to not emit line directives for
102    the current working directory.  1 means we want it to do it.  -1
103    means we should decide depending on whether debugging information
104    is being emitted or not.  */
105
106 int flag_working_directory = -1;
107
108 /* The current working directory of a translation.  It's generally the
109    directory from which compilation was initiated, but a preprocessed
110    file may specify the original directory in which it was
111    created.  */
112
113 static const char *src_pwd;
114
115 /* Initialize src_pwd with the given string, and return true.  If it
116    was already initialized, return false.  As a special case, it may
117    be called with a NULL argument to test whether src_pwd has NOT been
118    initialized yet.  */
119
120 /* From intl.c */
121 /* Opening quotation mark for diagnostics.  */
122 const char *open_quote = "'";
123
124 /* Closing quotation mark for diagnostics.  */
125 const char *close_quote = "'";
126 /* ----------- */
127
128 bool
129 set_src_pwd (const char *pwd)
130 {
131   if (src_pwd)
132     return false;
133
134   src_pwd = xstrdup (pwd);
135   return true;
136 }
137
138 /* Return the directory from which the translation unit was initiated,
139    in case set_src_pwd() was not called before to assign it a
140    different value.  */
141
142 const char *
143 get_src_pwd (void)
144 {
145   if (! src_pwd)
146     src_pwd = getpwd ();
147
148    return src_pwd;
149 }
150
151 /* SDCPP specific pragmas */
152 /* SDCC specific
153    sdcc_hash pragma */
154 static void
155 do_pragma_sdcc_hash (cpp_reader *pfile)
156 {
157     const cpp_token *tok = _cpp_lex_token (pfile);
158
159     if (tok->type == CPP_PLUS)
160     {
161         CPP_OPTION(pfile, allow_naked_hash)++;
162     }
163     else if (tok->type == CPP_MINUS)
164     {
165         CPP_OPTION(pfile, allow_naked_hash)--;
166     }
167     else
168     {
169         cpp_error (pfile, CPP_DL_ERROR,
170                    "invalid #pragma sdcc_hash directive, need '+' or '-'");
171     }
172 }
173
174 /* SDCC specific
175    pedantic_parse_number pragma */
176 static void
177 do_pragma_pedantic_parse_number (cpp_reader *pfile)
178 {
179     const cpp_token *tok = _cpp_lex_token (pfile);
180
181   if (tok->type == CPP_PLUS)
182     {
183       CPP_OPTION(pfile, pedantic_parse_number)++;
184     }
185   else if (tok->type == CPP_MINUS)
186     {
187       CPP_OPTION(pfile, pedantic_parse_number)--;
188     }
189   else
190     {
191       cpp_error (pfile, CPP_DL_ERROR,
192                  "invalid #pragma pedantic_parse_number directive, need '+' or '-'");
193     }
194 }
195
196 /* SDCC _asm specific
197    switch _asm block preprocessing on / off */
198 static void
199 do_pragma_preproc_asm (cpp_reader *pfile)
200 {
201   const cpp_token *tok = _cpp_lex_token (pfile);
202
203   if (tok->type == CPP_PLUS)
204     {
205       CPP_OPTION(pfile, preproc_asm)++;
206     }
207   else if (tok->type == CPP_MINUS)
208     {
209       CPP_OPTION(pfile, preproc_asm)--;
210     }
211   else
212     {
213       cpp_error (pfile, CPP_DL_ERROR,
214                  "invalid #pragma preproc_asm directive, need '+' or '-'");
215     }
216 }
217
218 /* SDCPP specific option initialization */
219 static unsigned int
220 sdcpp_init_options (unsigned int argc, const char **argv)
221 {
222   unsigned int ret = sdcpp_common_init_options(argc, argv);
223
224   CPP_OPTION (parse_in, allow_naked_hash) = 0;
225   CPP_OPTION (parse_in, preproc_asm) = 1;
226   CPP_OPTION (parse_in, pedantic_parse_number) = 0;
227   CPP_OPTION (parse_in, obj_ext) = NULL;
228
229   /* Kevin abuse for SDCC. */
230   cpp_register_pragma(parse_in, 0, "sdcc_hash", do_pragma_sdcc_hash, false);
231   /* SDCC _asm specific */
232   cpp_register_pragma(parse_in, 0, "preproc_asm", do_pragma_preproc_asm, false);
233   /* SDCC specific */
234   cpp_register_pragma(parse_in, 0, "pedantic_parse_number", do_pragma_pedantic_parse_number, false);
235
236   /* SDCC _asm specific */
237   parse_in->spec_nodes.n__asm = cpp_lookup (parse_in, DSC("_asm"));
238
239   return ret;
240 }
241
242 void
243 print_version (FILE *file, const char *indent)
244 {
245   fprintf (file, _("GNU CPP version %s (cpplib)"), version_string);
246 #ifdef TARGET_VERSION
247   TARGET_VERSION;
248 #endif
249   fputc ('\n', file);
250 }
251
252 /* Initialization of the front end environment, before command line
253    options are parsed.  Signal handlers, internationalization etc.
254    ARGV0 is main's argv[0].  */
255 static void
256 general_init (const char *argv0)
257 {
258   const char *p;
259
260   p = argv0 + strlen (argv0);
261   while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
262     --p;
263   progname = p;
264
265   xmalloc_set_program_name (progname);
266
267   hex_init ();
268
269   gcc_init_libintl ();
270 }
271
272 /* Process the options that have been parsed.  */
273 static void
274 process_options (void)
275 {
276   /* Allow the front end to perform consistency checks and do further
277      initialization based on the command line options.  This hook also
278      sets the original filename if appropriate (e.g. foo.i -> foo.c)
279      so we can correctly initialize debug output.  */
280   /*no_backend =*/ (*lang_hooks.post_options) (&main_input_filename);
281 }
282
283 /* Parse a -d... command line switch.  */
284
285 void
286 decode_d_option (const char *arg)
287 {
288   int c;
289
290   while (*arg)
291     switch (c = *arg++)
292       {
293       case 'D': /* These are handled by the preprocessor.  */
294       case 'I':
295       case 'M':
296       case 'N':
297         break;
298
299       default:
300         warning (0, "unrecognized gcc debugging option: %c", c);
301         break;
302       }
303 }
304
305 /* Diagnostic */
306
307 int errorcount = 0;
308
309 /* An informative note.  Use this for additional details on an error
310    message.  */
311 void
312 inform (const char *gmsgid, ...)
313 {
314   va_list ap;
315
316   va_start (ap, gmsgid);
317   fprintf (stderr, "%s: note: ", progname);
318   vfprintf (stderr, gmsgid, ap);
319   putc('\n', stderr);
320   va_end (ap);
321 }
322
323 /* A warning.  Use this for code which is correct according to the
324    relevant language specification but is likely to be buggy anyway.  */
325 void
326 warning (int opt, const char *gmsgid, ...)
327 {
328   va_list ap;
329
330   if CPP_OPTION (parse_in, warnings_are_errors)
331     ++errorcount;
332
333   va_start (ap, gmsgid);
334   fprintf (stderr, "%s: warning: ", progname);
335   vfprintf (stderr, gmsgid, ap);
336   putc('\n', stderr);
337   va_end (ap);
338 }
339
340 /* A hard error: the code is definitely ill-formed, and an object file
341    will not be produced.  */
342 void
343 error (const char *gmsgid, ...)
344 {
345   va_list ap;
346
347   ++errorcount;
348
349   va_start (ap, gmsgid);
350   fprintf (stderr, "%s: error: ", progname);
351   vfprintf (stderr, gmsgid, ap);
352   putc('\n', stderr);
353   va_end (ap);
354 }
355
356 /* An error which is severe enough that we make no attempt to
357    continue.  Do not use this for internal consistency checks; that's
358    internal_error.  Use of this function should be rare.  */
359 void
360 fatal_error (const char *gmsgid, ...)
361 {
362   va_list ap;
363
364   va_start (ap, gmsgid);
365   fprintf (stderr, "%s: fatal error: ", progname);
366   vfprintf (stderr, gmsgid, ap);
367   putc('\n', stderr);
368   va_end (ap);
369
370   exit (FATAL_EXIT_CODE);
371 }
372
373 /* An internal consistency check has failed.  We make no attempt to
374    continue.  Note that unless there is debugging value to be had from
375    a more specific message, or some other good reason, you should use
376    abort () instead of calling this function directly.  */
377 void
378 internal_error (const char *gmsgid, ...)
379 {
380   va_list ap;
381
382   va_start (ap, gmsgid);
383   fprintf (stderr, "%s: internal compiler error: ", progname);
384   vfprintf (stderr, gmsgid, ap);
385   putc('\n', stderr);
386   va_end (ap);
387
388   exit (FATAL_EXIT_CODE);
389 }
390
391 /* Report an internal compiler error in a friendly manner.  This is
392    the function that gets called upon use of abort() in the source
393    code generally, thanks to a special macro.  */
394
395 void
396 fancy_abort (const char *file, int line, const char *function)
397 {
398   internal_error ("in %s, at %s:%d", function, file, line);
399 }
400
401 /* Language-dependent initialization.  Returns nonzero on success.  */
402 static int
403 lang_dependent_init (const char *name)
404 {
405   /* Other front-end initialization.  */
406   if ((*lang_hooks.init) () == 0)
407     return 0;
408
409   return 1;
410 }
411
412 /* Clean up: close opened files, etc.  */
413
414 static void
415 finalize (void)
416 {
417   /* Language-specific end of compilation actions.  */
418   (*lang_hooks.finish) ();
419 }
420
421 /* Initialize the compiler, and compile the input file.  */
422 static void
423 do_compile (void)
424 {
425   process_options ();
426
427   /* Don't do any more if an error has already occurred.  */
428   if (!errorcount)
429     {
430       /* Language-dependent initialization.  Returns true on success.  */
431       lang_dependent_init (main_input_filename);
432
433       finalize ();
434     }
435 }
436
437 /* Entry point of sdcpp.
438    Exit code is FATAL_EXIT_CODE if can't open files or if there were
439    any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
440
441    It is not safe to call this function more than once.  */
442
443 int
444 main (int argc, const char **argv)
445 {
446   /* Initialization of SDCPP's environment.  */
447   general_init (argv[0]);
448
449   /* Parse the options and do minimal processing; basically just
450      enough to default flags appropriately.  */
451   decode_options (argc, argv);
452
453   /* Exit early if we can (e.g. -help).  */
454   if (!exit_after_options)
455     do_compile ();
456
457   if (errorcount)
458     return (FATAL_EXIT_CODE);
459
460   return (SUCCESS_EXIT_CODE);
461 }