* support/cpp/output.h, support/cpp/opts-common.c,
[fw/sdcc] / 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   line_table = XNEW (struct line_maps);
272 }
273
274 /* Process the options that have been parsed.  */
275 static void
276 process_options (void)
277 {
278   /* Allow the front end to perform consistency checks and do further
279      initialization based on the command line options.  This hook also
280      sets the original filename if appropriate (e.g. foo.i -> foo.c)
281      so we can correctly initialize debug output.  */
282   /*no_backend =*/ (*lang_hooks.post_options) (&main_input_filename);
283 }
284
285 /* Parse a -d... command line switch.  */
286
287 void
288 decode_d_option (const char *arg)
289 {
290   int c;
291
292   while (*arg)
293     switch (c = *arg++)
294       {
295       case 'D': /* These are handled by the preprocessor.  */
296       case 'I':
297       case 'M':
298       case 'N':
299         break;
300
301       default:
302         warning (0, "unrecognized gcc debugging option: %c", c);
303         break;
304       }
305 }
306
307 /* Diagnostic */
308
309 int errorcount = 0;
310
311 /* An informative note.  Use this for additional details on an error
312    message.  */
313 void
314 inform (const char *gmsgid, ...)
315 {
316   va_list ap;
317
318   va_start (ap, gmsgid);
319   fprintf (stderr, "%s: note: ", progname);
320   vfprintf (stderr, gmsgid, ap);
321   putc('\n', stderr);
322   va_end (ap);
323 }
324
325 /* A warning.  Use this for code which is correct according to the
326    relevant language specification but is likely to be buggy anyway.  */
327 void
328 warning (int opt, const char *gmsgid, ...)
329 {
330   va_list ap;
331
332   if CPP_OPTION (parse_in, warnings_are_errors)
333     ++errorcount;
334
335   va_start (ap, gmsgid);
336   fprintf (stderr, "%s: warning: ", progname);
337   vfprintf (stderr, gmsgid, ap);
338   putc('\n', stderr);
339   va_end (ap);
340 }
341
342 /* A hard error: the code is definitely ill-formed, and an object file
343    will not be produced.  */
344 void
345 error (const char *gmsgid, ...)
346 {
347   va_list ap;
348
349   ++errorcount;
350
351   va_start (ap, gmsgid);
352   fprintf (stderr, "%s: error: ", progname);
353   vfprintf (stderr, gmsgid, ap);
354   putc('\n', stderr);
355   va_end (ap);
356 }
357
358 /* An error which is severe enough that we make no attempt to
359    continue.  Do not use this for internal consistency checks; that's
360    internal_error.  Use of this function should be rare.  */
361 void
362 fatal_error (const char *gmsgid, ...)
363 {
364   va_list ap;
365
366   va_start (ap, gmsgid);
367   fprintf (stderr, "%s: fatal error: ", progname);
368   vfprintf (stderr, gmsgid, ap);
369   putc('\n', stderr);
370   va_end (ap);
371
372   exit (FATAL_EXIT_CODE);
373 }
374
375 /* An internal consistency check has failed.  We make no attempt to
376    continue.  Note that unless there is debugging value to be had from
377    a more specific message, or some other good reason, you should use
378    abort () instead of calling this function directly.  */
379 void
380 internal_error (const char *gmsgid, ...)
381 {
382   va_list ap;
383
384   va_start (ap, gmsgid);
385   fprintf (stderr, "%s: internal compiler error: ", progname);
386   vfprintf (stderr, gmsgid, ap);
387   putc('\n', stderr);
388   va_end (ap);
389
390   exit (FATAL_EXIT_CODE);
391 }
392
393 /* Report an internal compiler error in a friendly manner.  This is
394    the function that gets called upon use of abort() in the source
395    code generally, thanks to a special macro.  */
396
397 void
398 fancy_abort (const char *file, int line, const char *function)
399 {
400   internal_error ("in %s, at %s:%d", function, file, line);
401 }
402
403 /* Language-dependent initialization.  Returns nonzero on success.  */
404 static int
405 lang_dependent_init (const char *name)
406 {
407   /* Other front-end initialization.  */
408   if ((*lang_hooks.init) () == 0)
409     return 0;
410
411   return 1;
412 }
413
414 /* Clean up: close opened files, etc.  */
415
416 static void
417 finalize (void)
418 {
419   /* Language-specific end of compilation actions.  */
420   (*lang_hooks.finish) ();
421 }
422
423 /* Initialize the compiler, and compile the input file.  */
424 static void
425 do_compile (void)
426 {
427   process_options ();
428
429   /* Don't do any more if an error has already occurred.  */
430   if (!errorcount)
431     {
432       /* Language-dependent initialization.  Returns true on success.  */
433       lang_dependent_init (main_input_filename);
434
435       finalize ();
436     }
437 }
438
439 /* Entry point of sdcpp.
440    Exit code is FATAL_EXIT_CODE if can't open files or if there were
441    any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
442
443    It is not safe to call this function more than once.  */
444
445 int
446 main (int argc, const char **argv)
447 {
448   /* Initialization of SDCPP's environment.  */
449   general_init (argv[0]);
450
451   /* Parse the options and do minimal processing; basically just
452      enough to default flags appropriately.  */
453   decode_options (argc, argv);
454
455   /* Exit early if we can (e.g. -help).  */
456   if (!exit_after_options)
457     do_compile ();
458
459   if (errorcount)
460     return (FATAL_EXIT_CODE);
461
462   return (SUCCESS_EXIT_CODE);
463 }